cookiejar

package
v1.21.6 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package cookiejar はメモリ内で RFC 6265 に準拠した http.CookieJar を実装します。

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Jar

type Jar struct {
	// contains filtered or unexported fields
}

Jarはnet/httpパッケージのhttp.CookieJarインターフェースを実装しています。

func New

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

Newは新しいクッキージャーを返します。nilの*OptionsはゼロのOptionsと同等です。

Example
// クッキーを提供するためのサーバを開始する。
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)
}

// cookiejarのすべてのユーザーは、"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 Chip
After 2nd request:
  Flavor: Oatmeal Raisin

func (*Jar) Cookies

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

Cookiesはhttp.CookieJarインターフェースのCookiesメソッドを実装しています。

URLのスキームがHTTPまたはHTTPSでない場合、空のスライスを返します。

func (*Jar) SetCookies

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

SetCookiesはhttp.CookieJarインターフェースのSetCookiesメソッドを実装します。

URLのスキームがHTTPまたはHTTPSでない場合、何もしません。

type Options

type Options struct {

	// PublicSuffixListは、ドメインに対してHTTPサーバがクッキーを設定できるかどうかを決定する公開サフィックスリストです。
	//
	// nilの値は有効であり、テストには便利ですが、セキュリティ上の理由から使用するべきではありません。これは、foo.co.ukのHTTPサーバがbar.co.ukに対してクッキーを設定できることを意味します。
	PublicSuffixList PublicSuffixList
}

Options は新しい 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はドメインの公開サフィックスを提供します。例えば:

  • "example.com"の公開サフィックスは「com」です。
  • "foo1.foo2.foo3.co.uk"の公開サフィックスは「co.uk」です。
  • "bar.pvt.k12.ma.us"の公開サフィックスは「pvt.k12.ma.us」です。

PublicSuffixListの実装は、複数のゴルーチンに対して安全に同時に使用できる必要があります。

常に""を返す実装は有効であり、テストには便利ですが、安全ではありません。これは、foo.comのHTTPサーバがbar.comのためにクッキーを設定できることを意味します。

golang.org/x/net/publicsuffixパッケージには、公開サフィックスリストの実装があります。

Jump to

Keyboard shortcuts

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