version: 1.10

package cookiejar

import "net/http/cookiejar"

Overview

Package cookiejar implements an in-memory RFC 6265-compliant http.CookieJar.

Index

Examples

Package files

jar.go punycode.go

type Jar

  1. type Jar struct {
  2. // contains filtered or unexported fields
  3. }

Jar implements the http.CookieJar interface from the net/http package.

func New

  1. func New(o *Options) (*Jar, error)

New returns a new cookie jar. A nil *Options is equivalent to a zero Options.


Example:

  1. // Start a server to give us cookies.
  2. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  3. if cookie, err := r.Cookie("Flavor"); err != nil {
  4. http.SetCookie(w, &http.Cookie{Name: "Flavor", Value: "Chocolate Chip"})
  5. } else {
  6. cookie.Value = "Oatmeal Raisin"
  7. http.SetCookie(w, cookie)
  8. }
  9. }))
  10. defer ts.Close()
  11. u, err := url.Parse(ts.URL)
  12. if err != nil {
  13. log.Fatal(err)
  14. }
  15. // All users of cookiejar should import "golang.org/x/net/publicsuffix"
  16. jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
  17. if err != nil {
  18. log.Fatal(err)
  19. }
  20. client := &http.Client{
  21. Jar: jar,
  22. }
  23. if _, err = client.Get(u.String()); err != nil {
  24. log.Fatal(err)
  25. }
  26. fmt.Println("After 1st request:")
  27. for _, cookie := range jar.Cookies(u) {
  28. fmt.Printf(" %s: %s\n", cookie.Name, cookie.Value)
  29. }
  30. if _, err = client.Get(u.String()); err != nil {
  31. log.Fatal(err)
  32. }
  33. fmt.Println("After 2nd request:")
  34. for _, cookie := range jar.Cookies(u) {
  35. fmt.Printf(" %s: %s\n", cookie.Name, cookie.Value)
  36. }
  37. // Output:
  38. // After 1st request:
  39. // Flavor: Chocolate Chip
  40. // After 2nd request:
  41. // Flavor: Oatmeal Raisin

func (*Jar) Cookies

  1. func (j *Jar) Cookies(u *url.URL) (cookies []*http.Cookie)

Cookies implements the Cookies method of the http.CookieJar interface.

It returns an empty slice if the URL’s scheme is not HTTP or HTTPS.

func (*Jar) SetCookies

  1. func (j *Jar) SetCookies(u *url.URL, cookies []*http.Cookie)

SetCookies implements the SetCookies method of the http.CookieJar interface.

It does nothing if the URL’s scheme is not HTTP or HTTPS.

type Options

  1. type Options struct {
  2. // PublicSuffixList is the public suffix list that determines whether
  3. // an HTTP server can set a cookie for a domain.
  4. //
  5. // A nil value is valid and may be useful for testing but it is not
  6. // secure: it means that the HTTP server for foo.co.uk can set a cookie
  7. // for bar.co.uk.
  8. PublicSuffixList PublicSuffixList
  9. }

Options are the options for creating a new Jar.

type PublicSuffixList

  1. type PublicSuffixList interface {
  2. // PublicSuffix returns the public suffix of domain.
  3. //
  4. // TODO: specify which of the caller and callee is responsible for IP
  5. // addresses, for leading and trailing dots, for case sensitivity, and
  6. // for IDN/Punycode.
  7. PublicSuffix(domain string) string
  8.  
  9. // String returns a description of the source of this public suffix
  10. // list. The description will typically contain something like a time
  11. // stamp or version number.
  12. String() string
  13. }

PublicSuffixList provides the public suffix of a domain. For example:

  1. - the public suffix of "example.com" is "com",
  2. - the public suffix of "foo1.foo2.foo3.co.uk" is "co.uk", and
  3. - the public suffix of "bar.pvt.k12.ma.us" is "pvt.k12.ma.us".

Implementations of PublicSuffixList must be safe for concurrent use by multiple
goroutines.

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.

A public suffix list implementation is in the package
golang.org/x/net/publicsuffix.