ctx.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. // ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
  2. // 🤖 GitHub Repository: https://github.com/gofiber/fiber
  3. // 📌 API Documentation: https://docs.gofiber.io
  4. package fiber
  5. import (
  6. "context"
  7. "crypto/tls"
  8. "fmt"
  9. "io"
  10. "maps"
  11. "mime/multipart"
  12. "strconv"
  13. "strings"
  14. "sync/atomic"
  15. "time"
  16. "github.com/gofiber/utils/v2"
  17. utilsbytes "github.com/gofiber/utils/v2/bytes"
  18. "github.com/valyala/bytebufferpool"
  19. "github.com/valyala/fasthttp"
  20. )
  21. const (
  22. schemeHTTP = "http"
  23. schemeHTTPS = "https"
  24. )
  25. const (
  26. // maxParams defines the maximum number of parameters per route.
  27. maxParams = 30
  28. maxDetectionPaths = 3
  29. )
  30. var (
  31. _ io.Writer = (*DefaultCtx)(nil) // Compile-time check
  32. _ context.Context = (*DefaultCtx)(nil) // Compile-time check
  33. )
  34. // The contextKey type is unexported to prevent collisions with context keys defined in
  35. // other packages.
  36. type contextKey int
  37. // userContextKey define the key name for storing context.Context in *fasthttp.RequestCtx
  38. const (
  39. userContextKey contextKey = iota // __local_user_context__
  40. )
  41. // DefaultCtx is the default implementation of the Ctx interface
  42. // generation tool `go install github.com/vburenin/ifacemaker@f30b6f9bdbed4b5c4804ec9ba4a04a999525c202`
  43. // https://github.com/vburenin/ifacemaker/blob/f30b6f9bdbed4b5c4804ec9ba4a04a999525c202/ifacemaker.go#L14-L31
  44. //
  45. //go:generate ifacemaker --file ctx.go --file req.go --file res.go --struct DefaultCtx --iface Ctx --pkg fiber --promoted --output ctx_interface_gen.go --not-exported true --iface-comment "Ctx represents the Context which hold the HTTP request and response.\nIt has methods for the request query string, parameters, body, HTTP headers and so on."
  46. type DefaultCtx struct {
  47. handlerCtx CustomCtx // Active custom context implementation, if any
  48. DefaultReq // Default request api
  49. DefaultRes // Default response api
  50. app *App // Reference to *App
  51. route *Route // Reference to *Route
  52. fasthttp *fasthttp.RequestCtx // Reference to *fasthttp.RequestCtx
  53. bind *Bind // Default bind reference
  54. redirect *Redirect // Default redirect reference
  55. viewBindMap Map // Default view map to bind template engine
  56. values [maxParams]string // Route parameter values
  57. baseURI string // HTTP base uri
  58. pathOriginal string // Original HTTP path
  59. flashMessages redirectionMsgs // Flash messages
  60. path []byte // HTTP path with the modifications by the configuration
  61. detectionPath []byte // Route detection path
  62. treePathHash int // Hash of the path for the search in the tree
  63. indexRoute int // Index of the current route
  64. indexHandler int // Index of the current handler
  65. methodInt int // HTTP method INT equivalent
  66. abandoned atomic.Bool // If true, ctx won't be pooled until ForceRelease is called
  67. matched bool // Non use route matched
  68. skipNonUseRoutes bool // Skip non-use routes while iterating middleware
  69. }
  70. // TLSHandler hosts the callback hooks Fiber invokes while negotiating TLS
  71. // connections, including optional client certificate lookups.
  72. type TLSHandler struct {
  73. clientHelloInfo *tls.ClientHelloInfo
  74. }
  75. // GetClientInfo Callback function to set ClientHelloInfo
  76. // Must comply with the method structure of https://cs.opensource.google/go/go/+/refs/tags/go1.20:src/crypto/tls/common.go;l=554-563
  77. // Since we overlay the method of the TLS config in the listener method
  78. func (t *TLSHandler) GetClientInfo(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
  79. t.clientHelloInfo = info
  80. return nil, nil //nolint:nilnil // Not returning anything useful here is probably fine
  81. }
  82. // Views is the interface that wraps the Render function.
  83. type Views interface {
  84. Load() error
  85. Render(out io.Writer, name string, binding any, layout ...string) error
  86. }
  87. // App returns the *App reference to the instance of the Fiber application
  88. func (c *DefaultCtx) App() *App {
  89. return c.app
  90. }
  91. // BaseURL returns (protocol + host + base path).
  92. func (c *DefaultCtx) BaseURL() string {
  93. // TODO: Could be improved: 53.8 ns/op 32 B/op 1 allocs/op
  94. // Should work like https://codeigniter.com/user_guide/helpers/url_helper.html
  95. if c.baseURI != "" {
  96. return c.baseURI
  97. }
  98. c.baseURI = c.Scheme() + "://" + c.Host()
  99. return c.baseURI
  100. }
  101. // RequestCtx returns *fasthttp.RequestCtx that carries a deadline
  102. // a cancellation signal, and other values across API boundaries.
  103. func (c *DefaultCtx) RequestCtx() *fasthttp.RequestCtx {
  104. return c.fasthttp
  105. }
  106. // Context returns a context implementation that was set by
  107. // user earlier or returns a non-nil, empty context, if it was not set earlier.
  108. func (c *DefaultCtx) Context() context.Context {
  109. if c.fasthttp == nil {
  110. return context.Background()
  111. }
  112. if ctx, ok := c.fasthttp.UserValue(userContextKey).(context.Context); ok && ctx != nil {
  113. return ctx
  114. }
  115. ctx := context.Background()
  116. c.SetContext(ctx)
  117. return ctx
  118. }
  119. // SetContext sets a context implementation by user.
  120. func (c *DefaultCtx) SetContext(ctx context.Context) {
  121. if c.fasthttp == nil {
  122. return
  123. }
  124. c.fasthttp.SetUserValue(userContextKey, ctx)
  125. }
  126. // Deadline returns the time when work done on behalf of this context
  127. // should be canceled. Deadline returns ok==false when no deadline is
  128. // set. Successive calls to Deadline return the same results.
  129. //
  130. // Due to current limitations in how fasthttp works, Deadline operates as a nop.
  131. // See: https://github.com/valyala/fasthttp/issues/965#issuecomment-777268945
  132. func (*DefaultCtx) Deadline() (time.Time, bool) {
  133. return time.Time{}, false
  134. }
  135. // Done returns a channel that's closed when work done on behalf of this
  136. // context should be canceled. Done may return nil if this context can
  137. // never be canceled. Successive calls to Done return the same value.
  138. // The close of the Done channel may happen asynchronously,
  139. // after the cancel function returns.
  140. //
  141. // Due to current limitations in how fasthttp works, Done operates as a nop.
  142. // See: https://github.com/valyala/fasthttp/issues/965#issuecomment-777268945
  143. func (*DefaultCtx) Done() <-chan struct{} {
  144. return nil
  145. }
  146. // Err mirrors context.Err, returning nil until cancellation and then the terminal error value.
  147. //
  148. // Due to current limitations in how fasthttp works, Err operates as a nop.
  149. // See: https://github.com/valyala/fasthttp/issues/965#issuecomment-777268945
  150. func (*DefaultCtx) Err() error {
  151. return nil
  152. }
  153. // Request return the *fasthttp.Request object
  154. // This allows you to use all fasthttp request methods
  155. // https://godoc.org/github.com/valyala/fasthttp#Request
  156. // Returns nil if the context has been released.
  157. func (c *DefaultCtx) Request() *fasthttp.Request {
  158. if c.fasthttp == nil {
  159. return nil
  160. }
  161. return &c.fasthttp.Request
  162. }
  163. // Response return the *fasthttp.Response object
  164. // This allows you to use all fasthttp response methods
  165. // https://godoc.org/github.com/valyala/fasthttp#Response
  166. // Returns nil if the context has been released.
  167. func (c *DefaultCtx) Response() *fasthttp.Response {
  168. if c.fasthttp == nil {
  169. return nil
  170. }
  171. return &c.fasthttp.Response
  172. }
  173. // Get returns the HTTP request header specified by field.
  174. // Field names are case-insensitive
  175. // Returned value is only valid within the handler. Do not store any references.
  176. // Make copies or use the Immutable setting instead.
  177. func (c *DefaultCtx) Get(key string, defaultValue ...string) string {
  178. return c.DefaultReq.Get(key, defaultValue...)
  179. }
  180. // GetHeaders returns the HTTP request headers.
  181. // Returned value is only valid within the handler. Do not store any references.
  182. // Make copies or use the Immutable setting instead.
  183. func (c *DefaultCtx) GetHeaders() map[string][]string {
  184. return c.DefaultReq.GetHeaders()
  185. }
  186. // GetReqHeaders returns the HTTP request headers.
  187. // Returned value is only valid within the handler. Do not store any references.
  188. // Make copies or use the Immutable setting instead.
  189. func (c *DefaultCtx) GetReqHeaders() map[string][]string {
  190. return c.DefaultReq.GetHeaders()
  191. }
  192. // GetRespHeader returns the HTTP response header specified by field.
  193. // Field names are case-insensitive
  194. // Returned value is only valid within the handler. Do not store any references.
  195. // Make copies or use the Immutable setting instead.
  196. func (c *DefaultCtx) GetRespHeader(key string, defaultValue ...string) string {
  197. return c.DefaultRes.Get(key, defaultValue...)
  198. }
  199. // GetRespHeaders returns the HTTP response headers.
  200. // Returned value is only valid within the handler. Do not store any references.
  201. // Make copies or use the Immutable setting instead.
  202. func (c *DefaultCtx) GetRespHeaders() map[string][]string {
  203. return c.DefaultRes.GetHeaders()
  204. }
  205. // ClientHelloInfo return CHI from context
  206. func (c *DefaultCtx) ClientHelloInfo() *tls.ClientHelloInfo {
  207. if c.app.tlsHandler != nil {
  208. return c.app.tlsHandler.clientHelloInfo
  209. }
  210. return nil
  211. }
  212. // Next executes the next method in the stack that matches the current route.
  213. func (c *DefaultCtx) Next() error {
  214. // Increment handler index
  215. c.indexHandler++
  216. // Did we execute all route handlers?
  217. if c.indexHandler < len(c.route.Handlers) {
  218. if c.handlerCtx != nil {
  219. return c.route.Handlers[c.indexHandler](c.handlerCtx)
  220. }
  221. return c.route.Handlers[c.indexHandler](c)
  222. }
  223. if c.handlerCtx != nil {
  224. _, err := c.app.nextCustom(c.handlerCtx)
  225. return err
  226. }
  227. _, err := c.app.next(c)
  228. return err
  229. }
  230. // RestartRouting instead of going to the next handler. This may be useful after
  231. // changing the request path. Note that handlers might be executed again.
  232. func (c *DefaultCtx) RestartRouting() error {
  233. c.indexRoute = -1
  234. if c.handlerCtx != nil {
  235. _, err := c.app.nextCustom(c.handlerCtx)
  236. return err
  237. }
  238. _, err := c.app.next(c)
  239. return err
  240. }
  241. func (c *DefaultCtx) setHandlerCtx(ctx CustomCtx) {
  242. if ctx == nil {
  243. c.handlerCtx = nil
  244. return
  245. }
  246. if defaultCtx, ok := ctx.(*DefaultCtx); ok && defaultCtx == c {
  247. c.handlerCtx = nil
  248. return
  249. }
  250. c.handlerCtx = ctx
  251. }
  252. // OriginalURL contains the original request URL.
  253. // Returned value is only valid within the handler. Do not store any references.
  254. // Make copies or use the Immutable setting to use the value outside the Handler.
  255. func (c *DefaultCtx) OriginalURL() string {
  256. return c.app.toString(c.fasthttp.Request.Header.RequestURI())
  257. }
  258. // Path returns the path part of the request URL.
  259. // Optionally, you could override the path.
  260. // Make copies or use the Immutable setting to use the value outside the Handler.
  261. func (c *DefaultCtx) Path(override ...string) string {
  262. if len(override) != 0 && string(c.path) != override[0] {
  263. // Set new path to context
  264. c.pathOriginal = override[0]
  265. // Set new path to request context
  266. c.fasthttp.Request.URI().SetPath(c.pathOriginal)
  267. // Prettify path
  268. c.configDependentPaths()
  269. }
  270. return c.app.toString(c.path)
  271. }
  272. // RequestID returns the request identifier from the response header or request header.
  273. func (c *DefaultCtx) RequestID() string {
  274. if requestID := c.GetRespHeader(HeaderXRequestID); requestID != "" {
  275. return requestID
  276. }
  277. return c.Get(HeaderXRequestID)
  278. }
  279. // Req returns a convenience type whose API is limited to operations
  280. // on the incoming request.
  281. func (c *DefaultCtx) Req() Req {
  282. return &c.DefaultReq
  283. }
  284. // Res returns a convenience type whose API is limited to operations
  285. // on the outgoing response.
  286. func (c *DefaultCtx) Res() Res {
  287. return &c.DefaultRes
  288. }
  289. // Redirect returns the Redirect reference.
  290. // Use Redirect().Status() to set custom redirection status code.
  291. // If status is not specified, status defaults to 303 See Other.
  292. // You can use Redirect().To(), Redirect().Route() and Redirect().Back() for redirection.
  293. func (c *DefaultCtx) Redirect() *Redirect {
  294. if c.redirect == nil {
  295. c.redirect = AcquireRedirect()
  296. c.redirect.c = c
  297. }
  298. return c.redirect
  299. }
  300. // ViewBind Add vars to default view var map binding to template engine.
  301. // Variables are read by the Render method and may be overwritten.
  302. func (c *DefaultCtx) ViewBind(vars Map) error {
  303. // init viewBindMap - lazy map
  304. if c.viewBindMap == nil {
  305. c.viewBindMap = make(Map, len(vars))
  306. }
  307. maps.Copy(c.viewBindMap, vars)
  308. return nil
  309. }
  310. // Route returns the matched Route struct.
  311. func (c *DefaultCtx) Route() *Route {
  312. if c.route == nil {
  313. // Fallback for fasthttp error handler
  314. return &Route{
  315. path: c.pathOriginal,
  316. Path: c.pathOriginal,
  317. Method: c.Method(),
  318. Handlers: make([]Handler, 0),
  319. Params: make([]string, 0),
  320. }
  321. }
  322. return c.route
  323. }
  324. // FullPath returns the matched route path, including any group prefixes.
  325. func (c *DefaultCtx) FullPath() string {
  326. return c.Route().Path
  327. }
  328. // Matched returns true if the current request path was matched by the router.
  329. func (c *DefaultCtx) Matched() bool {
  330. return c.getMatched()
  331. }
  332. // IsMiddleware returns true if the current request handler was registered as middleware.
  333. func (c *DefaultCtx) IsMiddleware() bool {
  334. if c.route == nil {
  335. return false
  336. }
  337. if c.route.use {
  338. return true
  339. }
  340. // For route-level middleware, there will be a next handler in the chain
  341. return c.indexHandler+1 < len(c.route.Handlers)
  342. }
  343. // HasBody returns true if the request declares a body via Content-Length, Transfer-Encoding, or already buffered payload data.
  344. func (c *DefaultCtx) HasBody() bool {
  345. hdr := &c.fasthttp.Request.Header
  346. //nolint:revive // switch is exhaustive for all ContentLength() cases
  347. switch cl := hdr.ContentLength(); {
  348. case cl > 0:
  349. return true
  350. case cl == -1:
  351. // fasthttp reports -1 for Transfer-Encoding: chunked bodies.
  352. return true
  353. case cl == 0:
  354. if hasTransferEncodingBody(hdr) {
  355. return true
  356. }
  357. }
  358. return len(c.fasthttp.Request.Body()) > 0
  359. }
  360. // OverrideParam overwrites a route parameter value by name.
  361. // If the parameter name does not exist in the route, this method does nothing.
  362. func (c *DefaultCtx) OverrideParam(name, value string) {
  363. // If no route is matched, there are no parameters to update
  364. if !c.Matched() {
  365. return
  366. }
  367. // Normalize wildcard (*) and plus (+) tokens to their internal
  368. // representations (*1, +1) used by the router.
  369. if name == "*" || name == "+" {
  370. name += "1"
  371. }
  372. if c.app.config.CaseSensitive {
  373. for i, param := range c.route.Params {
  374. if param == name {
  375. c.values[i] = value
  376. return
  377. }
  378. }
  379. return
  380. }
  381. nameBytes := utils.UnsafeBytes(name)
  382. for i, param := range c.route.Params {
  383. if utils.EqualFold(utils.UnsafeBytes(param), nameBytes) {
  384. c.values[i] = value
  385. return
  386. }
  387. }
  388. }
  389. func hasTransferEncodingBody(hdr *fasthttp.RequestHeader) bool {
  390. teBytes := hdr.Peek(HeaderTransferEncoding)
  391. var te string
  392. if len(teBytes) > 0 {
  393. te = utils.UnsafeString(teBytes)
  394. } else {
  395. for key, value := range hdr.All() {
  396. if !utils.EqualFold(utils.UnsafeString(key), HeaderTransferEncoding) {
  397. continue
  398. }
  399. te = utils.UnsafeString(value)
  400. break
  401. }
  402. }
  403. if te == "" {
  404. return false
  405. }
  406. hasEncoding := false
  407. for raw := range strings.SplitSeq(te, ",") {
  408. token := utils.TrimSpace(raw)
  409. if token == "" {
  410. continue
  411. }
  412. if idx := strings.IndexByte(token, ';'); idx >= 0 {
  413. token = utils.TrimSpace(token[:idx])
  414. }
  415. if token == "" {
  416. continue
  417. }
  418. if utils.EqualFold(token, "identity") {
  419. continue
  420. }
  421. hasEncoding = true
  422. }
  423. return hasEncoding
  424. }
  425. // IsWebSocket returns true if the request includes a WebSocket upgrade handshake.
  426. func (c *DefaultCtx) IsWebSocket() bool {
  427. conn := c.fasthttp.Request.Header.Peek(HeaderConnection)
  428. var isUpgrade bool
  429. for v := range strings.SplitSeq(utils.UnsafeString(conn), ",") {
  430. if utils.EqualFold(utils.TrimSpace(v), "upgrade") {
  431. isUpgrade = true
  432. break
  433. }
  434. }
  435. if !isUpgrade {
  436. return false
  437. }
  438. return utils.EqualFold(c.fasthttp.Request.Header.Peek(HeaderUpgrade), websocketBytes)
  439. }
  440. // IsPreflight returns true if the request is a CORS preflight.
  441. func (c *DefaultCtx) IsPreflight() bool {
  442. if c.Method() != MethodOptions {
  443. return false
  444. }
  445. hdr := &c.fasthttp.Request.Header
  446. if len(hdr.Peek(HeaderAccessControlRequestMethod)) == 0 {
  447. return false
  448. }
  449. return len(hdr.Peek(HeaderOrigin)) > 0
  450. }
  451. // SaveFile saves any multipart file to disk.
  452. func (*DefaultCtx) SaveFile(fileheader *multipart.FileHeader, path string) error {
  453. return fasthttp.SaveMultipartFile(fileheader, path)
  454. }
  455. // SaveFileToStorage saves any multipart file to an external storage system.
  456. func (c *DefaultCtx) SaveFileToStorage(fileheader *multipart.FileHeader, path string, storage Storage) error {
  457. file, err := fileheader.Open()
  458. if err != nil {
  459. return fmt.Errorf("failed to open: %w", err)
  460. }
  461. defer file.Close() //nolint:errcheck // not needed
  462. maxUploadSize := c.app.config.BodyLimit
  463. if maxUploadSize <= 0 {
  464. maxUploadSize = DefaultBodyLimit
  465. }
  466. if fileheader.Size > 0 && fileheader.Size > int64(maxUploadSize) {
  467. return fmt.Errorf("failed to read: %w", fasthttp.ErrBodyTooLarge)
  468. }
  469. buf := bytebufferpool.Get()
  470. defer bytebufferpool.Put(buf)
  471. limitedReader := io.LimitReader(file, int64(maxUploadSize)+1)
  472. if _, err = buf.ReadFrom(limitedReader); err != nil {
  473. return fmt.Errorf("failed to read: %w", err)
  474. }
  475. if buf.Len() > maxUploadSize {
  476. return fmt.Errorf("failed to read: %w", fasthttp.ErrBodyTooLarge)
  477. }
  478. data := append([]byte(nil), buf.Bytes()...)
  479. if err := storage.SetWithContext(c.Context(), path, data, 0); err != nil {
  480. return fmt.Errorf("failed to store: %w", err)
  481. }
  482. return nil
  483. }
  484. // Secure returns whether a secure connection was established.
  485. func (c *DefaultCtx) Secure() bool {
  486. return c.Protocol() == schemeHTTPS
  487. }
  488. // Status sets the HTTP status for the response.
  489. // This method is chainable.
  490. func (c *DefaultCtx) Status(status int) Ctx {
  491. c.fasthttp.Response.SetStatusCode(status)
  492. return c
  493. }
  494. // String returns unique string representation of the ctx.
  495. //
  496. // The returned value may be useful for logging.
  497. func (c *DefaultCtx) String() string {
  498. // Get buffer from pool
  499. buf := bytebufferpool.Get()
  500. // Start with the ID, converting it to a hex string without fmt.Sprintf
  501. buf.WriteByte('#')
  502. // Convert ID to hexadecimal
  503. id := strconv.FormatUint(c.fasthttp.ID(), 16)
  504. // Pad with leading zeros to ensure 16 characters
  505. for i := 0; i < (16 - len(id)); i++ {
  506. buf.WriteByte('0')
  507. }
  508. buf.WriteString(id)
  509. buf.WriteString(" - ")
  510. // Add local and remote addresses directly
  511. buf.WriteString(c.fasthttp.LocalAddr().String())
  512. buf.WriteString(" <-> ")
  513. buf.WriteString(c.fasthttp.RemoteAddr().String())
  514. buf.WriteString(" - ")
  515. // Add method and URI
  516. buf.Write(c.fasthttp.Request.Header.Method())
  517. buf.WriteByte(' ')
  518. buf.Write(c.fasthttp.URI().FullURI())
  519. // Allocate string
  520. str := buf.String()
  521. // Reset buffer
  522. buf.Reset()
  523. bytebufferpool.Put(buf)
  524. return str
  525. }
  526. // Value makes it possible to retrieve values (Locals) under keys scoped to the request
  527. // and therefore available to all following routes that match the request. If the context
  528. // has been released and c.fasthttp is nil (for example, after ReleaseCtx), Value returns nil.
  529. func (c *DefaultCtx) Value(key any) any {
  530. if c.fasthttp == nil {
  531. return nil
  532. }
  533. return c.fasthttp.UserValue(key)
  534. }
  535. var (
  536. // xmlHTTPRequestBytes is precomputed for XHR detection
  537. xmlHTTPRequestBytes = []byte("xmlhttprequest")
  538. // websocketBytes is precomputed for WebSocket upgrade detection
  539. websocketBytes = []byte("websocket")
  540. )
  541. // XHR returns a Boolean property, that is true, if the request's X-Requested-With header field is XMLHttpRequest,
  542. // indicating that the request was issued by a client library (such as jQuery).
  543. func (c *DefaultCtx) XHR() bool {
  544. return utils.EqualFold(c.fasthttp.Request.Header.Peek(HeaderXRequestedWith), xmlHTTPRequestBytes)
  545. }
  546. // configDependentPaths set paths for route recognition and prepared paths for the user,
  547. // here the features for caseSensitive, decoded paths, strict paths are evaluated
  548. func (c *DefaultCtx) configDependentPaths() {
  549. c.path = append(c.path[:0], c.pathOriginal...)
  550. // If UnescapePath enabled, we decode the path and save it for the framework user
  551. if c.app.config.UnescapePath {
  552. c.path = fasthttp.AppendUnquotedArg(c.path[:0], c.path)
  553. }
  554. // another path is specified which is for routing recognition only
  555. // use the path that was changed by the previous configuration flags
  556. c.detectionPath = append(c.detectionPath[:0], c.path...)
  557. // If CaseSensitive is disabled, we lowercase the original path
  558. if !c.app.config.CaseSensitive {
  559. c.detectionPath = utilsbytes.UnsafeToLower(c.detectionPath)
  560. }
  561. // If StrictRouting is disabled, we strip all trailing slashes
  562. if !c.app.config.StrictRouting && len(c.detectionPath) > 1 && c.detectionPath[len(c.detectionPath)-1] == '/' {
  563. c.detectionPath = utils.TrimRight(c.detectionPath, '/')
  564. }
  565. // Define the path for dividing routes into areas for fast tree detection, so that fewer routes need to be traversed,
  566. // since the first three characters area select a list of routes
  567. c.treePathHash = 0
  568. if len(c.detectionPath) >= maxDetectionPaths {
  569. c.treePathHash = int(c.detectionPath[0])<<16 |
  570. int(c.detectionPath[1])<<8 |
  571. int(c.detectionPath[2])
  572. }
  573. }
  574. // Reset is a method to reset context fields by given request when to use server handlers.
  575. func (c *DefaultCtx) Reset(fctx *fasthttp.RequestCtx) {
  576. // Reset route and handler index
  577. c.indexRoute = -1
  578. c.indexHandler = 0
  579. // Reset matched flag
  580. c.matched = false
  581. c.skipNonUseRoutes = false
  582. // Set paths
  583. c.pathOriginal = c.app.toString(fctx.URI().PathOriginal())
  584. // Set method
  585. c.methodInt = c.app.methodInt(utils.UnsafeString(fctx.Request.Header.Method()))
  586. // Attach *fasthttp.RequestCtx to ctx
  587. c.fasthttp = fctx
  588. // reset base uri
  589. c.baseURI = ""
  590. // Prettify path
  591. c.configDependentPaths()
  592. c.DefaultReq.c = c
  593. c.DefaultRes.c = c
  594. c.fasthttp.SetUserValue(userContextKey, nil)
  595. }
  596. // release is a method to reset context fields when to use ReleaseCtx()
  597. func (c *DefaultCtx) release() {
  598. c.route = nil
  599. c.fasthttp = nil
  600. if c.bind != nil {
  601. ReleaseBind(c.bind)
  602. c.bind = nil
  603. }
  604. c.flashMessages = c.flashMessages[:0]
  605. // Clear viewBindMap by deleting all keys (reuse underlying map if possible)
  606. clear(c.viewBindMap)
  607. if c.redirect != nil {
  608. ReleaseRedirect(c.redirect)
  609. c.redirect = nil
  610. }
  611. c.skipNonUseRoutes = false
  612. // performance: no need for using c.abandoned.Store(false) here, as it is always set to false when it was true in ForceRelease
  613. c.handlerCtx = nil
  614. c.DefaultReq.release()
  615. c.DefaultRes.release()
  616. }
  617. // Abandon marks this context as abandoned. An abandoned context will not be
  618. // returned to the pool when ReleaseCtx is called.
  619. //
  620. // This is used by the timeout middleware to return immediately while the
  621. // handler goroutine continues using the context safely.
  622. //
  623. // Only call ForceRelease after Abandon if you can guarantee no other goroutine
  624. // (including Fiber's requestHandler and ErrorHandler) will touch the context.
  625. // The timeout middleware intentionally does NOT call ForceRelease to avoid
  626. // races, which means timed-out requests leak their contexts until a safe
  627. // reclamation strategy exists.
  628. func (c *DefaultCtx) Abandon() {
  629. c.abandoned.Store(true)
  630. }
  631. // IsAbandoned returns true if Abandon() was called on this context.
  632. func (c *DefaultCtx) IsAbandoned() bool {
  633. return c.abandoned.Load()
  634. }
  635. // ForceRelease releases an abandoned context back to the pool.
  636. // This MUST only be called after all goroutines (including requestHandler and
  637. // ErrorHandler) have completely finished using this context. Calling it while
  638. // any goroutine is still running causes races.
  639. func (c *DefaultCtx) ForceRelease() {
  640. c.abandoned.Store(false)
  641. c.app.ReleaseCtx(c)
  642. }
  643. func (c *DefaultCtx) renderExtensions(bind any) {
  644. if bindMap, ok := bind.(Map); ok {
  645. // Bind view map
  646. for key, value := range c.viewBindMap {
  647. if _, ok := bindMap[key]; !ok {
  648. bindMap[key] = value
  649. }
  650. }
  651. // Check if the PassLocalsToViews option is enabled (by default it is disabled)
  652. if c.app.config.PassLocalsToViews {
  653. // Loop through each local and set it in the map
  654. c.fasthttp.VisitUserValues(func(key []byte, val any) {
  655. // check if bindMap doesn't contain the key
  656. if _, ok := bindMap[c.app.toString(key)]; !ok {
  657. // Set the key and value in the bindMap
  658. bindMap[c.app.toString(key)] = val
  659. }
  660. })
  661. }
  662. }
  663. if len(c.app.mountFields.appListKeys) == 0 {
  664. c.app.generateAppListKeys()
  665. }
  666. }
  667. // Bind You can bind body, cookie, headers etc. into the map, map slice, struct easily by using Binding method.
  668. // It gives custom binding support, detailed binding options and more.
  669. // Replacement of: BodyParser, ParamsParser, GetReqHeaders, GetRespHeaders, AllParams, QueryParser, ReqHeaderParser
  670. func (c *DefaultCtx) Bind() *Bind {
  671. if c.bind == nil {
  672. c.bind = AcquireBind()
  673. }
  674. c.bind.ctx = c
  675. return c.bind
  676. }
  677. // Methods to use with next stack.
  678. func (c *DefaultCtx) getMethodInt() int {
  679. return c.methodInt
  680. }
  681. func (c *DefaultCtx) getIndexRoute() int {
  682. return c.indexRoute
  683. }
  684. func (c *DefaultCtx) getTreePathHash() int {
  685. return c.treePathHash
  686. }
  687. func (c *DefaultCtx) getDetectionPath() string {
  688. return c.app.toString(c.detectionPath)
  689. }
  690. func (c *DefaultCtx) getValues() *[maxParams]string {
  691. return &c.values
  692. }
  693. func (c *DefaultCtx) getMatched() bool {
  694. return c.matched
  695. }
  696. func (c *DefaultCtx) getSkipNonUseRoutes() bool {
  697. return c.skipNonUseRoutes
  698. }
  699. func (c *DefaultCtx) setIndexHandler(handler int) {
  700. c.indexHandler = handler
  701. }
  702. func (c *DefaultCtx) setIndexRoute(route int) {
  703. c.indexRoute = route
  704. }
  705. func (c *DefaultCtx) setMatched(matched bool) {
  706. c.matched = matched
  707. }
  708. func (c *DefaultCtx) setSkipNonUseRoutes(skip bool) {
  709. c.skipNonUseRoutes = skip
  710. }
  711. func (c *DefaultCtx) setRoute(route *Route) {
  712. c.route = route
  713. }
  714. func (c *DefaultCtx) getPathOriginal() string {
  715. return c.pathOriginal
  716. }