fasthttpadaptor

package
v1.76.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 17 Imported by: 438

Documentation

Overview

Package fasthttpadaptor provides helper functions for converting net/http request handlers to fasthttp request handlers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertNetHTTPRequestToFastHTTPRequest added in v1.75.0

func ConvertNetHTTPRequestToFastHTTPRequest(r *http.Request, req *fasthttp.Request)

ConvertNetHTTPRequestToFastHTTPRequest converts an http.Request to a fasthttp.Request.

req is not reset before the conversion: if it may contain data from a previous use, call req.Reset() first.

The request body is not copied. If r.Body is non-nil and not http.NoBody, it is attached to req via SetBodyStream. This means:

  • r.Body must remain readable for as long as req may need to read it.
  • r.Body should not be read from, written to, or closed by the caller until req is done with it.
  • The same r.Body must not be used concurrently from other goroutines while it is attached to req.

The body framing is derived only from the dedicated fields, mirroring net/http.Request.Write: Content-Length, Transfer-Encoding and Trailer entries in r.Header are ignored. The body size is taken from r.ContentLength, where zero and negative values mean the size is unknown, and a chunked r.TransferEncoding or a non-empty r.Trailer forces the unknown size too; a body of unknown size is streamed with chunked transfer encoding until EOF. Trailers force the chunked framing, unlike net/http which drops them on requests with a known size, because HTTP/2 requests carry a known length and trailers at the same time, and HTTP/1.x can transport trailers only after a chunked body.

The conversion never reads the body: nil and http.NoBody are the explicit representations of an empty body, and any other body of unknown size is streamed, even if it turns out to be empty. net/http.Client instead probes such a body for some request methods to tell an empty body from an unknown one; callers that need the bodiless form should pass nil or http.NoBody.

A body of known size is truncated to that size when it is written, like net/http does, so that a body longer than r.ContentLength cannot put bytes past the declared boundary, where a peer would parse them as the next request. Writing such a request body fails once the declared size has been written.

Derived state such as r.Form is not converted, and a body that was already consumed (e.g. by r.ParseForm) is attached as the drained stream it is.

The host is taken from r.Host, or r.URL.Host if r.Host is empty. A Host entry in r.Header is ignored, mirroring net/http.

The request target is taken from r.RequestURI, or derived from r.URL when r.RequestURI is empty. A CONNECT request targets an authority rather than a resource (RFC 9112, Section 3.2.3), so when its URL carries no path the target becomes r.URL.Opaque, or the host when there is no opaque value, like net/http does, instead of the origin-form "/" that would name no authority to tunnel to. Such an opaque target is written as it is, so it carries neither the scheme nor the query that r.URL.RequestURI would add. A CONNECT target keeps its form when req.URI() is accessed later, as fasthttp.Client does, since fasthttp rebuilds the target of a CONNECT request without path normalization.

An absolute-form target is kept as it is too, whatever its scheme, so a request written with req.Write to a proxy keeps naming the authority it addresses. This holds only until req.URI() is accessed: fasthttp then rebuilds the target in origin-form from the parsed URI. That is the form fasthttp.Client and fasthttp.HostClient send, since they send the request to the host of the URI itself, through a CONNECT tunnel when a proxy dialer is used.

Once the URI is parsed, fasthttp also normalizes the path of the target it rebuilds unless req.URI().DisablePathNormalizing is set, e.g. "//" in the path is collapsed into "/". The conversion parses the URI itself when it sets a scheme other than http or clears URL credentials, so req.Write may then write a normalized target, like fasthttp.Client always does unless its DisablePathNormalizing option is set.

When r.Header carries no Content-Type entry, the default Content-Type fasthttp writes for requests (application/octet-stream) is disabled on req via SetNoDefaultContentType, so the conversion does not add a header the source request did not carry; net/http never generates a Content-Type. Recipients of a message without a Content-Type may assume application/octet-stream themselves (RFC 9110, Section 8.3). Note that req.Reset() restores the fasthttp default.

Connection-level state is not converted, since a fasthttp.Request models only the wire message: r.RemoteAddr is ignored, and r.TLS is used only to derive the URI scheme, which is set from r.URL.Scheme if present, or to "https" if r.TLS is non-nil.

Trailer names that are forbidden in trailers (see fasthttp.ErrBadTrailer) are skipped, and without a body all trailers are dropped, since trailers can only travel after a chunked body. Trailer values present in r.Trailer are copied at conversion time and synchronized again when the attached body reaches EOF, since for server requests net/http populates r.Trailer only while the body is being read. fasthttp writes trailers after draining the body stream, so the synchronized values are the ones written to the wire. Materializing the body of a converted request before writing it (e.g. via req.Body()) keeps the trailers, since fasthttp writes a buffered body with registered trailers chunked.

Credentials follow the precedence of net/http.Client: an Authorization entry in r.Header takes precedence over URL credentials. Without such an entry, credentials in r.URL.User are written as a Basic Authorization header, including credentials with an empty username, just like net/http.Client sends them. With such an entry, userinfo embedded in an absolute-form r.RequestURI is dropped so it cannot displace the header. Either way an absolute-form target carrying credentials is written with an origin-form request line, since a request target must not contain userinfo (RFC 9112, Section 3.2.4), just like net/http.Request.Write. An authority-form CONNECT target names the authority to tunnel to and has no origin form, so its credentials are stripped from the target instead.

HTTP/2 and newer protocols are normalized to HTTP/1.1, since fasthttp only models HTTP/1.x messages and the HTTP version is a hop-by-hop property. An older protocol is also normalized to HTTP/1.1 when the conversion requires chunked framing, since chunked transfer encoding does not exist in HTTP/1.0; net/http always writes request lines as HTTP/1.1. The implied close semantics of an HTTP/1.0 request survive the normalization when r.Close is set, which net/http derives from the version and the Connection header when it parses a request.

func ConvertRequest added in v1.25.0

func ConvertRequest(ctx *fasthttp.RequestCtx, r *http.Request, forServer bool) error

ConvertRequest converts a fasthttp.Request to an http.Request. forServer should be set to true when the http.Request is going to be passed to a http.Handler.

The http.Request must not be used after the fasthttp handler has returned! Memory in use by the http.Request will be reused after your handler has returned!

func NewFastHTTPHandler

func NewFastHTTPHandler(h http.Handler) fasthttp.RequestHandler

NewFastHTTPHandler wraps net/http handler to fasthttp request handler, so it can be passed to fasthttp server.

While this function may be used for easy switching from net/http to fasthttp, it has the following drawbacks comparing to using manually written fasthttp request handler:

  • A lot of useful functionality provided by fasthttp is missing from net/http handler.
  • net/http -> fasthttp handler conversion has some overhead, so the returned handler will be always slower than manually written fasthttp handler.

So it is advisable using this function only for quick net/http -> fasthttp switching. Then manually convert net/http handlers to fasthttp handlers according to https://github.com/valyala/fasthttp#switching-from-nethttp-to-fasthttp .

func NewFastHTTPHandlerFunc

func NewFastHTTPHandlerFunc(h http.HandlerFunc) fasthttp.RequestHandler

NewFastHTTPHandlerFunc wraps net/http handler func to fasthttp request handler, so it can be passed to fasthttp server.

While this function may be used for easy switching from net/http to fasthttp, it has the following drawbacks comparing to using manually written fasthttp request handler:

  • A lot of useful functionality provided by fasthttp is missing from net/http handler.
  • net/http -> fasthttp handler conversion has some overhead, so the returned handler will be always slower than manually written fasthttp handler.

So it is advisable using this function only for quick net/http -> fasthttp switching. Then manually convert net/http handlers to fasthttp handlers according to https://github.com/valyala/fasthttp#switching-from-nethttp-to-fasthttp .

Types

This section is empty.

Jump to

Keyboard shortcuts

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