cookiejar

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 15, 2022 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Overview

Package cookiejar implements http.CookieJar and saves cookie entries to a Repository.

Index

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) Creation

func (obj Entry) Creation() time.Time

func (Entry) Domain

func (obj Entry) Domain() string

func (Entry) Expires

func (obj Entry) Expires() time.Time

func (Entry) HostOnly

func (obj Entry) HostOnly() bool

func (Entry) HttpOnly

func (obj Entry) HttpOnly() bool

func (*Entry) ID

func (e *Entry) ID() string

ID returns the domain;path;name triple of e as an ID.

func (Entry) IsExpiredAt

func (e Entry) IsExpiredAt(t time.Time) bool

func (Entry) Key

func (obj Entry) Key() string

func (Entry) Name

func (obj Entry) Name() string

func (Entry) Order added in v0.2.0

func (obj Entry) Order() int

Order used when path length and creation time is same. Only unique when entry created by same jar object.

func (Entry) Path

func (obj Entry) Path() string

func (Entry) Persistent

func (obj Entry) Persistent() bool

func (Entry) SameSite

func (obj Entry) SameSite() string

func (Entry) Secure

func (obj Entry) Secure() bool

func (Entry) Value

func (obj Entry) Value() string

type EntryIterator

type EntryIterator interface {
	ForEach(cb func(i Entry) (err error)) (err error)
}

type EntryIteratorFunc

type EntryIteratorFunc func(cb func(i Entry) (err error)) (err error)

func (EntryIteratorFunc) ForEach

func (fn EntryIteratorFunc) ForEach(cb func(i Entry) (err error)) (err error)

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

type Jar interface {
	http.CookieJar
}

func New

func New(ctx context.Context, options ...Option) (Jar, error)

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

func OptionOnError(v func(err error)) Option

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL