net.http三個坑的總結(jié)

0x00 | Host

當(dāng)你發(fā)送一個請求給下游服務(wù)的時候,如果你發(fā)送請求的時候是IP,這個時候你想要通過Header里傳遞Host。但是,如果你只是在header頭里設(shè)置Host: www.baidu.com,你會發(fā)現(xiàn)下游服務(wù)收到的Host還是IP。。

解決方案如下:

req, _ := http.NewRequest(method, url, bodyReader)
req.Host = req.Header.Get("Host") // https://github.com/golang/go/issues/7682

我們跟蹤到req.Host處也可以看到它的注釋

// For client requests Host optionally overrides the Host
// header to send. If empty, the Request.Write method uses
// the value of URL.Host. Host may contain an international
// domain name.
Host string

0x01 | Content-Length

這個和上一個問題類似,區(qū)別是上一個問題導(dǎo)致Host錯誤,這個問題可能會導(dǎo)致丟失Content-Length從而變成chunked。

這個問題很蛋疼,因為明明我在header里設(shè)置了Content-Length,但是實際卻變成了chunked。經(jīng)過反復(fù)的測試都沒有重現(xiàn),直到在github上找到了這個https://github.com/golang/go/issues/16264

后來我又查到了這段代碼。

func NewRequest(method, url string, body io.Reader) (*Request, error) {
    if method == "" {
        // We document that "" means "GET" for Request.Method, and people have
        // relied on that from NewRequest, so keep that working.
        // We still enforce validMethod for non-empty methods.
        method = "GET"
    }
    if !validMethod(method) {
        return nil, fmt.Errorf("net/http: invalid method %q", method)
    }
    u, err := parseURL(url) // Just url.Parse (url is shadowed for godoc).
    if err != nil {
        return nil, err
    }
    rc, ok := body.(io.ReadCloser)
    if !ok && body != nil {
        rc = ioutil.NopCloser(body)
    }
    // The host's colon:port should be normalized. See Issue 14836.
    u.Host = removeEmptyPort(u.Host)
    req := &Request{
        Method:     method,
        URL:        u,
        Proto:      "HTTP/1.1",
        ProtoMajor: 1,
        ProtoMinor: 1,
        Header:     make(Header),
        Body:       rc,
        Host:       u.Host,
    }
    if body != nil {
        switch v := body.(type) {
        case *bytes.Buffer:
            req.ContentLength = int64(v.Len())
            buf := v.Bytes()
            req.GetBody = func() (io.ReadCloser, error) {
                r := bytes.NewReader(buf)
                return ioutil.NopCloser(r), nil
            }
        case *bytes.Reader:
            req.ContentLength = int64(v.Len())
            snapshot := *v
            req.GetBody = func() (io.ReadCloser, error) {
                r := snapshot
                return ioutil.NopCloser(&r), nil
            }
        case *strings.Reader:
            req.ContentLength = int64(v.Len())
            snapshot := *v
            req.GetBody = func() (io.ReadCloser, error) {
                r := snapshot
                return ioutil.NopCloser(&r), nil
            }
        default:
            // This is where we'd set it to -1 (at least
            // if body != NoBody) to mean unknown, but
            // that broke people during the Go 1.8 testing
            // period. People depend on it being 0 I
            // guess. Maybe retry later. See Issue 18117.
        }
        // For client requests, Request.ContentLength of 0
        // means either actually 0, or unknown. The only way
        // to explicitly say that the ContentLength is zero is
        // to set the Body to nil. But turns out too much code
        // depends on NewRequest returning a non-nil Body,
        // so we use a well-known ReadCloser variable instead
        // and have the http package also treat that sentinel
        // variable to mean explicitly zero.
        if req.GetBody != nil && req.ContentLength == 0 {
            req.Body = NoBody
            req.GetBody = func() (io.ReadCloser, error) { return NoBody, nil }
        }
    }

    return req, nil
}

可以看到,這里面居然有個switch,當(dāng)你使用bytes.Buffer,bytes.Reader或者strings.Reader作為Body的時候,它會自動給你設(shè)置req.ContentLength...

所以,問題不是當(dāng)你Post一個ReadCloser的時候,就會變成chunked,而是你Post非這三種類型的body進來的時候都沒有Content-Length,需要自己顯式設(shè)置。代碼如下:

req, _ := http.NewRequest(method, url, bodyReader)
req.ContentLength = req.Header.Get("Content-Length")

0x02 | WriteHeader

這個問題就更蛋疼了。先看注釋

// WriteHeader sends an HTTP response header with status code.
// If WriteHeader is not called explicitly, the first call to Write
// will trigger an implicit WriteHeader(http.StatusOK).
// Thus explicit calls to WriteHeader are mainly used to
// send error codes.
WriteHeader(int)

注解只說了顯式調(diào)用一般是發(fā)送錯誤碼,一般不用調(diào)用,當(dāng)調(diào)用Write的時候默認(rèn)會設(shè)置http.StatusOK。但是這里卻沒有告訴我們在WriteHeader之后進行的任何Header操作都是不生效的!

這簡直就是個坑。因為我們經(jīng)常會根據(jù)不同的情況Write不同的Body,一般在Write body的時候才知道是不是需要進行一些特殊的header操作。本來想著,要不把WriteHeader放到最后去,但是發(fā)現(xiàn)最后設(shè)置的StatusCode壓根不生效。翻了下源碼才明白

func (w *response) WriteHeader(code int) {
    if w.conn.hijacked() {
        w.conn.server.logf("http: response.WriteHeader on hijacked connection")
        return
    }
    if w.wroteHeader {
        w.conn.server.logf("http: multiple response.WriteHeader calls")
        return
    }
    w.wroteHeader = true
    w.status = code

    if w.calledHeader && w.cw.header == nil {
        w.cw.header = w.handlerHeader.clone()
    }

    if cl := w.handlerHeader.get("Content-Length"); cl != "" {
        v, err := strconv.ParseInt(cl, 10, 64)
        if err == nil && v >= 0 {
            w.contentLength = v
        } else {
            w.conn.server.logf("http: invalid Content-Length of %q", cl)
            w.handlerHeader.Del("Content-Length")
        }
    }
}

// either dataB or dataS is non-zero.
func (w *response) write(lenData int, dataB []byte, dataS string) (n int, err error) {
    if w.conn.hijacked() {
        if lenData > 0 {
            w.conn.server.logf("http: response.Write on hijacked connection")
        }
        return 0, ErrHijacked
    }
    if !w.wroteHeader {
        w.WriteHeader(StatusOK)
    }
    if lenData == 0 {
        return 0, nil
    }
    if !w.bodyAllowed() {
        return 0, ErrBodyNotAllowed
    }

    w.written += int64(lenData) // ignoring errors, for errorKludge
    if w.contentLength != -1 && w.written > w.contentLength {
        return 0, ErrContentLength
    }
    if dataB != nil {
        return w.w.Write(dataB)
    } else {
        return w.w.WriteString(dataS)
    }
}

其實就是注解中那句話,如果沒有顯式調(diào)用WriteHeader,當(dāng)?shù)谝淮握{(diào)用Write的時候就會自動進行一次WriteHeader,所以,后續(xù)就不能再顯式調(diào)用WriteHeader了。。

這個坑逆天就逆天在你必須先Header.Set然后才能WriteHeader,最后才能Write。這個順序如果亂了,要不就是丟失StatusCode,要不就是丟失Header.

最終無奈之下我們只能遵循規(guī)則,在不同的判定條件里分別WriteHeader。。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容