utils.go 26 KB

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