http

package
v1.6.17 Latest Latest
Warning

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

Go to latest
Published: May 12, 2024 License: MIT Imports: 16 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"
	ContentGrpcWebHeaderValue = "application/grpc-web"

	ContentJsonUtf8HeaderValue = "application/json;charset=utf-8"

	ContentFormParamHeaderValue = "application/x-www-form-urlencoded;param=value"
)
View Source
const (
	HeaderDeviceInfo                  = "Device-AuthInfo"
	HeaderLocation                    = "Location"
	HeaderArea                        = "Area"
	HeaderUserAgent                   = "User-Agent"
	HeaderXForwardedFor               = "X-Forwarded-For"
	HeaderAuth                        = "HeaderAuth"
	HeaderContentType                 = "Content-Type"
	HeaderTrace                       = "Tracing"
	HeaderTraceID                     = "Tracing-ID"
	HeaderTraceBin                    = "Tracing-Bin"
	HeaderAuthorization               = "Authorization"
	HeaderCookie                      = "Cookie"
	HeaderCookieToken                 = "token"
	HeaderCookieDel                   = "del"
	HeaderContentDisposition          = "Content-Disposition"
	HeaderContentEncoding             = "Content-Encoding"
	HeaderReferer                     = "Referer"
	HeaderAccept                      = "Accept"
	HeaderAcceptLanguage              = "Accept-Language"
	HeaderAcceptEncoding              = "Accept-Encoding"
	HeaderCacheControl                = "Cache-Control"
	HeaderSetCookie                   = "Set-Cookie"
	HeaderTrailer                     = "Trailer"
	HeaderTransferEncoding            = "Transfer-Encoding"
	HeaderInternal                    = "Internal"
	HeaderTE                          = "TE"
	HeaderLastModified                = "Last-Modified"
	HeaderContentLength               = "Content-Length"
	HeaderAccessControlRequestMethod  = "Access-Control-Request-Method"
	HeaderAccessControlRequestHeaders = "Access-Control-Request-Headers"
	HeaderOrigin                      = "Origin"
	HeaderConnection                  = "Connection"
	HeaderRange                       = "Range"
	HeaderContentRange                = "Content-Range"
	HeaderAcceptRanges                = "Accept-Ranges"
)
View Source
const (
	HeaderGrpcTraceBin = "grpc-trace-bin"
	HeaderGrpcInternal = "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 Bind added in v1.3.0

func Bind(r *http.Request, obj interface{}) error

func BindJSON added in v1.3.0

func BindJSON(r *http.Request, obj interface{}) error

BindJSON is a shortcut for c.MustBindWith(obj, binding.JSON).

func BindQuery added in v1.3.0

func BindQuery(r *http.Request, obj interface{}) error

BindQuery is a shortcut for c.MustBindWith(obj, binding.Query).

func BindUri added in v1.3.0

func BindUri(r *http.Request, obj interface{}) error

MustBindWith binds the passed struct pointer using the specified binding engine. BindUri binds the passed struct pointer using binding.Uri. It will abort the request with HTTP 400 if any error occurs.

func BindXML added in v1.3.0

func BindXML(r *http.Request, obj interface{}) error

BindXML is a shortcut for c.MustBindWith(obj, binding.BindXML).

func BindYAML added in v1.3.0

func BindYAML(r *http.Request, obj interface{}) error

BindYAML is a shortcut for c.MustBindWith(obj, binding.YAML).

func Error added in v1.3.0

func Error(err error) string

func GetToken added in v1.3.0

func GetToken(r *http.Request) string

func MustBindWith added in v1.3.0

func MustBindWith(r *http.Request, obj interface{}, b binding.Binding) error

MustBindWith binds the passed struct pointer using the specified binding engine. It will abort the request with HTTP 400 if any error occurs. See the binding package.

func NewNoCloseRequest

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

func RespErr

func RespErr(w http.ResponseWriter, err error)

func RespErrMsg

func RespErrMsg(w http.ResponseWriter, msg string)

func RespErrRep

func RespErrRep(w http.ResponseWriter, rep *errorcode.ErrRep)

func RespErrcode

func RespErrcode(w http.ResponseWriter, code errorcode.ErrCode)

func Response

func Response[T any](w http.ResponseWriter, code errorcode.ErrCode, msg string, data T)

func ShouldBind added in v1.3.0

func ShouldBind(r *http.Request, obj interface{}) error

ShouldBind checks the Content-Type to select a binding engine automatically, Depending the "Content-Type" header different bindings are used:

"application/json" --> JSON binding
"application/xml"  --> XML binding

otherwise --> returns an error It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input. It decodes the json payload into the struct specified as a pointer. Like c.GinBind() but this method does not set the response status code to 400 and abort if the json is not valid.

func ShouldBindJSON added in v1.3.0

func ShouldBindJSON(r *http.Request, obj interface{}) error

ShouldBindJSON is a shortcut for c.ShouldBindWith(obj, binding.JSON).

func ShouldBindQuery added in v1.3.0

func ShouldBindQuery(r *http.Request, obj interface{}) error

ShouldBindQuery is a shortcut for c.ShouldBindWith(obj, binding.Query).

func ShouldBindUri added in v1.3.0

func ShouldBindUri(r *http.Request, obj interface{}) error

ShouldBindUri binds the passed struct pointer using the specified binding engine.

func ShouldBindWith added in v1.3.0

func ShouldBindWith(r *http.Request, obj interface{}, b binding.Binding) error

ShouldBindWith binds the passed struct pointer using the specified binding engine. See the binding package.

func ShouldBindXML added in v1.3.0

func ShouldBindXML(r *http.Request, obj interface{}) error

ShouldBindXML is a shortcut for c.ShouldBindWith(obj, binding.XML).

func ShouldBindYAML added in v1.3.0

func ShouldBindYAML(r *http.Request, obj interface{}) error

ShouldBindYAML is a shortcut for c.ShouldBindWith(obj, binding.YAML).

func Stream

func Stream(w http.ResponseWriter)

func StreamWriter

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

Types

type Body

type Body 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, r *http.Request)

type Handlers

type Handlers []http.Handler

func (Handlers) ServeHTTP

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

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 RequestAt added in v1.3.0

type RequestAt struct {
	Time       time.Time
	TimeStamp  int64
	TimeString string
}

func NewRequestAt added in v1.3.0

func NewRequestAt() *RequestAt

func NewRequestAtByTime added in v1.3.0

func NewRequestAtByTime(t time.Time) *RequestAt

func (*RequestAt) String added in v1.3.0

func (r *RequestAt) String() string

type ResAnyData

type ResAnyData = ResData[any]

type ResData

type ResData[T any] struct {
	Code    errorcode.ErrCode `json:"code"`
	Message string            `json:"message,omitempty"`
	//验证码
	Details T `json:"details,omitempty"`
}

func NewResData

func NewResData[T any](code errorcode.ErrCode, msg string, data T) *ResData[T]

func (*ResData[T]) Response

func (res *ResData[T]) Response(w http.ResponseWriter, httpcode int)

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.

Directories

Path Synopsis
api
fs
gin
web
`grpcweb` implements the gRPC-Web spec as a wrapper around a gRPC-Go Server.
`grpcweb` implements the gRPC-Web spec as a wrapper around a gRPC-Go Server.

Jump to

Keyboard shortcuts

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