Internal

internal/wiretarget

import "github.com/gofiber/fiber/v3/internal/wiretarget"

Package wiretarget spells the path the router matched the way a request line carries it, for middleware that hands the request on to something that reads request lines: a proxy upstream, a net/http or fasthttp handler.

Constants

Source: internal/wiretarget/wiretarget.go:63

const (
	// AllowSlashes lets an escaped slash and an empty segment through.
	AllowSlashes Allowance = 1 << iota
	// AllowParameters lets a path parameter through on a segment that has a
	// name other than "." and "..".
	AllowParameters
)

Source: internal/wiretarget/wiretarget.go:48

const (
	// Served means the handler ran.
	Served Outcome = iota
	// NotFound means the handler did not run, because it would read the routed path as another name than the router
	// matched: the caller answers 404 Not Found.
	NotFound
	// BadRequest means the handler did not run, because the routed path holds a "%" that begins no escape (see
	// HasStrayPercent), which net/http's own server answers 400 Bad Request for: the caller answers the same.
	BadRequest
)

Variables

TimeoutAnswerKey is the request local under which timeout.New leaves the *sync.Mutex it holds while it answers a request that timed out.

Source: internal/wiretarget/wiretarget.go:34

var TimeoutAnswerKey any = timeoutAnswerKey{}

TimeoutHeaderKey is the request local under which a middleware in front of timeout.New leaves a func(*fasthttp.ResponseHeader) error that timeout.New runs on the response OnTimeout writes, which does not come back through that middleware.

Source: internal/wiretarget/wiretarget.go:41

var TimeoutHeaderKey any = timeoutHeaderKey{}

Functions

func HasForgedEscape(target string) bool

Source: internal/wiretarget/wiretarget.go:531

HasForgedEscape reports whether target's path holds an escape of an unreserved character (RFC 3986 Section 2.3: a letter, a digit, "-", ".", "_" or "~"). The router decodes every such escape when it normalizes a path, so one still in c.Path() was forged by a stray "%" lining up with the escape after it: "/%%32e" reaches the router as "/%2e". A reader that decodes the path once more then sees a character the router never matched, "%2e%2e" becoming a ".." segment or "%70rivate" becoming "private". Under UnescapePath the routed path is escaped again segment by segment, which never escapes an unreserved character, so nothing is reported there.

func HasStrayPercent(path string) bool

Source: internal/wiretarget/wiretarget.go:555

HasStrayPercent reports whether path, which holds no query, holds a "%" that begins no escape: one that is not followed by two hex digits. net/http's server answers 400 Bad Request for a request line whose path holds one, since url.ParseRequestURI refuses it, so a net/http handler never reads "/100%" and "/100%25" as one name. The router keeps the two apart, a middleware mounted on one of them does not run for the other, and Routed would hand a handler the first as the second, so a handler that serves names, http.FileServer say, would be reached around that middleware. The net/http adapters therefore answer 400, as net/http's server does, instead.

func NoPath(line string, connect bool) bool

Source: internal/wiretarget/wiretarget.go:235

NoPath reports whether line, the request target as it arrived, holds no path: the asterisk form of OPTIONS ("*") and, when connect is set, the authority form of CONNECT ("example.com:443", "[::1]:443"). fasthttp and the router read the same path for such a target, so there is nothing to guard and no spelling to refuse: a fasthttp route handler is given the target as sent, a net/http handler the path the router rooted it as. Any other CONNECT target, such as "über" or "a[1]", is a path like any other.

func ParsesAsPath(target string, host []byte) bool

Source: internal/wiretarget/wiretarget.go:624

ParsesAsPath reports whether fasthttp reads target as a path when it is set as the request line of a request whose Host header is host. It is false for a target without a leading slash, which a request line reads as an absolute URL or an authority, for one that begins with "//" and holds "://", and for one that begins with "//" on a request without a Host header: fasthttp then reads the target as "[scheme:]//authority/path" and takes the request's host, and a Basic credential, from the authority, over the ones the application set. The router never sees such a target as an authority, and it need not have arrived as one: ".." resolution builds "//u:pw@evil.example/a://b" out of "//u:pw@evil.example/a:/x/..//b", and under UnescapePath a decoded "%3A%2F%2F" spells the "://".

func ReadAsOriginForm(fctx *fasthttp.RequestCtx, target []byte)

Source: internal/wiretarget/wiretarget.go:321

ReadAsOriginForm reads target, the request target of fctx's request when it begins with "//", as the path it is: RFC 9112 Section 3.2.1 makes a target that begins with "/" origin-form, whose host is the Host header, and only an absolute-form target (it begins with a scheme) carries its own. fasthttp's parser takes the authority out of such a target when it holds "://" anywhere, query and fragment included, or when the request has no Host header, and then c.Host() is the first path segment and c.Path() lacks it. The cause is in that parser, which this module cannot change. DefaultCtx.Reset calls this for the request line as it arrived, RestoreTarget for one a net/http middleware handed on.

What fasthttp parsed is looked at, not predicted from the Host header (see ParsesAsPath): a request built before its Host header was set, as adaptor.FiberApp builds one, was parsed without it, and the authority was taken out of a "//" target that holds no "://" as well. Whenever it took one, the path it kept is shorter than the path of the target.

func RestoreTarget(fctx *fasthttp.RequestCtx, original string)

Source: internal/wiretarget/wiretarget.go:378

RestoreTarget sets original, a request line, as fctx's request target. fasthttp parses it again, and takes the authority out of a "//" target again, so the origin-form reading Reset gave it is applied again. The Host header has to be back in place by then.

func RestoreURI(fctx *fasthttp.RequestCtx, original string, saved *fasthttp.URI)

Source: internal/wiretarget/wiretarget.go:369

RestoreURI sets original and saved back as fctx's request line and URI, and releases saved. Parsed from the line again, the URI would lose what a handler set through QueryArgs() and the origin-form reading of a "//" target.

func Routed[App any](c Ctx[App]) string

Source: internal/wiretarget/wiretarget.go:106

Routed returns the request target to hand on for c: the path the router matched, spelled as a request line has to carry it, then the query.

Under the default configuration c.Path() is nearly that spelling already: escapes of unreserved characters decoded, every other escape kept, dot segments resolved and empty segments kept. What it may still hold raw is written as an escape, the valid spelling of the same name: a stray "%" that begins no escape as "%25" (the net/http adapters answer such a path with 400 instead, see HasStrayPercent), and every byte RFC 3986 does not allow raw in a path, such as a space, "\", "|", "{", "}", '"', "^", "<", ">", a control byte or a byte of UTF-8, as its escape, as fasthttp's normalizing writer did before. Under UnescapePath c.Path() is fully decoded, so it is escaped again segment by segment, and a "/" that came from "%2F" stays the separator the router took it for.

The raw request line is deliberately not the source. fasthttp's own normalization of it decodes "%2F" into a separator, merges "//" and then resolves "..", so "/public/..%2Fadmin" and "//admin" reached a proxy upstream or a net/http handler as "/admin" although no middleware mounted on "/admin" had run for them. The routed path keeps them the distinct names the router matched.

The query is the one the client sent, even after a handler read it through c.Query(): fasthttp would serialize parsed arguments again, "%20" as "+" and ";" as "%3B", and a signed URL would no longer verify. Only when a handler changed the arguments through QueryArgs() are they forwarded as fasthttp serializes them, since that is the change it made.

The target may be the request line itself, a view of it rather than a copy, when that already spells the routed path, so read it before the request line is set to something else.

The target always begins with a slash. A path override that dropped it, a rewrite of "/go/*" to "$1" say, would otherwise hand on "http://u:pw@evil.example/x", which a request line reads as an absolute URL and so as a host and a credential; rooted, it is the path "/http://u:pw@evil.example/x".

func RoutedAsMatched[App any](c Ctx[App]) (string, bool)

Source: internal/wiretarget/wiretarget.go:514

RoutedAsMatched is Routed for a reader that takes the target as written, as a proxy upstream does. ok is false when Routed would escape a raw byte or a stray "%", whose escaped spelling the router keeps apart as another path.

func SaveURI(fctx *fasthttp.RequestCtx) *fasthttp.URI

Source: internal/wiretarget/wiretarget.go:361

SaveURI returns a pooled copy of the URI of fctx's request, with what handlers changed in it, for RestoreURI.

func SegmentsAsRouted(target string, allow Allowance) bool

Source: internal/wiretarget/wiretarget.go:419

SegmentsAsRouted reports whether a reader that decodes target's path and cleans it still sees the segments the router matched. It is false when the path holds an empty segment, an escaped slash or a forged escape (see HasForgedEscape): net/http's URL.Path reads "%2F" as a separator and a forged "%2e%2e" as "..", and its handlers clean "//" and ".." away, so "/public/..%2Fadmin", "//admin" and "/public/%2e%2e/admin" would be served as "/admin" by a handler no middleware mounted on "/admin" guarded. A proxied upstream that decodes "%2F" or merges "//" before it matches routes reads a target the same way, so the proxy handlers refuse one for which this is false, as the net/http handler adapters do.

It is false as well for a backslash, raw or escaped as "%5C", which WHATWG URL parsers and IIS read as a separator, and for a path parameter, a ";" in any segment. Servlet containers such as Tomcat and Jetty strip the parameters from every segment before they match routes, so "/admin;x/secret" and "/admin;/secret" are "/admin/secret" there, "/;/admin/secret" is "//admin/secret" and so "/admin/secret" once the empty segment is merged, and "/public/..;/admin" is "/admin".

allow lets some of this through. A "." or ".." segment that carries parameters, such as "..;x", is refused whatever it says, behind a raw slash or, with AllowSlashes, behind an escaped one: a reader that strips the parameters resolves it as a dot segment, and none has a use for it as a name. A segment made of parameters alone, such as ";x", is an empty segment once they are stripped, and is refused as one unless AllowSlashes is set too; as the last segment it spells the trailing slash and passes.

func Serve[App any](c Ctx[App], handler fasthttp.RequestHandler) Outcome

Source: internal/wiretarget/wiretarget.go:165

Serve runs handler, a net/http handler adapted by fasthttpadaptor, on a request line that spells the path the router matched (Routed) rather than the one that arrived, and puts the original back afterwards. The two differ for an escaped or non-canonical request, and net/http reads the raw one its own way: "/public/..%2Fadmin" decodes into a URL.Path that http.FileServer cleans to "/admin", although no middleware mounted on "/admin" has run. A routed path whose segments would not survive that reading, one with an empty segment or an escaped slash, is not handed on, nor is one that a handler passing the request on, such as httputil.ReverseProxy, would hand an upstream that reads it as another path: a backslash, or a segment a servlet container strips to a dot or an empty segment ("..;", "/;x/admin"). A path parameter on a named segment is handed on, since net/http reads it as part of the name; under UnescapePath, in the decoded path the handler reads (see parametersAsRouted). Unless UnescapePath makes the router decode every escape itself, a routed path that is not in the one spelling SingleSpelling accepts is not handed on either: the router keeps "/R&D" and "/R%26D", "/user@corp" and "/user%40corp", "/caf%C3%A9" and a raw "/café" apart, but the handler decodes each pair into one name. A target that holds no path (NoPath) has no spelling to refuse: "CONNECT [::1]:443" is routed as "/[::1]:443", whose raw brackets a path may not carry. A routed path holding a "%" that begins no escape (HasStrayPercent) is refused as net/http's own server refuses it, with a 400 rather than a 404: Routed would write "/100%" as "/100%25", and the handler would read both as one name where the router keeps two. Serve returns NotFound or BadRequest, without running handler, for a routed path it does not hand on.

func ServeNative[App any](c Ctx[App], handler fasthttp.RequestHandler) bool

Source: internal/wiretarget/wiretarget.go:179

ServeNative is Serve for a fasthttp handler registered as a route handler. The original line is put back once it returned, unless the response is a body stream or hijacked, which can belong to a net/http handler that fasthttpadaptor runs on a goroutine of its own and that goes on reading the line, or unless the handler timed out (fasthttp.TimeoutHandler, the timeout middleware), which leaves it running on the request. A "%" that begins no escape is handed on, as "%25": fasthttp decodes "/100%" and "/100%25" into one path for the handler, as it does without Fiber, so a second spelling is not what it gets (see HasStrayPercent).

func SetTarget[App any](c Ctx[App], target string)

Source: internal/wiretarget/wiretarget.go:298

SetTarget sets target, the routed request target, as c's request line. A target that is the line as it arrived is left alone, which spares a second parse of it. Otherwise the host fasthttp parsed is kept: a target that is a path makes fasthttp take the host from the Host header again, while an absolute-form request line names its own, which RFC 9112 Section 3.2.2 says the server uses and which c.Hostname() reported. Kept, the adapted handler sees in r.Host the host Fiber saw.

func SingleSpelling(path string) bool

Source: internal/wiretarget/wiretarget.go:489

SingleSpelling reports whether path, the path the router matched, is the one spelling of the path net/http decodes it to that a middleware registration can name. The router keeps "/R&D" and "/R%26D", "/user@corp" and "/user%40corp", "/caf%C3%A9" and a raw "/café" apart, so a middleware mounted on one of them does not run for the others, but net/http decodes them all into one URL.Path and serves the same directory for each. The spelling is the one a path has on the wire when its sub-delimiters, ":" and "@" are raw, an unreserved character is decoded (the router does that) and every other byte, a space or a byte of UTF-8 say, is an escape with uppercase hex digits. A "%" that begins no escape is not judged here: a net/http handler is answered 400 for one before this is asked (see HasStrayPercent), and a fasthttp route handler reads one as it always did, since fasthttp decodes both spellings into one path. Under UnescapePath the router decodes every escape itself, so the middleware sees one path and the caller skips this.

func StrayPercent[App any](c Ctx[App]) bool

Source: internal/wiretarget/wiretarget.go:572

StrayPercent reports whether the routed path of c, c.Path(), holds a "%" that begins no escape (HasStrayPercent). It does not under UnescapePath: the router has decoded every escape then, "/100%" and "/100%25" are one path to a middleware as to a handler, which is given it escaped again, and there is no second spelling to keep out.

func SurvivesNormalization(target string) bool

Source: internal/wiretarget/wiretarget.go:585

SurvivesNormalization reports whether fasthttp's path normalization would leave target's path naming what it names. Normalization merges every empty segment, decodes every escape and writes the path again with only some bytes escaped, so "//" and an escape that does not come back (see escapeSurvives) leave a normalizing client as a path the router never matched: "/public/..%2Fadmin" as "/admin" and "/..%3B/" as "/..;/". Any other escape, such as the "%C3%A9" Routed writes for "é", comes out as it went in, up to the case of its hex digits. The client also escapes a raw "!", "'", "(", ")" or "*", which changes the spelling but cannot add a separator.

Types

type Allowance

Source: internal/wiretarget/wiretarget.go:59

Allowance names what SegmentsAsRouted lets through that it refuses otherwise, for a caller that was told the reader keeps it as sent.

type Allowance uint8

type Ctx

Source: internal/wiretarget/wiretarget.go:19

Ctx is what this package reads of a fiber.Ctx, App being *fiber.App. Naming either would be an import cycle: package fiber serves net/http handlers here.

type Ctx[App any] interface {
	App() App
	OriginalURL() string
	Path(override ...string) string
	Request() *fasthttp.Request
	Response() *fasthttp.Response
	RequestCtx() *fasthttp.RequestCtx
	IsAbandoned() bool
	Locals(key any, value ...any) any
}
Methods
  • App func() App
  • OriginalURL func() string
  • Path func(override ...string) string
  • Request func() *fasthttp.Request
  • Response func() *fasthttp.Response
  • RequestCtx func() *fasthttp.RequestCtx
  • IsAbandoned func() bool
  • Locals func(key any, value ...any) any

type Outcome

Source: internal/wiretarget/wiretarget.go:44

Outcome is what Serve did with a request, and so the answer its caller gives when the handler did not run.

type Outcome uint8