header.go 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553
  1. package fasthttp
  2. import (
  3. "bufio"
  4. "bytes"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "sync"
  9. "sync/atomic"
  10. "time"
  11. )
  12. const (
  13. rChar = byte('\r')
  14. nChar = byte('\n')
  15. )
  16. // ResponseHeader represents HTTP response header.
  17. //
  18. // It is forbidden copying ResponseHeader instances.
  19. // Create new instances instead and use CopyTo.
  20. //
  21. // ResponseHeader instance MUST NOT be used from concurrently running
  22. // goroutines.
  23. type ResponseHeader struct {
  24. noCopy noCopy
  25. disableNormalizing bool
  26. noHTTP11 bool
  27. connectionClose bool
  28. noDefaultContentType bool
  29. noDefaultDate bool
  30. statusCode int
  31. statusMessage []byte
  32. protocol []byte
  33. contentLength int
  34. contentLengthBytes []byte
  35. secureErrorLogMessage bool
  36. contentType []byte
  37. contentEncoding []byte
  38. server []byte
  39. mulHeader [][]byte
  40. h []argsKV
  41. trailer []argsKV
  42. bufKV argsKV
  43. cookies []argsKV
  44. }
  45. // RequestHeader represents HTTP request header.
  46. //
  47. // It is forbidden copying RequestHeader instances.
  48. // Create new instances instead and use CopyTo.
  49. //
  50. // RequestHeader instance MUST NOT be used from concurrently running
  51. // goroutines.
  52. type RequestHeader struct {
  53. noCopy noCopy
  54. disableNormalizing bool
  55. noHTTP11 bool
  56. connectionClose bool
  57. noDefaultContentType bool
  58. disableSpecialHeader bool
  59. // These two fields have been moved close to other bool fields
  60. // for reducing RequestHeader object size.
  61. cookiesCollected bool
  62. contentLength int
  63. contentLengthBytes []byte
  64. secureErrorLogMessage bool
  65. method []byte
  66. requestURI []byte
  67. proto []byte
  68. host []byte
  69. contentType []byte
  70. userAgent []byte
  71. mulHeader [][]byte
  72. h []argsKV
  73. trailer []argsKV
  74. bufKV argsKV
  75. cookies []argsKV
  76. // stores an immutable copy of headers as they were received from the
  77. // wire.
  78. rawHeaders []byte
  79. }
  80. // SetContentRange sets 'Content-Range: bytes startPos-endPos/contentLength'
  81. // header.
  82. func (h *ResponseHeader) SetContentRange(startPos, endPos, contentLength int) {
  83. b := h.bufKV.value[:0]
  84. b = append(b, strBytes...)
  85. b = append(b, ' ')
  86. b = AppendUint(b, startPos)
  87. b = append(b, '-')
  88. b = AppendUint(b, endPos)
  89. b = append(b, '/')
  90. b = AppendUint(b, contentLength)
  91. h.bufKV.value = b
  92. h.setNonSpecial(strContentRange, h.bufKV.value)
  93. }
  94. // SetByteRange sets 'Range: bytes=startPos-endPos' header.
  95. //
  96. // - If startPos is negative, then 'bytes=-startPos' value is set.
  97. // - If endPos is negative, then 'bytes=startPos-' value is set.
  98. func (h *RequestHeader) SetByteRange(startPos, endPos int) {
  99. b := h.bufKV.value[:0]
  100. b = append(b, strBytes...)
  101. b = append(b, '=')
  102. if startPos >= 0 {
  103. b = AppendUint(b, startPos)
  104. } else {
  105. endPos = -startPos
  106. }
  107. b = append(b, '-')
  108. if endPos >= 0 {
  109. b = AppendUint(b, endPos)
  110. }
  111. h.bufKV.value = b
  112. h.setNonSpecial(strRange, h.bufKV.value)
  113. }
  114. // StatusCode returns response status code.
  115. func (h *ResponseHeader) StatusCode() int {
  116. if h.statusCode == 0 {
  117. return StatusOK
  118. }
  119. return h.statusCode
  120. }
  121. // SetStatusCode sets response status code.
  122. func (h *ResponseHeader) SetStatusCode(statusCode int) {
  123. h.statusCode = statusCode
  124. }
  125. // StatusMessage returns response status message.
  126. func (h *ResponseHeader) StatusMessage() []byte {
  127. return h.statusMessage
  128. }
  129. // SetStatusMessage sets response status message bytes.
  130. func (h *ResponseHeader) SetStatusMessage(statusMessage []byte) {
  131. h.statusMessage = append(h.statusMessage[:0], statusMessage...)
  132. }
  133. // Protocol returns response protocol bytes.
  134. func (h *ResponseHeader) Protocol() []byte {
  135. if len(h.protocol) > 0 {
  136. return h.protocol
  137. }
  138. return strHTTP11
  139. }
  140. // SetProtocol sets response protocol bytes.
  141. func (h *ResponseHeader) SetProtocol(protocol []byte) {
  142. h.protocol = append(h.protocol[:0], protocol...)
  143. }
  144. // SetLastModified sets 'Last-Modified' header to the given value.
  145. func (h *ResponseHeader) SetLastModified(t time.Time) {
  146. h.bufKV.value = AppendHTTPDate(h.bufKV.value[:0], t)
  147. h.setNonSpecial(strLastModified, h.bufKV.value)
  148. }
  149. // ConnectionClose returns true if 'Connection: close' header is set.
  150. func (h *ResponseHeader) ConnectionClose() bool {
  151. return h.connectionClose
  152. }
  153. // SetConnectionClose sets 'Connection: close' header.
  154. func (h *ResponseHeader) SetConnectionClose() {
  155. h.connectionClose = true
  156. }
  157. // ResetConnectionClose clears 'Connection: close' header if it exists.
  158. func (h *ResponseHeader) ResetConnectionClose() {
  159. if h.connectionClose {
  160. h.connectionClose = false
  161. h.h = delAllArgsBytes(h.h, strConnection)
  162. }
  163. }
  164. // ConnectionClose returns true if 'Connection: close' header is set.
  165. func (h *RequestHeader) ConnectionClose() bool {
  166. return h.connectionClose
  167. }
  168. // SetConnectionClose sets 'Connection: close' header.
  169. func (h *RequestHeader) SetConnectionClose() {
  170. h.connectionClose = true
  171. }
  172. // ResetConnectionClose clears 'Connection: close' header if it exists.
  173. func (h *RequestHeader) ResetConnectionClose() {
  174. if h.connectionClose {
  175. h.connectionClose = false
  176. h.h = delAllArgsBytes(h.h, strConnection)
  177. }
  178. }
  179. // ConnectionUpgrade returns true if 'Connection: Upgrade' header is set.
  180. func (h *ResponseHeader) ConnectionUpgrade() bool {
  181. return hasHeaderValue(h.Peek(HeaderConnection), strUpgrade)
  182. }
  183. // ConnectionUpgrade returns true if 'Connection: Upgrade' header is set.
  184. func (h *RequestHeader) ConnectionUpgrade() bool {
  185. return hasHeaderValue(h.Peek(HeaderConnection), strUpgrade)
  186. }
  187. // PeekCookie is able to returns cookie by a given key from response.
  188. func (h *ResponseHeader) PeekCookie(key string) []byte {
  189. return peekArgStr(h.cookies, key)
  190. }
  191. // ContentLength returns Content-Length header value.
  192. //
  193. // It may be negative:
  194. // -1 means Transfer-Encoding: chunked.
  195. // -2 means Transfer-Encoding: identity.
  196. func (h *ResponseHeader) ContentLength() int {
  197. return h.contentLength
  198. }
  199. // SetContentLength sets Content-Length header value.
  200. //
  201. // Content-Length may be negative:
  202. // -1 means Transfer-Encoding: chunked.
  203. // -2 means Transfer-Encoding: identity.
  204. func (h *ResponseHeader) SetContentLength(contentLength int) {
  205. if h.mustSkipContentLength() {
  206. return
  207. }
  208. h.contentLength = contentLength
  209. if contentLength >= 0 {
  210. h.contentLengthBytes = AppendUint(h.contentLengthBytes[:0], contentLength)
  211. h.h = delAllArgsBytes(h.h, strTransferEncoding)
  212. } else {
  213. h.contentLengthBytes = h.contentLengthBytes[:0]
  214. value := strChunked
  215. if contentLength == -2 {
  216. h.SetConnectionClose()
  217. value = strIdentity
  218. }
  219. h.h = setArgBytes(h.h, strTransferEncoding, value, argsHasValue)
  220. }
  221. }
  222. func (h *ResponseHeader) mustSkipContentLength() bool {
  223. // From http/1.1 specs:
  224. // All 1xx (informational), 204 (no content), and 304 (not modified) responses MUST NOT include a message-body
  225. statusCode := h.StatusCode()
  226. // Fast path.
  227. if statusCode < 100 || statusCode == StatusOK {
  228. return false
  229. }
  230. // Slow path.
  231. return statusCode == StatusNotModified || statusCode == StatusNoContent || statusCode < 200
  232. }
  233. // ContentLength returns Content-Length header value.
  234. //
  235. // It may be negative:
  236. // -1 means Transfer-Encoding: chunked.
  237. func (h *RequestHeader) ContentLength() int {
  238. return h.realContentLength()
  239. }
  240. // realContentLength returns the actual Content-Length set in the request,
  241. // including positive lengths for GET/HEAD requests.
  242. func (h *RequestHeader) realContentLength() int {
  243. return h.contentLength
  244. }
  245. // SetContentLength sets Content-Length header value.
  246. //
  247. // Negative content-length sets 'Transfer-Encoding: chunked' header.
  248. func (h *RequestHeader) SetContentLength(contentLength int) {
  249. h.contentLength = contentLength
  250. if contentLength >= 0 {
  251. h.contentLengthBytes = AppendUint(h.contentLengthBytes[:0], contentLength)
  252. h.h = delAllArgsBytes(h.h, strTransferEncoding)
  253. } else {
  254. h.contentLengthBytes = h.contentLengthBytes[:0]
  255. h.h = setArgBytes(h.h, strTransferEncoding, strChunked, argsHasValue)
  256. }
  257. }
  258. func (h *ResponseHeader) isCompressibleContentType() bool {
  259. contentType := h.ContentType()
  260. return bytes.HasPrefix(contentType, strTextSlash) ||
  261. bytes.HasPrefix(contentType, strApplicationSlash) ||
  262. bytes.HasPrefix(contentType, strImageSVG) ||
  263. bytes.HasPrefix(contentType, strImageIcon) ||
  264. bytes.HasPrefix(contentType, strFontSlash) ||
  265. bytes.HasPrefix(contentType, strMultipartSlash)
  266. }
  267. // ContentType returns Content-Type header value.
  268. func (h *ResponseHeader) ContentType() []byte {
  269. contentType := h.contentType
  270. if !h.noDefaultContentType && len(h.contentType) == 0 {
  271. contentType = defaultContentType
  272. }
  273. return contentType
  274. }
  275. // SetContentType sets Content-Type header value.
  276. func (h *ResponseHeader) SetContentType(contentType string) {
  277. h.contentType = append(h.contentType[:0], contentType...)
  278. }
  279. // SetContentTypeBytes sets Content-Type header value.
  280. func (h *ResponseHeader) SetContentTypeBytes(contentType []byte) {
  281. h.contentType = append(h.contentType[:0], contentType...)
  282. }
  283. // ContentEncoding returns Content-Encoding header value.
  284. func (h *ResponseHeader) ContentEncoding() []byte {
  285. return h.contentEncoding
  286. }
  287. // SetContentEncoding sets Content-Encoding header value.
  288. func (h *ResponseHeader) SetContentEncoding(contentEncoding string) {
  289. h.contentEncoding = append(h.contentEncoding[:0], contentEncoding...)
  290. }
  291. // SetContentEncodingBytes sets Content-Encoding header value.
  292. func (h *ResponseHeader) SetContentEncodingBytes(contentEncoding []byte) {
  293. h.contentEncoding = append(h.contentEncoding[:0], contentEncoding...)
  294. }
  295. // addVaryBytes add value to the 'Vary' header if it's not included.
  296. func (h *ResponseHeader) addVaryBytes(value []byte) {
  297. v := h.peek(strVary)
  298. if len(v) == 0 {
  299. // 'Vary' is not set
  300. h.SetBytesV(HeaderVary, value)
  301. } else if !bytes.Contains(v, value) {
  302. // 'Vary' is set and not contains target value
  303. h.SetBytesV(HeaderVary, append(append(v, ','), value...))
  304. } // else: 'Vary' is set and contains target value
  305. }
  306. // Server returns Server header value.
  307. func (h *ResponseHeader) Server() []byte {
  308. return h.server
  309. }
  310. // SetServer sets Server header value.
  311. func (h *ResponseHeader) SetServer(server string) {
  312. h.server = append(h.server[:0], server...)
  313. }
  314. // SetServerBytes sets Server header value.
  315. func (h *ResponseHeader) SetServerBytes(server []byte) {
  316. h.server = append(h.server[:0], server...)
  317. }
  318. // ContentType returns Content-Type header value.
  319. func (h *RequestHeader) ContentType() []byte {
  320. if h.disableSpecialHeader {
  321. return peekArgBytes(h.h, []byte(HeaderContentType))
  322. }
  323. return h.contentType
  324. }
  325. // SetContentType sets Content-Type header value.
  326. func (h *RequestHeader) SetContentType(contentType string) {
  327. h.contentType = append(h.contentType[:0], contentType...)
  328. }
  329. // SetContentTypeBytes sets Content-Type header value.
  330. func (h *RequestHeader) SetContentTypeBytes(contentType []byte) {
  331. h.contentType = append(h.contentType[:0], contentType...)
  332. }
  333. // ContentEncoding returns Content-Encoding header value.
  334. func (h *RequestHeader) ContentEncoding() []byte {
  335. return peekArgBytes(h.h, strContentEncoding)
  336. }
  337. // SetContentEncoding sets Content-Encoding header value.
  338. func (h *RequestHeader) SetContentEncoding(contentEncoding string) {
  339. h.SetBytesK(strContentEncoding, contentEncoding)
  340. }
  341. // SetContentEncodingBytes sets Content-Encoding header value.
  342. func (h *RequestHeader) SetContentEncodingBytes(contentEncoding []byte) {
  343. h.setNonSpecial(strContentEncoding, contentEncoding)
  344. }
  345. // SetMultipartFormBoundary sets the following Content-Type:
  346. // 'multipart/form-data; boundary=...'
  347. // where ... is substituted by the given boundary.
  348. func (h *RequestHeader) SetMultipartFormBoundary(boundary string) {
  349. b := h.bufKV.value[:0]
  350. b = append(b, strMultipartFormData...)
  351. b = append(b, ';', ' ')
  352. b = append(b, strBoundary...)
  353. b = append(b, '=')
  354. b = append(b, boundary...)
  355. h.bufKV.value = b
  356. h.SetContentTypeBytes(h.bufKV.value)
  357. }
  358. // SetMultipartFormBoundaryBytes sets the following Content-Type:
  359. // 'multipart/form-data; boundary=...'
  360. // where ... is substituted by the given boundary.
  361. func (h *RequestHeader) SetMultipartFormBoundaryBytes(boundary []byte) {
  362. b := h.bufKV.value[:0]
  363. b = append(b, strMultipartFormData...)
  364. b = append(b, ';', ' ')
  365. b = append(b, strBoundary...)
  366. b = append(b, '=')
  367. b = append(b, boundary...)
  368. h.bufKV.value = b
  369. h.SetContentTypeBytes(h.bufKV.value)
  370. }
  371. // SetTrailer sets header Trailer value for chunked response
  372. // to indicate which headers will be sent after the body.
  373. //
  374. // Use Set to set the trailer header later.
  375. //
  376. // Trailers are only supported with chunked transfer.
  377. // Trailers allow the sender to include additional headers at the end of chunked messages.
  378. //
  379. // The following trailers are forbidden:
  380. // 1. necessary for message framing (e.g., Transfer-Encoding and Content-Length),
  381. // 2. routing (e.g., Host),
  382. // 3. request modifiers (e.g., controls and conditionals in Section 5 of [RFC7231]),
  383. // 4. authentication (e.g., see [RFC7235] and [RFC6265]),
  384. // 5. response control data (e.g., see Section 7.1 of [RFC7231]),
  385. // 6. determining how to process the payload (e.g., Content-Encoding, Content-Type, Content-Range, and Trailer)
  386. //
  387. // Return ErrBadTrailer if contain any forbidden trailers.
  388. func (h *ResponseHeader) SetTrailer(trailer string) error {
  389. return h.SetTrailerBytes(s2b(trailer))
  390. }
  391. // SetTrailerBytes sets Trailer header value for chunked response
  392. // to indicate which headers will be sent after the body.
  393. //
  394. // Use Set to set the trailer header later.
  395. //
  396. // Trailers are only supported with chunked transfer.
  397. // Trailers allow the sender to include additional headers at the end of chunked messages.
  398. //
  399. // The following trailers are forbidden:
  400. // 1. necessary for message framing (e.g., Transfer-Encoding and Content-Length),
  401. // 2. routing (e.g., Host),
  402. // 3. request modifiers (e.g., controls and conditionals in Section 5 of [RFC7231]),
  403. // 4. authentication (e.g., see [RFC7235] and [RFC6265]),
  404. // 5. response control data (e.g., see Section 7.1 of [RFC7231]),
  405. // 6. determining how to process the payload (e.g., Content-Encoding, Content-Type, Content-Range, and Trailer)
  406. //
  407. // Return ErrBadTrailer if contain any forbidden trailers.
  408. func (h *ResponseHeader) SetTrailerBytes(trailer []byte) error {
  409. h.trailer = h.trailer[:0]
  410. return h.AddTrailerBytes(trailer)
  411. }
  412. // AddTrailer add Trailer header value for chunked response
  413. // to indicate which headers will be sent after the body.
  414. //
  415. // Use Set to set the trailer header later.
  416. //
  417. // Trailers are only supported with chunked transfer.
  418. // Trailers allow the sender to include additional headers at the end of chunked messages.
  419. //
  420. // The following trailers are forbidden:
  421. // 1. necessary for message framing (e.g., Transfer-Encoding and Content-Length),
  422. // 2. routing (e.g., Host),
  423. // 3. request modifiers (e.g., controls and conditionals in Section 5 of [RFC7231]),
  424. // 4. authentication (e.g., see [RFC7235] and [RFC6265]),
  425. // 5. response control data (e.g., see Section 7.1 of [RFC7231]),
  426. // 6. determining how to process the payload (e.g., Content-Encoding, Content-Type, Content-Range, and Trailer)
  427. //
  428. // Return ErrBadTrailer if contain any forbidden trailers.
  429. func (h *ResponseHeader) AddTrailer(trailer string) error {
  430. return h.AddTrailerBytes(s2b(trailer))
  431. }
  432. var ErrBadTrailer = errors.New("contain forbidden trailer")
  433. // AddTrailerBytes add Trailer header value for chunked response
  434. // to indicate which headers will be sent after the body.
  435. //
  436. // Use Set to set the trailer header later.
  437. //
  438. // Trailers are only supported with chunked transfer.
  439. // Trailers allow the sender to include additional headers at the end of chunked messages.
  440. //
  441. // The following trailers are forbidden:
  442. // 1. necessary for message framing (e.g., Transfer-Encoding and Content-Length),
  443. // 2. routing (e.g., Host),
  444. // 3. request modifiers (e.g., controls and conditionals in Section 5 of [RFC7231]),
  445. // 4. authentication (e.g., see [RFC7235] and [RFC6265]),
  446. // 5. response control data (e.g., see Section 7.1 of [RFC7231]),
  447. // 6. determining how to process the payload (e.g., Content-Encoding, Content-Type, Content-Range, and Trailer)
  448. //
  449. // Return ErrBadTrailer if contain any forbidden trailers.
  450. func (h *ResponseHeader) AddTrailerBytes(trailer []byte) error {
  451. var err error
  452. for i := -1; i+1 < len(trailer); {
  453. trailer = trailer[i+1:]
  454. i = bytes.IndexByte(trailer, ',')
  455. if i < 0 {
  456. i = len(trailer)
  457. }
  458. key := trailer[:i]
  459. for len(key) > 0 && key[0] == ' ' {
  460. key = key[1:]
  461. }
  462. for len(key) > 0 && key[len(key)-1] == ' ' {
  463. key = key[:len(key)-1]
  464. }
  465. // Forbidden by RFC 7230, section 4.1.2
  466. if isBadTrailer(key) {
  467. err = ErrBadTrailer
  468. continue
  469. }
  470. h.bufKV.key = append(h.bufKV.key[:0], key...)
  471. normalizeHeaderKey(h.bufKV.key, h.disableNormalizing)
  472. h.trailer = appendArgBytes(h.trailer, h.bufKV.key, nil, argsNoValue)
  473. }
  474. return err
  475. }
  476. // validHeaderFieldByte returns true if c is a valid tchar as defined
  477. // by section 5.6.2 of [RFC9110].
  478. func validHeaderFieldByte(c byte) bool {
  479. return c < 128 && tcharTable[c] == 1
  480. }
  481. // VisitHeaderParams calls f for each parameter in the given header bytes.
  482. // It stops processing when f returns false or an invalid parameter is found.
  483. // Parameter values may be quoted, in which case \ is treated as an escape
  484. // character, and the value is unquoted before being passed to value.
  485. // See: https://www.rfc-editor.org/rfc/rfc9110#section-5.6.6
  486. //
  487. // f must not retain references to key and/or value after returning.
  488. // Copy key and/or value contents before returning if you need retaining them.
  489. func VisitHeaderParams(b []byte, f func(key, value []byte) bool) {
  490. for len(b) > 0 {
  491. idxSemi := 0
  492. for idxSemi < len(b) && b[idxSemi] != ';' {
  493. idxSemi++
  494. }
  495. if idxSemi >= len(b) {
  496. return
  497. }
  498. b = b[idxSemi+1:]
  499. for len(b) > 0 && b[0] == ' ' {
  500. b = b[1:]
  501. }
  502. n := 0
  503. if len(b) == 0 || !validHeaderFieldByte(b[n]) {
  504. return
  505. }
  506. n++
  507. for n < len(b) && validHeaderFieldByte(b[n]) {
  508. n++
  509. }
  510. if n >= len(b)-1 || b[n] != '=' {
  511. return
  512. }
  513. param := b[:n]
  514. n++
  515. switch {
  516. case validHeaderFieldByte(b[n]):
  517. m := n
  518. n++
  519. for n < len(b) && validHeaderFieldByte(b[n]) {
  520. n++
  521. }
  522. if !f(param, b[m:n]) {
  523. return
  524. }
  525. case b[n] == '"':
  526. foundEndQuote := false
  527. escaping := false
  528. n++
  529. m := n
  530. for ; n < len(b); n++ {
  531. if b[n] == '"' && !escaping {
  532. foundEndQuote = true
  533. break
  534. }
  535. escaping = (b[n] == '\\' && !escaping)
  536. }
  537. if !foundEndQuote {
  538. return
  539. }
  540. if !f(param, b[m:n]) {
  541. return
  542. }
  543. n++
  544. default:
  545. return
  546. }
  547. b = b[n:]
  548. }
  549. }
  550. // MultipartFormBoundary returns boundary part
  551. // from 'multipart/form-data; boundary=...' Content-Type.
  552. func (h *RequestHeader) MultipartFormBoundary() []byte {
  553. b := h.ContentType()
  554. if !bytes.HasPrefix(b, strMultipartFormData) {
  555. return nil
  556. }
  557. b = b[len(strMultipartFormData):]
  558. if len(b) == 0 || b[0] != ';' {
  559. return nil
  560. }
  561. var n int
  562. for len(b) > 0 {
  563. n++
  564. for len(b) > n && b[n] == ' ' {
  565. n++
  566. }
  567. b = b[n:]
  568. if !bytes.HasPrefix(b, strBoundary) {
  569. if n = bytes.IndexByte(b, ';'); n < 0 {
  570. return nil
  571. }
  572. continue
  573. }
  574. b = b[len(strBoundary):]
  575. if len(b) == 0 || b[0] != '=' {
  576. return nil
  577. }
  578. b = b[1:]
  579. if n = bytes.IndexByte(b, ';'); n >= 0 {
  580. b = b[:n]
  581. }
  582. if len(b) > 1 && b[0] == '"' && b[len(b)-1] == '"' {
  583. b = b[1 : len(b)-1]
  584. }
  585. return b
  586. }
  587. return nil
  588. }
  589. // Host returns Host header value.
  590. func (h *RequestHeader) Host() []byte {
  591. if h.disableSpecialHeader {
  592. return peekArgBytes(h.h, []byte(HeaderHost))
  593. }
  594. return h.host
  595. }
  596. // SetHost sets Host header value.
  597. func (h *RequestHeader) SetHost(host string) {
  598. h.host = append(h.host[:0], host...)
  599. }
  600. // SetHostBytes sets Host header value.
  601. func (h *RequestHeader) SetHostBytes(host []byte) {
  602. h.host = append(h.host[:0], host...)
  603. }
  604. // UserAgent returns User-Agent header value.
  605. func (h *RequestHeader) UserAgent() []byte {
  606. if h.disableSpecialHeader {
  607. return peekArgBytes(h.h, []byte(HeaderUserAgent))
  608. }
  609. return h.userAgent
  610. }
  611. // SetUserAgent sets User-Agent header value.
  612. func (h *RequestHeader) SetUserAgent(userAgent string) {
  613. h.userAgent = append(h.userAgent[:0], userAgent...)
  614. }
  615. // SetUserAgentBytes sets User-Agent header value.
  616. func (h *RequestHeader) SetUserAgentBytes(userAgent []byte) {
  617. h.userAgent = append(h.userAgent[:0], userAgent...)
  618. }
  619. // Referer returns Referer header value.
  620. func (h *RequestHeader) Referer() []byte {
  621. return peekArgBytes(h.h, strReferer)
  622. }
  623. // SetReferer sets Referer header value.
  624. func (h *RequestHeader) SetReferer(referer string) {
  625. h.SetBytesK(strReferer, referer)
  626. }
  627. // SetRefererBytes sets Referer header value.
  628. func (h *RequestHeader) SetRefererBytes(referer []byte) {
  629. h.setNonSpecial(strReferer, referer)
  630. }
  631. // Method returns HTTP request method.
  632. func (h *RequestHeader) Method() []byte {
  633. if len(h.method) == 0 {
  634. return []byte(MethodGet)
  635. }
  636. return h.method
  637. }
  638. // SetMethod sets HTTP request method.
  639. func (h *RequestHeader) SetMethod(method string) {
  640. h.method = append(h.method[:0], method...)
  641. }
  642. // SetMethodBytes sets HTTP request method.
  643. func (h *RequestHeader) SetMethodBytes(method []byte) {
  644. h.method = append(h.method[:0], method...)
  645. }
  646. // Protocol returns HTTP protocol.
  647. func (h *RequestHeader) Protocol() []byte {
  648. if len(h.proto) == 0 {
  649. return strHTTP11
  650. }
  651. return h.proto
  652. }
  653. // SetProtocol sets HTTP request protocol.
  654. func (h *RequestHeader) SetProtocol(method string) {
  655. h.proto = append(h.proto[:0], method...)
  656. h.noHTTP11 = !bytes.Equal(h.proto, strHTTP11)
  657. }
  658. // SetProtocolBytes sets HTTP request protocol.
  659. func (h *RequestHeader) SetProtocolBytes(method []byte) {
  660. h.proto = append(h.proto[:0], method...)
  661. h.noHTTP11 = !bytes.Equal(h.proto, strHTTP11)
  662. }
  663. // RequestURI returns RequestURI from the first HTTP request line.
  664. func (h *RequestHeader) RequestURI() []byte {
  665. requestURI := h.requestURI
  666. if len(requestURI) == 0 {
  667. requestURI = strSlash
  668. }
  669. return requestURI
  670. }
  671. // SetRequestURI sets RequestURI for the first HTTP request line.
  672. // RequestURI must be properly encoded.
  673. // Use URI.RequestURI for constructing proper RequestURI if unsure.
  674. func (h *RequestHeader) SetRequestURI(requestURI string) {
  675. h.requestURI = append(h.requestURI[:0], requestURI...)
  676. }
  677. // SetRequestURIBytes sets RequestURI for the first HTTP request line.
  678. // RequestURI must be properly encoded.
  679. // Use URI.RequestURI for constructing proper RequestURI if unsure.
  680. func (h *RequestHeader) SetRequestURIBytes(requestURI []byte) {
  681. h.requestURI = append(h.requestURI[:0], requestURI...)
  682. }
  683. // SetTrailer sets Trailer header value for chunked request
  684. // to indicate which headers will be sent after the body.
  685. //
  686. // Use Set to set the trailer header later.
  687. //
  688. // Trailers are only supported with chunked transfer.
  689. // Trailers allow the sender to include additional headers at the end of chunked messages.
  690. //
  691. // The following trailers are forbidden:
  692. // 1. necessary for message framing (e.g., Transfer-Encoding and Content-Length),
  693. // 2. routing (e.g., Host),
  694. // 3. request modifiers (e.g., controls and conditionals in Section 5 of [RFC7231]),
  695. // 4. authentication (e.g., see [RFC7235] and [RFC6265]),
  696. // 5. response control data (e.g., see Section 7.1 of [RFC7231]),
  697. // 6. determining how to process the payload (e.g., Content-Encoding, Content-Type, Content-Range, and Trailer)
  698. //
  699. // Return ErrBadTrailer if contain any forbidden trailers.
  700. func (h *RequestHeader) SetTrailer(trailer string) error {
  701. return h.SetTrailerBytes(s2b(trailer))
  702. }
  703. // SetTrailerBytes sets Trailer header value for chunked request
  704. // to indicate which headers will be sent after the body.
  705. //
  706. // Use Set to set the trailer header later.
  707. //
  708. // Trailers are only supported with chunked transfer.
  709. // Trailers allow the sender to include additional headers at the end of chunked messages.
  710. //
  711. // The following trailers are forbidden:
  712. // 1. necessary for message framing (e.g., Transfer-Encoding and Content-Length),
  713. // 2. routing (e.g., Host),
  714. // 3. request modifiers (e.g., controls and conditionals in Section 5 of [RFC7231]),
  715. // 4. authentication (e.g., see [RFC7235] and [RFC6265]),
  716. // 5. response control data (e.g., see Section 7.1 of [RFC7231]),
  717. // 6. determining how to process the payload (e.g., Content-Encoding, Content-Type, Content-Range, and Trailer)
  718. //
  719. // Return ErrBadTrailer if contain any forbidden trailers.
  720. func (h *RequestHeader) SetTrailerBytes(trailer []byte) error {
  721. h.trailer = h.trailer[:0]
  722. return h.AddTrailerBytes(trailer)
  723. }
  724. // AddTrailer add Trailer header value for chunked request
  725. // to indicate which headers will be sent after the body.
  726. //
  727. // Use Set to set the trailer header later.
  728. //
  729. // Trailers are only supported with chunked transfer.
  730. // Trailers allow the sender to include additional headers at the end of chunked messages.
  731. //
  732. // The following trailers are forbidden:
  733. // 1. necessary for message framing (e.g., Transfer-Encoding and Content-Length),
  734. // 2. routing (e.g., Host),
  735. // 3. request modifiers (e.g., controls and conditionals in Section 5 of [RFC7231]),
  736. // 4. authentication (e.g., see [RFC7235] and [RFC6265]),
  737. // 5. response control data (e.g., see Section 7.1 of [RFC7231]),
  738. // 6. determining how to process the payload (e.g., Content-Encoding, Content-Type, Content-Range, and Trailer)
  739. //
  740. // Return ErrBadTrailer if contain any forbidden trailers.
  741. func (h *RequestHeader) AddTrailer(trailer string) error {
  742. return h.AddTrailerBytes(s2b(trailer))
  743. }
  744. // AddTrailerBytes add Trailer header value for chunked request
  745. // to indicate which headers will be sent after the body.
  746. //
  747. // Use Set to set the trailer header later.
  748. //
  749. // Trailers are only supported with chunked transfer.
  750. // Trailers allow the sender to include additional headers at the end of chunked messages.
  751. //
  752. // The following trailers are forbidden:
  753. // 1. necessary for message framing (e.g., Transfer-Encoding and Content-Length),
  754. // 2. routing (e.g., Host),
  755. // 3. request modifiers (e.g., controls and conditionals in Section 5 of [RFC7231]),
  756. // 4. authentication (e.g., see [RFC7235] and [RFC6265]),
  757. // 5. response control data (e.g., see Section 7.1 of [RFC7231]),
  758. // 6. determining how to process the payload (e.g., Content-Encoding, Content-Type, Content-Range, and Trailer)
  759. //
  760. // Return ErrBadTrailer if contain any forbidden trailers.
  761. func (h *RequestHeader) AddTrailerBytes(trailer []byte) error {
  762. var err error
  763. for i := -1; i+1 < len(trailer); {
  764. trailer = trailer[i+1:]
  765. i = bytes.IndexByte(trailer, ',')
  766. if i < 0 {
  767. i = len(trailer)
  768. }
  769. key := trailer[:i]
  770. for len(key) > 0 && key[0] == ' ' {
  771. key = key[1:]
  772. }
  773. for len(key) > 0 && key[len(key)-1] == ' ' {
  774. key = key[:len(key)-1]
  775. }
  776. // Forbidden by RFC 7230, section 4.1.2
  777. if isBadTrailer(key) {
  778. err = ErrBadTrailer
  779. continue
  780. }
  781. h.bufKV.key = append(h.bufKV.key[:0], key...)
  782. normalizeHeaderKey(h.bufKV.key, h.disableNormalizing)
  783. h.trailer = appendArgBytes(h.trailer, h.bufKV.key, nil, argsNoValue)
  784. }
  785. return err
  786. }
  787. // IsGet returns true if request method is GET.
  788. func (h *RequestHeader) IsGet() bool {
  789. return string(h.Method()) == MethodGet
  790. }
  791. // IsPost returns true if request method is POST.
  792. func (h *RequestHeader) IsPost() bool {
  793. return string(h.Method()) == MethodPost
  794. }
  795. // IsPut returns true if request method is PUT.
  796. func (h *RequestHeader) IsPut() bool {
  797. return string(h.Method()) == MethodPut
  798. }
  799. // IsHead returns true if request method is HEAD.
  800. func (h *RequestHeader) IsHead() bool {
  801. return string(h.Method()) == MethodHead
  802. }
  803. // IsDelete returns true if request method is DELETE.
  804. func (h *RequestHeader) IsDelete() bool {
  805. return string(h.Method()) == MethodDelete
  806. }
  807. // IsConnect returns true if request method is CONNECT.
  808. func (h *RequestHeader) IsConnect() bool {
  809. return string(h.Method()) == MethodConnect
  810. }
  811. // IsOptions returns true if request method is OPTIONS.
  812. func (h *RequestHeader) IsOptions() bool {
  813. return string(h.Method()) == MethodOptions
  814. }
  815. // IsTrace returns true if request method is TRACE.
  816. func (h *RequestHeader) IsTrace() bool {
  817. return string(h.Method()) == MethodTrace
  818. }
  819. // IsPatch returns true if request method is PATCH.
  820. func (h *RequestHeader) IsPatch() bool {
  821. return string(h.Method()) == MethodPatch
  822. }
  823. // IsHTTP11 returns true if the request is HTTP/1.1.
  824. func (h *RequestHeader) IsHTTP11() bool {
  825. return !h.noHTTP11
  826. }
  827. // IsHTTP11 returns true if the response is HTTP/1.1.
  828. func (h *ResponseHeader) IsHTTP11() bool {
  829. return !h.noHTTP11
  830. }
  831. // HasAcceptEncoding returns true if the header contains
  832. // the given Accept-Encoding value.
  833. func (h *RequestHeader) HasAcceptEncoding(acceptEncoding string) bool {
  834. h.bufKV.value = append(h.bufKV.value[:0], acceptEncoding...)
  835. return h.HasAcceptEncodingBytes(h.bufKV.value)
  836. }
  837. // HasAcceptEncodingBytes returns true if the header contains
  838. // the given Accept-Encoding value.
  839. func (h *RequestHeader) HasAcceptEncodingBytes(acceptEncoding []byte) bool {
  840. ae := h.peek(strAcceptEncoding)
  841. n := bytes.Index(ae, acceptEncoding)
  842. if n < 0 {
  843. return false
  844. }
  845. b := ae[n+len(acceptEncoding):]
  846. if len(b) > 0 && b[0] != ',' {
  847. return false
  848. }
  849. if n == 0 {
  850. return true
  851. }
  852. return ae[n-1] == ' '
  853. }
  854. // Len returns the number of headers set,
  855. // i.e. the number of times f is called in VisitAll.
  856. func (h *ResponseHeader) Len() int {
  857. n := 0
  858. h.VisitAll(func(_, _ []byte) { n++ })
  859. return n
  860. }
  861. // Len returns the number of headers set,
  862. // i.e. the number of times f is called in VisitAll.
  863. func (h *RequestHeader) Len() int {
  864. n := 0
  865. h.VisitAll(func(_, _ []byte) { n++ })
  866. return n
  867. }
  868. // DisableSpecialHeader disables special header processing.
  869. // fasthttp will not set any special headers for you, such as Host, Content-Type, User-Agent, etc.
  870. // You must set everything yourself.
  871. // If RequestHeader.Read() is called, special headers will be ignored.
  872. // This can be used to control case and order of special headers.
  873. // This is generally not recommended.
  874. func (h *RequestHeader) DisableSpecialHeader() {
  875. h.disableSpecialHeader = true
  876. }
  877. // EnableSpecialHeader enables special header processing.
  878. // fasthttp will send Host, Content-Type, User-Agent, etc headers for you.
  879. // This is suggested and enabled by default.
  880. func (h *RequestHeader) EnableSpecialHeader() {
  881. h.disableSpecialHeader = false
  882. }
  883. // DisableNormalizing disables header names' normalization.
  884. //
  885. // By default all the header names are normalized by uppercasing
  886. // the first letter and all the first letters following dashes,
  887. // while lowercasing all the other letters.
  888. // Examples:
  889. //
  890. // - CONNECTION -> Connection
  891. // - conteNT-tYPE -> Content-Type
  892. // - foo-bar-baz -> Foo-Bar-Baz
  893. //
  894. // Disable header names' normalization only if know what are you doing.
  895. func (h *RequestHeader) DisableNormalizing() {
  896. h.disableNormalizing = true
  897. }
  898. // EnableNormalizing enables header names' normalization.
  899. //
  900. // Header names are normalized by uppercasing the first letter and
  901. // all the first letters following dashes, while lowercasing all
  902. // the other letters.
  903. // Examples:
  904. //
  905. // - CONNECTION -> Connection
  906. // - conteNT-tYPE -> Content-Type
  907. // - foo-bar-baz -> Foo-Bar-Baz
  908. //
  909. // This is enabled by default unless disabled using DisableNormalizing().
  910. func (h *RequestHeader) EnableNormalizing() {
  911. h.disableNormalizing = false
  912. }
  913. // DisableNormalizing disables header names' normalization.
  914. //
  915. // By default all the header names are normalized by uppercasing
  916. // the first letter and all the first letters following dashes,
  917. // while lowercasing all the other letters.
  918. // Examples:
  919. //
  920. // - CONNECTION -> Connection
  921. // - conteNT-tYPE -> Content-Type
  922. // - foo-bar-baz -> Foo-Bar-Baz
  923. //
  924. // Disable header names' normalization only if know what are you doing.
  925. func (h *ResponseHeader) DisableNormalizing() {
  926. h.disableNormalizing = true
  927. }
  928. // EnableNormalizing enables header names' normalization.
  929. //
  930. // Header names are normalized by uppercasing the first letter and
  931. // all the first letters following dashes, while lowercasing all
  932. // the other letters.
  933. // Examples:
  934. //
  935. // - CONNECTION -> Connection
  936. // - conteNT-tYPE -> Content-Type
  937. // - foo-bar-baz -> Foo-Bar-Baz
  938. //
  939. // This is enabled by default unless disabled using DisableNormalizing().
  940. func (h *ResponseHeader) EnableNormalizing() {
  941. h.disableNormalizing = false
  942. }
  943. // SetNoDefaultContentType allows you to control if a default Content-Type header will be set (false) or not (true).
  944. func (h *ResponseHeader) SetNoDefaultContentType(noDefaultContentType bool) {
  945. h.noDefaultContentType = noDefaultContentType
  946. }
  947. // Reset clears response header.
  948. func (h *ResponseHeader) Reset() {
  949. h.disableNormalizing = false
  950. h.SetNoDefaultContentType(false)
  951. h.noDefaultDate = false
  952. h.resetSkipNormalize()
  953. }
  954. func (h *ResponseHeader) resetSkipNormalize() {
  955. h.noHTTP11 = false
  956. h.connectionClose = false
  957. h.statusCode = 0
  958. h.statusMessage = h.statusMessage[:0]
  959. h.protocol = h.protocol[:0]
  960. h.contentLength = 0
  961. h.contentLengthBytes = h.contentLengthBytes[:0]
  962. h.contentType = h.contentType[:0]
  963. h.contentEncoding = h.contentEncoding[:0]
  964. h.server = h.server[:0]
  965. h.h = h.h[:0]
  966. h.cookies = h.cookies[:0]
  967. h.trailer = h.trailer[:0]
  968. h.mulHeader = h.mulHeader[:0]
  969. }
  970. // SetNoDefaultContentType allows you to control if a default Content-Type header will be set (false) or not (true).
  971. func (h *RequestHeader) SetNoDefaultContentType(noDefaultContentType bool) {
  972. h.noDefaultContentType = noDefaultContentType
  973. }
  974. // Reset clears request header.
  975. func (h *RequestHeader) Reset() {
  976. h.disableSpecialHeader = false
  977. h.disableNormalizing = false
  978. h.SetNoDefaultContentType(false)
  979. h.resetSkipNormalize()
  980. }
  981. func (h *RequestHeader) resetSkipNormalize() {
  982. h.noHTTP11 = false
  983. h.connectionClose = false
  984. h.contentLength = 0
  985. h.contentLengthBytes = h.contentLengthBytes[:0]
  986. h.method = h.method[:0]
  987. h.proto = h.proto[:0]
  988. h.requestURI = h.requestURI[:0]
  989. h.host = h.host[:0]
  990. h.contentType = h.contentType[:0]
  991. h.userAgent = h.userAgent[:0]
  992. h.trailer = h.trailer[:0]
  993. h.mulHeader = h.mulHeader[:0]
  994. h.h = h.h[:0]
  995. h.cookies = h.cookies[:0]
  996. h.cookiesCollected = false
  997. h.rawHeaders = h.rawHeaders[:0]
  998. }
  999. // CopyTo copies all the headers to dst.
  1000. func (h *ResponseHeader) CopyTo(dst *ResponseHeader) {
  1001. dst.Reset()
  1002. dst.disableNormalizing = h.disableNormalizing
  1003. dst.noHTTP11 = h.noHTTP11
  1004. dst.connectionClose = h.connectionClose
  1005. dst.noDefaultContentType = h.noDefaultContentType
  1006. dst.noDefaultDate = h.noDefaultDate
  1007. dst.statusCode = h.statusCode
  1008. dst.statusMessage = append(dst.statusMessage, h.statusMessage...)
  1009. dst.protocol = append(dst.protocol, h.protocol...)
  1010. dst.contentLength = h.contentLength
  1011. dst.contentLengthBytes = append(dst.contentLengthBytes, h.contentLengthBytes...)
  1012. dst.contentType = append(dst.contentType, h.contentType...)
  1013. dst.contentEncoding = append(dst.contentEncoding, h.contentEncoding...)
  1014. dst.server = append(dst.server, h.server...)
  1015. dst.h = copyArgs(dst.h, h.h)
  1016. dst.cookies = copyArgs(dst.cookies, h.cookies)
  1017. dst.trailer = copyArgs(dst.trailer, h.trailer)
  1018. }
  1019. // CopyTo copies all the headers to dst.
  1020. func (h *RequestHeader) CopyTo(dst *RequestHeader) {
  1021. dst.Reset()
  1022. dst.disableNormalizing = h.disableNormalizing
  1023. dst.noHTTP11 = h.noHTTP11
  1024. dst.connectionClose = h.connectionClose
  1025. dst.noDefaultContentType = h.noDefaultContentType
  1026. dst.contentLength = h.contentLength
  1027. dst.contentLengthBytes = append(dst.contentLengthBytes, h.contentLengthBytes...)
  1028. dst.method = append(dst.method, h.method...)
  1029. dst.proto = append(dst.proto, h.proto...)
  1030. dst.requestURI = append(dst.requestURI, h.requestURI...)
  1031. dst.host = append(dst.host, h.host...)
  1032. dst.contentType = append(dst.contentType, h.contentType...)
  1033. dst.userAgent = append(dst.userAgent, h.userAgent...)
  1034. dst.trailer = append(dst.trailer, h.trailer...)
  1035. dst.h = copyArgs(dst.h, h.h)
  1036. dst.cookies = copyArgs(dst.cookies, h.cookies)
  1037. dst.cookiesCollected = h.cookiesCollected
  1038. dst.rawHeaders = append(dst.rawHeaders, h.rawHeaders...)
  1039. }
  1040. // VisitAll calls f for each header.
  1041. //
  1042. // f must not retain references to key and/or value after returning.
  1043. // Copy key and/or value contents before returning if you need retaining them.
  1044. func (h *ResponseHeader) VisitAll(f func(key, value []byte)) {
  1045. if len(h.contentLengthBytes) > 0 {
  1046. f(strContentLength, h.contentLengthBytes)
  1047. }
  1048. contentType := h.ContentType()
  1049. if len(contentType) > 0 {
  1050. f(strContentType, contentType)
  1051. }
  1052. contentEncoding := h.ContentEncoding()
  1053. if len(contentEncoding) > 0 {
  1054. f(strContentEncoding, contentEncoding)
  1055. }
  1056. server := h.Server()
  1057. if len(server) > 0 {
  1058. f(strServer, server)
  1059. }
  1060. if len(h.cookies) > 0 {
  1061. visitArgs(h.cookies, func(_, v []byte) {
  1062. f(strSetCookie, v)
  1063. })
  1064. }
  1065. if len(h.trailer) > 0 {
  1066. f(strTrailer, appendArgsKeyBytes(nil, h.trailer, strCommaSpace))
  1067. }
  1068. visitArgs(h.h, f)
  1069. if h.ConnectionClose() {
  1070. f(strConnection, strClose)
  1071. }
  1072. }
  1073. // VisitAllTrailer calls f for each response Trailer.
  1074. //
  1075. // f must not retain references to value after returning.
  1076. func (h *ResponseHeader) VisitAllTrailer(f func(value []byte)) {
  1077. visitArgsKey(h.trailer, f)
  1078. }
  1079. // VisitAllTrailer calls f for each request Trailer.
  1080. //
  1081. // f must not retain references to value after returning.
  1082. func (h *RequestHeader) VisitAllTrailer(f func(value []byte)) {
  1083. visitArgsKey(h.trailer, f)
  1084. }
  1085. // VisitAllCookie calls f for each response cookie.
  1086. //
  1087. // Cookie name is passed in key and the whole Set-Cookie header value
  1088. // is passed in value on each f invocation. Value may be parsed
  1089. // with Cookie.ParseBytes().
  1090. //
  1091. // f must not retain references to key and/or value after returning.
  1092. func (h *ResponseHeader) VisitAllCookie(f func(key, value []byte)) {
  1093. visitArgs(h.cookies, f)
  1094. }
  1095. // VisitAllCookie calls f for each request cookie.
  1096. //
  1097. // f must not retain references to key and/or value after returning.
  1098. func (h *RequestHeader) VisitAllCookie(f func(key, value []byte)) {
  1099. h.collectCookies()
  1100. visitArgs(h.cookies, f)
  1101. }
  1102. // VisitAll calls f for each header.
  1103. //
  1104. // f must not retain references to key and/or value after returning.
  1105. // Copy key and/or value contents before returning if you need retaining them.
  1106. //
  1107. // To get the headers in order they were received use VisitAllInOrder.
  1108. func (h *RequestHeader) VisitAll(f func(key, value []byte)) {
  1109. host := h.Host()
  1110. if len(host) > 0 {
  1111. f(strHost, host)
  1112. }
  1113. if len(h.contentLengthBytes) > 0 {
  1114. f(strContentLength, h.contentLengthBytes)
  1115. }
  1116. contentType := h.ContentType()
  1117. if len(contentType) > 0 {
  1118. f(strContentType, contentType)
  1119. }
  1120. userAgent := h.UserAgent()
  1121. if len(userAgent) > 0 {
  1122. f(strUserAgent, userAgent)
  1123. }
  1124. if len(h.trailer) > 0 {
  1125. f(strTrailer, appendArgsKeyBytes(nil, h.trailer, strCommaSpace))
  1126. }
  1127. h.collectCookies()
  1128. if len(h.cookies) > 0 {
  1129. h.bufKV.value = appendRequestCookieBytes(h.bufKV.value[:0], h.cookies)
  1130. f(strCookie, h.bufKV.value)
  1131. }
  1132. visitArgs(h.h, f)
  1133. if h.ConnectionClose() {
  1134. f(strConnection, strClose)
  1135. }
  1136. }
  1137. // VisitAllInOrder calls f for each header in the order they were received.
  1138. //
  1139. // f must not retain references to key and/or value after returning.
  1140. // Copy key and/or value contents before returning if you need retaining them.
  1141. //
  1142. // This function is slightly slower than VisitAll because it has to reparse the
  1143. // raw headers to get the order.
  1144. func (h *RequestHeader) VisitAllInOrder(f func(key, value []byte)) {
  1145. var s headerScanner
  1146. s.b = h.rawHeaders
  1147. s.disableNormalizing = h.disableNormalizing
  1148. for s.next() {
  1149. if len(s.key) > 0 {
  1150. f(s.key, s.value)
  1151. }
  1152. }
  1153. }
  1154. // Del deletes header with the given key.
  1155. func (h *ResponseHeader) Del(key string) {
  1156. k := getHeaderKeyBytes(&h.bufKV, key, h.disableNormalizing)
  1157. h.del(k)
  1158. }
  1159. // DelBytes deletes header with the given key.
  1160. func (h *ResponseHeader) DelBytes(key []byte) {
  1161. h.bufKV.key = append(h.bufKV.key[:0], key...)
  1162. normalizeHeaderKey(h.bufKV.key, h.disableNormalizing)
  1163. h.del(h.bufKV.key)
  1164. }
  1165. func (h *ResponseHeader) del(key []byte) {
  1166. switch string(key) {
  1167. case HeaderContentType:
  1168. h.contentType = h.contentType[:0]
  1169. case HeaderContentEncoding:
  1170. h.contentEncoding = h.contentEncoding[:0]
  1171. case HeaderServer:
  1172. h.server = h.server[:0]
  1173. case HeaderSetCookie:
  1174. h.cookies = h.cookies[:0]
  1175. case HeaderContentLength:
  1176. h.contentLength = 0
  1177. h.contentLengthBytes = h.contentLengthBytes[:0]
  1178. case HeaderConnection:
  1179. h.connectionClose = false
  1180. case HeaderTrailer:
  1181. h.trailer = h.trailer[:0]
  1182. }
  1183. h.h = delAllArgsBytes(h.h, key)
  1184. }
  1185. // Del deletes header with the given key.
  1186. func (h *RequestHeader) Del(key string) {
  1187. k := getHeaderKeyBytes(&h.bufKV, key, h.disableNormalizing)
  1188. h.del(k)
  1189. }
  1190. // DelBytes deletes header with the given key.
  1191. func (h *RequestHeader) DelBytes(key []byte) {
  1192. h.bufKV.key = append(h.bufKV.key[:0], key...)
  1193. normalizeHeaderKey(h.bufKV.key, h.disableNormalizing)
  1194. h.del(h.bufKV.key)
  1195. }
  1196. func (h *RequestHeader) del(key []byte) {
  1197. switch string(key) {
  1198. case HeaderHost:
  1199. h.host = h.host[:0]
  1200. case HeaderContentType:
  1201. h.contentType = h.contentType[:0]
  1202. case HeaderUserAgent:
  1203. h.userAgent = h.userAgent[:0]
  1204. case HeaderCookie:
  1205. h.cookies = h.cookies[:0]
  1206. case HeaderContentLength:
  1207. h.contentLength = 0
  1208. h.contentLengthBytes = h.contentLengthBytes[:0]
  1209. case HeaderConnection:
  1210. h.connectionClose = false
  1211. case HeaderTrailer:
  1212. h.trailer = h.trailer[:0]
  1213. }
  1214. h.h = delAllArgsBytes(h.h, key)
  1215. }
  1216. // setSpecialHeader handles special headers and return true when a header is processed.
  1217. func (h *ResponseHeader) setSpecialHeader(key, value []byte) bool {
  1218. if len(key) == 0 {
  1219. return false
  1220. }
  1221. switch key[0] | 0x20 {
  1222. case 'c':
  1223. switch {
  1224. case caseInsensitiveCompare(strContentType, key):
  1225. h.SetContentTypeBytes(value)
  1226. return true
  1227. case caseInsensitiveCompare(strContentLength, key):
  1228. if contentLength, err := parseContentLength(value); err == nil {
  1229. h.contentLength = contentLength
  1230. h.contentLengthBytes = append(h.contentLengthBytes[:0], value...)
  1231. }
  1232. return true
  1233. case caseInsensitiveCompare(strContentEncoding, key):
  1234. h.SetContentEncodingBytes(value)
  1235. return true
  1236. case caseInsensitiveCompare(strConnection, key):
  1237. if bytes.Equal(strClose, value) {
  1238. h.SetConnectionClose()
  1239. } else {
  1240. h.ResetConnectionClose()
  1241. h.setNonSpecial(key, value)
  1242. }
  1243. return true
  1244. }
  1245. case 's':
  1246. if caseInsensitiveCompare(strServer, key) {
  1247. h.SetServerBytes(value)
  1248. return true
  1249. } else if caseInsensitiveCompare(strSetCookie, key) {
  1250. var kv *argsKV
  1251. h.cookies, kv = allocArg(h.cookies)
  1252. kv.key = getCookieKey(kv.key, value)
  1253. kv.value = append(kv.value[:0], value...)
  1254. return true
  1255. }
  1256. case 't':
  1257. if caseInsensitiveCompare(strTransferEncoding, key) {
  1258. // Transfer-Encoding is managed automatically.
  1259. return true
  1260. } else if caseInsensitiveCompare(strTrailer, key) {
  1261. _ = h.SetTrailerBytes(value)
  1262. return true
  1263. }
  1264. case 'd':
  1265. if caseInsensitiveCompare(strDate, key) {
  1266. // Date is managed automatically.
  1267. return true
  1268. }
  1269. }
  1270. return false
  1271. }
  1272. // setNonSpecial directly put into map i.e. not a basic header.
  1273. func (h *ResponseHeader) setNonSpecial(key []byte, value []byte) {
  1274. h.h = setArgBytes(h.h, key, value, argsHasValue)
  1275. }
  1276. // setSpecialHeader handles special headers and return true when a header is processed.
  1277. func (h *RequestHeader) setSpecialHeader(key, value []byte) bool {
  1278. if len(key) == 0 || h.disableSpecialHeader {
  1279. return false
  1280. }
  1281. switch key[0] | 0x20 {
  1282. case 'c':
  1283. switch {
  1284. case caseInsensitiveCompare(strContentType, key):
  1285. h.SetContentTypeBytes(value)
  1286. return true
  1287. case caseInsensitiveCompare(strContentLength, key):
  1288. if contentLength, err := parseContentLength(value); err == nil {
  1289. h.contentLength = contentLength
  1290. h.contentLengthBytes = append(h.contentLengthBytes[:0], value...)
  1291. }
  1292. return true
  1293. case caseInsensitiveCompare(strConnection, key):
  1294. if bytes.Equal(strClose, value) {
  1295. h.SetConnectionClose()
  1296. } else {
  1297. h.ResetConnectionClose()
  1298. h.setNonSpecial(key, value)
  1299. }
  1300. return true
  1301. case caseInsensitiveCompare(strCookie, key):
  1302. h.collectCookies()
  1303. h.cookies = parseRequestCookies(h.cookies, value)
  1304. return true
  1305. }
  1306. case 't':
  1307. if caseInsensitiveCompare(strTransferEncoding, key) {
  1308. // Transfer-Encoding is managed automatically.
  1309. return true
  1310. } else if caseInsensitiveCompare(strTrailer, key) {
  1311. _ = h.SetTrailerBytes(value)
  1312. return true
  1313. }
  1314. case 'h':
  1315. if caseInsensitiveCompare(strHost, key) {
  1316. h.SetHostBytes(value)
  1317. return true
  1318. }
  1319. case 'u':
  1320. if caseInsensitiveCompare(strUserAgent, key) {
  1321. h.SetUserAgentBytes(value)
  1322. return true
  1323. }
  1324. }
  1325. return false
  1326. }
  1327. // setNonSpecial directly put into map i.e. not a basic header.
  1328. func (h *RequestHeader) setNonSpecial(key []byte, value []byte) {
  1329. h.h = setArgBytes(h.h, key, value, argsHasValue)
  1330. }
  1331. // Add adds the given 'key: value' header.
  1332. //
  1333. // Multiple headers with the same key may be added with this function.
  1334. // Use Set for setting a single header for the given key.
  1335. //
  1336. // the Content-Type, Content-Length, Connection, Server, Set-Cookie,
  1337. // Transfer-Encoding and Date headers can only be set once and will
  1338. // overwrite the previous value.
  1339. //
  1340. // If the header is set as a Trailer (forbidden trailers will not be set, see AddTrailer for more details),
  1341. // it will be sent after the chunked response body.
  1342. func (h *ResponseHeader) Add(key, value string) {
  1343. h.AddBytesKV(s2b(key), s2b(value))
  1344. }
  1345. // AddBytesK adds the given 'key: value' header.
  1346. //
  1347. // Multiple headers with the same key may be added with this function.
  1348. // Use SetBytesK for setting a single header for the given key.
  1349. //
  1350. // the Content-Type, Content-Length, Connection, Server, Set-Cookie,
  1351. // Transfer-Encoding and Date headers can only be set once and will
  1352. // overwrite the previous value.
  1353. //
  1354. // If the header is set as a Trailer (forbidden trailers will not be set, see AddTrailer for more details),
  1355. // it will be sent after the chunked response body.
  1356. func (h *ResponseHeader) AddBytesK(key []byte, value string) {
  1357. h.AddBytesKV(key, s2b(value))
  1358. }
  1359. // AddBytesV adds the given 'key: value' header.
  1360. //
  1361. // Multiple headers with the same key may be added with this function.
  1362. // Use SetBytesV for setting a single header for the given key.
  1363. //
  1364. // the Content-Type, Content-Length, Connection, Server, Set-Cookie,
  1365. // Transfer-Encoding and Date headers can only be set once and will
  1366. // overwrite the previous value.
  1367. //
  1368. // If the header is set as a Trailer (forbidden trailers will not be set, see AddTrailer for more details),
  1369. // it will be sent after the chunked response body.
  1370. func (h *ResponseHeader) AddBytesV(key string, value []byte) {
  1371. h.AddBytesKV(s2b(key), value)
  1372. }
  1373. // AddBytesKV adds the given 'key: value' header.
  1374. //
  1375. // Multiple headers with the same key may be added with this function.
  1376. // Use SetBytesKV for setting a single header for the given key.
  1377. //
  1378. // the Content-Type, Content-Length, Connection, Server, Set-Cookie,
  1379. // Transfer-Encoding and Date headers can only be set once and will
  1380. // overwrite the previous value.
  1381. //
  1382. // If the header is set as a Trailer (forbidden trailers will not be set, see AddTrailer for more details),
  1383. // it will be sent after the chunked response body.
  1384. func (h *ResponseHeader) AddBytesKV(key, value []byte) {
  1385. if h.setSpecialHeader(key, value) {
  1386. return
  1387. }
  1388. k := getHeaderKeyBytes(&h.bufKV, b2s(key), h.disableNormalizing)
  1389. h.h = appendArgBytes(h.h, k, value, argsHasValue)
  1390. }
  1391. // Set sets the given 'key: value' header.
  1392. //
  1393. // If the header is set as a Trailer (forbidden trailers will not be set, see SetTrailer for more details),
  1394. // it will be sent after the chunked response body.
  1395. //
  1396. // Use Add for setting multiple header values under the same key.
  1397. func (h *ResponseHeader) Set(key, value string) {
  1398. initHeaderKV(&h.bufKV, key, value, h.disableNormalizing)
  1399. h.SetCanonical(h.bufKV.key, h.bufKV.value)
  1400. }
  1401. // SetBytesK sets the given 'key: value' header.
  1402. //
  1403. // If the header is set as a Trailer (forbidden trailers will not be set, see SetTrailer for more details),
  1404. // it will be sent after the chunked response body.
  1405. //
  1406. // Use AddBytesK for setting multiple header values under the same key.
  1407. func (h *ResponseHeader) SetBytesK(key []byte, value string) {
  1408. h.bufKV.value = append(h.bufKV.value[:0], value...)
  1409. h.SetBytesKV(key, h.bufKV.value)
  1410. }
  1411. // SetBytesV sets the given 'key: value' header.
  1412. //
  1413. // If the header is set as a Trailer (forbidden trailers will not be set, see SetTrailer for more details),
  1414. // it will be sent after the chunked response body.
  1415. //
  1416. // Use AddBytesV for setting multiple header values under the same key.
  1417. func (h *ResponseHeader) SetBytesV(key string, value []byte) {
  1418. k := getHeaderKeyBytes(&h.bufKV, key, h.disableNormalizing)
  1419. h.SetCanonical(k, value)
  1420. }
  1421. // SetBytesKV sets the given 'key: value' header.
  1422. //
  1423. // If the header is set as a Trailer (forbidden trailers will not be set, see SetTrailer for more details),
  1424. // it will be sent after the chunked response body.
  1425. //
  1426. // Use AddBytesKV for setting multiple header values under the same key.
  1427. func (h *ResponseHeader) SetBytesKV(key, value []byte) {
  1428. h.bufKV.key = append(h.bufKV.key[:0], key...)
  1429. normalizeHeaderKey(h.bufKV.key, h.disableNormalizing)
  1430. h.SetCanonical(h.bufKV.key, value)
  1431. }
  1432. // SetCanonical sets the given 'key: value' header assuming that
  1433. // key is in canonical form.
  1434. //
  1435. // If the header is set as a Trailer (forbidden trailers will not be set, see SetTrailer for more details),
  1436. // it will be sent after the chunked response body.
  1437. func (h *ResponseHeader) SetCanonical(key, value []byte) {
  1438. if h.setSpecialHeader(key, value) {
  1439. return
  1440. }
  1441. h.setNonSpecial(key, value)
  1442. }
  1443. // SetCookie sets the given response cookie.
  1444. //
  1445. // It is safe re-using the cookie after the function returns.
  1446. func (h *ResponseHeader) SetCookie(cookie *Cookie) {
  1447. h.cookies = setArgBytes(h.cookies, cookie.Key(), cookie.Cookie(), argsHasValue)
  1448. }
  1449. // SetCookie sets 'key: value' cookies.
  1450. func (h *RequestHeader) SetCookie(key, value string) {
  1451. h.collectCookies()
  1452. h.cookies = setArg(h.cookies, key, value, argsHasValue)
  1453. }
  1454. // SetCookieBytesK sets 'key: value' cookies.
  1455. func (h *RequestHeader) SetCookieBytesK(key []byte, value string) {
  1456. h.SetCookie(b2s(key), value)
  1457. }
  1458. // SetCookieBytesKV sets 'key: value' cookies.
  1459. func (h *RequestHeader) SetCookieBytesKV(key, value []byte) {
  1460. h.SetCookie(b2s(key), b2s(value))
  1461. }
  1462. // DelClientCookie instructs the client to remove the given cookie.
  1463. // This doesn't work for a cookie with specific domain or path,
  1464. // you should delete it manually like:
  1465. //
  1466. // c := AcquireCookie()
  1467. // c.SetKey(key)
  1468. // c.SetDomain("example.com")
  1469. // c.SetPath("/path")
  1470. // c.SetExpire(CookieExpireDelete)
  1471. // h.SetCookie(c)
  1472. // ReleaseCookie(c)
  1473. //
  1474. // Use DelCookie if you want just removing the cookie from response header.
  1475. func (h *ResponseHeader) DelClientCookie(key string) {
  1476. h.DelCookie(key)
  1477. c := AcquireCookie()
  1478. c.SetKey(key)
  1479. c.SetExpire(CookieExpireDelete)
  1480. h.SetCookie(c)
  1481. ReleaseCookie(c)
  1482. }
  1483. // DelClientCookieBytes instructs the client to remove the given cookie.
  1484. // This doesn't work for a cookie with specific domain or path,
  1485. // you should delete it manually like:
  1486. //
  1487. // c := AcquireCookie()
  1488. // c.SetKey(key)
  1489. // c.SetDomain("example.com")
  1490. // c.SetPath("/path")
  1491. // c.SetExpire(CookieExpireDelete)
  1492. // h.SetCookie(c)
  1493. // ReleaseCookie(c)
  1494. //
  1495. // Use DelCookieBytes if you want just removing the cookie from response header.
  1496. func (h *ResponseHeader) DelClientCookieBytes(key []byte) {
  1497. h.DelClientCookie(b2s(key))
  1498. }
  1499. // DelCookie removes cookie under the given key from response header.
  1500. //
  1501. // Note that DelCookie doesn't remove the cookie from the client.
  1502. // Use DelClientCookie instead.
  1503. func (h *ResponseHeader) DelCookie(key string) {
  1504. h.cookies = delAllArgs(h.cookies, key)
  1505. }
  1506. // DelCookieBytes removes cookie under the given key from response header.
  1507. //
  1508. // Note that DelCookieBytes doesn't remove the cookie from the client.
  1509. // Use DelClientCookieBytes instead.
  1510. func (h *ResponseHeader) DelCookieBytes(key []byte) {
  1511. h.DelCookie(b2s(key))
  1512. }
  1513. // DelCookie removes cookie under the given key.
  1514. func (h *RequestHeader) DelCookie(key string) {
  1515. h.collectCookies()
  1516. h.cookies = delAllArgs(h.cookies, key)
  1517. }
  1518. // DelCookieBytes removes cookie under the given key.
  1519. func (h *RequestHeader) DelCookieBytes(key []byte) {
  1520. h.DelCookie(b2s(key))
  1521. }
  1522. // DelAllCookies removes all the cookies from response headers.
  1523. func (h *ResponseHeader) DelAllCookies() {
  1524. h.cookies = h.cookies[:0]
  1525. }
  1526. // DelAllCookies removes all the cookies from request headers.
  1527. func (h *RequestHeader) DelAllCookies() {
  1528. h.collectCookies()
  1529. h.cookies = h.cookies[:0]
  1530. }
  1531. // Add adds the given 'key: value' header.
  1532. //
  1533. // Multiple headers with the same key may be added with this function.
  1534. // Use Set for setting a single header for the given key.
  1535. //
  1536. // If the header is set as a Trailer (forbidden trailers will not be set, see AddTrailer for more details),
  1537. // it will be sent after the chunked request body.
  1538. func (h *RequestHeader) Add(key, value string) {
  1539. h.AddBytesKV(s2b(key), s2b(value))
  1540. }
  1541. // AddBytesK adds the given 'key: value' header.
  1542. //
  1543. // Multiple headers with the same key may be added with this function.
  1544. // Use SetBytesK for setting a single header for the given key.
  1545. //
  1546. // If the header is set as a Trailer (forbidden trailers will not be set, see AddTrailer for more details),
  1547. // it will be sent after the chunked request body.
  1548. func (h *RequestHeader) AddBytesK(key []byte, value string) {
  1549. h.AddBytesKV(key, s2b(value))
  1550. }
  1551. // AddBytesV adds the given 'key: value' header.
  1552. //
  1553. // Multiple headers with the same key may be added with this function.
  1554. // Use SetBytesV for setting a single header for the given key.
  1555. //
  1556. // If the header is set as a Trailer (forbidden trailers will not be set, see AddTrailer for more details),
  1557. // it will be sent after the chunked request body.
  1558. func (h *RequestHeader) AddBytesV(key string, value []byte) {
  1559. h.AddBytesKV(s2b(key), value)
  1560. }
  1561. // AddBytesKV adds the given 'key: value' header.
  1562. //
  1563. // Multiple headers with the same key may be added with this function.
  1564. // Use SetBytesKV for setting a single header for the given key.
  1565. //
  1566. // the Content-Type, Content-Length, Connection, Cookie,
  1567. // Transfer-Encoding, Host and User-Agent headers can only be set once
  1568. // and will overwrite the previous value.
  1569. //
  1570. // If the header is set as a Trailer (forbidden trailers will not be set, see AddTrailer for more details),
  1571. // it will be sent after the chunked request body.
  1572. func (h *RequestHeader) AddBytesKV(key, value []byte) {
  1573. if h.setSpecialHeader(key, value) {
  1574. return
  1575. }
  1576. k := getHeaderKeyBytes(&h.bufKV, b2s(key), h.disableNormalizing)
  1577. h.h = appendArgBytes(h.h, k, value, argsHasValue)
  1578. }
  1579. // Set sets the given 'key: value' header.
  1580. //
  1581. // If the header is set as a Trailer (forbidden trailers will not be set, see SetTrailer for more details),
  1582. // it will be sent after the chunked request body.
  1583. //
  1584. // Use Add for setting multiple header values under the same key.
  1585. func (h *RequestHeader) Set(key, value string) {
  1586. initHeaderKV(&h.bufKV, key, value, h.disableNormalizing)
  1587. h.SetCanonical(h.bufKV.key, h.bufKV.value)
  1588. }
  1589. // SetBytesK sets the given 'key: value' header.
  1590. //
  1591. // If the header is set as a Trailer (forbidden trailers will not be set, see SetTrailer for more details),
  1592. // it will be sent after the chunked request body.
  1593. //
  1594. // Use AddBytesK for setting multiple header values under the same key.
  1595. func (h *RequestHeader) SetBytesK(key []byte, value string) {
  1596. h.bufKV.value = append(h.bufKV.value[:0], value...)
  1597. h.SetBytesKV(key, h.bufKV.value)
  1598. }
  1599. // SetBytesV sets the given 'key: value' header.
  1600. //
  1601. // If the header is set as a Trailer (forbidden trailers will not be set, see SetTrailer for more details),
  1602. // it will be sent after the chunked request body.
  1603. //
  1604. // Use AddBytesV for setting multiple header values under the same key.
  1605. func (h *RequestHeader) SetBytesV(key string, value []byte) {
  1606. k := getHeaderKeyBytes(&h.bufKV, key, h.disableNormalizing)
  1607. h.SetCanonical(k, value)
  1608. }
  1609. // SetBytesKV sets the given 'key: value' header.
  1610. //
  1611. // If the header is set as a Trailer (forbidden trailers will not be set, see SetTrailer for more details),
  1612. // it will be sent after the chunked request body.
  1613. //
  1614. // Use AddBytesKV for setting multiple header values under the same key.
  1615. func (h *RequestHeader) SetBytesKV(key, value []byte) {
  1616. h.bufKV.key = append(h.bufKV.key[:0], key...)
  1617. normalizeHeaderKey(h.bufKV.key, h.disableNormalizing)
  1618. h.SetCanonical(h.bufKV.key, value)
  1619. }
  1620. // SetCanonical sets the given 'key: value' header assuming that
  1621. // key is in canonical form.
  1622. //
  1623. // If the header is set as a Trailer (forbidden trailers will not be set, see SetTrailer for more details),
  1624. // it will be sent after the chunked request body.
  1625. func (h *RequestHeader) SetCanonical(key, value []byte) {
  1626. if h.setSpecialHeader(key, value) {
  1627. return
  1628. }
  1629. h.setNonSpecial(key, value)
  1630. }
  1631. // Peek returns header value for the given key.
  1632. //
  1633. // The returned value is valid until the response is released,
  1634. // either though ReleaseResponse or your request handler returning.
  1635. // Do not store references to the returned value. Make copies instead.
  1636. func (h *ResponseHeader) Peek(key string) []byte {
  1637. k := getHeaderKeyBytes(&h.bufKV, key, h.disableNormalizing)
  1638. return h.peek(k)
  1639. }
  1640. // PeekBytes returns header value for the given key.
  1641. //
  1642. // The returned value is valid until the response is released,
  1643. // either though ReleaseResponse or your request handler returning.
  1644. // Do not store references to returned value. Make copies instead.
  1645. func (h *ResponseHeader) PeekBytes(key []byte) []byte {
  1646. h.bufKV.key = append(h.bufKV.key[:0], key...)
  1647. normalizeHeaderKey(h.bufKV.key, h.disableNormalizing)
  1648. return h.peek(h.bufKV.key)
  1649. }
  1650. // Peek returns header value for the given key.
  1651. //
  1652. // The returned value is valid until the request is released,
  1653. // either though ReleaseRequest or your request handler returning.
  1654. // Do not store references to returned value. Make copies instead.
  1655. func (h *RequestHeader) Peek(key string) []byte {
  1656. k := getHeaderKeyBytes(&h.bufKV, key, h.disableNormalizing)
  1657. return h.peek(k)
  1658. }
  1659. // PeekBytes returns header value for the given key.
  1660. //
  1661. // The returned value is valid until the request is released,
  1662. // either though ReleaseRequest or your request handler returning.
  1663. // Do not store references to returned value. Make copies instead.
  1664. func (h *RequestHeader) PeekBytes(key []byte) []byte {
  1665. h.bufKV.key = append(h.bufKV.key[:0], key...)
  1666. normalizeHeaderKey(h.bufKV.key, h.disableNormalizing)
  1667. return h.peek(h.bufKV.key)
  1668. }
  1669. func (h *ResponseHeader) peek(key []byte) []byte {
  1670. switch string(key) {
  1671. case HeaderContentType:
  1672. return h.ContentType()
  1673. case HeaderContentEncoding:
  1674. return h.ContentEncoding()
  1675. case HeaderServer:
  1676. return h.Server()
  1677. case HeaderConnection:
  1678. if h.ConnectionClose() {
  1679. return strClose
  1680. }
  1681. return peekArgBytes(h.h, key)
  1682. case HeaderContentLength:
  1683. return h.contentLengthBytes
  1684. case HeaderSetCookie:
  1685. return appendResponseCookieBytes(nil, h.cookies)
  1686. case HeaderTrailer:
  1687. return appendArgsKeyBytes(nil, h.trailer, strCommaSpace)
  1688. default:
  1689. return peekArgBytes(h.h, key)
  1690. }
  1691. }
  1692. func (h *RequestHeader) peek(key []byte) []byte {
  1693. switch string(key) {
  1694. case HeaderHost:
  1695. return h.Host()
  1696. case HeaderContentType:
  1697. return h.ContentType()
  1698. case HeaderUserAgent:
  1699. return h.UserAgent()
  1700. case HeaderConnection:
  1701. if h.ConnectionClose() {
  1702. return strClose
  1703. }
  1704. return peekArgBytes(h.h, key)
  1705. case HeaderContentLength:
  1706. return h.contentLengthBytes
  1707. case HeaderCookie:
  1708. if h.cookiesCollected {
  1709. return appendRequestCookieBytes(nil, h.cookies)
  1710. }
  1711. return peekArgBytes(h.h, key)
  1712. case HeaderTrailer:
  1713. return appendArgsKeyBytes(nil, h.trailer, strCommaSpace)
  1714. default:
  1715. return peekArgBytes(h.h, key)
  1716. }
  1717. }
  1718. // PeekAll returns all header value for the given key.
  1719. //
  1720. // The returned value is valid until the request is released,
  1721. // either though ReleaseRequest or your request handler returning.
  1722. // Any future calls to the Peek* will modify the returned value.
  1723. // Do not store references to returned value. Make copies instead.
  1724. func (h *RequestHeader) PeekAll(key string) [][]byte {
  1725. k := getHeaderKeyBytes(&h.bufKV, key, h.disableNormalizing)
  1726. return h.peekAll(k)
  1727. }
  1728. func (h *RequestHeader) peekAll(key []byte) [][]byte {
  1729. h.mulHeader = h.mulHeader[:0]
  1730. switch string(key) {
  1731. case HeaderHost:
  1732. if host := h.Host(); len(host) > 0 {
  1733. h.mulHeader = append(h.mulHeader, host)
  1734. }
  1735. case HeaderContentType:
  1736. if contentType := h.ContentType(); len(contentType) > 0 {
  1737. h.mulHeader = append(h.mulHeader, contentType)
  1738. }
  1739. case HeaderUserAgent:
  1740. if ua := h.UserAgent(); len(ua) > 0 {
  1741. h.mulHeader = append(h.mulHeader, ua)
  1742. }
  1743. case HeaderConnection:
  1744. if h.ConnectionClose() {
  1745. h.mulHeader = append(h.mulHeader, strClose)
  1746. } else {
  1747. h.mulHeader = peekAllArgBytesToDst(h.mulHeader, h.h, key)
  1748. }
  1749. case HeaderContentLength:
  1750. h.mulHeader = append(h.mulHeader, h.contentLengthBytes)
  1751. case HeaderCookie:
  1752. if h.cookiesCollected {
  1753. h.mulHeader = append(h.mulHeader, appendRequestCookieBytes(nil, h.cookies))
  1754. } else {
  1755. h.mulHeader = peekAllArgBytesToDst(h.mulHeader, h.h, key)
  1756. }
  1757. case HeaderTrailer:
  1758. h.mulHeader = append(h.mulHeader, appendArgsKeyBytes(nil, h.trailer, strCommaSpace))
  1759. default:
  1760. h.mulHeader = peekAllArgBytesToDst(h.mulHeader, h.h, key)
  1761. }
  1762. return h.mulHeader
  1763. }
  1764. // PeekAll returns all header value for the given key.
  1765. //
  1766. // The returned value is valid until the request is released,
  1767. // either though ReleaseResponse or your request handler returning.
  1768. // Any future calls to the Peek* will modify the returned value.
  1769. // Do not store references to returned value. Make copies instead.
  1770. func (h *ResponseHeader) PeekAll(key string) [][]byte {
  1771. k := getHeaderKeyBytes(&h.bufKV, key, h.disableNormalizing)
  1772. return h.peekAll(k)
  1773. }
  1774. func (h *ResponseHeader) peekAll(key []byte) [][]byte {
  1775. h.mulHeader = h.mulHeader[:0]
  1776. switch string(key) {
  1777. case HeaderContentType:
  1778. if contentType := h.ContentType(); len(contentType) > 0 {
  1779. h.mulHeader = append(h.mulHeader, contentType)
  1780. }
  1781. case HeaderContentEncoding:
  1782. if contentEncoding := h.ContentEncoding(); len(contentEncoding) > 0 {
  1783. h.mulHeader = append(h.mulHeader, contentEncoding)
  1784. }
  1785. case HeaderServer:
  1786. if server := h.Server(); len(server) > 0 {
  1787. h.mulHeader = append(h.mulHeader, server)
  1788. }
  1789. case HeaderConnection:
  1790. if h.ConnectionClose() {
  1791. h.mulHeader = append(h.mulHeader, strClose)
  1792. } else {
  1793. h.mulHeader = peekAllArgBytesToDst(h.mulHeader, h.h, key)
  1794. }
  1795. case HeaderContentLength:
  1796. h.mulHeader = append(h.mulHeader, h.contentLengthBytes)
  1797. case HeaderSetCookie:
  1798. h.mulHeader = append(h.mulHeader, appendResponseCookieBytes(nil, h.cookies))
  1799. case HeaderTrailer:
  1800. h.mulHeader = append(h.mulHeader, appendArgsKeyBytes(nil, h.trailer, strCommaSpace))
  1801. default:
  1802. h.mulHeader = peekAllArgBytesToDst(h.mulHeader, h.h, key)
  1803. }
  1804. return h.mulHeader
  1805. }
  1806. // PeekKeys return all header keys.
  1807. //
  1808. // The returned value is valid until the request is released,
  1809. // either though ReleaseRequest or your request handler returning.
  1810. // Any future calls to the Peek* will modify the returned value.
  1811. // Do not store references to returned value. Make copies instead.
  1812. func (h *RequestHeader) PeekKeys() [][]byte {
  1813. h.mulHeader = h.mulHeader[:0]
  1814. h.mulHeader = peekArgsKeys(h.mulHeader, h.h)
  1815. return h.mulHeader
  1816. }
  1817. // PeekTrailerKeys return all trailer keys.
  1818. //
  1819. // The returned value is valid until the request is released,
  1820. // either though ReleaseRequest or your request handler returning.
  1821. // Any future calls to the Peek* will modify the returned value.
  1822. // Do not store references to returned value. Make copies instead.
  1823. func (h *RequestHeader) PeekTrailerKeys() [][]byte {
  1824. h.mulHeader = h.mulHeader[:0]
  1825. h.mulHeader = peekArgsKeys(h.mulHeader, h.trailer)
  1826. return h.mulHeader
  1827. }
  1828. // PeekKeys return all header keys.
  1829. //
  1830. // The returned value is valid until the request is released,
  1831. // either though ReleaseResponse or your request handler returning.
  1832. // Any future calls to the Peek* will modify the returned value.
  1833. // Do not store references to returned value. Make copies instead.
  1834. func (h *ResponseHeader) PeekKeys() [][]byte {
  1835. h.mulHeader = h.mulHeader[:0]
  1836. h.mulHeader = peekArgsKeys(h.mulHeader, h.h)
  1837. return h.mulHeader
  1838. }
  1839. // PeekTrailerKeys return all trailer keys.
  1840. //
  1841. // The returned value is valid until the request is released,
  1842. // either though ReleaseResponse or your request handler returning.
  1843. // Any future calls to the Peek* will modify the returned value.
  1844. // Do not store references to returned value. Make copies instead.
  1845. func (h *ResponseHeader) PeekTrailerKeys() [][]byte {
  1846. h.mulHeader = h.mulHeader[:0]
  1847. h.mulHeader = peekArgsKeys(h.mulHeader, h.trailer)
  1848. return h.mulHeader
  1849. }
  1850. // Cookie returns cookie for the given key.
  1851. func (h *RequestHeader) Cookie(key string) []byte {
  1852. h.collectCookies()
  1853. return peekArgStr(h.cookies, key)
  1854. }
  1855. // CookieBytes returns cookie for the given key.
  1856. func (h *RequestHeader) CookieBytes(key []byte) []byte {
  1857. h.collectCookies()
  1858. return peekArgBytes(h.cookies, key)
  1859. }
  1860. // Cookie fills cookie for the given cookie.Key.
  1861. //
  1862. // Returns false if cookie with the given cookie.Key is missing.
  1863. func (h *ResponseHeader) Cookie(cookie *Cookie) bool {
  1864. v := peekArgBytes(h.cookies, cookie.Key())
  1865. if v == nil {
  1866. return false
  1867. }
  1868. cookie.ParseBytes(v) //nolint:errcheck
  1869. return true
  1870. }
  1871. // Read reads response header from r.
  1872. //
  1873. // io.EOF is returned if r is closed before reading the first header byte.
  1874. func (h *ResponseHeader) Read(r *bufio.Reader) error {
  1875. n := 1
  1876. for {
  1877. err := h.tryRead(r, n)
  1878. if err == nil {
  1879. return nil
  1880. }
  1881. if err != errNeedMore {
  1882. h.resetSkipNormalize()
  1883. return err
  1884. }
  1885. n = r.Buffered() + 1
  1886. }
  1887. }
  1888. func (h *ResponseHeader) tryRead(r *bufio.Reader, n int) error {
  1889. h.resetSkipNormalize()
  1890. b, err := r.Peek(n)
  1891. if len(b) == 0 {
  1892. // Return ErrTimeout on any timeout.
  1893. if x, ok := err.(interface{ Timeout() bool }); ok && x.Timeout() {
  1894. return ErrTimeout
  1895. }
  1896. // treat all other errors on the first byte read as EOF
  1897. if n == 1 || err == io.EOF {
  1898. return io.EOF
  1899. }
  1900. // This is for go 1.6 bug. See https://github.com/golang/go/issues/14121 .
  1901. if err == bufio.ErrBufferFull {
  1902. if h.secureErrorLogMessage {
  1903. return &ErrSmallBuffer{
  1904. error: fmt.Errorf("error when reading response headers"),
  1905. }
  1906. }
  1907. return &ErrSmallBuffer{
  1908. error: fmt.Errorf("error when reading response headers: %w", errSmallBuffer),
  1909. }
  1910. }
  1911. return fmt.Errorf("error when reading response headers: %w", err)
  1912. }
  1913. b = mustPeekBuffered(r)
  1914. headersLen, errParse := h.parse(b)
  1915. if errParse != nil {
  1916. return headerError("response", err, errParse, b, h.secureErrorLogMessage)
  1917. }
  1918. mustDiscard(r, headersLen)
  1919. return nil
  1920. }
  1921. // ReadTrailer reads response trailer header from r.
  1922. //
  1923. // io.EOF is returned if r is closed before reading the first byte.
  1924. func (h *ResponseHeader) ReadTrailer(r *bufio.Reader) error {
  1925. n := 1
  1926. for {
  1927. err := h.tryReadTrailer(r, n)
  1928. if err == nil {
  1929. return nil
  1930. }
  1931. if err != errNeedMore {
  1932. return err
  1933. }
  1934. n = r.Buffered() + 1
  1935. }
  1936. }
  1937. func (h *ResponseHeader) tryReadTrailer(r *bufio.Reader, n int) error {
  1938. b, err := r.Peek(n)
  1939. if len(b) == 0 {
  1940. // Return ErrTimeout on any timeout.
  1941. if x, ok := err.(interface{ Timeout() bool }); ok && x.Timeout() {
  1942. return ErrTimeout
  1943. }
  1944. if n == 1 || err == io.EOF {
  1945. return io.EOF
  1946. }
  1947. // This is for go 1.6 bug. See https://github.com/golang/go/issues/14121 .
  1948. if err == bufio.ErrBufferFull {
  1949. if h.secureErrorLogMessage {
  1950. return &ErrSmallBuffer{
  1951. error: fmt.Errorf("error when reading response trailer"),
  1952. }
  1953. }
  1954. return &ErrSmallBuffer{
  1955. error: fmt.Errorf("error when reading response trailer: %w", errSmallBuffer),
  1956. }
  1957. }
  1958. return fmt.Errorf("error when reading response trailer: %w", err)
  1959. }
  1960. b = mustPeekBuffered(r)
  1961. headersLen, errParse := h.parseTrailer(b)
  1962. if errParse != nil {
  1963. if err == io.EOF {
  1964. return err
  1965. }
  1966. return headerError("response", err, errParse, b, h.secureErrorLogMessage)
  1967. }
  1968. mustDiscard(r, headersLen)
  1969. return nil
  1970. }
  1971. func headerError(typ string, err, errParse error, b []byte, secureErrorLogMessage bool) error {
  1972. if errParse != errNeedMore {
  1973. return headerErrorMsg(typ, errParse, b, secureErrorLogMessage)
  1974. }
  1975. if err == nil {
  1976. return errNeedMore
  1977. }
  1978. // Buggy servers may leave trailing CRLFs after http body.
  1979. // Treat this case as EOF.
  1980. if isOnlyCRLF(b) {
  1981. return io.EOF
  1982. }
  1983. if err != bufio.ErrBufferFull {
  1984. return headerErrorMsg(typ, err, b, secureErrorLogMessage)
  1985. }
  1986. return &ErrSmallBuffer{
  1987. error: headerErrorMsg(typ, errSmallBuffer, b, secureErrorLogMessage),
  1988. }
  1989. }
  1990. func headerErrorMsg(typ string, err error, b []byte, secureErrorLogMessage bool) error {
  1991. if secureErrorLogMessage {
  1992. return fmt.Errorf("error when reading %s headers: %w. Buffer size=%d", typ, err, len(b))
  1993. }
  1994. return fmt.Errorf("error when reading %s headers: %w. Buffer size=%d, contents: %s", typ, err, len(b), bufferSnippet(b))
  1995. }
  1996. // Read reads request header from r.
  1997. //
  1998. // io.EOF is returned if r is closed before reading the first header byte.
  1999. func (h *RequestHeader) Read(r *bufio.Reader) error {
  2000. return h.readLoop(r, true)
  2001. }
  2002. // readLoop reads request header from r optionally loops until it has enough data.
  2003. //
  2004. // io.EOF is returned if r is closed before reading the first header byte.
  2005. func (h *RequestHeader) readLoop(r *bufio.Reader, waitForMore bool) error {
  2006. n := 1
  2007. for {
  2008. err := h.tryRead(r, n)
  2009. if err == nil {
  2010. return nil
  2011. }
  2012. if !waitForMore || err != errNeedMore {
  2013. h.resetSkipNormalize()
  2014. return err
  2015. }
  2016. n = r.Buffered() + 1
  2017. }
  2018. }
  2019. // ReadTrailer reads request trailer header from r.
  2020. //
  2021. // io.EOF is returned if r is closed before reading the first byte.
  2022. func (h *RequestHeader) ReadTrailer(r *bufio.Reader) error {
  2023. n := 1
  2024. for {
  2025. err := h.tryReadTrailer(r, n)
  2026. if err == nil {
  2027. return nil
  2028. }
  2029. if err != errNeedMore {
  2030. return err
  2031. }
  2032. n = r.Buffered() + 1
  2033. }
  2034. }
  2035. func (h *RequestHeader) tryReadTrailer(r *bufio.Reader, n int) error {
  2036. b, err := r.Peek(n)
  2037. if len(b) == 0 {
  2038. // Return ErrTimeout on any timeout.
  2039. if x, ok := err.(interface{ Timeout() bool }); ok && x.Timeout() {
  2040. return ErrTimeout
  2041. }
  2042. if n == 1 || err == io.EOF {
  2043. return io.EOF
  2044. }
  2045. // This is for go 1.6 bug. See https://github.com/golang/go/issues/14121 .
  2046. if err == bufio.ErrBufferFull {
  2047. if h.secureErrorLogMessage {
  2048. return &ErrSmallBuffer{
  2049. error: fmt.Errorf("error when reading request trailer"),
  2050. }
  2051. }
  2052. return &ErrSmallBuffer{
  2053. error: fmt.Errorf("error when reading request trailer: %w", errSmallBuffer),
  2054. }
  2055. }
  2056. return fmt.Errorf("error when reading request trailer: %w", err)
  2057. }
  2058. b = mustPeekBuffered(r)
  2059. headersLen, errParse := h.parseTrailer(b)
  2060. if errParse != nil {
  2061. if err == io.EOF {
  2062. return err
  2063. }
  2064. return headerError("request", err, errParse, b, h.secureErrorLogMessage)
  2065. }
  2066. mustDiscard(r, headersLen)
  2067. return nil
  2068. }
  2069. func (h *RequestHeader) tryRead(r *bufio.Reader, n int) error {
  2070. h.resetSkipNormalize()
  2071. b, err := r.Peek(n)
  2072. if len(b) == 0 {
  2073. if err == io.EOF {
  2074. return err
  2075. }
  2076. if err == nil {
  2077. panic("bufio.Reader.Peek() returned nil, nil")
  2078. }
  2079. // This is for go 1.6 bug. See https://github.com/golang/go/issues/14121 .
  2080. if err == bufio.ErrBufferFull {
  2081. return &ErrSmallBuffer{
  2082. error: fmt.Errorf("error when reading request headers: %w (n=%d, r.Buffered()=%d)", errSmallBuffer, n, r.Buffered()),
  2083. }
  2084. }
  2085. // n == 1 on the first read for the request.
  2086. if n == 1 {
  2087. // We didn't read a single byte.
  2088. return ErrNothingRead{err}
  2089. }
  2090. return fmt.Errorf("error when reading request headers: %w", err)
  2091. }
  2092. b = mustPeekBuffered(r)
  2093. headersLen, errParse := h.parse(b)
  2094. if errParse != nil {
  2095. return headerError("request", err, errParse, b, h.secureErrorLogMessage)
  2096. }
  2097. mustDiscard(r, headersLen)
  2098. return nil
  2099. }
  2100. func bufferSnippet(b []byte) string {
  2101. n := len(b)
  2102. start := 200
  2103. end := n - start
  2104. if start >= end {
  2105. start = n
  2106. end = n
  2107. }
  2108. bStart, bEnd := b[:start], b[end:]
  2109. if len(bEnd) == 0 {
  2110. return fmt.Sprintf("%q", b)
  2111. }
  2112. return fmt.Sprintf("%q...%q", bStart, bEnd)
  2113. }
  2114. func isOnlyCRLF(b []byte) bool {
  2115. for _, ch := range b {
  2116. if ch != rChar && ch != nChar {
  2117. return false
  2118. }
  2119. }
  2120. return true
  2121. }
  2122. func updateServerDate() {
  2123. refreshServerDate()
  2124. go func() {
  2125. for {
  2126. time.Sleep(time.Second)
  2127. refreshServerDate()
  2128. }
  2129. }()
  2130. }
  2131. var (
  2132. serverDate atomic.Value
  2133. serverDateOnce sync.Once // serverDateOnce.Do(updateServerDate)
  2134. )
  2135. func refreshServerDate() {
  2136. b := AppendHTTPDate(nil, time.Now())
  2137. serverDate.Store(b)
  2138. }
  2139. // Write writes response header to w.
  2140. func (h *ResponseHeader) Write(w *bufio.Writer) error {
  2141. _, err := w.Write(h.Header())
  2142. return err
  2143. }
  2144. // WriteTo writes response header to w.
  2145. //
  2146. // WriteTo implements io.WriterTo interface.
  2147. func (h *ResponseHeader) WriteTo(w io.Writer) (int64, error) {
  2148. n, err := w.Write(h.Header())
  2149. return int64(n), err
  2150. }
  2151. // Header returns response header representation.
  2152. //
  2153. // Headers that set as Trailer will not represent. Use TrailerHeader for trailers.
  2154. //
  2155. // The returned value is valid until the request is released,
  2156. // either though ReleaseRequest or your request handler returning.
  2157. // Do not store references to returned value. Make copies instead.
  2158. func (h *ResponseHeader) Header() []byte {
  2159. h.bufKV.value = h.AppendBytes(h.bufKV.value[:0])
  2160. return h.bufKV.value
  2161. }
  2162. // writeTrailer writes response trailer to w.
  2163. func (h *ResponseHeader) writeTrailer(w *bufio.Writer) error {
  2164. _, err := w.Write(h.TrailerHeader())
  2165. return err
  2166. }
  2167. // TrailerHeader returns response trailer header representation.
  2168. //
  2169. // Trailers will only be received with chunked transfer.
  2170. //
  2171. // The returned value is valid until the request is released,
  2172. // either though ReleaseRequest or your request handler returning.
  2173. // Do not store references to returned value. Make copies instead.
  2174. func (h *ResponseHeader) TrailerHeader() []byte {
  2175. h.bufKV.value = h.bufKV.value[:0]
  2176. for _, t := range h.trailer {
  2177. value := h.peek(t.key)
  2178. h.bufKV.value = appendHeaderLine(h.bufKV.value, t.key, value)
  2179. }
  2180. h.bufKV.value = append(h.bufKV.value, strCRLF...)
  2181. return h.bufKV.value
  2182. }
  2183. // String returns response header representation.
  2184. func (h *ResponseHeader) String() string {
  2185. return string(h.Header())
  2186. }
  2187. // appendStatusLine appends the response status line to dst and returns
  2188. // the extended dst.
  2189. func (h *ResponseHeader) appendStatusLine(dst []byte) []byte {
  2190. statusCode := h.StatusCode()
  2191. if statusCode < 0 {
  2192. statusCode = StatusOK
  2193. }
  2194. return formatStatusLine(dst, h.Protocol(), statusCode, h.StatusMessage())
  2195. }
  2196. // AppendBytes appends response header representation to dst and returns
  2197. // the extended dst.
  2198. func (h *ResponseHeader) AppendBytes(dst []byte) []byte {
  2199. dst = h.appendStatusLine(dst[:0])
  2200. server := h.Server()
  2201. if len(server) != 0 {
  2202. dst = appendHeaderLine(dst, strServer, server)
  2203. }
  2204. if !h.noDefaultDate {
  2205. serverDateOnce.Do(updateServerDate)
  2206. dst = appendHeaderLine(dst, strDate, serverDate.Load().([]byte))
  2207. }
  2208. // Append Content-Type only for non-zero responses
  2209. // or if it is explicitly set.
  2210. // See https://github.com/valyala/fasthttp/issues/28 .
  2211. if h.ContentLength() != 0 || len(h.contentType) > 0 {
  2212. contentType := h.ContentType()
  2213. if len(contentType) > 0 {
  2214. dst = appendHeaderLine(dst, strContentType, contentType)
  2215. }
  2216. }
  2217. contentEncoding := h.ContentEncoding()
  2218. if len(contentEncoding) > 0 {
  2219. dst = appendHeaderLine(dst, strContentEncoding, contentEncoding)
  2220. }
  2221. if len(h.contentLengthBytes) > 0 {
  2222. dst = appendHeaderLine(dst, strContentLength, h.contentLengthBytes)
  2223. }
  2224. for i, n := 0, len(h.h); i < n; i++ {
  2225. kv := &h.h[i]
  2226. // Exclude trailer from header
  2227. exclude := false
  2228. for _, t := range h.trailer {
  2229. if bytes.Equal(kv.key, t.key) {
  2230. exclude = true
  2231. break
  2232. }
  2233. }
  2234. if !exclude && (h.noDefaultDate || !bytes.Equal(kv.key, strDate)) {
  2235. dst = appendHeaderLine(dst, kv.key, kv.value)
  2236. }
  2237. }
  2238. if len(h.trailer) > 0 {
  2239. dst = appendHeaderLine(dst, strTrailer, appendArgsKeyBytes(nil, h.trailer, strCommaSpace))
  2240. }
  2241. n := len(h.cookies)
  2242. if n > 0 {
  2243. for i := 0; i < n; i++ {
  2244. kv := &h.cookies[i]
  2245. dst = appendHeaderLine(dst, strSetCookie, kv.value)
  2246. }
  2247. }
  2248. if h.ConnectionClose() {
  2249. dst = appendHeaderLine(dst, strConnection, strClose)
  2250. }
  2251. return append(dst, strCRLF...)
  2252. }
  2253. // Write writes request header to w.
  2254. func (h *RequestHeader) Write(w *bufio.Writer) error {
  2255. _, err := w.Write(h.Header())
  2256. return err
  2257. }
  2258. // WriteTo writes request header to w.
  2259. //
  2260. // WriteTo implements io.WriterTo interface.
  2261. func (h *RequestHeader) WriteTo(w io.Writer) (int64, error) {
  2262. n, err := w.Write(h.Header())
  2263. return int64(n), err
  2264. }
  2265. // Header returns request header representation.
  2266. //
  2267. // Headers that set as Trailer will not represent. Use TrailerHeader for trailers.
  2268. //
  2269. // The returned value is valid until the request is released,
  2270. // either though ReleaseRequest or your request handler returning.
  2271. // Do not store references to returned value. Make copies instead.
  2272. func (h *RequestHeader) Header() []byte {
  2273. h.bufKV.value = h.AppendBytes(h.bufKV.value[:0])
  2274. return h.bufKV.value
  2275. }
  2276. // writeTrailer writes request trailer to w.
  2277. func (h *RequestHeader) writeTrailer(w *bufio.Writer) error {
  2278. _, err := w.Write(h.TrailerHeader())
  2279. return err
  2280. }
  2281. // TrailerHeader returns request trailer header representation.
  2282. //
  2283. // Trailers will only be received with chunked transfer.
  2284. //
  2285. // The returned value is valid until the request is released,
  2286. // either though ReleaseRequest or your request handler returning.
  2287. // Do not store references to returned value. Make copies instead.
  2288. func (h *RequestHeader) TrailerHeader() []byte {
  2289. h.bufKV.value = h.bufKV.value[:0]
  2290. for _, t := range h.trailer {
  2291. value := h.peek(t.key)
  2292. h.bufKV.value = appendHeaderLine(h.bufKV.value, t.key, value)
  2293. }
  2294. h.bufKV.value = append(h.bufKV.value, strCRLF...)
  2295. return h.bufKV.value
  2296. }
  2297. // RawHeaders returns raw header key/value bytes.
  2298. //
  2299. // Depending on server configuration, header keys may be normalized to
  2300. // capital-case in place.
  2301. //
  2302. // This copy is set aside during parsing, so empty slice is returned for all
  2303. // cases where parsing did not happen. Similarly, request line is not stored
  2304. // during parsing and can not be returned.
  2305. //
  2306. // The slice is not safe to use after the handler returns.
  2307. func (h *RequestHeader) RawHeaders() []byte {
  2308. return h.rawHeaders
  2309. }
  2310. // String returns request header representation.
  2311. func (h *RequestHeader) String() string {
  2312. return string(h.Header())
  2313. }
  2314. // AppendBytes appends request header representation to dst and returns
  2315. // the extended dst.
  2316. func (h *RequestHeader) AppendBytes(dst []byte) []byte {
  2317. dst = append(dst, h.Method()...)
  2318. dst = append(dst, ' ')
  2319. dst = append(dst, h.RequestURI()...)
  2320. dst = append(dst, ' ')
  2321. dst = append(dst, h.Protocol()...)
  2322. dst = append(dst, strCRLF...)
  2323. userAgent := h.UserAgent()
  2324. if len(userAgent) > 0 && !h.disableSpecialHeader {
  2325. dst = appendHeaderLine(dst, strUserAgent, userAgent)
  2326. }
  2327. host := h.Host()
  2328. if len(host) > 0 && !h.disableSpecialHeader {
  2329. dst = appendHeaderLine(dst, strHost, host)
  2330. }
  2331. contentType := h.ContentType()
  2332. if !h.noDefaultContentType && len(contentType) == 0 && !h.ignoreBody() {
  2333. contentType = strDefaultContentType
  2334. }
  2335. if len(contentType) > 0 && !h.disableSpecialHeader {
  2336. dst = appendHeaderLine(dst, strContentType, contentType)
  2337. }
  2338. if len(h.contentLengthBytes) > 0 && !h.disableSpecialHeader {
  2339. dst = appendHeaderLine(dst, strContentLength, h.contentLengthBytes)
  2340. }
  2341. for i, n := 0, len(h.h); i < n; i++ {
  2342. kv := &h.h[i]
  2343. // Exclude trailer from header
  2344. exclude := false
  2345. for _, t := range h.trailer {
  2346. if bytes.Equal(kv.key, t.key) {
  2347. exclude = true
  2348. break
  2349. }
  2350. }
  2351. if !exclude {
  2352. dst = appendHeaderLine(dst, kv.key, kv.value)
  2353. }
  2354. }
  2355. if len(h.trailer) > 0 {
  2356. dst = appendHeaderLine(dst, strTrailer, appendArgsKeyBytes(nil, h.trailer, strCommaSpace))
  2357. }
  2358. // there is no need in h.collectCookies() here, since if cookies aren't collected yet,
  2359. // they all are located in h.h.
  2360. n := len(h.cookies)
  2361. if n > 0 && !h.disableSpecialHeader {
  2362. dst = append(dst, strCookie...)
  2363. dst = append(dst, strColonSpace...)
  2364. dst = appendRequestCookieBytes(dst, h.cookies)
  2365. dst = append(dst, strCRLF...)
  2366. }
  2367. if h.ConnectionClose() && !h.disableSpecialHeader {
  2368. dst = appendHeaderLine(dst, strConnection, strClose)
  2369. }
  2370. return append(dst, strCRLF...)
  2371. }
  2372. func appendHeaderLine(dst, key, value []byte) []byte {
  2373. dst = append(dst, key...)
  2374. dst = append(dst, strColonSpace...)
  2375. dst = append(dst, value...)
  2376. return append(dst, strCRLF...)
  2377. }
  2378. func (h *ResponseHeader) parse(buf []byte) (int, error) {
  2379. m, err := h.parseFirstLine(buf)
  2380. if err != nil {
  2381. return 0, err
  2382. }
  2383. n, err := h.parseHeaders(buf[m:])
  2384. if err != nil {
  2385. return 0, err
  2386. }
  2387. return m + n, nil
  2388. }
  2389. func (h *ResponseHeader) parseTrailer(buf []byte) (int, error) {
  2390. // Skip any 0 length chunk.
  2391. if buf[0] == '0' {
  2392. skip := len(strCRLF) + 1
  2393. if len(buf) < skip {
  2394. return 0, io.EOF
  2395. }
  2396. buf = buf[skip:]
  2397. }
  2398. var s headerScanner
  2399. s.b = buf
  2400. s.disableNormalizing = h.disableNormalizing
  2401. var err error
  2402. for s.next() {
  2403. if len(s.key) > 0 {
  2404. if bytes.IndexByte(s.key, ' ') != -1 || bytes.IndexByte(s.key, '\t') != -1 {
  2405. err = fmt.Errorf("invalid trailer key %q", s.key)
  2406. continue
  2407. }
  2408. // Forbidden by RFC 7230, section 4.1.2
  2409. if isBadTrailer(s.key) {
  2410. err = fmt.Errorf("forbidden trailer key %q", s.key)
  2411. continue
  2412. }
  2413. h.h = appendArgBytes(h.h, s.key, s.value, argsHasValue)
  2414. }
  2415. }
  2416. if s.err != nil {
  2417. return 0, s.err
  2418. }
  2419. if err != nil {
  2420. return 0, err
  2421. }
  2422. return s.hLen, nil
  2423. }
  2424. func (h *RequestHeader) ignoreBody() bool {
  2425. return h.IsGet() || h.IsHead()
  2426. }
  2427. func (h *RequestHeader) parse(buf []byte) (int, error) {
  2428. m, err := h.parseFirstLine(buf)
  2429. if err != nil {
  2430. return 0, err
  2431. }
  2432. h.rawHeaders, _, err = readRawHeaders(h.rawHeaders[:0], buf[m:])
  2433. if err != nil {
  2434. return 0, err
  2435. }
  2436. var n int
  2437. n, err = h.parseHeaders(buf[m:])
  2438. if err != nil {
  2439. return 0, err
  2440. }
  2441. return m + n, nil
  2442. }
  2443. func (h *RequestHeader) parseTrailer(buf []byte) (int, error) {
  2444. // Skip any 0 length chunk.
  2445. if buf[0] == '0' {
  2446. skip := len(strCRLF) + 1
  2447. if len(buf) < skip {
  2448. return 0, io.EOF
  2449. }
  2450. buf = buf[skip:]
  2451. }
  2452. var s headerScanner
  2453. s.b = buf
  2454. s.disableNormalizing = h.disableNormalizing
  2455. var err error
  2456. for s.next() {
  2457. if len(s.key) > 0 {
  2458. if bytes.IndexByte(s.key, ' ') != -1 || bytes.IndexByte(s.key, '\t') != -1 {
  2459. err = fmt.Errorf("invalid trailer key %q", s.key)
  2460. continue
  2461. }
  2462. // Forbidden by RFC 7230, section 4.1.2
  2463. if isBadTrailer(s.key) {
  2464. err = fmt.Errorf("forbidden trailer key %q", s.key)
  2465. continue
  2466. }
  2467. h.h = appendArgBytes(h.h, s.key, s.value, argsHasValue)
  2468. }
  2469. }
  2470. if s.err != nil {
  2471. return 0, s.err
  2472. }
  2473. if err != nil {
  2474. return 0, err
  2475. }
  2476. return s.hLen, nil
  2477. }
  2478. func isBadTrailer(key []byte) bool {
  2479. if len(key) == 0 {
  2480. return true
  2481. }
  2482. switch key[0] | 0x20 {
  2483. case 'a':
  2484. return caseInsensitiveCompare(key, strAuthorization)
  2485. case 'c':
  2486. if len(key) > len(HeaderContentType) && caseInsensitiveCompare(key[:8], strContentType[:8]) {
  2487. // skip compare prefix 'Content-'
  2488. return caseInsensitiveCompare(key[8:], strContentEncoding[8:]) ||
  2489. caseInsensitiveCompare(key[8:], strContentLength[8:]) ||
  2490. caseInsensitiveCompare(key[8:], strContentType[8:]) ||
  2491. caseInsensitiveCompare(key[8:], strContentRange[8:])
  2492. }
  2493. return caseInsensitiveCompare(key, strConnection)
  2494. case 'e':
  2495. return caseInsensitiveCompare(key, strExpect)
  2496. case 'h':
  2497. return caseInsensitiveCompare(key, strHost)
  2498. case 'k':
  2499. return caseInsensitiveCompare(key, strKeepAlive)
  2500. case 'm':
  2501. return caseInsensitiveCompare(key, strMaxForwards)
  2502. case 'p':
  2503. if len(key) > len(HeaderProxyConnection) && caseInsensitiveCompare(key[:6], strProxyConnection[:6]) {
  2504. // skip compare prefix 'Proxy-'
  2505. return caseInsensitiveCompare(key[6:], strProxyConnection[6:]) ||
  2506. caseInsensitiveCompare(key[6:], strProxyAuthenticate[6:]) ||
  2507. caseInsensitiveCompare(key[6:], strProxyAuthorization[6:])
  2508. }
  2509. case 'r':
  2510. return caseInsensitiveCompare(key, strRange)
  2511. case 't':
  2512. return caseInsensitiveCompare(key, strTE) ||
  2513. caseInsensitiveCompare(key, strTrailer) ||
  2514. caseInsensitiveCompare(key, strTransferEncoding)
  2515. case 'w':
  2516. return caseInsensitiveCompare(key, strWWWAuthenticate)
  2517. }
  2518. return false
  2519. }
  2520. func (h *ResponseHeader) parseFirstLine(buf []byte) (int, error) {
  2521. bNext := buf
  2522. var b []byte
  2523. var err error
  2524. for len(b) == 0 {
  2525. if b, bNext, err = nextLine(bNext); err != nil {
  2526. return 0, err
  2527. }
  2528. }
  2529. // parse protocol
  2530. n := bytes.IndexByte(b, ' ')
  2531. if n < 0 {
  2532. if h.secureErrorLogMessage {
  2533. return 0, fmt.Errorf("cannot find whitespace in the first line of response")
  2534. }
  2535. return 0, fmt.Errorf("cannot find whitespace in the first line of response %q", buf)
  2536. }
  2537. h.noHTTP11 = !bytes.Equal(b[:n], strHTTP11)
  2538. b = b[n+1:]
  2539. // parse status code
  2540. h.statusCode, n, err = parseUintBuf(b)
  2541. if err != nil {
  2542. if h.secureErrorLogMessage {
  2543. return 0, fmt.Errorf("cannot parse response status code: %w", err)
  2544. }
  2545. return 0, fmt.Errorf("cannot parse response status code: %w. Response %q", err, buf)
  2546. }
  2547. if len(b) > n && b[n] != ' ' {
  2548. if h.secureErrorLogMessage {
  2549. return 0, fmt.Errorf("unexpected char at the end of status code")
  2550. }
  2551. return 0, fmt.Errorf("unexpected char at the end of status code. Response %q", buf)
  2552. }
  2553. if len(b) > n+1 {
  2554. h.SetStatusMessage(b[n+1:])
  2555. }
  2556. return len(buf) - len(bNext), nil
  2557. }
  2558. func (h *RequestHeader) parseFirstLine(buf []byte) (int, error) {
  2559. bNext := buf
  2560. var b []byte
  2561. var err error
  2562. for len(b) == 0 {
  2563. if b, bNext, err = nextLine(bNext); err != nil {
  2564. return 0, err
  2565. }
  2566. }
  2567. // parse method
  2568. n := bytes.IndexByte(b, ' ')
  2569. if n <= 0 {
  2570. if h.secureErrorLogMessage {
  2571. return 0, fmt.Errorf("cannot find http request method")
  2572. }
  2573. return 0, fmt.Errorf("cannot find http request method in %q", buf)
  2574. }
  2575. h.method = append(h.method[:0], b[:n]...)
  2576. b = b[n+1:]
  2577. // parse requestURI
  2578. n = bytes.LastIndexByte(b, ' ')
  2579. if n < 0 {
  2580. return 0, fmt.Errorf("cannot find whitespace in the first line of request %q", buf)
  2581. } else if n == 0 {
  2582. if h.secureErrorLogMessage {
  2583. return 0, fmt.Errorf("requestURI cannot be empty")
  2584. }
  2585. return 0, fmt.Errorf("requestURI cannot be empty in %q", buf)
  2586. }
  2587. protoStr := b[n+1:]
  2588. // Follow RFCs 7230 and 9112 and require that HTTP versions match the following pattern: HTTP/[0-9]\.[0-9]
  2589. if len(protoStr) != len(strHTTP11) {
  2590. if h.secureErrorLogMessage {
  2591. return 0, fmt.Errorf("unsupported HTTP version %q", protoStr)
  2592. }
  2593. return 0, fmt.Errorf("unsupported HTTP version %q in %q", protoStr, buf)
  2594. }
  2595. if !bytes.HasPrefix(protoStr, strHTTP11[:5]) {
  2596. if h.secureErrorLogMessage {
  2597. return 0, fmt.Errorf("unsupported HTTP version %q", protoStr)
  2598. }
  2599. return 0, fmt.Errorf("unsupported HTTP version %q in %q", protoStr, buf)
  2600. }
  2601. if protoStr[5] < '0' || protoStr[5] > '9' || protoStr[7] < '0' || protoStr[7] > '9' {
  2602. if h.secureErrorLogMessage {
  2603. return 0, fmt.Errorf("unsupported HTTP version %q", protoStr)
  2604. }
  2605. return 0, fmt.Errorf("unsupported HTTP version %q in %q", protoStr, buf)
  2606. }
  2607. h.noHTTP11 = !bytes.Equal(protoStr, strHTTP11)
  2608. h.proto = append(h.proto[:0], protoStr...)
  2609. h.requestURI = append(h.requestURI[:0], b[:n]...)
  2610. return len(buf) - len(bNext), nil
  2611. }
  2612. func readRawHeaders(dst, buf []byte) ([]byte, int, error) {
  2613. n := bytes.IndexByte(buf, nChar)
  2614. if n < 0 {
  2615. return dst[:0], 0, errNeedMore
  2616. }
  2617. if (n == 1 && buf[0] == rChar) || n == 0 {
  2618. // empty headers
  2619. return dst, n + 1, nil
  2620. }
  2621. n++
  2622. b := buf
  2623. m := n
  2624. for {
  2625. b = b[m:]
  2626. m = bytes.IndexByte(b, nChar)
  2627. if m < 0 {
  2628. return dst, 0, errNeedMore
  2629. }
  2630. m++
  2631. n += m
  2632. if (m == 2 && b[0] == rChar) || m == 1 {
  2633. dst = append(dst, buf[:n]...)
  2634. return dst, n, nil
  2635. }
  2636. }
  2637. }
  2638. func (h *ResponseHeader) parseHeaders(buf []byte) (int, error) {
  2639. // 'identity' content-length by default
  2640. h.contentLength = -2
  2641. var s headerScanner
  2642. s.b = buf
  2643. s.disableNormalizing = h.disableNormalizing
  2644. var err error
  2645. var kv *argsKV
  2646. for s.next() {
  2647. if len(s.key) > 0 {
  2648. switch s.key[0] | 0x20 {
  2649. case 'c':
  2650. if caseInsensitiveCompare(s.key, strContentType) {
  2651. h.contentType = append(h.contentType[:0], s.value...)
  2652. continue
  2653. }
  2654. if caseInsensitiveCompare(s.key, strContentEncoding) {
  2655. h.contentEncoding = append(h.contentEncoding[:0], s.value...)
  2656. continue
  2657. }
  2658. if caseInsensitiveCompare(s.key, strContentLength) {
  2659. if h.contentLength != -1 {
  2660. if h.contentLength, err = parseContentLength(s.value); err != nil {
  2661. h.contentLength = -2
  2662. } else {
  2663. h.contentLengthBytes = append(h.contentLengthBytes[:0], s.value...)
  2664. }
  2665. }
  2666. continue
  2667. }
  2668. if caseInsensitiveCompare(s.key, strConnection) {
  2669. if bytes.Equal(s.value, strClose) {
  2670. h.connectionClose = true
  2671. } else {
  2672. h.connectionClose = false
  2673. h.h = appendArgBytes(h.h, s.key, s.value, argsHasValue)
  2674. }
  2675. continue
  2676. }
  2677. case 's':
  2678. if caseInsensitiveCompare(s.key, strServer) {
  2679. h.server = append(h.server[:0], s.value...)
  2680. continue
  2681. }
  2682. if caseInsensitiveCompare(s.key, strSetCookie) {
  2683. h.cookies, kv = allocArg(h.cookies)
  2684. kv.key = getCookieKey(kv.key, s.value)
  2685. kv.value = append(kv.value[:0], s.value...)
  2686. continue
  2687. }
  2688. case 't':
  2689. if caseInsensitiveCompare(s.key, strTransferEncoding) {
  2690. if len(s.value) > 0 && !bytes.Equal(s.value, strIdentity) {
  2691. h.contentLength = -1
  2692. h.h = setArgBytes(h.h, strTransferEncoding, strChunked, argsHasValue)
  2693. }
  2694. continue
  2695. }
  2696. if caseInsensitiveCompare(s.key, strTrailer) {
  2697. err = h.SetTrailerBytes(s.value)
  2698. continue
  2699. }
  2700. }
  2701. h.h = appendArgBytes(h.h, s.key, s.value, argsHasValue)
  2702. }
  2703. }
  2704. if s.err != nil {
  2705. h.connectionClose = true
  2706. return 0, s.err
  2707. }
  2708. if h.contentLength < 0 {
  2709. h.contentLengthBytes = h.contentLengthBytes[:0]
  2710. }
  2711. if h.contentLength == -2 && !h.ConnectionUpgrade() && !h.mustSkipContentLength() {
  2712. h.h = setArgBytes(h.h, strTransferEncoding, strIdentity, argsHasValue)
  2713. h.connectionClose = true
  2714. }
  2715. if h.noHTTP11 && !h.connectionClose {
  2716. // close connection for non-http/1.1 response unless 'Connection: keep-alive' is set.
  2717. v := peekArgBytes(h.h, strConnection)
  2718. h.connectionClose = !hasHeaderValue(v, strKeepAlive)
  2719. }
  2720. return len(buf) - len(s.b), err
  2721. }
  2722. func (h *RequestHeader) parseHeaders(buf []byte) (int, error) {
  2723. h.contentLength = -2
  2724. contentLengthSeen := false
  2725. var s headerScanner
  2726. s.b = buf
  2727. s.disableNormalizing = h.disableNormalizing
  2728. var err error
  2729. for s.next() {
  2730. if len(s.key) > 0 {
  2731. // Spaces between the header key and colon are not allowed.
  2732. // See RFC 7230, Section 3.2.4.
  2733. if bytes.IndexByte(s.key, ' ') != -1 || bytes.IndexByte(s.key, '\t') != -1 {
  2734. err = fmt.Errorf("invalid header key %q", s.key)
  2735. continue
  2736. }
  2737. if h.disableSpecialHeader {
  2738. h.h = appendArgBytes(h.h, s.key, s.value, argsHasValue)
  2739. continue
  2740. }
  2741. switch s.key[0] | 0x20 {
  2742. case 'h':
  2743. if caseInsensitiveCompare(s.key, strHost) {
  2744. h.host = append(h.host[:0], s.value...)
  2745. continue
  2746. }
  2747. case 'u':
  2748. if caseInsensitiveCompare(s.key, strUserAgent) {
  2749. h.userAgent = append(h.userAgent[:0], s.value...)
  2750. continue
  2751. }
  2752. case 'c':
  2753. if caseInsensitiveCompare(s.key, strContentType) {
  2754. h.contentType = append(h.contentType[:0], s.value...)
  2755. continue
  2756. }
  2757. if caseInsensitiveCompare(s.key, strContentLength) {
  2758. if contentLengthSeen {
  2759. return 0, fmt.Errorf("duplicate Content-Length header")
  2760. }
  2761. contentLengthSeen = true
  2762. if h.contentLength != -1 {
  2763. var nerr error
  2764. if h.contentLength, nerr = parseContentLength(s.value); nerr != nil {
  2765. if err == nil {
  2766. err = nerr
  2767. }
  2768. h.contentLength = -2
  2769. } else {
  2770. h.contentLengthBytes = append(h.contentLengthBytes[:0], s.value...)
  2771. }
  2772. }
  2773. continue
  2774. }
  2775. if caseInsensitiveCompare(s.key, strConnection) {
  2776. if bytes.Equal(s.value, strClose) {
  2777. h.connectionClose = true
  2778. } else {
  2779. h.connectionClose = false
  2780. h.h = appendArgBytes(h.h, s.key, s.value, argsHasValue)
  2781. }
  2782. continue
  2783. }
  2784. case 't':
  2785. if caseInsensitiveCompare(s.key, strTransferEncoding) {
  2786. isIdentity := caseInsensitiveCompare(s.value, strIdentity)
  2787. isChunked := caseInsensitiveCompare(s.value, strChunked)
  2788. if !isIdentity && !isChunked {
  2789. if h.secureErrorLogMessage {
  2790. return 0, fmt.Errorf("unsupported Transfer-Encoding")
  2791. }
  2792. return 0, fmt.Errorf("unsupported Transfer-Encoding: %q", s.value)
  2793. }
  2794. if isChunked {
  2795. h.contentLength = -1
  2796. h.h = setArgBytes(h.h, strTransferEncoding, strChunked, argsHasValue)
  2797. }
  2798. continue
  2799. }
  2800. if caseInsensitiveCompare(s.key, strTrailer) {
  2801. if nerr := h.SetTrailerBytes(s.value); nerr != nil {
  2802. if err == nil {
  2803. err = nerr
  2804. }
  2805. }
  2806. continue
  2807. }
  2808. }
  2809. }
  2810. h.h = appendArgBytes(h.h, s.key, s.value, argsHasValue)
  2811. }
  2812. if s.err != nil && err == nil {
  2813. err = s.err
  2814. }
  2815. if err != nil {
  2816. h.connectionClose = true
  2817. return 0, err
  2818. }
  2819. if h.contentLength < 0 {
  2820. h.contentLengthBytes = h.contentLengthBytes[:0]
  2821. }
  2822. if h.noHTTP11 && !h.connectionClose {
  2823. // close connection for non-http/1.1 request unless 'Connection: keep-alive' is set.
  2824. v := peekArgBytes(h.h, strConnection)
  2825. h.connectionClose = !hasHeaderValue(v, strKeepAlive)
  2826. }
  2827. return s.hLen, nil
  2828. }
  2829. func (h *RequestHeader) collectCookies() {
  2830. if h.cookiesCollected {
  2831. return
  2832. }
  2833. for i, n := 0, len(h.h); i < n; i++ {
  2834. kv := &h.h[i]
  2835. if caseInsensitiveCompare(kv.key, strCookie) {
  2836. h.cookies = parseRequestCookies(h.cookies, kv.value)
  2837. tmp := *kv
  2838. copy(h.h[i:], h.h[i+1:])
  2839. n--
  2840. i--
  2841. h.h[n] = tmp
  2842. h.h = h.h[:n]
  2843. }
  2844. }
  2845. h.cookiesCollected = true
  2846. }
  2847. var errNonNumericChars = errors.New("non-numeric chars found")
  2848. func parseContentLength(b []byte) (int, error) {
  2849. v, n, err := parseUintBuf(b)
  2850. if err != nil {
  2851. return -1, fmt.Errorf("cannot parse Content-Length: %w", err)
  2852. }
  2853. if n != len(b) {
  2854. return -1, fmt.Errorf("cannot parse Content-Length: %w", errNonNumericChars)
  2855. }
  2856. return v, nil
  2857. }
  2858. type headerScanner struct {
  2859. b []byte
  2860. key []byte
  2861. value []byte
  2862. err error
  2863. // hLen stores header subslice len
  2864. hLen int
  2865. disableNormalizing bool
  2866. // by checking whether the next line contains a colon or not to tell
  2867. // it's a header entry or a multi line value of current header entry.
  2868. // the side effect of this operation is that we know the index of the
  2869. // next colon and new line, so this can be used during next iteration,
  2870. // instead of find them again.
  2871. nextColon int
  2872. nextNewLine int
  2873. initialized bool
  2874. }
  2875. func (s *headerScanner) next() bool {
  2876. if !s.initialized {
  2877. s.nextColon = -1
  2878. s.nextNewLine = -1
  2879. s.initialized = true
  2880. }
  2881. bLen := len(s.b)
  2882. if bLen >= 2 && s.b[0] == rChar && s.b[1] == nChar {
  2883. s.b = s.b[2:]
  2884. s.hLen += 2
  2885. return false
  2886. }
  2887. if bLen >= 1 && s.b[0] == nChar {
  2888. s.b = s.b[1:]
  2889. s.hLen++
  2890. return false
  2891. }
  2892. var n int
  2893. if s.nextColon >= 0 {
  2894. n = s.nextColon
  2895. s.nextColon = -1
  2896. } else {
  2897. n = bytes.IndexByte(s.b, ':')
  2898. // There can't be a \n inside the header name, check for this.
  2899. x := bytes.IndexByte(s.b, nChar)
  2900. if x < 0 {
  2901. // A header name should always at some point be followed by a \n
  2902. // even if it's the one that terminates the header block.
  2903. s.err = errNeedMore
  2904. return false
  2905. }
  2906. if x < n {
  2907. // There was a \n before the :
  2908. s.err = errInvalidName
  2909. return false
  2910. }
  2911. }
  2912. if n < 0 {
  2913. s.err = errNeedMore
  2914. return false
  2915. }
  2916. s.key = s.b[:n]
  2917. normalizeHeaderKey(s.key, s.disableNormalizing)
  2918. n++
  2919. for len(s.b) > n && s.b[n] == ' ' {
  2920. n++
  2921. // the newline index is a relative index, and lines below trimmed `s.b` by `n`,
  2922. // so the relative newline index also shifted forward. it's safe to decrease
  2923. // to a minus value, it means it's invalid, and will find the newline again.
  2924. s.nextNewLine--
  2925. }
  2926. s.hLen += n
  2927. s.b = s.b[n:]
  2928. if s.nextNewLine >= 0 {
  2929. n = s.nextNewLine
  2930. s.nextNewLine = -1
  2931. } else {
  2932. n = bytes.IndexByte(s.b, nChar)
  2933. }
  2934. if n < 0 {
  2935. s.err = errNeedMore
  2936. return false
  2937. }
  2938. isMultiLineValue := false
  2939. for {
  2940. if n+1 >= len(s.b) {
  2941. break
  2942. }
  2943. if s.b[n+1] != ' ' && s.b[n+1] != '\t' {
  2944. break
  2945. }
  2946. d := bytes.IndexByte(s.b[n+1:], nChar)
  2947. if d <= 0 {
  2948. break
  2949. } else if d == 1 && s.b[n+1] == rChar {
  2950. break
  2951. }
  2952. e := n + d + 1
  2953. if c := bytes.IndexByte(s.b[n+1:e], ':'); c >= 0 {
  2954. s.nextColon = c
  2955. s.nextNewLine = d - c - 1
  2956. break
  2957. }
  2958. isMultiLineValue = true
  2959. n = e
  2960. }
  2961. if n >= len(s.b) {
  2962. s.err = errNeedMore
  2963. return false
  2964. }
  2965. oldB := s.b
  2966. s.value = s.b[:n]
  2967. s.hLen += n + 1
  2968. s.b = s.b[n+1:]
  2969. if n > 0 && s.value[n-1] == rChar {
  2970. n--
  2971. }
  2972. for n > 0 && s.value[n-1] == ' ' {
  2973. n--
  2974. }
  2975. s.value = s.value[:n]
  2976. if isMultiLineValue {
  2977. s.value, s.b, s.hLen = normalizeHeaderValue(s.value, oldB, s.hLen)
  2978. }
  2979. return true
  2980. }
  2981. type headerValueScanner struct {
  2982. b []byte
  2983. value []byte
  2984. }
  2985. func (s *headerValueScanner) next() bool {
  2986. b := s.b
  2987. if len(b) == 0 {
  2988. return false
  2989. }
  2990. n := bytes.IndexByte(b, ',')
  2991. if n < 0 {
  2992. s.value = stripSpace(b)
  2993. s.b = b[len(b):]
  2994. return true
  2995. }
  2996. s.value = stripSpace(b[:n])
  2997. s.b = b[n+1:]
  2998. return true
  2999. }
  3000. func stripSpace(b []byte) []byte {
  3001. for len(b) > 0 && b[0] == ' ' {
  3002. b = b[1:]
  3003. }
  3004. for len(b) > 0 && b[len(b)-1] == ' ' {
  3005. b = b[:len(b)-1]
  3006. }
  3007. return b
  3008. }
  3009. func hasHeaderValue(s, value []byte) bool {
  3010. var vs headerValueScanner
  3011. vs.b = s
  3012. for vs.next() {
  3013. if caseInsensitiveCompare(vs.value, value) {
  3014. return true
  3015. }
  3016. }
  3017. return false
  3018. }
  3019. func nextLine(b []byte) ([]byte, []byte, error) {
  3020. nNext := bytes.IndexByte(b, nChar)
  3021. if nNext < 0 {
  3022. return nil, nil, errNeedMore
  3023. }
  3024. n := nNext
  3025. if n > 0 && b[n-1] == rChar {
  3026. n--
  3027. }
  3028. return b[:n], b[nNext+1:], nil
  3029. }
  3030. func initHeaderKV(kv *argsKV, key, value string, disableNormalizing bool) {
  3031. kv.key = getHeaderKeyBytes(kv, key, disableNormalizing)
  3032. // https://tools.ietf.org/html/rfc7230#section-3.2.4
  3033. kv.value = append(kv.value[:0], value...)
  3034. kv.value = removeNewLines(kv.value)
  3035. }
  3036. func getHeaderKeyBytes(kv *argsKV, key string, disableNormalizing bool) []byte {
  3037. kv.key = append(kv.key[:0], key...)
  3038. normalizeHeaderKey(kv.key, disableNormalizing)
  3039. return kv.key
  3040. }
  3041. func normalizeHeaderValue(ov, ob []byte, headerLength int) (nv, nb []byte, nhl int) {
  3042. nv = ov
  3043. length := len(ov)
  3044. if length <= 0 {
  3045. return
  3046. }
  3047. write := 0
  3048. shrunk := 0
  3049. lineStart := false
  3050. for read := 0; read < length; read++ {
  3051. c := ov[read]
  3052. switch {
  3053. case c == rChar || c == nChar:
  3054. shrunk++
  3055. if c == nChar {
  3056. lineStart = true
  3057. }
  3058. continue
  3059. case lineStart && c == '\t':
  3060. c = ' '
  3061. default:
  3062. lineStart = false
  3063. }
  3064. nv[write] = c
  3065. write++
  3066. }
  3067. nv = nv[:write]
  3068. copy(ob[write:], ob[write+shrunk:])
  3069. // Check if we need to skip \r\n or just \n
  3070. skip := 0
  3071. if ob[write] == rChar {
  3072. if ob[write+1] == nChar {
  3073. skip += 2
  3074. } else {
  3075. skip++
  3076. }
  3077. } else if ob[write] == nChar {
  3078. skip++
  3079. }
  3080. nb = ob[write+skip : len(ob)-shrunk]
  3081. nhl = headerLength - shrunk
  3082. return
  3083. }
  3084. func normalizeHeaderKey(b []byte, disableNormalizing bool) {
  3085. if disableNormalizing {
  3086. return
  3087. }
  3088. n := len(b)
  3089. if n == 0 {
  3090. return
  3091. }
  3092. b[0] = toUpperTable[b[0]]
  3093. for i := 1; i < n; i++ {
  3094. p := &b[i]
  3095. if *p == '-' {
  3096. i++
  3097. if i < n {
  3098. b[i] = toUpperTable[b[i]]
  3099. }
  3100. continue
  3101. }
  3102. *p = toLowerTable[*p]
  3103. }
  3104. }
  3105. // removeNewLines will replace `\r` and `\n` with an empty space.
  3106. func removeNewLines(raw []byte) []byte {
  3107. // check if a `\r` is present and save the position.
  3108. // if no `\r` is found, check if a `\n` is present.
  3109. foundR := bytes.IndexByte(raw, rChar)
  3110. foundN := bytes.IndexByte(raw, nChar)
  3111. start := 0
  3112. switch {
  3113. case foundN != -1:
  3114. if foundR > foundN {
  3115. start = foundN
  3116. } else if foundR != -1 {
  3117. start = foundR
  3118. }
  3119. case foundR != -1:
  3120. start = foundR
  3121. default:
  3122. return raw
  3123. }
  3124. for i := start; i < len(raw); i++ {
  3125. switch raw[i] {
  3126. case rChar, nChar:
  3127. raw[i] = ' '
  3128. default:
  3129. continue
  3130. }
  3131. }
  3132. return raw
  3133. }
  3134. // AppendNormalizedHeaderKey appends normalized header key (name) to dst
  3135. // and returns the resulting dst.
  3136. //
  3137. // Normalized header key starts with uppercase letter. The first letters
  3138. // after dashes are also uppercased. All the other letters are lowercased.
  3139. // Examples:
  3140. //
  3141. // - coNTENT-TYPe -> Content-Type
  3142. // - HOST -> Host
  3143. // - foo-bar-baz -> Foo-Bar-Baz
  3144. func AppendNormalizedHeaderKey(dst []byte, key string) []byte {
  3145. dst = append(dst, key...)
  3146. normalizeHeaderKey(dst[len(dst)-len(key):], false)
  3147. return dst
  3148. }
  3149. // AppendNormalizedHeaderKeyBytes appends normalized header key (name) to dst
  3150. // and returns the resulting dst.
  3151. //
  3152. // Normalized header key starts with uppercase letter. The first letters
  3153. // after dashes are also uppercased. All the other letters are lowercased.
  3154. // Examples:
  3155. //
  3156. // - coNTENT-TYPe -> Content-Type
  3157. // - HOST -> Host
  3158. // - foo-bar-baz -> Foo-Bar-Baz
  3159. func AppendNormalizedHeaderKeyBytes(dst, key []byte) []byte {
  3160. return AppendNormalizedHeaderKey(dst, b2s(key))
  3161. }
  3162. func appendArgsKeyBytes(dst []byte, args []argsKV, sep []byte) []byte {
  3163. for i, n := 0, len(args); i < n; i++ {
  3164. kv := &args[i]
  3165. dst = append(dst, kv.key...)
  3166. if i+1 < n {
  3167. dst = append(dst, sep...)
  3168. }
  3169. }
  3170. return dst
  3171. }
  3172. var (
  3173. errNeedMore = errors.New("need more data: cannot find trailing lf")
  3174. errInvalidName = errors.New("invalid header name")
  3175. errSmallBuffer = errors.New("small read buffer. Increase ReadBufferSize")
  3176. )
  3177. // ErrNothingRead is returned when a keep-alive connection is closed,
  3178. // either because the remote closed it or because of a read timeout.
  3179. type ErrNothingRead struct {
  3180. error
  3181. }
  3182. // ErrSmallBuffer is returned when the provided buffer size is too small
  3183. // for reading request and/or response headers.
  3184. //
  3185. // ReadBufferSize value from Server or clients should reduce the number
  3186. // of such errors.
  3187. type ErrSmallBuffer struct {
  3188. error
  3189. }
  3190. func mustPeekBuffered(r *bufio.Reader) []byte {
  3191. buf, err := r.Peek(r.Buffered())
  3192. if len(buf) == 0 || err != nil {
  3193. panic(fmt.Sprintf("bufio.Reader.Peek() returned unexpected data (%q, %v)", buf, err))
  3194. }
  3195. return buf
  3196. }
  3197. func mustDiscard(r *bufio.Reader, n int) {
  3198. if _, err := r.Discard(n); err != nil {
  3199. panic(fmt.Sprintf("bufio.Reader.Discard(%d) failed: %v", n, err))
  3200. }
  3201. }