关于cookiejar用法的疑问

看文章上写cookiejar会自动帮忙管理服务端返回的cookies,于是我做了个实验。

实现服务端

package main

import (
    "fmt"
    "net/http"
    "strconv"
    "time"
)

var i int

func receiveReq(w http.ResponseWriter, r *http.Request) {
    //i记录请求的次数
    i++
    fmt.Printf("第 %d 次请求的Cookie: ", i)
    fmt.Println(r.Cookies())
    //设置cookie
    tNow := time.Now()
    cookie := &http.Cookie{
        Name:    "XMEN",
        Value:   "STORM" + strconv.Itoa(i),
        Expires: tNow.AddDate(1, 0, i),
    }
    http.SetCookie(w, cookie)
    //返回信息
    w.Write(([]byte("your cookie has been received")))

}

func main() {
    fmt.Println("Server Start Now!")
    http.HandleFunc("/test", receiveReq)
    err := http.ListenAndServe("127.0.0.1:8889", nil)
    if err != nil {
        fmt.Println("ListenAndServe ERROR: ", err)
    }

}

实现client端

package main

import (
    "fmt"
    "net/http"
    "net/http/cookiejar"
)

func main() {
    jar, _ := cookiejar.New(nil)
    fmt.Println("Start Request Server")
    client := http.Client{
        Jar: jar,
    }
    url := "http://127.0.0.1:8889/test"
    req, _ := http.NewRequest("GET", url, nil)
    //第一次发请求
    client.Do(req)
    fmt.Printf("第一次 %s \n", req.Cookies())

    //第二次发请求
    client.Do(req)
    fmt.Printf("第二次 %s \n", req.Cookies())

    //第三次发请求
    client.Do(req)
    fmt.Printf("第三次 %s \n", req.Cookies())

    //第四次发请求
    client.Do(req)
    fmt.Printf("第四次 %s \n", req.Cookies())

    //第五次发请求
    client.Do(req)
    fmt.Printf("第五次 %s \n", req.Cookies())

}

先通过浏览器请求5次,再通过client端请求5次

Server Start Now! 第 1 次请求的Cookie: [] 第 2 次请求的Cookie: [XMEN=STORM1] 第 3 次请求的Cookie: [XMEN=STORM2] 第 4 次请求的Cookie: [XMEN=STORM3] 第 5 次请求的Cookie: [XMEN=STORM4] 第 6 次请求的Cookie: [] 第 7 次请求的Cookie: [XMEN=STORM6] 第 8 次请求的Cookie: [XMEN=STORM6 XMEN=STORM7] 第 9 次请求的Cookie: [XMEN=STORM6 XMEN=STORM7 XMEN=STORM8] 第 10 次请求的Cookie: [XMEN=STORM6 XMEN=STORM7 XMEN=STORM8 XMEN=STORM9]

那么问题来了:cookiejar为什么会存留所有历史cookie

已邀请:

stirlingx - https://github.com/liyue201

赞同来自: huhuyou2 niugou

http包里面是这样的。

// SetCookie adds a Set-Cookie header to the provided ResponseWriter's headers.
// The provided cookie must have a valid Name. Invalid cookies may be
// silently dropped.
func SetCookie(w ResponseWriter, cookie *Cookie) {
    if v := cookie.String(); v != "" {
        w.Header().Add("Set-Cookie", v)
    }
}

所以你的代码可以改成这样

w.Header().Set("Set-Cookie", cookie.String())

这就是go开源的好处,遇到问题的时候可以先跟踪源码,很多时候会发现问题其实很简单。

caoqianli - Programing

赞同来自: huhuyou2

你这个问题是因为多次http请求reuse了同一个request 实例吧,每次新new一个就没问题了。

huhuyou2 - fish

赞同来自:

上面的问题我下午又跟踪了一下代码,如下:

从Do开始

    //从Do开始
    client.Do(req)

进入

func (c *Client) Do(req *Request) (*Response, error) {
    if req.URL == nil {
        req.closeBody()
        return nil, errors.New("http: nil Request.URL")
    }

    var (
        deadline    = c.deadline()
        reqs        []*Request
        resp        *Response
        copyHeaders = c.makeHeadersCopier(req)

        // Redirect behavior:
        redirectMethod string
        includeBody    bool
    )

然后,看到copyHeaders = c.makeHeadersCopier(req)

// makeHeadersCopier makes a function that copies headers from the
// initial Request, ireq. For every redirect, this function must be called
// so that it can copy headers into the upcoming Request.
func (c *Client) makeHeadersCopier(ireq *Request) func(*Request) {
    // The headers to copy are from the very initial request.
    // We use a closured callback to keep a reference to these original headers.
    var (
        ireqhdr  = ireq.Header.clone()
        icookies map[string][]*Cookie
    )
    if c.Jar != nil && ireq.Header.Get("Cookie") != "" {
        icookies = make(map[string][]*Cookie)
        for _, c := range ireq.Cookies() {
            icookies[c.Name] = append(icookies[c.Name], c)
        }
    }

貌似不在这里面,就去看看发送的过程

// didTimeout is non-nil only if err != nil.
func (c *Client) send(req *Request, deadline time.Time) (resp *Response, didTimeout func() bool, err error) {
    if c.Jar != nil {
        for _, cookie := range c.Jar.Cookies(req.URL) {
            req.AddCookie(cookie)
        }

嗯,貌似找到了

// AddCookie adds a cookie to the request. Per RFC 6265 section 5.4,
// AddCookie does not attach more than one Cookie header field. That
// means all cookies, if any, are written into the same line,
// separated by semicolon.
func (r *Request) AddCookie(c *Cookie) {
    s := fmt.Sprintf("%s=%s", sanitizeCookieName(c.Name), sanitizeCookieValue(c.Value))
    if c := r.Header.Get("Cookie"); c != "" {
        r.Header.Set("Cookie", c+"; "+s)
    } else {
        r.Header.Set("Cookie", s)
    }
}

看了下变量监视中的值,r里面存的上次请求的request struct,里面的header存的上次发送时候的cookie,c里面是c.Jar.Cookies(req.URL),即[]*Cookie

    // Cookies returns the cookies to send in a request for the given URL.
    // It is up to the implementation to honor the standard cookie use
    // restrictions such as in RFC 6265. (大坑)
    Cookies(u *url.URL) []*Cookie

可是这AddCookie中c的语法完全没懂,参数里面传了个c,后面又

c := r.Header.Get("Cookie")

c的类型变成了string,真是难以理解,还得再琢磨琢磨

要回复问题请先登录注册