mozcookie

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2025 License: BSD-3-Clause Imports: 11 Imported by: 0

README

mozcookie

GoDoc CI License

mozcookie is a simple package providing tools to read and write cookies from and to files in the Netscape HTTP Cookie File format.

For more informations about this format, see:

Example

	cookies := []*http.Cookie{
		&http.Cookie{Name: "cookie-1", Value: "v$1", Domain: "golang.org", Path: "/pkg/"},
		&http.Cookie{Name: "cookie-2", Value: "v$2", Domain: "golang.org", Path: "/pkg/", Secure: true},
		&http.Cookie{Name: "cookie-3", Value: "v$3", Domain: "golang.org", Path: "/pkg/", HttpOnly: true},
	}
	err := mozcookie.Write("testdata/cookie.txt", cookies)
	if err != nil {
		log.Fatalf("could not write cookies: %+v", err)
	}

	cs, err := mozcookie.Read("testdata/cookie.txt")
	if err != nil {
		log.Fatalf("could not read cookies: %+v", err)
	}

	fmt.Printf("cookies:\n")
	for _, c := range cs {
		fmt.Printf("%s: %q\n", c.Name, c.Value)
	}

License

mozcookie is released under the BSD-3 license.

Documentation

Overview

Package mozcookie is a simple package providing tools to read and write cookies from and to files in the Netscape HTTP Cookie File format.

For more informations, see:

http://curl.haxx.se/rfc/cookie_spec.html
Example
package main

import (
	"fmt"
	"log"
	"net/http"
	"os"

	"codeberg.org/sbinet/mozcookie"
)

func main() {
	cookies := []*http.Cookie{
		&http.Cookie{Name: "cookie-1", Value: "v$1", Domain: "golang.org", Path: "/pkg/"},
		&http.Cookie{Name: "cookie-2", Value: "v$2", Domain: "golang.org", Path: "/pkg/", Secure: true},
		&http.Cookie{Name: "cookie-3", Value: "v$3", Domain: "golang.org", Path: "/pkg/", HttpOnly: true},
	}
	err := mozcookie.Write("testdata/cookie.txt", cookies)
	if err != nil {
		log.Fatalf("could not write cookies: %+v", err)
	}

	cs, err := mozcookie.Read("testdata/cookie.txt")
	if err != nil {
		log.Fatalf("could not read cookies: %+v", err)
	}

	fmt.Printf("cookies:\n")
	for _, c := range cs {
		fmt.Printf("%s: %q\n", c.Name, c.Value)
	}

	fmt.Printf("\ncookies-file:\n=============\n")
	o, err := os.ReadFile("./testdata/cookie.txt")
	if err != nil {
		log.Fatalf("could not read cookies file: %+v", err)
	}
	fmt.Printf("%s=============\n", o)

}
Output:

cookies:
cookie-1: "v$1"
cookie-2: "v$2"
cookie-3: "v$3"

cookies-file:
=============
# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This is a generated file!  Do not edit.

golang.org	FALSE	/pkg/	FALSE	0	cookie-1	v$1
golang.org	FALSE	/pkg/	TRUE	0	cookie-2	v$2
#HttpOnly_golang.org	FALSE	/pkg/	FALSE	0	cookie-3	v$3
=============

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(r io.Reader) ([]*http.Cookie, error)

func Encode

func Encode(w io.Writer, cookies []*http.Cookie) error

func Read

func Read(name string) ([]*http.Cookie, error)

Read returns the cookies stored in the name file.

func Write

func Write(name string, cookies []*http.Cookie) error

Types

type Encoder

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

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

func (*Encoder) Encode

func (enc *Encoder) Encode(c *http.Cookie) error

Jump to

Keyboard shortcuts

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