cookiejar

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2024 License: BSD-3-Clause Imports: 20 Imported by: 1

README

cookiejar

-- import "github.com/juju/persistent-cookiejar"

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

This implementation is a fork of net/http/cookiejar which also implements methods for dumping the cookies to persistent storage and retrieving them.

Usage

func DefaultCookieFile
func DefaultCookieFile() string

DefaultCookieFile returns the default cookie file to use for persisting cookie data. The following names will be used in decending order of preference:

- the value of the $GOCOOKIES environment variable.
- $HOME/.go-cookies
type Jar
type Jar struct {
}

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

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

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

New will return an error if the cookies could not be loaded from the file for any reason than if the file does not exist.

func (*Jar) Cookies
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) Save
func (j *Jar) Save() error

Save saves the cookies to the persistent cookie file. Before the file is written, it reads any cookies that have been stored from it and merges them into j.

func (*Jar) SetCookies
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
type Options struct {
	// PublicSuffixList is the public suffix list that determines whether
	// an HTTP server can set a cookie for a domain.
	//
	// If this is nil, the public suffix list implementation in golang.org/x/net/publicsuffix
	// is used.
	PublicSuffixList PublicSuffixList

	// Filename holds the file to use for storage of the cookies.
	// If it is empty, the value of DefaultCookieFile will be used.
	Filename string
}

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.

Documentation

Overview

cookiejar 包实现了内存中符合 RFC 6265 的 http.CookieJar。

这个实现是 net/http/cookiejar 的一个分支,它也 实现将 cookie 转储到持久化的方法 存储和检索它们。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultCookieFile

func DefaultCookieFile() string

DefaultCookieFile 返回要使用的默认 cookie 文件 用于持久化 cookie 数据。 以下名称将按优先级降序使用: -$GOCOOKIES 环境变量的值。 -$HOME/.go-cookies

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 返回一个新的 cookie jar。 nil *Options 相当于零 选项。

如果 cookie 无法加载,New 将返回错误 出于任何原因从文件中读取,而不是文件不存在。

func (*Jar) AllCookies

func (j *Jar) AllCookies() (cookies []*http.Cookie)

AllCookies 返回 jar 中的所有 cookie。返回的cookie将 填写了 Domain、Expires、HttpOnly、Name、Secure、Path 和 Value 出去。过期的 cookie 将不会被退回。该功能不 修改cookie jar。

func (*Jar) Cookies

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

func (j *Jar) LoadJSON(str string) error

将cookie加载到当前jar中

func (*Jar) MarshalJSON

func (j *Jar) MarshalJSON() ([]byte, error)

MarshalJSON 通过编码所有持久性 cookie 来实现 json.Marshaler 当前在罐子中。

func (*Jar) RemoveAll

func (j *Jar) RemoveAll()

RemoveAll 会从罐子中删除所有 cookie。

func (*Jar) RemoveAllHost

func (j *Jar) RemoveAllHost(host string)

RemoveAllHost 从 jar 中删除为给定主机设置的所有 cookie。

func (*Jar) RemoveCookie

func (j *Jar) RemoveCookie(c *http.Cookie)

RemoveCookie 删除与名称、域和路径匹配的cookie 由 c 指定。

func (*Jar) Save

func (j *Jar) Save() error

Save saves the cookies to the persistent cookie file. Before the file is written, it reads any cookies that have been stored from it and merges them into j.

func (*Jar) SetCookies

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

SetCookies 实现了 http.CookieJar 接口的 SetCookies 方法。

如果 URL 的方案不是 HTTP 或 HTTPS,则不会执行任何操作。

func (*Jar) ToJSON

func (j *Jar) ToJSON() string

type Options

type Options struct {
	//PublicSuffixList为公共后缀列表,判断是否
	//HTTP 服务器可以为域设置 cookie。
	//
	//如果为 nil,则为 golang.org/x/net/publicsuffix 中的公共后缀列表实现
	//被使用。
	PublicSuffixList PublicSuffixList

	//Filename 保存用于存储 cookie 的文件。
	//如果为空,则使用 DefaultCookieFile 的值。
	Filename string

	//NoPersist 指定是否不应该使用持久性
	//(对于测试有用)。如果这是真的,文件名的值将是
	//被忽略。
	NoPersist bool
}

Options 是创建新 Jar 的选项。

type PublicSuffixList

type PublicSuffixList interface {
	//PublicSuffix 返回域的公共后缀。
	//
	//TODO: 指定调用者和被调用者中的哪一个负责 IP
	//地址,前导点和尾随点,区分大小写,以及
	//对于 IDN/Punycode。
	PublicSuffix(domain string) string

	//String 返回此公共后缀来源的描述
	//列表。描述通常会包含诸如时间之类的内容
	//标记或版本号。
	String() string
}

PublicSuffixList 提供域的公共后缀。例如: -“example.com”的公共后缀是“com”, -“foo1.foo2.foo3.co.uk”的公共后缀是“co.uk”,并且 -“bar.pvt.k12.ma.us”的公共后缀是“pvt.k12.ma.us”。

PublicSuffixList 的实现必须对于并发使用是安全的 多个 goroutine。

始终返回“”的实现是有效的并且可能对 测试但不安全:这意味着 foo.com 的 HTTP 服务器可以 为 bar.com 设置 cookie。

包中包含公共后缀列表实现 golang.org/x/net/publicsuffix。

Jump to

Keyboard shortcuts

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