helpers.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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. "bytes"
  7. "crypto/tls"
  8. "fmt"
  9. "hash/crc32"
  10. "io"
  11. "log"
  12. "net"
  13. "os"
  14. "path/filepath"
  15. "reflect"
  16. "strings"
  17. "time"
  18. "unsafe"
  19. "github.com/gofiber/fiber/v2/utils"
  20. "github.com/valyala/bytebufferpool"
  21. "github.com/valyala/fasthttp"
  22. )
  23. // getTLSConfig returns a net listener's tls config
  24. func getTLSConfig(ln net.Listener) *tls.Config {
  25. // Get listener type
  26. pointer := reflect.ValueOf(ln)
  27. // Is it a tls.listener?
  28. if pointer.String() == "<*tls.listener Value>" {
  29. // Copy value from pointer
  30. if val := reflect.Indirect(pointer); val.Type() != nil {
  31. // Get private field from value
  32. if field := val.FieldByName("config"); field.Type() != nil {
  33. // Copy value from pointer field (unsafe)
  34. newval := reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())) //nolint:gosec // Probably the only way to extract the *tls.Config from a net.Listener. TODO: Verify there really is no easier way without using unsafe.
  35. if newval.Type() != nil {
  36. // Get element from pointer
  37. if elem := newval.Elem(); elem.Type() != nil {
  38. // Cast value to *tls.Config
  39. c, ok := elem.Interface().(*tls.Config)
  40. if !ok {
  41. panic(fmt.Errorf("failed to type-assert to *tls.Config"))
  42. }
  43. return c
  44. }
  45. }
  46. }
  47. }
  48. }
  49. return nil
  50. }
  51. // readContent opens a named file and read content from it
  52. func readContent(rf io.ReaderFrom, name string) (int64, error) {
  53. // Read file
  54. f, err := os.Open(filepath.Clean(name))
  55. if err != nil {
  56. return 0, fmt.Errorf("failed to open: %w", err)
  57. }
  58. defer func() {
  59. if err = f.Close(); err != nil {
  60. log.Printf("Error closing file: %s\n", err)
  61. }
  62. }()
  63. if n, err := rf.ReadFrom(f); err != nil {
  64. return n, fmt.Errorf("failed to read: %w", err)
  65. }
  66. return 0, nil
  67. }
  68. // quoteString escape special characters in a given string
  69. func (app *App) quoteString(raw string) string {
  70. bb := bytebufferpool.Get()
  71. // quoted := string(fasthttp.AppendQuotedArg(bb.B, getBytes(raw)))
  72. quoted := app.getString(fasthttp.AppendQuotedArg(bb.B, app.getBytes(raw)))
  73. bytebufferpool.Put(bb)
  74. return quoted
  75. }
  76. // Scan stack if other methods match the request
  77. func (app *App) methodExist(ctx *Ctx) bool {
  78. var exists bool
  79. methods := app.config.RequestMethods
  80. for i := 0; i < len(methods); i++ {
  81. // Skip original method
  82. if ctx.methodINT == i {
  83. continue
  84. }
  85. // Reset stack index
  86. indexRoute := -1
  87. tree, ok := ctx.app.treeStack[i][ctx.treePath]
  88. if !ok {
  89. tree = ctx.app.treeStack[i][""]
  90. }
  91. // Get stack length
  92. lenr := len(tree) - 1
  93. // Loop over the route stack starting from previous index
  94. for indexRoute < lenr {
  95. // Increment route index
  96. indexRoute++
  97. // Get *Route
  98. route := tree[indexRoute]
  99. // Skip use routes
  100. if route.use {
  101. continue
  102. }
  103. // Check if it matches the request path
  104. match := route.match(ctx.detectionPath, ctx.path, &ctx.values)
  105. // No match, next route
  106. if match {
  107. // We matched
  108. exists = true
  109. // Add method to Allow header
  110. ctx.Append(HeaderAllow, methods[i])
  111. // Break stack loop
  112. break
  113. }
  114. }
  115. }
  116. return exists
  117. }
  118. // uniqueRouteStack drop all not unique routes from the slice
  119. func uniqueRouteStack(stack []*Route) []*Route {
  120. var unique []*Route
  121. m := make(map[*Route]int)
  122. for _, v := range stack {
  123. if _, ok := m[v]; !ok {
  124. // Unique key found. Record position and collect
  125. // in result.
  126. m[v] = len(unique)
  127. unique = append(unique, v)
  128. }
  129. }
  130. return unique
  131. }
  132. // defaultString returns the value or a default value if it is set
  133. func defaultString(value string, defaultValue []string) string {
  134. if len(value) == 0 && len(defaultValue) > 0 {
  135. return defaultValue[0]
  136. }
  137. return value
  138. }
  139. const normalizedHeaderETag = "Etag"
  140. // Generate and set ETag header to response
  141. func setETag(c *Ctx, weak bool) { //nolint: revive // Accepting a bool param is fine here
  142. // Don't generate ETags for invalid responses
  143. if c.fasthttp.Response.StatusCode() != StatusOK {
  144. return
  145. }
  146. body := c.fasthttp.Response.Body()
  147. // Skips ETag if no response body is present
  148. if len(body) == 0 {
  149. return
  150. }
  151. // Get ETag header from request
  152. clientEtag := c.Get(HeaderIfNoneMatch)
  153. // Generate ETag for response
  154. const pol = 0xD5828281
  155. crc32q := crc32.MakeTable(pol)
  156. etag := fmt.Sprintf("\"%d-%v\"", len(body), crc32.Checksum(body, crc32q))
  157. // Enable weak tag
  158. if weak {
  159. etag = "W/" + etag
  160. }
  161. // Check if client's ETag is weak
  162. if strings.HasPrefix(clientEtag, "W/") {
  163. // Check if server's ETag is weak
  164. if clientEtag[2:] == etag || clientEtag[2:] == etag[2:] {
  165. // W/1 == 1 || W/1 == W/1
  166. if err := c.SendStatus(StatusNotModified); err != nil {
  167. log.Printf("setETag: failed to SendStatus: %v\n", err)
  168. }
  169. c.fasthttp.ResetBody()
  170. return
  171. }
  172. // W/1 != W/2 || W/1 != 2
  173. c.setCanonical(normalizedHeaderETag, etag)
  174. return
  175. }
  176. if strings.Contains(clientEtag, etag) {
  177. // 1 == 1
  178. if err := c.SendStatus(StatusNotModified); err != nil {
  179. log.Printf("setETag: failed to SendStatus: %v\n", err)
  180. }
  181. c.fasthttp.ResetBody()
  182. return
  183. }
  184. // 1 != 2
  185. c.setCanonical(normalizedHeaderETag, etag)
  186. }
  187. func getGroupPath(prefix, path string) string {
  188. if len(path) == 0 {
  189. return prefix
  190. }
  191. if path[0] != '/' {
  192. path = "/" + path
  193. }
  194. return utils.TrimRight(prefix, '/') + path
  195. }
  196. // acceptsOffer This function determines if an offer matches a given specification.
  197. // It checks if the specification ends with a '*' or if the offer has the prefix of the specification.
  198. // Returns true if the offer matches the specification, false otherwise.
  199. func acceptsOffer(spec, offer string) bool {
  200. if len(spec) >= 1 && spec[len(spec)-1] == '*' {
  201. return true
  202. } else if strings.HasPrefix(spec, offer) {
  203. return true
  204. }
  205. return false
  206. }
  207. // acceptsOfferType This function determines if an offer type matches a given specification.
  208. // It checks if the specification is equal to */* (i.e., all types are accepted).
  209. // It gets the MIME type of the offer (either from the offer itself or by its file extension).
  210. // It checks if the offer MIME type matches the specification MIME type or if the specification is of the form <MIME_type>/* and the offer MIME type has the same MIME type.
  211. // Returns true if the offer type matches the specification, false otherwise.
  212. func acceptsOfferType(spec, offerType string) bool {
  213. // Accept: */*
  214. if spec == "*/*" {
  215. return true
  216. }
  217. var mimetype string
  218. if strings.IndexByte(offerType, '/') != -1 {
  219. mimetype = offerType // MIME type
  220. } else {
  221. mimetype = utils.GetMIME(offerType) // extension
  222. }
  223. if spec == mimetype {
  224. // Accept: <MIME_type>/<MIME_subtype>
  225. return true
  226. }
  227. s := strings.IndexByte(mimetype, '/')
  228. // Accept: <MIME_type>/*
  229. if strings.HasPrefix(spec, mimetype[:s]) && (spec[s:] == "/*" || mimetype[s:] == "/*") {
  230. return true
  231. }
  232. return false
  233. }
  234. // getOffer return valid offer for header negotiation
  235. func getOffer(header string, isAccepted func(spec, offer string) bool, offers ...string) string {
  236. if len(offers) == 0 {
  237. return ""
  238. } else if header == "" {
  239. return offers[0]
  240. }
  241. for _, offer := range offers {
  242. if len(offer) == 0 {
  243. continue
  244. }
  245. spec, commaPos := "", 0
  246. for len(header) > 0 && commaPos != -1 {
  247. commaPos = strings.IndexByte(header, ',')
  248. if commaPos != -1 {
  249. spec = utils.Trim(header[:commaPos], ' ')
  250. } else {
  251. spec = utils.TrimLeft(header, ' ')
  252. }
  253. if factorSign := strings.IndexByte(spec, ';'); factorSign != -1 {
  254. spec = spec[:factorSign]
  255. }
  256. // isAccepted if the current offer is accepted
  257. if isAccepted(spec, offer) {
  258. return offer
  259. }
  260. if commaPos != -1 {
  261. header = header[commaPos+1:]
  262. }
  263. }
  264. }
  265. return ""
  266. }
  267. func matchEtag(s, etag string) bool {
  268. if s == etag || s == "W/"+etag || "W/"+s == etag {
  269. return true
  270. }
  271. return false
  272. }
  273. func (app *App) isEtagStale(etag string, noneMatchBytes []byte) bool {
  274. var start, end int
  275. // Adapted from:
  276. // https://github.com/jshttp/fresh/blob/10e0471669dbbfbfd8de65bc6efac2ddd0bfa057/index.js#L110
  277. for i := range noneMatchBytes {
  278. switch noneMatchBytes[i] {
  279. case 0x20:
  280. if start == end {
  281. start = i + 1
  282. end = i + 1
  283. }
  284. case 0x2c:
  285. if matchEtag(app.getString(noneMatchBytes[start:end]), etag) {
  286. return false
  287. }
  288. start = i + 1
  289. end = i + 1
  290. default:
  291. end = i + 1
  292. }
  293. }
  294. return !matchEtag(app.getString(noneMatchBytes[start:end]), etag)
  295. }
  296. func parseAddr(raw string) (string, string) { //nolint:revive // Returns (host, port)
  297. if i := strings.LastIndex(raw, ":"); i != -1 {
  298. return raw[:i], raw[i+1:]
  299. }
  300. return raw, ""
  301. }
  302. const noCacheValue = "no-cache"
  303. // isNoCache checks if the cacheControl header value is a `no-cache`.
  304. func isNoCache(cacheControl string) bool {
  305. i := strings.Index(cacheControl, noCacheValue)
  306. if i == -1 {
  307. return false
  308. }
  309. // Xno-cache
  310. if i > 0 && !(cacheControl[i-1] == ' ' || cacheControl[i-1] == ',') {
  311. return false
  312. }
  313. // bla bla, no-cache
  314. if i+len(noCacheValue) == len(cacheControl) {
  315. return true
  316. }
  317. // bla bla, no-cacheX
  318. if cacheControl[i+len(noCacheValue)] != ',' {
  319. return false
  320. }
  321. // OK
  322. return true
  323. }
  324. type testConn struct {
  325. r bytes.Buffer
  326. w bytes.Buffer
  327. }
  328. func (c *testConn) Read(b []byte) (int, error) { return c.r.Read(b) } //nolint:wrapcheck // This must not be wrapped
  329. func (c *testConn) Write(b []byte) (int, error) { return c.w.Write(b) } //nolint:wrapcheck // This must not be wrapped
  330. func (*testConn) Close() error { return nil }
  331. func (*testConn) LocalAddr() net.Addr { return &net.TCPAddr{Port: 0, Zone: "", IP: net.IPv4zero} }
  332. func (*testConn) RemoteAddr() net.Addr { return &net.TCPAddr{Port: 0, Zone: "", IP: net.IPv4zero} }
  333. func (*testConn) SetDeadline(_ time.Time) error { return nil }
  334. func (*testConn) SetReadDeadline(_ time.Time) error { return nil }
  335. func (*testConn) SetWriteDeadline(_ time.Time) error { return nil }
  336. func getStringImmutable(b []byte) string {
  337. return string(b)
  338. }
  339. func getBytesImmutable(s string) []byte {
  340. return []byte(s)
  341. }
  342. // HTTP methods and their unique INTs
  343. func (app *App) methodInt(s string) int {
  344. // For better performance
  345. if len(app.configured.RequestMethods) == 0 {
  346. // TODO: Use iota instead
  347. switch s {
  348. case MethodGet:
  349. return 0
  350. case MethodHead:
  351. return 1
  352. case MethodPost:
  353. return 2
  354. case MethodPut:
  355. return 3
  356. case MethodDelete:
  357. return 4
  358. case MethodConnect:
  359. return 5
  360. case MethodOptions:
  361. return 6
  362. case MethodTrace:
  363. return 7
  364. case MethodPatch:
  365. return 8
  366. default:
  367. return -1
  368. }
  369. }
  370. // For method customization
  371. for i, v := range app.config.RequestMethods {
  372. if s == v {
  373. return i
  374. }
  375. }
  376. return -1
  377. }
  378. // IsMethodSafe reports whether the HTTP method is considered safe.
  379. // See https://datatracker.ietf.org/doc/html/rfc9110#section-9.2.1
  380. func IsMethodSafe(m string) bool {
  381. switch m {
  382. case MethodGet,
  383. MethodHead,
  384. MethodOptions,
  385. MethodTrace:
  386. return true
  387. default:
  388. return false
  389. }
  390. }
  391. // IsMethodIdempotent reports whether the HTTP method is considered idempotent.
  392. // See https://datatracker.ietf.org/doc/html/rfc9110#section-9.2.2
  393. func IsMethodIdempotent(m string) bool {
  394. if IsMethodSafe(m) {
  395. return true
  396. }
  397. switch m {
  398. case MethodPut, MethodDelete:
  399. return true
  400. default:
  401. return false
  402. }
  403. }
  404. // HTTP methods were copied from net/http.
  405. const (
  406. MethodGet = "GET" // RFC 7231, 4.3.1
  407. MethodHead = "HEAD" // RFC 7231, 4.3.2
  408. MethodPost = "POST" // RFC 7231, 4.3.3
  409. MethodPut = "PUT" // RFC 7231, 4.3.4
  410. MethodPatch = "PATCH" // RFC 5789
  411. MethodDelete = "DELETE" // RFC 7231, 4.3.5
  412. MethodConnect = "CONNECT" // RFC 7231, 4.3.6
  413. MethodOptions = "OPTIONS" // RFC 7231, 4.3.7
  414. MethodTrace = "TRACE" // RFC 7231, 4.3.8
  415. methodUse = "USE"
  416. )
  417. // MIME types that are commonly used
  418. const (
  419. MIMETextXML = "text/xml"
  420. MIMETextHTML = "text/html"
  421. MIMETextPlain = "text/plain"
  422. MIMETextJavaScript = "text/javascript"
  423. MIMEApplicationXML = "application/xml"
  424. MIMEApplicationJSON = "application/json"
  425. // Deprecated: use MIMETextJavaScript instead
  426. MIMEApplicationJavaScript = "application/javascript"
  427. MIMEApplicationForm = "application/x-www-form-urlencoded"
  428. MIMEOctetStream = "application/octet-stream"
  429. MIMEMultipartForm = "multipart/form-data"
  430. MIMETextXMLCharsetUTF8 = "text/xml; charset=utf-8"
  431. MIMETextHTMLCharsetUTF8 = "text/html; charset=utf-8"
  432. MIMETextPlainCharsetUTF8 = "text/plain; charset=utf-8"
  433. MIMETextJavaScriptCharsetUTF8 = "text/javascript; charset=utf-8"
  434. MIMEApplicationXMLCharsetUTF8 = "application/xml; charset=utf-8"
  435. MIMEApplicationJSONCharsetUTF8 = "application/json; charset=utf-8"
  436. // Deprecated: use MIMETextJavaScriptCharsetUTF8 instead
  437. MIMEApplicationJavaScriptCharsetUTF8 = "application/javascript; charset=utf-8"
  438. )
  439. // HTTP status codes were copied from https://github.com/nginx/nginx/blob/67d2a9541826ecd5db97d604f23460210fd3e517/conf/mime.types with the following updates:
  440. // - Rename StatusNonAuthoritativeInfo to StatusNonAuthoritativeInformation
  441. // - Add StatusSwitchProxy (306)
  442. // NOTE: Keep this list in sync with statusMessage
  443. const (
  444. StatusContinue = 100 // RFC 9110, 15.2.1
  445. StatusSwitchingProtocols = 101 // RFC 9110, 15.2.2
  446. StatusProcessing = 102 // RFC 2518, 10.1
  447. StatusEarlyHints = 103 // RFC 8297
  448. StatusOK = 200 // RFC 9110, 15.3.1
  449. StatusCreated = 201 // RFC 9110, 15.3.2
  450. StatusAccepted = 202 // RFC 9110, 15.3.3
  451. StatusNonAuthoritativeInformation = 203 // RFC 9110, 15.3.4
  452. StatusNoContent = 204 // RFC 9110, 15.3.5
  453. StatusResetContent = 205 // RFC 9110, 15.3.6
  454. StatusPartialContent = 206 // RFC 9110, 15.3.7
  455. StatusMultiStatus = 207 // RFC 4918, 11.1
  456. StatusAlreadyReported = 208 // RFC 5842, 7.1
  457. StatusIMUsed = 226 // RFC 3229, 10.4.1
  458. StatusMultipleChoices = 300 // RFC 9110, 15.4.1
  459. StatusMovedPermanently = 301 // RFC 9110, 15.4.2
  460. StatusFound = 302 // RFC 9110, 15.4.3
  461. StatusSeeOther = 303 // RFC 9110, 15.4.4
  462. StatusNotModified = 304 // RFC 9110, 15.4.5
  463. StatusUseProxy = 305 // RFC 9110, 15.4.6
  464. StatusSwitchProxy = 306 // RFC 9110, 15.4.7 (Unused)
  465. StatusTemporaryRedirect = 307 // RFC 9110, 15.4.8
  466. StatusPermanentRedirect = 308 // RFC 9110, 15.4.9
  467. StatusBadRequest = 400 // RFC 9110, 15.5.1
  468. StatusUnauthorized = 401 // RFC 9110, 15.5.2
  469. StatusPaymentRequired = 402 // RFC 9110, 15.5.3
  470. StatusForbidden = 403 // RFC 9110, 15.5.4
  471. StatusNotFound = 404 // RFC 9110, 15.5.5
  472. StatusMethodNotAllowed = 405 // RFC 9110, 15.5.6
  473. StatusNotAcceptable = 406 // RFC 9110, 15.5.7
  474. StatusProxyAuthRequired = 407 // RFC 9110, 15.5.8
  475. StatusRequestTimeout = 408 // RFC 9110, 15.5.9
  476. StatusConflict = 409 // RFC 9110, 15.5.10
  477. StatusGone = 410 // RFC 9110, 15.5.11
  478. StatusLengthRequired = 411 // RFC 9110, 15.5.12
  479. StatusPreconditionFailed = 412 // RFC 9110, 15.5.13
  480. StatusRequestEntityTooLarge = 413 // RFC 9110, 15.5.14
  481. StatusRequestURITooLong = 414 // RFC 9110, 15.5.15
  482. StatusUnsupportedMediaType = 415 // RFC 9110, 15.5.16
  483. StatusRequestedRangeNotSatisfiable = 416 // RFC 9110, 15.5.17
  484. StatusExpectationFailed = 417 // RFC 9110, 15.5.18
  485. StatusTeapot = 418 // RFC 9110, 15.5.19 (Unused)
  486. StatusMisdirectedRequest = 421 // RFC 9110, 15.5.20
  487. StatusUnprocessableEntity = 422 // RFC 9110, 15.5.21
  488. StatusLocked = 423 // RFC 4918, 11.3
  489. StatusFailedDependency = 424 // RFC 4918, 11.4
  490. StatusTooEarly = 425 // RFC 8470, 5.2.
  491. StatusUpgradeRequired = 426 // RFC 9110, 15.5.22
  492. StatusPreconditionRequired = 428 // RFC 6585, 3
  493. StatusTooManyRequests = 429 // RFC 6585, 4
  494. StatusRequestHeaderFieldsTooLarge = 431 // RFC 6585, 5
  495. StatusUnavailableForLegalReasons = 451 // RFC 7725, 3
  496. StatusInternalServerError = 500 // RFC 9110, 15.6.1
  497. StatusNotImplemented = 501 // RFC 9110, 15.6.2
  498. StatusBadGateway = 502 // RFC 9110, 15.6.3
  499. StatusServiceUnavailable = 503 // RFC 9110, 15.6.4
  500. StatusGatewayTimeout = 504 // RFC 9110, 15.6.5
  501. StatusHTTPVersionNotSupported = 505 // RFC 9110, 15.6.6
  502. StatusVariantAlsoNegotiates = 506 // RFC 2295, 8.1
  503. StatusInsufficientStorage = 507 // RFC 4918, 11.5
  504. StatusLoopDetected = 508 // RFC 5842, 7.2
  505. StatusNotExtended = 510 // RFC 2774, 7
  506. StatusNetworkAuthenticationRequired = 511 // RFC 6585, 6
  507. )
  508. // Errors
  509. var (
  510. ErrBadRequest = NewError(StatusBadRequest) // 400
  511. ErrUnauthorized = NewError(StatusUnauthorized) // 401
  512. ErrPaymentRequired = NewError(StatusPaymentRequired) // 402
  513. ErrForbidden = NewError(StatusForbidden) // 403
  514. ErrNotFound = NewError(StatusNotFound) // 404
  515. ErrMethodNotAllowed = NewError(StatusMethodNotAllowed) // 405
  516. ErrNotAcceptable = NewError(StatusNotAcceptable) // 406
  517. ErrProxyAuthRequired = NewError(StatusProxyAuthRequired) // 407
  518. ErrRequestTimeout = NewError(StatusRequestTimeout) // 408
  519. ErrConflict = NewError(StatusConflict) // 409
  520. ErrGone = NewError(StatusGone) // 410
  521. ErrLengthRequired = NewError(StatusLengthRequired) // 411
  522. ErrPreconditionFailed = NewError(StatusPreconditionFailed) // 412
  523. ErrRequestEntityTooLarge = NewError(StatusRequestEntityTooLarge) // 413
  524. ErrRequestURITooLong = NewError(StatusRequestURITooLong) // 414
  525. ErrUnsupportedMediaType = NewError(StatusUnsupportedMediaType) // 415
  526. ErrRequestedRangeNotSatisfiable = NewError(StatusRequestedRangeNotSatisfiable) // 416
  527. ErrExpectationFailed = NewError(StatusExpectationFailed) // 417
  528. ErrTeapot = NewError(StatusTeapot) // 418
  529. ErrMisdirectedRequest = NewError(StatusMisdirectedRequest) // 421
  530. ErrUnprocessableEntity = NewError(StatusUnprocessableEntity) // 422
  531. ErrLocked = NewError(StatusLocked) // 423
  532. ErrFailedDependency = NewError(StatusFailedDependency) // 424
  533. ErrTooEarly = NewError(StatusTooEarly) // 425
  534. ErrUpgradeRequired = NewError(StatusUpgradeRequired) // 426
  535. ErrPreconditionRequired = NewError(StatusPreconditionRequired) // 428
  536. ErrTooManyRequests = NewError(StatusTooManyRequests) // 429
  537. ErrRequestHeaderFieldsTooLarge = NewError(StatusRequestHeaderFieldsTooLarge) // 431
  538. ErrUnavailableForLegalReasons = NewError(StatusUnavailableForLegalReasons) // 451
  539. ErrInternalServerError = NewError(StatusInternalServerError) // 500
  540. ErrNotImplemented = NewError(StatusNotImplemented) // 501
  541. ErrBadGateway = NewError(StatusBadGateway) // 502
  542. ErrServiceUnavailable = NewError(StatusServiceUnavailable) // 503
  543. ErrGatewayTimeout = NewError(StatusGatewayTimeout) // 504
  544. ErrHTTPVersionNotSupported = NewError(StatusHTTPVersionNotSupported) // 505
  545. ErrVariantAlsoNegotiates = NewError(StatusVariantAlsoNegotiates) // 506
  546. ErrInsufficientStorage = NewError(StatusInsufficientStorage) // 507
  547. ErrLoopDetected = NewError(StatusLoopDetected) // 508
  548. ErrNotExtended = NewError(StatusNotExtended) // 510
  549. ErrNetworkAuthenticationRequired = NewError(StatusNetworkAuthenticationRequired) // 511
  550. )
  551. // HTTP Headers were copied from net/http.
  552. const (
  553. HeaderAuthorization = "Authorization"
  554. HeaderProxyAuthenticate = "Proxy-Authenticate"
  555. HeaderProxyAuthorization = "Proxy-Authorization"
  556. HeaderWWWAuthenticate = "WWW-Authenticate"
  557. HeaderAge = "Age"
  558. HeaderCacheControl = "Cache-Control"
  559. HeaderClearSiteData = "Clear-Site-Data"
  560. HeaderExpires = "Expires"
  561. HeaderPragma = "Pragma"
  562. HeaderWarning = "Warning"
  563. HeaderAcceptCH = "Accept-CH"
  564. HeaderAcceptCHLifetime = "Accept-CH-Lifetime"
  565. HeaderContentDPR = "Content-DPR"
  566. HeaderDPR = "DPR"
  567. HeaderEarlyData = "Early-Data"
  568. HeaderSaveData = "Save-Data"
  569. HeaderViewportWidth = "Viewport-Width"
  570. HeaderWidth = "Width"
  571. HeaderETag = "ETag"
  572. HeaderIfMatch = "If-Match"
  573. HeaderIfModifiedSince = "If-Modified-Since"
  574. HeaderIfNoneMatch = "If-None-Match"
  575. HeaderIfUnmodifiedSince = "If-Unmodified-Since"
  576. HeaderLastModified = "Last-Modified"
  577. HeaderVary = "Vary"
  578. HeaderConnection = "Connection"
  579. HeaderKeepAlive = "Keep-Alive"
  580. HeaderAccept = "Accept"
  581. HeaderAcceptCharset = "Accept-Charset"
  582. HeaderAcceptEncoding = "Accept-Encoding"
  583. HeaderAcceptLanguage = "Accept-Language"
  584. HeaderCookie = "Cookie"
  585. HeaderExpect = "Expect"
  586. HeaderMaxForwards = "Max-Forwards"
  587. HeaderSetCookie = "Set-Cookie"
  588. HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials"
  589. HeaderAccessControlAllowHeaders = "Access-Control-Allow-Headers"
  590. HeaderAccessControlAllowMethods = "Access-Control-Allow-Methods"
  591. HeaderAccessControlAllowOrigin = "Access-Control-Allow-Origin"
  592. HeaderAccessControlExposeHeaders = "Access-Control-Expose-Headers"
  593. HeaderAccessControlMaxAge = "Access-Control-Max-Age"
  594. HeaderAccessControlRequestHeaders = "Access-Control-Request-Headers"
  595. HeaderAccessControlRequestMethod = "Access-Control-Request-Method"
  596. HeaderOrigin = "Origin"
  597. HeaderTimingAllowOrigin = "Timing-Allow-Origin"
  598. HeaderXPermittedCrossDomainPolicies = "X-Permitted-Cross-Domain-Policies"
  599. HeaderDNT = "DNT"
  600. HeaderTk = "Tk"
  601. HeaderContentDisposition = "Content-Disposition"
  602. HeaderContentEncoding = "Content-Encoding"
  603. HeaderContentLanguage = "Content-Language"
  604. HeaderContentLength = "Content-Length"
  605. HeaderContentLocation = "Content-Location"
  606. HeaderContentType = "Content-Type"
  607. HeaderForwarded = "Forwarded"
  608. HeaderVia = "Via"
  609. HeaderXForwardedFor = "X-Forwarded-For"
  610. HeaderXForwardedHost = "X-Forwarded-Host"
  611. HeaderXForwardedProto = "X-Forwarded-Proto"
  612. HeaderXForwardedProtocol = "X-Forwarded-Protocol"
  613. HeaderXForwardedSsl = "X-Forwarded-Ssl"
  614. HeaderXUrlScheme = "X-Url-Scheme"
  615. HeaderLocation = "Location"
  616. HeaderFrom = "From"
  617. HeaderHost = "Host"
  618. HeaderReferer = "Referer"
  619. HeaderReferrerPolicy = "Referrer-Policy"
  620. HeaderUserAgent = "User-Agent"
  621. HeaderAllow = "Allow"
  622. HeaderServer = "Server"
  623. HeaderAcceptRanges = "Accept-Ranges"
  624. HeaderContentRange = "Content-Range"
  625. HeaderIfRange = "If-Range"
  626. HeaderRange = "Range"
  627. HeaderContentSecurityPolicy = "Content-Security-Policy"
  628. HeaderContentSecurityPolicyReportOnly = "Content-Security-Policy-Report-Only"
  629. HeaderCrossOriginResourcePolicy = "Cross-Origin-Resource-Policy"
  630. HeaderExpectCT = "Expect-CT"
  631. // Deprecated: use HeaderPermissionsPolicy instead
  632. HeaderFeaturePolicy = "Feature-Policy"
  633. HeaderPermissionsPolicy = "Permissions-Policy"
  634. HeaderPublicKeyPins = "Public-Key-Pins"
  635. HeaderPublicKeyPinsReportOnly = "Public-Key-Pins-Report-Only"
  636. HeaderStrictTransportSecurity = "Strict-Transport-Security"
  637. HeaderUpgradeInsecureRequests = "Upgrade-Insecure-Requests"
  638. HeaderXContentTypeOptions = "X-Content-Type-Options"
  639. HeaderXDownloadOptions = "X-Download-Options"
  640. HeaderXFrameOptions = "X-Frame-Options"
  641. HeaderXPoweredBy = "X-Powered-By"
  642. HeaderXXSSProtection = "X-XSS-Protection"
  643. HeaderLastEventID = "Last-Event-ID"
  644. HeaderNEL = "NEL"
  645. HeaderPingFrom = "Ping-From"
  646. HeaderPingTo = "Ping-To"
  647. HeaderReportTo = "Report-To"
  648. HeaderTE = "TE"
  649. HeaderTrailer = "Trailer"
  650. HeaderTransferEncoding = "Transfer-Encoding"
  651. HeaderSecWebSocketAccept = "Sec-WebSocket-Accept"
  652. HeaderSecWebSocketExtensions = "Sec-WebSocket-Extensions"
  653. HeaderSecWebSocketKey = "Sec-WebSocket-Key"
  654. HeaderSecWebSocketProtocol = "Sec-WebSocket-Protocol"
  655. HeaderSecWebSocketVersion = "Sec-WebSocket-Version"
  656. HeaderAcceptPatch = "Accept-Patch"
  657. HeaderAcceptPushPolicy = "Accept-Push-Policy"
  658. HeaderAcceptSignature = "Accept-Signature"
  659. HeaderAltSvc = "Alt-Svc"
  660. HeaderDate = "Date"
  661. HeaderIndex = "Index"
  662. HeaderLargeAllocation = "Large-Allocation"
  663. HeaderLink = "Link"
  664. HeaderPushPolicy = "Push-Policy"
  665. HeaderRetryAfter = "Retry-After"
  666. HeaderServerTiming = "Server-Timing"
  667. HeaderSignature = "Signature"
  668. HeaderSignedHeaders = "Signed-Headers"
  669. HeaderSourceMap = "SourceMap"
  670. HeaderUpgrade = "Upgrade"
  671. HeaderXDNSPrefetchControl = "X-DNS-Prefetch-Control"
  672. HeaderXPingback = "X-Pingback"
  673. HeaderXRequestID = "X-Request-ID"
  674. HeaderXRequestedWith = "X-Requested-With"
  675. HeaderXRobotsTag = "X-Robots-Tag"
  676. HeaderXUACompatible = "X-UA-Compatible"
  677. )
  678. // Network types that are commonly used
  679. const (
  680. NetworkTCP = "tcp"
  681. NetworkTCP4 = "tcp4"
  682. NetworkTCP6 = "tcp6"
  683. )
  684. // Compression types
  685. const (
  686. StrGzip = "gzip"
  687. StrBr = "br"
  688. StrDeflate = "deflate"
  689. StrBrotli = "brotli"
  690. )
  691. // Cookie SameSite
  692. // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7
  693. const (
  694. CookieSameSiteDisabled = "disabled" // not in RFC, just control "SameSite" attribute will not be set.
  695. CookieSameSiteLaxMode = "lax"
  696. CookieSameSiteStrictMode = "strict"
  697. CookieSameSiteNoneMode = "none"
  698. )
  699. // Route Constraints
  700. const (
  701. ConstraintInt = "int"
  702. ConstraintBool = "bool"
  703. ConstraintFloat = "float"
  704. ConstraintAlpha = "alpha"
  705. ConstraintGuid = "guid" //nolint:revive,stylecheck // TODO: Rename to "ConstraintGUID" in v3
  706. ConstraintMinLen = "minLen"
  707. ConstraintMaxLen = "maxLen"
  708. ConstraintLen = "len"
  709. ConstraintBetweenLen = "betweenLen"
  710. ConstraintMinLenLower = "minlen"
  711. ConstraintMaxLenLower = "maxlen"
  712. ConstraintBetweenLenLower = "betweenlen"
  713. ConstraintMin = "min"
  714. ConstraintMax = "max"
  715. ConstraintRange = "range"
  716. ConstraintDatetime = "datetime"
  717. ConstraintRegex = "regex"
  718. )
  719. func IndexRune(str string, needle int32) bool {
  720. for _, b := range str {
  721. if b == needle {
  722. return true
  723. }
  724. }
  725. return false
  726. }