percook

package module
v0.0.0-...-33ff5f6 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2020 License: MIT Imports: 6 Imported by: 0

README

percook: persistent cookiejar

percook makes any http.CookieJar implementations persistable.

This library requires Go 1.9 because using sync.Map.

This project was frozen!

Because some implementation of http.CookieJar Cookies(*url.URL) method (e.g. net/http/CookieJar) returns http.Cookie with Name & Value only, I must write up code infering Domain, Path and Expires. I finally wrote code can infer Domain and Path in percook.go (please see CookieJar.AllCookies() method), but there are no way to get Expires.

I started this project because I don't like to use libraries forked from standard libraries, but I currently accept net/http/CookieJar forked libraries. It's required to implement.

:(

Usage

Here is example with https://httpbin.org/'s Cookies endpoints.

package main

import (
	"fmt"
	"net/http"
	"net/http/cookiejar"

	"github.com/otofune/go-percook"
)

func panicGet(c *http.Client, url string) {
	if _, err := c.Get(url); err != nil {
		panic(err)
	}
}

func main() {
	httpJar, err := cookiejar.New(&cookiejar.Options{})
	if err != nil {
		panic(err)
	}
	pjar := percook.New(httpJar)
	client := http.Client{Jar: pjar}

	panicGet(&client, "https://httpbin.org/cookies/set/abc/123")
	panicGet(&client, "https://httpbin.org/cookies/set/edf/123")
	panicGet(&client, "https://httpbin.org/cookies/set/will_be_removed/100")
	panicGet(&client, "https://httpbin.org/cookies/delete?will_be_removed=")

	for u, cookies := range pjar.AllCookies() {
		for _, cookie := range cookies {
			fmt.Printf("%s: %s\n", u.String(), cookie.String())
		}
	}
}

Output expected:

$ # output
$ go run ./examples/readme
http://httpbin.org/: abc=123
http://httpbin.org/: edf=123
$

LICENSE

This project licensed under MIT unless otherwise specified.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CookieJar

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

func New

func New(jar http.CookieJar) *CookieJar

func (*CookieJar) AllCookies

func (pjar *CookieJar) AllCookies() CookiesMap

func (*CookieJar) Cookies

func (pjar *CookieJar) Cookies(u *url.URL) []*http.Cookie

func (*CookieJar) SetCookies

func (pjar *CookieJar) SetCookies(u *url.URL, cookies []*http.Cookie)

type CookiesMap

type CookiesMap map[*url.URL][]*http.Cookie

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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