Documentation ¶
Overview ¶
Package httputil provides helper for net/http, i.e. unix domain socket client, http request logger
Index ¶
- func DiscardBody(res *http.Response)
- func ListenAndServeUnix(srv *http.Server, addr string) error
- func NewClient(tr *http.Transport) *http.Client
- func NewPooledClient() *http.Client
- func NewPooledTransport() *http.Transport
- func NewPooledUnixTransport(sockFile string) *http.Transport
- func NewUnPooledClient() *http.Client
- func NewUnPooledTransport() *http.Transport
- func NewUnPooledUnixTransport(socketFile string) *http.Transport
- type Method
- type TrackedWriter
- func (tracker *TrackedWriter) Flush()
- func (tracker *TrackedWriter) Header() http.Header
- func (tracker *TrackedWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (tracker *TrackedWriter) Push(target string, opts *http.PushOptions) error
- func (tracker *TrackedWriter) Size() int
- func (tracker *TrackedWriter) Status() int
- func (tracker *TrackedWriter) Write(b []byte) (int, error)
- func (tracker *TrackedWriter) WriteHeader(status int)
- type UserAgent
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DiscardBody ¶ added in v0.0.10
DiscardBody drain and close the body and ignore all the errors, It should be used for test
func NewClient ¶ added in v0.0.9
NewClient panics if transport is nil or http.DefaultTransport, You should always bring your own http.Transport instead of using the default one, because all the third party libraries can modify it without the application knowing
func NewPooledClient ¶ added in v0.0.9
NewPooledClient returns a net/http client with a fresh http.Transport using NewPooledTransport. The transport is a connection pool, you should reuse the client instead of creating new one with new transport, http.Client does not keep internal state in struct except cookie jar
If you do need multiple clients to reuse same connections, you should use NewClient and pass a transport
func NewPooledTransport ¶ added in v0.0.9
NewPooledTransport is same as DefaultTransport in net/http, but it is not shared and won't be alerted by other library
func NewPooledUnixTransport ¶
NewPooledUnixTransport creates transport for unix domain socket i.e. /var/run/docker.sock
func NewUnPooledClient ¶ added in v0.0.9
NewUnPooledClient returns a net/http client with a fresh http.Transport that has connection pooling disabled
func NewUnPooledTransport ¶ added in v0.0.9
NewUnPooledTransport uses NewPooledTransport with keep alive and idle connection disabled
func NewUnPooledUnixTransport ¶ added in v0.0.9
NewUnPooledUnixTransport uses NewPooledUnixTransport with keep alive and idle connection disabled
Types ¶
type Method ¶ added in v0.0.9
type Method string
Method gives a type for http method See https://github.com/bradfitz/exp-httpclient/blob/master/http/method.go
const ( Get Method = http.MethodGet Head Method = http.MethodHead Post Method = http.MethodPost Put Method = http.MethodPut Patch Method = http.MethodPatch Delete Method = http.MethodDelete Connect Method = http.MethodConnect Options Method = http.MethodOptions Trace Method = http.MethodTrace )
type TrackedWriter ¶ added in v0.0.9
type TrackedWriter struct {
// contains filtered or unexported fields
}
TrackedWriter keeps track of status code and bytes written so it can be used by logger. It proxies all the interfaces except Hijacker, since it is not supported by HTTP/2. Most methods comments are copied from net/http It is based on https://github.com/gorilla/handlers but put all interface into one struct
func NewTrackedWriter ¶ added in v0.0.10
func NewTrackedWriter(w http.ResponseWriter) TrackedWriter
NewTrackedWriter set the underlying writer based on argument, It returns a value instead of pointer so it can be allocated on stack. TODO: add benchmark to prove it ...
func (*TrackedWriter) Flush ¶ added in v0.0.9
func (tracker *TrackedWriter) Flush()
Flush calls Flush on underlying ResponseWriter if it implemented http.Flusher
Flusher interface is implemented by ResponseWriters that allow an HTTP handler to flush buffered data to the client. The default HTTP/1.x and HTTP/2 ResponseWriter implementations support Flusher
func (*TrackedWriter) Header ¶ added in v0.0.9
func (tracker *TrackedWriter) Header() http.Header
Header returns the header map of the underlying ResponseWriter
Changing the header map after a call to WriteHeader (or Write) has no effect unless the modified headers are trailers.
func (*TrackedWriter) Hijack ¶ added in v0.0.10
func (tracker *TrackedWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)
Hijack implements http.Hijacker, which is used by websocket etc. It returns http.ErrNotSupported with nil pointer if the underlying writer does not support it NOTE: HTTP/1.x supports it but HTTP/2 does not
func (*TrackedWriter) Push ¶ added in v0.0.9
func (tracker *TrackedWriter) Push(target string, opts *http.PushOptions) error
Push returns http.ErrNotSupported if underlying ResponseWriter does not implement http.Pusher
Push initiates an HTTP/2 server push, returns ErrNotSupported if the client has disabled push or if push is not supported on the underlying connection.
func (*TrackedWriter) Size ¶ added in v0.0.9
func (tracker *TrackedWriter) Size() int
Size return number of bytes written through Write, returns 0 if Write has not been called
func (*TrackedWriter) Status ¶ added in v0.0.9
func (tracker *TrackedWriter) Status() int
Status return the tracked status code, returns 0 if WriteHeader has not been called
func (*TrackedWriter) Write ¶ added in v0.0.9
func (tracker *TrackedWriter) Write(b []byte) (int, error)
Write keeps track of bytes written of the underlying ResponseWriter
Write writes the data to the connection as part of an HTTP reply.
If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK) before writing the data. If the Header does not contain a Content-Type line, Write adds a Content-Type set to the result of passing the initial 512 bytes of written data to DetectContentType.
func (*TrackedWriter) WriteHeader ¶ added in v0.0.9
func (tracker *TrackedWriter) WriteHeader(status int)
WriteHeader keep track of status code and call the underlying ResponseWriter
WriteHeader sends an HTTP response header with status code. If WriteHeader is not called explicitly, the first call to Write will trigger an implicit WriteHeader(http.StatusOK). Thus explicit calls to WriteHeader are mainly used to send error codes.
type UserAgent ¶ added in v0.0.9
type UserAgent string
UserAgent data are from https://techblog.willshouse.com/2012/01/03/most-common-user-agents/ For UserAgent spec, see MDN https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent
const ( DefaultUA UserAgent = "Go" // TODO: should put real version number into the constant, need to use generator // TODO: should list more UAs, including mobile devices https://deviceatlas.com/blog/list-of-user-agent-strings // TODO: os and browser versions are too old in those UAs ... UACurl UserAgent = "curl/7.60.0" UAChromeWin UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36" UAChromeLinux UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36" UAChromeMac UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36" )