httpprot

package
v2.7.3 Latest Latest
Warning

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

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

Documentation

Overview

Package httpprot implements the HTTP protocol.

Index

Constants

View Source
const DefaultMaxPayloadSize = 4 * 1024 * 1024

DefaultMaxPayloadSize is the default max allowed payload size.

Variables

View Source
var (
	// ErrRequestEntityTooLarge means the request entity is too large.
	ErrRequestEntityTooLarge = fmt.Errorf("request entity too large")
)
View Source
var ErrResponseEntityTooLarge = fmt.Errorf("response entity too large, you may need to increase 'serverMaxBodySize' or set it to -1")

ErrResponseEntityTooLarge means the request entity is too large.

Functions

func RequestScheme

func RequestScheme(r *http.Request) string

RequestScheme returns the scheme of the request.

Types

type Header struct {
	http.Header
}

Header wraps the http header.

func (*Header) Add

func (h *Header) Add(key string, value interface{})

Add adds the key value pair to the header.

func (*Header) Clone

func (h *Header) Clone() protocols.Header

Clone returns a copy of h.

func (*Header) Del

func (h *Header) Del(key string)

Del deletes the values associated with key.

func (*Header) Get

func (h *Header) Get(key string) interface{}

Get gets the first value associated with the given key.

func (*Header) Set

func (h *Header) Set(key string, value interface{})

Set sets the header entries associated with key to value.

func (*Header) Walk

func (h *Header) Walk(fn func(key string, value interface{}) bool)

Walk calls fn for each key value pair of the header.

type Protocol

type Protocol struct{}

Protocol implements protocols.Protocol for HTTP.

func (*Protocol) BuildRequest

func (p *Protocol) BuildRequest(reqInfo interface{}) (protocols.Request, error)

BuildRequest builds and returns a request according to the given reqInfo.

func (*Protocol) BuildResponse

func (p *Protocol) BuildResponse(respInfo interface{}) (protocols.Response, error)

BuildResponse builds and returns a response according to the given respInfo.

func (*Protocol) CreateRequest

func (p *Protocol) CreateRequest(req interface{}) (protocols.Request, error)

CreateRequest creates a new request. The input argument should be a *http.Request or nil.

The caller need to handle the close of the body of the input request, if it need to be closed. Particularly, the body of the input request may be replaced, so the caller must save a reference of the original body and close it when it is no longer needed.

func (*Protocol) CreateResponse

func (p *Protocol) CreateResponse(resp interface{}) (protocols.Response, error)

CreateResponse creates a new response. The input argument should be a *http.Response or nil.

The caller need to handle the close of the body of the input response, if it need to be closed. Particularly, the body of the input response may be replaced, so the caller must save a reference of the original body and close it when it is no longer needed.

func (*Protocol) NewRequestInfo

func (p *Protocol) NewRequestInfo() interface{}

NewRequestInfo returns a new requestInfo.

func (*Protocol) NewResponseInfo

func (p *Protocol) NewResponseInfo() interface{}

NewResponseInfo returns a new responseInfo.

type Request

type Request struct {
	*http.Request
	// contains filtered or unexported fields
}

Request wraps http.Request.

The payload of the request can be replaced with a new one, but it will never replace the body of the underlying http.Request.

Code should always use payload functions of this request to read the body of the original request, and never use the Body of the original request directly.

func NewRequest

func NewRequest(stdr *http.Request) (*Request, error)

NewRequest creates a new request from a standard request. If stdr is not nil, FetchPayload must be called before any read of the request body.

func (*Request) AddCookie

func (r *Request) AddCookie(cookie *http.Cookie)

AddCookie add a cookie to the request.

func (*Request) Close

func (r *Request) Close()

Close closes the request.

func (*Request) Context

func (r *Request) Context() context.Context

Context returns the request context.

func (*Request) Cookie

func (r *Request) Cookie(name string) (*http.Cookie, error)

Cookie returns the named cookie.

func (*Request) Cookies

func (r *Request) Cookies() []*http.Cookie

Cookies returns all cookies.

func (*Request) FetchPayload

func (r *Request) FetchPayload(maxPayloadSize int64) error

FetchPayload reads the body of the underlying http.Request and initializes the payload.

if maxPayloadSize is a negative number, the payload is treated as a stream. if maxPayloadSize is zero, DefaultMaxPayloadSize is used.

func (*Request) GetPayload

func (r *Request) GetPayload() io.Reader

GetPayload returns a payload reader. For non-stream payload, the returned reader is always a new one, which contains the full data. For stream payload, the function always returns the same reader.

func (*Request) HTTPHeader

func (r *Request) HTTPHeader() http.Header

HTTPHeader returns the header of the request in type http.Header.

func (*Request) Header

func (r *Request) Header() protocols.Header

Header returns the header of the request in type protocols.Header.

func (*Request) Host

func (r *Request) Host() string

Host returns host of the request.

func (*Request) IsStream

func (r *Request) IsStream() bool

IsStream returns whether the payload of the request is a stream.

func (*Request) MetaSize

func (r *Request) MetaSize() int64

MetaSize returns the meta data size of the request.

func (*Request) Method

func (r *Request) Method() string

Method returns method of the request.

func (*Request) Path

func (r *Request) Path() string

Path returns path.

func (*Request) PayloadSize

func (r *Request) PayloadSize() int64

PayloadSize returns the size of the payload. If the payload is a stream, it returns the bytes count that have been currently read out.

func (*Request) Proto

func (r *Request) Proto() string

Proto returns proto of the request.

func (*Request) RawPayload

func (r *Request) RawPayload() []byte

RawPayload returns the payload in []byte, the caller should not modify its content. The function panic if the payload is a stream.

func (*Request) RealIP

func (r *Request) RealIP() string

RealIP returns the real IP of the request. TODO: if a request is cloned and modified, RealIP maybe wrong.

func (*Request) Scheme

func (r *Request) Scheme() string

Scheme returns the scheme of the request.

func (*Request) SetHost

func (r *Request) SetHost(host string)

SetHost sets host.

func (*Request) SetMethod

func (r *Request) SetMethod(method string)

SetMethod sets the request method.

func (*Request) SetPath

func (r *Request) SetPath(path string)

SetPath sets path of the request.

func (*Request) SetPayload

func (r *Request) SetPayload(payload interface{})

SetPayload set the payload of the request to payload. The payload could be a string, a byte slice, or an io.Reader, and if it is an io.Reader, it will be treated as a stream, if this is not desired, please read the data to a byte slice, and set the byte slice as the payload.

func (*Request) Std

func (r *Request) Std() *http.Request

Std returns the underlying http.Request.

func (*Request) ToBuilderRequest

func (r *Request) ToBuilderRequest(name string) interface{}

ToBuilderRequest wraps the request and returns the wrapper, the return value can be used in the template of the Builder filters.

func (*Request) URL

func (r *Request) URL() *url.URL

URL returns url of the request.

type Response

type Response struct {
	// TODO: we only need StatusCode, Header and Body, that's can avoid
	// using the big http.Response object.
	*http.Response
	// contains filtered or unexported fields
}

Response wraps http.Response.

The payload of the response can be replaced with a new one, but it will never replace the body of the original http.Response.

Code should always use payload functions of this response to read the body of the original response, and never use the Body of the original response directly.

func NewResponse

func NewResponse(stdr *http.Response) (*Response, error)

NewResponse creates a new response from a standard response. If stdr is not nil, FetchPayload must be called before any read of the response body.

func (*Response) Close

func (r *Response) Close()

Close closes the response.

func (*Response) FetchPayload

func (r *Response) FetchPayload(maxPayloadSize int64) error

FetchPayload reads the body of the underlying http.Response and initializes the payload.

if maxPayloadSize is a negative number, the payload is treated as a stream. if maxPayloadSize is zero, DefaultMaxPayloadSize is used.

func (*Response) GetPayload

func (r *Response) GetPayload() io.Reader

GetPayload returns a payload reader. For non-stream payload, the returned reader is always a new one, which contains the full data. For stream payload, the function always returns the same reader.

func (*Response) HTTPHeader

func (r *Response) HTTPHeader() http.Header

HTTPHeader returns the header of the response in type http.Header.

func (*Response) Header

func (r *Response) Header() protocols.Header

Header returns the header of the response in type protocols.Header.

func (*Response) IsStream

func (r *Response) IsStream() bool

IsStream returns whether the payload of the response is a stream.

func (*Response) MetaSize

func (r *Response) MetaSize() int64

MetaSize returns the meta data size of the response.

func (*Response) PayloadSize

func (r *Response) PayloadSize() int64

PayloadSize returns the size of the payload. If the payload is a stream, it returns the bytes count that have been currently read out.

func (*Response) RawPayload

func (r *Response) RawPayload() []byte

RawPayload returns the payload in []byte, the caller should not modify its content. The function panic if the payload is a stream.

func (*Response) SetCookie

func (r *Response) SetCookie(cookie *http.Cookie)

SetCookie adds a Set-Cookie header to the response's headers.

func (*Response) SetPayload

func (r *Response) SetPayload(payload interface{})

SetPayload set the payload of the response to payload. The payload could be a string, a byte slice, or an io.Reader, and if it is an io.Reader, it will be treated as a stream, if this is not desired, please read the data to a byte slice, and set the byte slice as the payload.

func (*Response) SetStatusCode

func (r *Response) SetStatusCode(code int)

SetStatusCode sets the status code of the response.

func (*Response) StatusCode

func (r *Response) StatusCode() int

StatusCode returns the status code of the response.

func (*Response) Std

func (r *Response) Std() *http.Response

Std returns the underlying http.Response.

func (*Response) ToBuilderResponse

func (r *Response) ToBuilderResponse(name string) interface{}

ToBuilderResponse wraps the response and returns the wrapper, the return value can be used in the template of the Builder filters.

func (*Response) Trailer

func (r *Response) Trailer() protocols.Trailer

Trailer returns the trailer of the response in type protocols.Trailer.

Directories

Path Synopsis
Package httpheader provides HTTP Header related functions.
Package httpheader provides HTTP Header related functions.
Package httpstat implements the statistics tool for HTTP traffic.
Package httpstat implements the statistics tool for HTTP traffic.

Jump to

Keyboard shortcuts

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