當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Response.TLS方法代碼示例

本文整理匯總了Golang中net/http.Response.TLS方法的典型用法代碼示例。如果您正苦於以下問題:Golang Response.TLS方法的具體用法?Golang Response.TLS怎麽用?Golang Response.TLS使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在net/http.Response的用法示例。


在下文中一共展示了Response.TLS方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: readLoop

func (pc *persistConn) readLoop() {
	// eofc is used to block http.Handler goroutines reading from Response.Body
	// at EOF until this goroutines has (potentially) added the connection
	// back to the idle pool.
	eofc := make(chan struct{})
	defer close(eofc) // unblock reader on errors

	// Read this once, before loop starts. (to avoid races in tests)
	testHookMu.Lock()
	testHookReadLoopBeforeNextRead := testHookReadLoopBeforeNextRead
	testHookMu.Unlock()

	alive := true
	for alive {
		pb, err := pc.br.Peek(1)

		pc.lk.Lock()
		if pc.numExpectedResponses == 0 {
			if !pc.closed {
				pc.closeLocked()
				if len(pb) > 0 {
					log.Printf("Unsolicited response received on idle HTTP channel starting with %q; err=%v",
						string(pb), err)
				}
			}
			pc.lk.Unlock()
			return
		}
		pc.lk.Unlock()

		rc := <-pc.reqch

		var resp *http.Response
		if err == nil {
			resp, err = http.ReadResponse(pc.br, rc.req)
			if err == nil && resp.StatusCode == 100 {
				// Skip any 100-continue for now.
				// TODO(bradfitz): if rc.req had "Expect: 100-continue",
				// actually block the request body write and signal the
				// writeLoop now to begin sending it. (Issue 2184) For now we
				// eat it, since we're never expecting one.
				resp, err = http.ReadResponse(pc.br, rc.req)
			}
		}

		if resp != nil {
			resp.TLS = pc.tlsState
		}

		hasBody := resp != nil && rc.req.Method != "HEAD" && resp.ContentLength != 0

		if err != nil {
			pc.close()
		} else {
			resp.Body = &bodyEOFSignal{body: resp.Body}
		}

		if err != nil || resp.Close || rc.req.Close || resp.StatusCode <= 199 {
			// Don't do keep-alive on error if either party requested a close
			// or we get an unexpected informational (1xx) response.
			// StatusCode 100 is already handled above.
			alive = false
		}

		var waitForBodyRead chan bool // channel is nil when there's no body
		if hasBody {
			waitForBodyRead = make(chan bool, 2)
			resp.Body.(*bodyEOFSignal).earlyCloseFn = func() error {
				waitForBodyRead <- false
				return nil
			}
			resp.Body.(*bodyEOFSignal).fn = func(err error) error {
				isEOF := err == io.EOF
				waitForBodyRead <- isEOF
				if isEOF {
					<-eofc // see comment at top
				} else if err != nil && pc.isCanceled() {
					return errRequestCanceled
				}
				return err
			}
		}

		pc.lk.Lock()
		pc.numExpectedResponses--
		pc.lk.Unlock()

		// The connection might be going away when we put the
		// idleConn below. When that happens, we close the response channel to signal
		// to roundTrip that the connection is gone. roundTrip waits for
		// both closing and a response in a select, so it might choose
		// the close channel, rather than the response.
		// We send the response first so that roundTrip can check
		// if there is a pending one with a non-blocking select
		// on the response channel before erroring out.
		rc.ch <- responseAndError{resp, err}

		if hasBody {
			// To avoid a race, wait for the just-returned
			// response body to be fully consumed before peek on
//.........這裏部分代碼省略.........
開發者ID:vanloswang,項目名稱:flannel,代碼行數:101,代碼來源:transport.go


注:本文中的net/http.Response.TLS方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。