helpers.go 25 KB

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