httpi

package
v0.0.0-...-1f4ee57 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2021 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ContentBinaryHeaderValue header value for binary data.
	ContentBinaryHeaderValue = "application/octet-stream"
	// ContentWebassemblyHeaderValue header value for web assembly files.
	ContentWebassemblyHeaderValue = "application/wasm"
	// ContentHTMLHeaderValue is the  string of text/html response header's content type value.
	ContentHTMLHeaderValue = "text/html"
	// ContentJSONHeaderValue header value for JSON data.
	ContentJSONHeaderValue = "application/json"
	// ContentJSONProblemHeaderValue header value for JSON API problem error.
	// Read more at: https://tools.ietf.org/html/rfc7807
	ContentJSONProblemHeaderValue = "application/problem+json"
	// ContentXMLProblemHeaderValue header value for XML API problem error.
	// Read more at: https://tools.ietf.org/html/rfc7807
	ContentXMLProblemHeaderValue = "application/problem+xml"
	// ContentJavascriptHeaderValue header value for JSONP & Javascript data.
	ContentJavascriptHeaderValue = "text/javascript"
	// ContentTextHeaderValue header value for Text data.
	ContentTextHeaderValue = "text/plain"
	// ContentXMLHeaderValue header value for XML data.
	ContentXMLHeaderValue = "text/xml"
	// ContentXMLUnreadableHeaderValue obselete header value for XML.
	ContentXMLUnreadableHeaderValue = "application/xml"
	// ContentMarkdownHeaderValue custom key/content type, the real is the text/html.
	ContentMarkdownHeaderValue = "text/markdown"
	// ContentYAMLHeaderValue header value for YAML data.
	ContentYAMLHeaderValue = "application/x-yaml"
	// ContentYAMLTextHeaderValue header value for YAML plain text.
	ContentYAMLTextHeaderValue = "text/yaml"
	// ContentProtobufHeaderValue header value for Protobuf messages data.
	ContentProtobufHeaderValue = "application/x-protobuf"
	// ContentMsgPackHeaderValue header value for MsgPack data.
	ContentMsgPackHeaderValue = "application/msgpack"
	// ContentMsgPack2HeaderValue alternative header value for MsgPack data.
	ContentMsgPack2HeaderValue = "application/x-msgpack"
	// ContentFormHeaderValue header value for post form data.
	ContentFormHeaderValue = "application/x-www-form-urlencoded"
	// ContentFormMultipartHeaderValue header value for post multipart form data.
	ContentFormMultipartHeaderValue = "multipart/form-data"
	// ContentGRPCHeaderValue Content-Type header value for gRPC.
	ContentGRPCHeaderValue = "application/grpc"
)
View Source
const (
	HeaderDeviceInfo         = "Device-AuthInfo"
	HeaderLocation           = "Location"
	HeaderArea               = "Area"
	HeaderUserAgent          = "User-Agent"
	HeaderXForwardedFor      = "X-Forwarded-For"
	HeaderAuth               = "HeaderAuth"
	HeaderContentType        = "Content-Type"
	HeaderTrace              = "Trace"
	HeaderTraceID            = "TraceID"
	HeaderAuthorization      = "Authorization"
	HeaderCookie             = "Cookie"
	HeaderCookieToken        = "token"
	HeaderCookieDel          = "del"
	HeaderContentDisposition = "Content-Disposition"
	HeaderContentEncoding    = "Content-Encoding"
	HeaderReferer            = "Referer"
	HeaderCacheControl       = "Cache-Control"
	HeaderSetCookie          = "Set-Cookie"
	HeaderTrailer            = "Trailer"
	HeaderTransferEncoding   = "Transfer-Encoding"
	HeaderInternal           = "Internal"
	HeaderTE                 = "TE"
)
View Source
const (
	GrpcTraceBin = "grpc-trace-bin"
	GrpcInternal = "grpc-internal"
)
View Source
const DefaultRemoteAddr = "1.2.3.4"

DefaultRemoteAddr is the default remote address to return in RemoteAddr if an explicit DefaultRemoteAddr isn't set on ResponseRecorder.

Variables

View Source
var ResponseOk = []byte(`{"code":0,"message":"OK"}`)
View Source
var ResponseSysErr = []byte(`{"code":10000,"message":"系统错误"}`)

Functions

func CleanPath

func CleanPath(p string) string

CleanPath is the URL version of path.Clean, it returns a canonical URL path for p, eliminating . and .. elements.

The following rules are applied iteratively until no further processing can be done:

  1. Replace multiple slashes with a single slash.
  2. Eliminate each . path name element (the current directory).
  3. Eliminate each inner .. path name element (the parent directory) along with the non-.. element that precedes it.
  4. Eliminate .. elements that begin a rooted path: that is, replace "/.." by "/" at the beginning of a path.

If the result of this process is an empty string, "/" is returned

func Debug

func Debug() http.Handler

func GetToken

func GetToken(r *http.Request) string

func NewNoCloseRequest

func NewNoCloseRequest(req *http.Request, s string)

func PromHandler

func PromHandler() http.Handler

func Res

func Res(w http.ResponseWriter, code uint32, msg string, data interface{})

func Response

func Response(w http.ResponseWriter, res ...interface{})

先信息后数据最后状态码 入参1. data interface{},msg string,code int 2.msg,code |data默认nil 3.data,msg |code默认SUCCESS 4.msg |data默认nil code默认ERROR 5.data |msg默认"",code默认SUCCESS

func Stream

func Stream(w http.ResponseWriter)

func StreamWriter

func StreamWriter(w http.ResponseWriter, writer func(w io.Writer) bool)

Types

type File

type File struct {
	File http.File
	Name string
}

type H

type H map[string]interface{}

type HandlerFuncs

type HandlerFuncs []http.HandlerFunc

func (*HandlerFuncs) Add

func (hs *HandlerFuncs) Add(handler http.HandlerFunc)

func (HandlerFuncs) HandlerFunc

func (hs HandlerFuncs) HandlerFunc() http.HandlerFunc

func (HandlerFuncs) ServeHTTP

func (hs HandlerFuncs) ServeHTTP(w http.ResponseWriter, req *http.Request)

type Handlers

type Handlers []http.Handler

func (Handlers) ServeHTTP

func (hs Handlers) ServeHTTP(w http.ResponseWriter, req *http.Request)

type HttpFile

type HttpFile interface {
	io.Reader
	Name() string
}

type NoCloseBody

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

func NewNoCloseBody

func NewNoCloseBody(s string) *NoCloseBody

适用于轮询

func (*NoCloseBody) Close

func (r *NoCloseBody) Close() error

func (*NoCloseBody) Len

func (r *NoCloseBody) Len() int

func (*NoCloseBody) Read

func (r *NoCloseBody) Read(b []byte) (n int, err error)

func (*NoCloseBody) Size

func (r *NoCloseBody) Size() int64

type ResData

type ResData struct {
	Code    uint32 `json:"code"`
	Message string `json:"message,omitempty"`
	//验证码
	Details interface{} `json:"details,omitempty"`
}

func (*ResData) Response

func (res *ResData) Response(w http.ResponseWriter)

type ResponseRecorder

type ResponseRecorder struct {
	// Code is the HTTP response code set by WriteHeader.
	//
	// Note that if a Handler never calls WriteHeader or Write,
	// this might end up being 0, rather than the implicit
	// http.StatusOK. To get the implicit value, use the Result
	// method.
	Code int

	HeaderMap http.Header

	// Body is the buffer to which the Handler's Write calls are sent.
	// If nil, the Writes are silently discarded.
	Body *bytes.Buffer

	// Flushed is whether the Handler called Flush.
	Flushed bool
	// contains filtered or unexported fields
}

ResponseRecorder is an implementation of http.ResponseWriter that records its mutations for later inspection in tests.

func NewRecorder

func NewRecorder(header http.Header) *ResponseRecorder

NewRecorder returns an initialized ResponseRecorder.

func (*ResponseRecorder) Flush

func (rw *ResponseRecorder) Flush()

Flush implements http.Flusher. To test whether Flush was called, see rw.Flushed.

func (*ResponseRecorder) Header

func (rw *ResponseRecorder) Header() http.Header

Header implements http.ResponseWriter. It returns the response headers to mutate within a handler. To test the headers that were written after a handler completes, use the Result method and see the returned Response value's Header.

func (*ResponseRecorder) Reset

func (rw *ResponseRecorder) Reset()

func (*ResponseRecorder) Result

func (rw *ResponseRecorder) Result() *http.Response

Result returns the response generated by the handler.

The returned Response will have at least its StatusCode, Header, Body, and optionally Trailer populated. More fields may be populated in the future, so callers should not DeepEqual the result in tests.

The Response.Header is a snapshot of the headers at the time of the first write call, or at the time of this call, if the handler never did a write.

The Response.Body is guaranteed to be non-nil and Body.Read call is guaranteed to not return any error other than io.EOF.

Result must only be called after the handler has finished running.

func (*ResponseRecorder) Write

func (rw *ResponseRecorder) Write(buf []byte) (int, error)

Write implements http.ResponseWriter. The data in buf is written to rw.Body, if not nil.

func (*ResponseRecorder) WriteHeader

func (rw *ResponseRecorder) WriteHeader(code int)

WriteHeader implements http.ResponseWriter.

func (*ResponseRecorder) WriteString

func (rw *ResponseRecorder) WriteString(str string) (int, error)

WriteString implements io.StringWriter. The data in str is written to rw.Body, if not nil.

Jump to

Keyboard shortcuts

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