Documentation ¶
Overview ¶
Package cookiejar implements http.CookieJar and saves cookie entries to a Repository.
Index ¶
- type Entry
- func (obj Entry) Creation() time.Time
- func (obj Entry) Domain() string
- func (obj Entry) Expires() time.Time
- func (obj Entry) HostOnly() bool
- func (obj Entry) HttpOnly() bool
- func (e *Entry) ID() string
- func (e Entry) IsExpiredAt(t time.Time) bool
- func (obj Entry) Key() string
- func (obj Entry) Name() string
- func (obj Entry) Order() int
- func (obj Entry) Path() string
- func (obj Entry) Persistent() bool
- func (obj Entry) SameSite() string
- func (obj Entry) Secure() bool
- func (obj Entry) Value() string
- type EntryIterator
- type EntryIteratorFunc
- type EntryRepository
- type Jar
- type MultiEntryRepository
- type Option
- type Options
- type PublicSuffixList
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Entry ¶
type Entry struct {
// contains filtered or unexported fields
}
Entry is the internal representation of a cookie.
This struct type is not used outside of this package per se, but the exported fields are those of RFC 6265.
func EntryFromRepository ¶
func EntryFromRepository( key string, name string, value string, domain string, path string, sameSite string, secure bool, httpOnly bool, persistent bool, hostOnly bool, expires time.Time, creation time.Time, order int, ) (obj *Entry, err error)
EntryFromRepository recreate object DO NOT use this as constructor
func (Entry) Order ¶ added in v0.2.0
Order used when path length and creation time is same. Only unique when entry created by same jar object.
func (Entry) Persistent ¶
type EntryIterator ¶
type EntryIteratorFunc ¶
type EntryRepository ¶
type EntryRepository interface { Find(ctx context.Context, key string) EntryIterator DeleteMany(ctx context.Context, id []string) (err error) Delete(ctx context.Context, id string) (err error) // Save should keep CreationTime and CreationIndex from previously saved entry. Save(ctx context.Context, entry Entry) (err error) }
func NewInMemoryEntryRepository ¶
func NewInMemoryEntryRepository() EntryRepository
func NewMultiEntryRepository ¶
func NewMultiEntryRepository(targets ...EntryRepository) EntryRepository
type Jar ¶
func New ¶
New returns a new cookie jar.
Example ¶
package main import ( "context" "fmt" "log" "net/http" "net/http/httptest" "net/url" "github.com/NateScarlet/cookiejar/pkg/cookiejar" ) func main() { // 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) } var ctx = context.Background() jar, err := cookiejar.New(ctx) 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 Chip After 2nd request: Flavor: Oatmeal Raisin
type MultiEntryRepository ¶
type MultiEntryRepository interface { EntryRepository }
MultiEntryRepository write to all, read from first non-empty result.
type Option ¶
type Option func(opts *Options)
func OptionEntryRepository ¶
func OptionEntryRepository(v EntryRepository) Option
OptionEntryRepository specify repository implementation, defaults to in-memory repository.
func OptionOnError ¶
OptionOnError defines error callback, defaults to panic(err).
func OptionPublicSuffixList ¶
func OptionPublicSuffixList(v PublicSuffixList) Option
OptionPublicSuffixList is the public suffix list that determines whether an HTTP server can set a cookie for a domain.
defaults to golang.org/x/net/publicsuffix.List
type Options ¶
type Options struct {
// contains filtered or unexported fields
}
Options are the options for creating a new Jar.
type PublicSuffixList ¶
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 }
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 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.