Documentation
¶
Overview ¶
Package httpx carries the request and response semantics the service had under fiber onto net/http, so the HTTP contract stays byte-identical across the move. scripts/e2e-golden is the contract; the comments here say which fiber behaviour each piece reproduces.
Index ¶
- Constants
- Variables
- func BindBody(r *http.Request, out any) error
- func Bytes(w http.ResponseWriter, status int, contentType string, body []byte)
- func DetectionPath(raw string) string
- func DropLate(w http.ResponseWriter)
- func FormFile(r *http.Request, key string) (*multipart.FileHeader, error)
- func FormValue(r *http.Request, key string) string
- func JSON(w http.ResponseWriter, status int, v any)
- func Late(w http.ResponseWriter, fn func(http.Header))
- func MultipartForm(r *http.Request) (*multipart.Form, error)
- func Param(r *http.Request, name string) string
- func Prepare(next http.Handler) http.Handler
- func Query(r *http.Request, key string) string
- func RawPath(r *http.Request) string
- func SendFile(w http.ResponseWriter, r *http.Request, path string)
- func SendStream(w http.ResponseWriter, r *http.Request, body io.ReadCloser, size int64)
- func Text(w http.ResponseWriter, status int, body string)
- func Wrap(writeTimeout time.Duration, next http.Handler) http.Handler
- type Handler
- type Writer
Constants ¶
const ( TextMethodNotAllowed = "Method Not Allowed" TextUpgradeRequired = "Upgrade Required" TextInvalidMethod = "Invalid http method" TextTooLarge = "Request Entity Too Large" )
Bodies of the plain-text answers fiber produced itself.
Variables ¶
var ErrUnprocessableEntity = errors.New("Unprocessable Entity")
ErrUnprocessableEntity is what fiber's BodyParser returned for a content type it could not bind.
Functions ¶
func BindBody ¶
BindBody is fiber's BodyParser: JSON for any "+json"/"json" type, form fields for urlencoded and multipart bodies, XML, and an error otherwise.
func Bytes ¶
func Bytes(w http.ResponseWriter, status int, contentType string, body []byte)
Bytes writes a complete body with an explicit length. net/http would switch to chunked encoding above 2 KB, which fasthttp never did for a buffered body.
func DetectionPath ¶
DetectionPath is fiber's routing view of a path: ASCII lower-cased and without trailing slashes. Its length equals the raw path's minus those slashes, which is what lets Param slice values out of the raw path.
func DropLate ¶
func DropLate(w http.ResponseWriter)
DropLate forgets registered late headers: a panic unwound past the fiber middleware that would have set them.
func FormValue ¶
FormValue looks up a value where fasthttp did, in this order: query string, urlencoded body, multipart body. An empty value falls through to the next.
func JSON ¶
func JSON(w http.ResponseWriter, status int, v any)
JSON writes v as fiber's c.JSON did: encoding/json.Marshal (sorted map keys, HTML escaped, no trailing newline) and Content-Type application/json.
func Late ¶
func Late(w http.ResponseWriter, fn func(http.Header))
Late registers headers to apply when the status is written.
func MultipartForm ¶
MultipartForm parses the body once. Parts over multipartMemory spill to temp files, which Prepare removes when the request ends.
func Param ¶
Param returns a route parameter as fiber's c.Params did: original case and encoding, with trailing slashes left out of a trailing wildcard. "*" names the wildcard; a parameter the matched route does not have is "".
func Prepare ¶
Prepare records the raw path and routes chi on fiber's detection path. It has to run inside the chi mux, where the route context already exists.
It also removes the temp files of every multipart form parsed below it: net/http only cleans up the form of the request it created, and handlers parse on copies made by WithContext.
func RawPath ¶
RawPath is the path exactly as the client sent it, undecoded, which is what fiber matched against and handed to handlers (fasthttp's PathOriginal).
func SendFile ¶
func SendFile(w http.ResponseWriter, r *http.Request, path string)
SendFile answers with a file the way fiber's c.SendFile did through fasthttp's file server: Last-Modified and If-Modified-Since, single byte ranges, and for 304 and 416 a response reset of everything set earlier.
func SendStream ¶
func SendStream(w http.ResponseWriter, r *http.Request, body io.ReadCloser, size int64)
SendStream is fiber's c.SendStream(r, size): a known length, then the stream, closed afterwards. A HEAD request closes it unread, as fasthttp's SkipBody did; net/http would discard the bytes, but only after pulling the whole object out of MinIO, or out of S3 on the archive path.
func Text ¶
func Text(w http.ResponseWriter, status int, body string)
Text writes a plain-text body the way fiber's default error handler did.
Types ¶
type Handler ¶
type Handler func(http.ResponseWriter, *http.Request) error
Handler is a handler that may return an error, as fiber handlers did. A returned error becomes fiber's default error response: 500, text/plain, the error text.
type Writer ¶
type Writer struct {
http.ResponseWriter
// contains filtered or unexported fields
}
Writer is the one ResponseWriter wrapper of a request, installed by Wrap.
Late headers reproduce fiber middleware that set headers after c.Next(): they are applied when the status is written, innermost first, so an outer middleware's value wins exactly as it did when it ran last.
func From ¶
func From(w http.ResponseWriter) *Writer
From returns the request's Writer, or nil when Wrap is not installed.
func (*Writer) ResetHeaders ¶
func (w *Writer) ResetHeaders()
ResetHeaders drops every header set so far, as fasthttp's file server did by resetting the response for 304 and 416. Late headers survive: fiber's limiter set them after c.Next() returned, so after the reset.
func (*Writer) Unwrap ¶
func (w *Writer) Unwrap() http.ResponseWriter