mux

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2021 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContentEncoding = "Content-Encoding"
	ContentLanguage = "Content-Language"
	ContentLength   = "Content-Length"
	ContentLocation = "Content-Location"
	ContentMD5      = "Content-MD5"
	ContentRange    = "Content-Range"
	ContentType     = "Content-Type"

	AcceptRanges = "Accept-Ranges"
	Bytes        = "bytes"

	AcceptEncoding = "Accept-Encoding"
	GZIP           = "gzip"
	DEFLATE        = "deflate"

	Vary          = "Vary"
	Charset       = "charset"
	CharsetPrefix = "charset="
	UTF8          = "UTF-8"
	GB18030       = "GB18030"
	GBK           = "GBK"

	Semicolon = ";"
	Comma     = ","

	ContentTypeJSON = "application/json"
	ContentTypeXML  = "text/xml"
	ContentTypeHTML = "text/html"
	ContentTypeText = "text/plain"

	AccessControlAllowOrigin      = "Access-Control-Allow-Origin"
	AccessControlAllowCredentials = "Access-Control-Allow-Credentials"
	AccessControlMaxAge           = "Access-Control-Max-Age"
	AccessControlAllowMethods     = "Access-Control-Allow-Methods"
	AccessControlAllowHeaders     = "Access-Control-Allow-Headers"
)
View Source
const (
	XRealIP       = "X-Real-IP"
	XForwardedFor = "X-Forwarded-For"
)
View Source
const MaxConnPerHost = 16384
View Source
const TimeFormat = "Mon, 02 Jan 2006 15:04:05 GMT"

TimeFormat is the time format to use when generating times in HTTP headers. It is like time.RFC1123 but hard-codes GMT as the time zone. The time being formatted must be in UTC for Format to generate the correct format.

For parsing this time format, see ParseTime.

Variables

View Source
var DefaultServer = NewRoute()

DefaultServer is the default HTTP server.

Functions

func AllowCredentials

func AllowCredentials(w http.ResponseWriter)

func AllowHeaders

func AllowHeaders(w http.ResponseWriter, Headers ...string)

func AllowMethods

func AllowMethods(w http.ResponseWriter, Methods ...string)

func AllowOrigin

func AllowOrigin(w http.ResponseWriter, value string)

func AllowOriginAll

func AllowOriginAll(w http.ResponseWriter)

func Body

func Body(w http.ResponseWriter, r *http.Request, body []byte, code int) (int, error)

func CheckAcceptEncoding

func CheckAcceptEncoding(r *http.Request, compressType string) bool

func Deflate

func Deflate(w http.ResponseWriter, r *http.Request, body []byte, code int) (int, error)

func DelHeader

func DelHeader(w http.ResponseWriter, key string)

DelHeader delete a response header

func Execute

func Execute(w http.ResponseWriter, r *http.Request, text string, data interface{}, code int) (int, error)

func File

func File(w http.ResponseWriter, r *http.Request, name string, code int) (int, error)

func FreeBufioReader

func FreeBufioReader(br *bufio.Reader)

FreeBufioReader frees the bufio.Reader.

func FreeBufioWriter

func FreeBufioWriter(bw *bufio.Writer)

FreeBufioWriter frees the bufio.Writer.

func FreeRequest

func FreeRequest(r *http.Request)

FreeRequest frees the request.

func FreeResponse

func FreeResponse(w http.ResponseWriter)

FreeResponse frees the response.

func GetRequestHeader

func GetRequestHeader(r *http.Request, key string) (value string)

GetRequestHeader get a request header

func GetResponseHeader

func GetResponseHeader(w http.ResponseWriter, key string) (value string)

GetResponseHeader get response a header

func Gzip

func Gzip(w http.ResponseWriter, r *http.Request, body []byte, code int) (int, error)

func IpTo4

func IpTo4(addr string) uint32

func JSON

func JSON(w http.ResponseWriter, r *http.Request, v interface{}, code int) (int, error)

func ListenAndServe

func ListenAndServe(addr string, handler http.Handler) error

ListenAndServe listens on the TCP network address addr and then calls Serve with handler to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives.

The handler is typically nil, in which case the DefaultServeMux is used.

ListenAndServe always returns a non-nil error.

func ListenAndServeTLS

func ListenAndServeTLS(addr, certFile, keyFile string, handler http.Handler) error

ListenAndServeTLS acts identically to ListenAndServe, except that it expects HTTPS connections. Additionally, files containing a certificate and matching private key for the server must be provided. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate, any intermediates, and the CA's certificate.

func MaxAge

func MaxAge(w http.ResponseWriter, age int)

func NewBufioReader

func NewBufioReader(r io.Reader) *bufio.Reader

NewBufioReader returns a new bufio.Reader with r.

func NewBufioWriter

func NewBufioWriter(w io.Writer) *bufio.Writer

NewBufioWriter returns a new bufio.Writer with w.

func NewBufioWriterSize

func NewBufioWriterSize(w io.Writer, size int) *bufio.Writer

NewBufioWriterSize returns a new bufio.Writer with w and size.

func Proxy

func Proxy(w http.ResponseWriter, r *http.Request, targetUrl string)

func ReadFastRequest

func ReadFastRequest(b *bufio.Reader) (*http.Request, error)

ReadFastRequest is like ReadRequest but with the simple request parser.

func ReadRequest

func ReadRequest(b *bufio.Reader) (*http.Request, error)

ReadRequest reads and parses an incoming request from b.

ReadRequest is a low-level function and should only be used for specialized applications; most code should use the Server to read requests and handle them via the Handler interface. ReadRequest only supports HTTP/1.x requests.

func Redirect

func Redirect(w http.ResponseWriter, r *http.Request, url string)

func RemoteAddr

func RemoteAddr(req *http.Request) (addr string)

func RunUnix

func RunUnix(file string) error

RunUnix todo

func ServeFile

func ServeFile(w http.ResponseWriter, r *http.Request, name string)

func SetCharset

func SetCharset(w http.ResponseWriter, charset string)

func SetContentEncoding

func SetContentEncoding(w http.ResponseWriter, compressType string)

func SetContentLength

func SetContentLength(w http.ResponseWriter, length int)

func SetContentType

func SetContentType(w http.ResponseWriter, value string)

func SetContentTypeWithCharset

func SetContentTypeWithCharset(w http.ResponseWriter, value string, charset string)

func SetHeader

func SetHeader(w http.ResponseWriter, key, value string)

SetHeader set a response header

func Text

func Text(w http.ResponseWriter, r *http.Request, text string, code int) (int, error)

func WriteHeader

func WriteHeader(w http.ResponseWriter, code int)

WriteHeader write a response header

func XML

func XML(w http.ResponseWriter, r *http.Request, v interface{}, code int) (int, error)

func Zlib

func Zlib(w http.ResponseWriter, r *http.Request, body []byte, code int) (int, error)

Types

type CompressWriter

type CompressWriter interface {
	Write(p []byte) (int, error)
	Flush() error
	Close() error
}

type Compressor

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

func NewDeflateWriter

func NewDeflateWriter(w http.ResponseWriter, r *http.Request) *Compressor

func NewGzipWriter

func NewGzipWriter(w http.ResponseWriter, r *http.Request) *Compressor

func NewZlibWriter

func NewZlibWriter(w http.ResponseWriter, r *http.Request) *Compressor

func (*Compressor) Close

func (c *Compressor) Close() error

Close a Compressor

func (*Compressor) Write

func (c *Compressor) Write(b []byte) (int, error)

Write byte

type Render

type Render struct {
	// contains filtered or unexported fields
}
var DefaultRender *Render

func NewRender

func NewRender() *Render

func (*Render) Body

func (render *Render) Body(w http.ResponseWriter, r *http.Request, body []byte, code int) (int, error)

func (*Render) Charset

func (render *Render) Charset(charset string) *Render

func (*Render) DeflateAll

func (render *Render) DeflateAll() *Render

func (*Render) Execute

func (render *Render) Execute(w http.ResponseWriter, r *http.Request, data interface{}, code int) (int, error)

func (*Render) ExecuteTemplate

func (render *Render) ExecuteTemplate(w http.ResponseWriter, r *http.Request, name string, data interface{}, code int) (int, error)

func (*Render) File

func (render *Render) File(w http.ResponseWriter, r *http.Request, name string, code int) (int, error)

func (*Render) GzipAll

func (render *Render) GzipAll() *Render

func (*Render) JSON

func (render *Render) JSON(w http.ResponseWriter, r *http.Request, v interface{}, code int) (int, error)

func (*Render) Parse

func (render *Render) Parse(text string) error

func (*Render) ParseTemplate

func (render *Render) ParseTemplate(name, text string) error

func (*Render) Redirect

func (render *Render) Redirect(w http.ResponseWriter, r *http.Request, url string)

func (*Render) ServeFile

func (render *Render) ServeFile(w http.ResponseWriter, r *http.Request, name string)

func (*Render) Text

func (render *Render) Text(w http.ResponseWriter, r *http.Request, text string, code int) (int, error)

func (*Render) XML

func (render *Render) XML(w http.ResponseWriter, r *http.Request, v interface{}, code int) (int, error)

type Response

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

Response implements the http.ResponseWriter interface.

func NewResponse

func NewResponse(req *http.Request, conn net.Conn, rw *bufio.ReadWriter) *Response

NewResponse returns a new response.

func NewResponseSize

func NewResponseSize(req *http.Request, conn net.Conn, rw *bufio.ReadWriter, size int) *Response

NewResponseSize returns a new response whose buffer has at least the specified size.

func (*Response) FinishRequest

func (w *Response) FinishRequest()

FinishRequest finishes a request.

func (*Response) Flush

func (w *Response) Flush()

Flush implements the http.Flusher interface.

Flush writes any buffered data to the underlying connection.

func (*Response) Header

func (w *Response) Header() http.Header

Header returns the header map that will be sent by WriteHeader.

func (*Response) Hijack

func (w *Response) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack implements the http.Hijacker interface.

Hijack lets the caller take over the connection. After a call to Hijack the HTTP server library will not do anything else with the connection.

func (*Response) Write

func (w *Response) Write(data []byte) (n int, err error)

Write writes the data to the connection as part of an HTTP reply.

func (*Response) WriteHeader

func (w *Response) WriteHeader(code int)

WriteHeader sends an HTTP response header with the provided status code.

type Route

type Route struct {
	*mux.Router
	Handler http.Handler
	// TLSConfig optionally provides a TLS configuration for use
	// by ServeTLS and ListenAndServeTLS. Note that this value is
	// cloned by ServeTLS and ListenAndServeTLS, so it's not
	// possible to modify the configuration with methods like
	// tls.Config.SetSessionTicketKeys. To use
	// SetSessionTicketKeys, use Server.Serve with a TLS Listener
	// instead.
	TLSConfig *tls.Config
	// contains filtered or unexported fields
}

Route is an HTTP server.

func NewRoute

func NewRoute() *Route

NewRoute returns a new NewRouter instance.

func (*Route) Close added in v0.0.5

func (m *Route) Close() error

Close closes the HTTP server.

func (*Route) Run added in v0.0.5

func (m *Route) Run(addr string) error

Run listens on the TCP network address addr and then calls Serve with m to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives.

Run always returns a non-nil error.

func (*Route) RunTLS added in v0.0.5

func (m *Route) RunTLS(addr string, certFile, keyFile string) error

RunTLS is like Run but with a cert file and a key file.

func (*Route) Serve added in v0.0.5

func (m *Route) Serve(l net.Listener) error

Serve accepts incoming connections on the Listener l, creating a new service goroutine for each, or registering the conn fd to poll that will trigger the fd to read requests and then call handler to reply to them.

func (*Route) ServeTLS added in v0.0.5

func (m *Route) ServeTLS(l net.Listener, certFile, keyFile string) error

ServeTLS accepts incoming connections on the Listener l, creating a new service goroutine for each. The service goroutines perform TLS setup and then read requests, calling srv.Handler to reply to them.

Files containing a certificate and matching private key for the server must be provided if neither the Server's TLSConfig.Certificates nor TLSConfig.GetCertificate are populated. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate, any intermediates, and the CA's certificate.

ServeTLS always returns a non-nil error. After Shutdown or Close, the returned error is ErrServerClosed.

func (*Route) SetFast added in v0.0.5

func (m *Route) SetFast(fast bool)

SetFast enables the Server to use simple request parser.

func (*Route) SetPoll added in v0.0.5

func (m *Route) SetPoll(poll bool)

SetPoll enables the Server to use netpoll based on epoll/kqueue.

type Tmpl

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

func NewTmpl

func NewTmpl() *Tmpl

func NewTmplWithRender

func NewTmplWithRender(render *Render) *Tmpl

func (*Tmpl) Execute

func (t *Tmpl) Execute(w http.ResponseWriter, r *http.Request, data interface{}, code int) (int, error)

func (*Tmpl) ExecuteTemplate

func (t *Tmpl) ExecuteTemplate(w http.ResponseWriter, r *http.Request, name string, data interface{}, code int) (int, error)

func (*Tmpl) Parse

func (t *Tmpl) Parse(text string) error

func (*Tmpl) ParseFiles

func (t *Tmpl) ParseFiles(filenames ...string) error

func (*Tmpl) ParseTemplate

func (t *Tmpl) ParseTemplate(name, text string) error

Jump to

Keyboard shortcuts

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