cookiejar
packagestandard libraryThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
Documentation¶
Overview¶
Package cookiejar implements an in-memoryRFC 6265-compliant http.CookieJar.
Index¶
Examples¶
Constants¶
This section is empty.
Variables¶
This section is empty.
Functions¶
This section is empty.
Types¶
typeJar¶
type Jar struct {// contains filtered or unexported fields}Jar implements the http.CookieJar interface from the net/http package.
funcNew¶
New returns a new cookie jar. A nil*Options is equivalent to a zeroOptions.
Example¶
// 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, &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(&cookiejar.Options{PublicSuffixList: publicsuffix.List})if err != nil {log.Fatal(err)}client := &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)}Output:After 1st request: Flavor: Chocolate ChipAfter 2nd request: Flavor: Oatmeal Raisin
func (*Jar)Cookies¶
Cookies implements the Cookies method of thehttp.CookieJar interface.
It returns an empty slice if the URL's scheme is not HTTP or HTTPS.
func (*Jar)SetCookies¶
SetCookies implements the SetCookies method of thehttp.CookieJar interface.
It does nothing if the URL's scheme is not HTTP or HTTPS.
typeOptions¶
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.PublicSuffixListPublicSuffixList}Options are the options for creating a new Jar.
typePublicSuffixList¶
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(domainstring)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}PublicSuffixList provides the public suffix of a domain. For example:
- the public suffix of "example.com" is "com",
- the public suffix of "foo1.foo2.foo3.co.uk" is "co.uk", and
- the public suffix of "bar.pvt.k12.ma.us" is "pvt.k12.ma.us".
Implementations of PublicSuffixList must be safe for concurrent use bymultiple goroutines.
An implementation that always returns "" is valid and may be useful fortesting but it is not secure: it means that the HTTP server for foo.com canset a cookie for bar.com.
A public suffix list implementation is in the packagegolang.org/x/net/publicsuffix.