cookiejar

package module
v0.0.0-...-344320c Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2018 License: MIT Imports: 4 Imported by: 0

README

cookiejar

CookieJar for fasthttp.

Example

package main

import (
	"fmt"

	"github.com/dgrr/cookiejar"
	"github.com/valyala/fasthttp"
)

func main() {
	server := fasthttp.Server{
		Handler: handler,
	}
	go server.ListenAndServe(":8080")

	doRequest("http://localhost:8080")
	server.Shutdown()
}

func handler(ctx *fasthttp.RequestCtx) {
	// Acquire cookie jar
	cj := cookiejar.AcquireCookieJar()
	defer cookiejar.ReleaseCookieJar(cj)

	// filling cookiejar
	cj.Set("Hello", "world")
	cj.Set("make", "fasthttp")
	cj.Set("great", "again")

	// writing values to the response
	cj.FillResponse(&ctx.Response)
}

func doRequest(addr string) {
	req, res := fasthttp.AcquireRequest(), fasthttp.AcquireResponse()
	defer fasthttp.ReleaseRequest(req)
	defer fasthttp.ReleaseResponse(res)

	// Acquire cookie jar
	cj := cookiejar.AcquireCookieJar()
	defer cookiejar.ReleaseCookieJar(cj)

	req.SetRequestURI(addr)
	req.SetConnectionClose()
	err := fasthttp.Do(req, res)
	if err != nil {
		panic(err)
	}
	// Read cookies from the response
	cj.ReadResponse(res)

	for {
		// Read cookie by cookie
		c := cj.Get()
		if c == nil {
			break
		}
		fmt.Printf("Collected cookies: %s=%s\n", c.Key(), c.Value())
		fasthttp.ReleaseCookie(c)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReleaseCookieJar

func ReleaseCookieJar(c *CookieJar)

ReleaseCookieJar returns CookieJar to the pool

Types

type CookieJar

type CookieJar map[string]*fasthttp.Cookie

CookieJar is container of cookies

This object is used to handle multiple cookies

func AcquireCookieJar

func AcquireCookieJar() *CookieJar

AcquireCookieJar returns an empty CookieJar object from pool

func (*CookieJar) FillRequest

func (cj *CookieJar) FillRequest(r *fasthttp.Request)

FillRequest dumps all cookies stored in cj into Request adding this values to Cookie header.

func (*CookieJar) FillResponse

func (cj *CookieJar) FillResponse(r *fasthttp.Response)

FillResponse dumps all cookies stored in cj into Response adding this values to Cookie header.

func (*CookieJar) Get

func (cj *CookieJar) Get() *fasthttp.Cookie

Get returns and delete a value from cookiejar.

func (*CookieJar) Peek

func (cj *CookieJar) Peek(key string) *fasthttp.Cookie

Peek peeks cookie value using key.

This function does not delete cookie

func (*CookieJar) PeekValue

func (cj *CookieJar) PeekValue(key string) []byte

PeekValue returns value of specified cookie-key.

func (*CookieJar) Put

func (cj *CookieJar) Put(cookie *fasthttp.Cookie)

SetCookie sets cookie using its key.

After that you can use Peek or Get function to get cookie value.

func (*CookieJar) ReadRequest

func (cj *CookieJar) ReadRequest(r *fasthttp.Request)

ReadRequest gets all cookies from a Request reading Set-Cookie header.

func (*CookieJar) ReadResponse

func (cj *CookieJar) ReadResponse(r *fasthttp.Response)

ReadResponse gets all Response cookies reading Set-Cookie header.

func (*CookieJar) Release

func (cj *CookieJar) Release()

Release releases all cookie values.

func (*CookieJar) ReleaseCookie

func (cj *CookieJar) ReleaseCookie(key string)

ReleaseCookie releases a cookie specified by parsed key.

func (*CookieJar) Set

func (cj *CookieJar) Set(key, value string)

Set sets cookie using key-value

This function can replace an existent cookie

func (*CookieJar) SetBytesK

func (cj *CookieJar) SetBytesK(key []byte, value string)

SetBytesK sets cookie using key=value

This function can replace an existent cookie.

func (*CookieJar) SetBytesKV

func (cj *CookieJar) SetBytesKV(key, value []byte)

SetBytesKV sets cookie using key=value

This function can replace an existent cookie.

func (*CookieJar) SetBytesV

func (cj *CookieJar) SetBytesV(key string, value []byte)

SetBytesV sets cookie using key=value

This function can replace an existent cookie.

func (*CookieJar) WriteTo

func (cj *CookieJar) WriteTo(w io.Writer) (n int64, err error)

WriteTo writes all cookies representation to w.

Jump to

Keyboard shortcuts

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