summaryrefslogtreecommitdiff
path: root/devdocs/go/net%2Fhttp%2Fcookiejar%2Findex.html
blob: 68b22eea76d1b18be9b02a36feb5f024be550d91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<h1> Package cookiejar  </h1>     <ul id="short-nav">
<li><code>import "net/http/cookiejar"</code></li>
<li><a href="#pkg-overview" class="overviewLink">Overview</a></li>
<li><a href="#pkg-index" class="indexLink">Index</a></li>
<li><a href="#pkg-examples" class="examplesLink">Examples</a></li>
</ul>     <h2 id="pkg-overview">Overview </h2> <p>Package cookiejar implements an in-memory RFC 6265-compliant http.CookieJar. </p>     <h2 id="pkg-index">Index </h2>  <ul id="manual-nav">
<li><a href="#Jar">type Jar</a></li>
<li> <a href="#New">func New(o *Options) (*Jar, error)</a>
</li>
<li> <a href="#Jar.Cookies">func (j *Jar) Cookies(u *url.URL) (cookies []*http.Cookie)</a>
</li>
<li> <a href="#Jar.SetCookies">func (j *Jar) SetCookies(u *url.URL, cookies []*http.Cookie)</a>
</li>
<li><a href="#Options">type Options</a></li>
<li><a href="#PublicSuffixList">type PublicSuffixList</a></li>
</ul> <div id="pkg-examples"> <h3>Examples</h3>  <dl> <dd><a class="exampleLink" href="#example_New">New</a></dd> </dl> </div> <h3>Package files</h3> <p>  <span>jar.go</span> <span>punycode.go</span>  </p>   <h2 id="Jar">type <span>Jar</span>  <span title="Added in Go 1.1">1.1</span> </h2> <p>Jar implements the http.CookieJar interface from the net/http package. </p>
<pre data-language="go">type Jar struct {
    // contains filtered or unexported fields
}
</pre> <h3 id="New">func <span>New</span>  <span title="Added in Go 1.1">1.1</span> </h3> <pre data-language="go">func New(o *Options) (*Jar, error)</pre> <p>New returns a new cookie jar. A nil <a href="#Options">*Options</a> is equivalent to a zero Options. </p>   <h4 id="example_New"> <span class="text">Example</span>
</h4> <p>Code:</p> <pre class="code" data-language="go">// Start a server to give us cookies.
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    if cookie, err := r.Cookie("Flavor"); err != nil {
        http.SetCookie(w, &amp;http.Cookie{Name: "Flavor", Value: "Chocolate Chip"})
    } else {
        cookie.Value = "Oatmeal Raisin"
        http.SetCookie(w, cookie)
    }
}))
defer ts.Close()

u, err := url.Parse(ts.URL)
if err != nil {
    log.Fatal(err)
}

// All users of cookiejar should import "golang.org/x/net/publicsuffix"
jar, err := cookiejar.New(&amp;cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
    log.Fatal(err)
}

client := &amp;http.Client{
    Jar: jar,
}

if _, err = client.Get(u.String()); err != nil {
    log.Fatal(err)
}

fmt.Println("After 1st request:")
for _, cookie := range jar.Cookies(u) {
    fmt.Printf("  %s: %s\n", cookie.Name, cookie.Value)
}

if _, err = client.Get(u.String()); err != nil {
    log.Fatal(err)
}

fmt.Println("After 2nd request:")
for _, cookie := range jar.Cookies(u) {
    fmt.Printf("  %s: %s\n", cookie.Name, cookie.Value)
}
</pre> <p>Output:</p> <pre class="output" data-language="go">After 1st request:
  Flavor: Chocolate Chip
After 2nd request:
  Flavor: Oatmeal Raisin
</pre>   <h3 id="Jar.Cookies">func (*Jar) <span>Cookies</span>  <span title="Added in Go 1.1">1.1</span> </h3> <pre data-language="go">func (j *Jar) Cookies(u *url.URL) (cookies []*http.Cookie)</pre> <p>Cookies implements the Cookies method of the <span>http.CookieJar</span> interface. </p>
<p>It returns an empty slice if the URL's scheme is not HTTP or HTTPS. </p>
<h3 id="Jar.SetCookies">func (*Jar) <span>SetCookies</span>  <span title="Added in Go 1.1">1.1</span> </h3> <pre data-language="go">func (j *Jar) SetCookies(u *url.URL, cookies []*http.Cookie)</pre> <p>SetCookies implements the SetCookies method of the <span>http.CookieJar</span> interface. </p>
<p>It does nothing if the URL's scheme is not HTTP or HTTPS. </p>
<h2 id="Options">type <span>Options</span>  <span title="Added in Go 1.1">1.1</span> </h2> <p>Options are the options for creating a new Jar. </p>
<pre data-language="go">type Options struct {
    // PublicSuffixList is the public suffix list that determines whether
    // an HTTP server can set a cookie for a domain.
    //
    // A nil value is valid and may be useful for testing but it is not
    // secure: it means that the HTTP server for foo.co.uk can set a cookie
    // for bar.co.uk.
    PublicSuffixList PublicSuffixList
}
</pre> <h2 id="PublicSuffixList">type <span>PublicSuffixList</span>  <span title="Added in Go 1.1">1.1</span> </h2> <p>PublicSuffixList provides the public suffix of a domain. For example: </p>
<ul> <li>the public suffix of "example.com" is "com", </li>
<li>the public suffix of "foo1.foo2.foo3.co.uk" is "co.uk", and </li>
<li>the public suffix of "bar.pvt.k12.ma.us" is "pvt.k12.ma.us". </li>
</ul> <p>Implementations of PublicSuffixList must be safe for concurrent use by multiple goroutines. </p>
<p>An implementation that always returns "" is valid and may be useful for testing but it is not secure: it means that the HTTP server for foo.com can set a cookie for bar.com. </p>
<p>A public suffix list implementation is in the package golang.org/x/net/publicsuffix. </p>
<pre data-language="go">type PublicSuffixList interface {
    // PublicSuffix returns the public suffix of domain.
    //
    // TODO: specify which of the caller and callee is responsible for IP
    // addresses, for leading and trailing dots, for case sensitivity, and
    // for IDN/Punycode.
    PublicSuffix(domain string) string

    // String returns a description of the source of this public suffix
    // list. The description will typically contain something like a time
    // stamp or version number.
    String() string
}</pre><div class="_attribution">
  <p class="_attribution-p">
    &copy; Google, Inc.<br>Licensed under the Creative Commons Attribution License 3.0.<br>
    <a href="http://golang.org/pkg/net/http/cookiejar/" class="_attribution-link">http://golang.org/pkg/net/http/cookiejar/</a>
  </p>
</div>