har

package
v3.0.0-...-4d1af02 Latest Latest
Warning

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

Go to latest
Published: May 26, 2024 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package har collects HTTP requests and responses and stores them in HAR format.

For more information on HAR, see: https://w3c.github.io/web-performance/specs/HAR/Overview.html

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewExportHandler

func NewExportHandler(l *Logger) http.Handler

NewExportHandler returns an http.Handler for requesting HAR logs.

func NewResetHandler

func NewResetHandler(l *Logger) http.Handler

NewResetHandler returns an http.Handler for clearing in-memory log entries.

Types

type Cache

type Cache struct {
}

Cache contains information about a request coming from browser cache.

type Content

type Content struct {
	// Size is the length of the returned content in bytes. Should be equal to
	// response.bodySize if there is no compression and bigger when the content
	// has been compressed.
	Size int64 `json:"size"`
	// MimeType is the MIME type of the response text (value of the Content-Type
	// response header).
	MimeType string `json:"mimeType"`
	// Text contains the response body sent from the server or loaded from the
	// browser cache. This field is populated with fully decoded version of the
	// respose body.
	Text []byte `json:"text,omitempty"`
	// The desired encoding to use for the text field when encoding to JSON.
	Encoding string `json:"encoding,omitempty"`
}

Content describes details about response content.

func (Content) MarshalJSON

func (c Content) MarshalJSON() ([]byte, error)

MarshalJSON marshals the byte slice into json after encoding based on c.Encoding.

func (*Content) UnmarshalJSON

func (c *Content) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the bytes slice into Content.

type Cookie struct {
	// Name is the cookie name.
	Name string `json:"name"`
	// Value is the cookie value.
	Value string `json:"value"`
	// Path is the path pertaining to the cookie.
	Path string `json:"path,omitempty"`
	// Domain is the host of the cookie.
	Domain string `json:"domain,omitempty"`
	// Expires contains cookie expiration time.
	Expires time.Time `json:"-"`
	// Expires8601 contains cookie expiration time in ISO 8601 format.
	Expires8601 string `json:"expires,omitempty"`
	// HTTPOnly is set to true if the cookie is HTTP only, false otherwise.
	HTTPOnly bool `json:"httpOnly,omitempty"`
	// Secure is set to true if the cookie was transmitted over SSL, false
	// otherwise.
	Secure bool `json:"secure,omitempty"`
}

Cookie is the data about a cookie on a request or response.

type Creator

type Creator struct {
	// Name of the log creator application.
	Name string `json:"name"`
	// Version of the log creator application.
	Version string `json:"version"`
}

Creator is the program responsible for generating the log. Martian, in this case.

type Entry

type Entry struct {
	// ID is the unique ID for the entry.
	ID string `json:"_id"`
	// StartedDateTime is the date and time stamp of the request start (ISO 8601).
	StartedDateTime time.Time `json:"startedDateTime"`
	// Time is the total elapsed time of the request in milliseconds.
	Time int64 `json:"time"`
	// Request contains the detailed information about the request.
	Request *Request `json:"request"`
	// Response contains the detailed information about the response.
	Response *Response `json:"response,omitempty"`
	// Cache contains information about a request coming from browser cache.
	Cache *Cache `json:"cache"`
	// Timings describes various phases within request-response round trip. All
	// times are specified in milliseconds.
	Timings *Timings `json:"timings"`
	// contains filtered or unexported fields
}

Entry is a individual log entry for a request or response.

type HAR

type HAR struct {
	Log *Log `json:"log"`
}

HAR is the top level object of a HAR log.

type Header struct {
	// Name is the header name.
	Name string `json:"name"`
	// Value is the header value.
	Value string `json:"value"`
}

Header is an HTTP request or response header.

type Log

type Log struct {
	// Version number of the HAR format.
	Version string `json:"version"`
	// Creator holds information about the log creator application.
	Creator *Creator `json:"creator"`
	// Entries is a list containing requests and responses.
	Entries []*Entry `json:"entries"`
}

Log is the HAR HTTP request and response log.

type Logger

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

Logger maintains request and response log entries.

func NewLogger

func NewLogger() *Logger

NewLogger returns a HAR logger. The returned logger logs all request post data and response bodies by default.

func (*Logger) Export

func (l *Logger) Export() *HAR

Export returns the in-memory log.

func (*Logger) ExportAndReset

func (l *Logger) ExportAndReset() *HAR

ExportAndReset returns the in-memory log for completed requests, clearing them.

func (*Logger) ModifyRequest

func (l *Logger) ModifyRequest(req *http.Request) error

ModifyRequest logs requests.

func (*Logger) ModifyResponse

func (l *Logger) ModifyResponse(res *http.Response) error

ModifyResponse logs responses.

func (*Logger) RecordRequest

func (l *Logger) RecordRequest(id string, req *http.Request) error

RecordRequest logs the HTTP request with the given ID. The ID should be unique per request/response pair.

func (*Logger) RecordResponse

func (l *Logger) RecordResponse(id string, res *http.Response) error

RecordResponse logs an HTTP response, associating it with the previously-logged HTTP request with the same ID.

func (*Logger) Reset

func (l *Logger) Reset()

Reset clears the in-memory log of entries.

func (*Logger) SetOption

func (l *Logger) SetOption(opts ...Option)

SetOption sets configurable options on the logger.

type Option

type Option func(l *Logger)

Option is a configurable setting for the logger.

func BodyLogging

func BodyLogging(enabled bool) Option

BodyLogging returns an option that configures response body logging.

func BodyLoggingForContentTypes

func BodyLoggingForContentTypes(cts ...string) Option

BodyLoggingForContentTypes returns an option that logs response bodies based on opting in to the Content-Type of the response.

func PostDataLogging

func PostDataLogging(enabled bool) Option

PostDataLogging returns an option that configures request post data logging.

func PostDataLoggingForContentTypes

func PostDataLoggingForContentTypes(cts ...string) Option

PostDataLoggingForContentTypes returns an option that logs request bodies based on opting in to the Content-Type of the request.

func SkipBodyLoggingForContentTypes

func SkipBodyLoggingForContentTypes(cts ...string) Option

SkipBodyLoggingForContentTypes returns an option that logs response bodies based on opting out of the Content-Type of the response.

func SkipPostDataLoggingForContentTypes

func SkipPostDataLoggingForContentTypes(cts ...string) Option

SkipPostDataLoggingForContentTypes returns an option that logs request bodies based on opting out of the Content-Type of the request.

type Param

type Param struct {
	// Name of the posted parameter.
	Name string `json:"name"`
	// Value of the posted parameter.
	Value string `json:"value,omitempty"`
	// Filename of a posted file.
	Filename string `json:"fileName,omitempty"`
	// ContentType is the content type of a posted file.
	ContentType string `json:"contentType,omitempty"`
}

Param describes an individual posted parameter.

type PostData

type PostData struct {
	// MimeType is the MIME type of the posted data.
	MimeType string `json:"mimeType"`
	// Params is a list of posted parameters (in case of URL encoded parameters).
	Params []Param `json:"params"`
	// Text contains the posted data. Although its type is string, it may contain
	// binary data.
	Text string `json:"text"`
}

PostData describes posted data on a request.

func (*PostData) MarshalJSON

func (p *PostData) MarshalJSON() ([]byte, error)

MarshalJSON returns a JSON representation of binary PostData.

func (*PostData) UnmarshalJSON

func (p *PostData) UnmarshalJSON(data []byte) error

UnmarshalJSON populates PostData based on the []byte representation of the binary PostData.

type QueryString

type QueryString struct {
	// Name is the query parameter name.
	Name string `json:"name"`
	// Value is the query parameter value.
	Value string `json:"value"`
}

QueryString is a query string parameter on a request.

type Request

type Request struct {
	// Method is the request method (GET, POST, ...).
	Method string `json:"method"`
	// URL is the absolute URL of the request (fragments are not included).
	URL string `json:"url"`
	// HTTPVersion is the Request HTTP version (HTTP/1.1).
	HTTPVersion string `json:"httpVersion"`
	// Cookies is a list of cookies.
	Cookies []Cookie `json:"cookies"`
	// Headers is a list of headers.
	Headers []Header `json:"headers"`
	// QueryString is a list of query parameters.
	QueryString []QueryString `json:"queryString"`
	// PostData is the posted data information.
	PostData *PostData `json:"postData,omitempty"`
	// HeaderSize is the Total number of bytes from the start of the HTTP request
	// message until (and including) the double CLRF before the body. Set to -1
	// if the info is not available.
	HeadersSize int64 `json:"headersSize"`
	// BodySize is the size of the request body (POST data payload) in bytes. Set
	// to -1 if the info is not available.
	BodySize int64 `json:"bodySize"`
}

Request holds data about an individual HTTP request.

func NewRequest

func NewRequest(req *http.Request, withBody bool) (*Request, error)

NewRequest constructs and returns a Request from req. If withBody is true, req.Body is read to EOF and replaced with a copy in a bytes.Buffer. An error is returned (and req.Body may be in an intermediate state) if an error is returned from req.Body.Read.

type Response

type Response struct {
	// Status is the response status code.
	Status int `json:"status"`
	// StatusText is the response status description.
	StatusText string `json:"statusText"`
	// HTTPVersion is the Response HTTP version (HTTP/1.1).
	HTTPVersion string `json:"httpVersion"`
	// Cookies is a list of cookies.
	Cookies []Cookie `json:"cookies"`
	// Headers is a list of headers.
	Headers []Header `json:"headers"`
	// Content contains the details of the response body.
	Content *Content `json:"content"`
	// RedirectURL is the target URL from the Location response header.
	RedirectURL string `json:"redirectURL"`
	// HeadersSize is the total number of bytes from the start of the HTTP
	// request message until (and including) the double CLRF before the body.
	// Set to -1 if the info is not available.
	HeadersSize int64 `json:"headersSize"`
	// BodySize is the size of the request body (POST data payload) in bytes. Set
	// to -1 if the info is not available.
	BodySize int64 `json:"bodySize"`
}

Response holds data about an individual HTTP response.

func NewResponse

func NewResponse(res *http.Response, withBody bool) (*Response, error)

NewResponse constructs and returns a Response from resp. If withBody is true, resp.Body is read to EOF and replaced with a copy in a bytes.Buffer. An error is returned (and resp.Body may be in an intermediate state) if an error is returned from resp.Body.Read.

type Timings

type Timings struct {
	// Send is the time required to send HTTP request to the server.
	Send int64 `json:"send"`
	// Wait is the time spent waiting for a response from the server.
	Wait int64 `json:"wait"`
	// Receive is the time required to read entire response from server or cache.
	Receive int64 `json:"receive"`
}

Timings describes various phases within request-response round trip. All times are specified in milliseconds

Jump to

Keyboard shortcuts

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