Documentation ¶
Index ¶
- Constants
- Variables
- func ConstainsHeader(req *http.Request, name, value string) bool
- func CopyHeaders(dst, src http.Header)
- func CopyURL(i *url.URL) *url.URL
- func EnsureTransporterFinalized(httpTransport *http.Transport)
- func HasHeaders(names []string, headers http.Header) bool
- func IsWebsocketRequest(req *http.Request) bool
- func NewDefaultPooledTransport() *http.Transport
- func NewDefaultTransport() *http.Transport
- func NewID() string
- func NewLen(length int) string
- func NewLenChars(length int, chars []byte) string
- func NopWriteCloser(w io.Writer) io.WriteCloser
- func RemoveHeaders(headers http.Header, names ...string)
- type BufferWriter
- type ErrorHandler
- type ErrorHandlerFunc
- type FileLogger
- type LogLevel
- type Logger
- type ProxyWriter
- type StdHandler
- type WriterStub
Constants ¶
const ( // StdLen is a standard length of id string to achive ~95 bits of entropy. StdLen = 16 // UUIDLen is a length of id string to achive ~119 bits of entropy, closest // to what can be losslessly converted to UUIDv4 (122 bits). UUIDLen = 20 )
const ( // INFO log severity code. INFO = iota // WARN log severity code. WARN // ERROR log severity code. ERROR )
Variables ¶
var DefaultTransport *http.Transport
DefaultTransport is the default implementation of http.Transport as in http.DefaultTransport. Designed to be reused by multiple packages and efficient in terms of GC (won't leak).
var StdChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
StdChars is a set of standard characters allowed in id string.
Functions ¶
func ConstainsHeader ¶
ConstainsHeader checks if the given header field is present if the given HTTP request.
func CopyHeaders ¶
CopyHeaders copies http headers from source to destination, it does not overide, but adds multiple headers
func EnsureTransporterFinalized ¶
EnsureTransporterFinalized will ensure that when the HTTP client is GCed the runtime will close the idle connections (so that they won't leak) this function was adopted from Hashicorp's go-cleanhttp package.
func HasHeaders ¶
HasHeaders determines whether any of the header names is present in the http headers
func IsWebsocketRequest ¶
IsWebsocketRequest determines if the specified HTTP request is a websocket handshake request.
func NewDefaultPooledTransport ¶
NewDefaultPooledTransport returns a new http.Transport with similar default values to http.DefaultTransport. Do not use this for transient transports as it can leak file descriptors over time. Only use this for transports that will be re-used for the same host(s).
func NewDefaultTransport ¶
NewDefaultTransport returns a new http.Transport with the same default values as http.DefaultTransport, but with idle connections and keepalives disabled.
func NewID ¶
func NewID() string
NewID returns a new random string of the standard length, consisting of standard characters.
func NewLen ¶
NewLen returns a new random string of the provided length, consisting of standard characters.
func NewLenChars ¶
NewLenChars returns a new random string of the provided length, consisting of the provided byte slice of allowed characters (maximum 256).
func NopWriteCloser ¶
func NopWriteCloser(w io.Writer) io.WriteCloser
NopWriteCloser returns a WriteCloser with a no-op Close method wrapping the provided Writer w.
func RemoveHeaders ¶
RemoveHeaders removes the header with the given names from the headers map
Types ¶
type BufferWriter ¶
type BufferWriter struct { Code int H http.Header W io.WriteCloser }
BufferWriter represents an HTTP writable entity.
func NewBufferWriter ¶
func NewBufferWriter(w io.WriteCloser) *BufferWriter
NewBufferWriter creates a new writer buffer.
func (*BufferWriter) Close ¶
func (b *BufferWriter) Close() error
Close closes the used WriteCloser.
func (*BufferWriter) Write ¶
func (b *BufferWriter) Write(buf []byte) (int, error)
Write writes the giben
func (*BufferWriter) WriteHeader ¶
func (b *BufferWriter) WriteHeader(code int)
WriteHeader sets rw.Code.
type ErrorHandler ¶
type ErrorHandler interface {
ServeHTTP(w http.ResponseWriter, req *http.Request, err error)
}
ErrorHandler represents the error-specific interface required by error handlers.
var DefaultHandler ErrorHandler = &StdHandler{}
DefaultHandler stores the default error handled to be used, which is an no-op.
type ErrorHandlerFunc ¶
type ErrorHandlerFunc func(http.ResponseWriter, *http.Request, error)
ErrorHandlerFunc represents function interface for error handlers.
func (ErrorHandlerFunc) ServeHTTP ¶
func (f ErrorHandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request, err error)
ServeHTTP calls f(w, r).
type FileLogger ¶
type FileLogger struct {
// contains filtered or unexported fields
}
FileLogger represents a high-level logger supporting multiple log levels.
func NewFileLogger ¶
func NewFileLogger(w io.Writer, lvl LogLevel) *FileLogger
NewFileLogger creates a new FileLogger that writes in the given io.Writer.
func (*FileLogger) Errorf ¶
func (f *FileLogger) Errorf(format string, args ...interface{})
Errorf writes an error event in the log.
func (*FileLogger) Infof ¶
func (f *FileLogger) Infof(format string, args ...interface{})
Infof writes an info event in the log.
func (*FileLogger) Warningf ¶
func (f *FileLogger) Warningf(format string, args ...interface{})
Warningf writes a warning event in the log.
type Logger ¶
type Logger interface { Infof(format string, args ...interface{}) Warningf(format string, args ...interface{}) Errorf(format string, args ...interface{}) }
Logger defines a simple logging interface
var NullLogger Logger = &nopLogger{}
NullLogger stores the default empty logger to be used.
type ProxyWriter ¶
type ProxyWriter struct { Code int W http.ResponseWriter }
ProxyWriter helps to capture response headers and status code from the ServeHTTP. It can be safely passed to ServeHTTP handler, wrapping the real response writer.
func (*ProxyWriter) Flush ¶
func (p *ProxyWriter) Flush()
Flush flushes the cached data, if possible.
func (*ProxyWriter) StatusCode ¶
func (p *ProxyWriter) StatusCode() int
StatusCode defines the status code required.
func (*ProxyWriter) Write ¶
func (p *ProxyWriter) Write(buf []byte) (int, error)
Write writes the given slice of bytes in the internal buffer.
func (*ProxyWriter) WriteHeader ¶
func (p *ProxyWriter) WriteHeader(code int)
WriteHeader writes the HTTP response status code.
type StdHandler ¶
type StdHandler struct{}
StdHandler is an empty struct.
func (*StdHandler) ServeHTTP ¶
func (e *StdHandler) ServeHTTP(w http.ResponseWriter, req *http.Request, err error)
ServeHTTP replies with the proper status code based on the given error and writes the body.
type WriterStub ¶
WriterStub implements a http.ResponseWriter compatible interface desiged for stub during testing.
func NewWriterStub ¶
func NewWriterStub() *WriterStub
NewWriterStub creates a new WriterStub which implements a http.ResponseWriter interface.
func (*WriterStub) Write ¶
func (p *WriterStub) Write(buf []byte) (int, error)
Write writes the given slice of bytes in the internal buffer.
func (*WriterStub) WriteHeader ¶
func (p *WriterStub) WriteHeader(code int)
WriteHeader writes the HTTP response status code.