header.go 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564
  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 && validHeaderFieldByteTable[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, 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, 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: errors.New("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: errors.New("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: errors.New("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, errors.New("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, errors.New("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, errors.New("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, errors.New("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. outer:
  2647. for s.next() {
  2648. if len(s.key) > 0 {
  2649. for _, ch := range s.key {
  2650. if !validHeaderFieldByte(ch) {
  2651. err = fmt.Errorf("invalid header key %q", s.key)
  2652. continue outer
  2653. }
  2654. }
  2655. switch s.key[0] | 0x20 {
  2656. case 'c':
  2657. if caseInsensitiveCompare(s.key, strContentType) {
  2658. h.contentType = append(h.contentType[:0], s.value...)
  2659. continue
  2660. }
  2661. if caseInsensitiveCompare(s.key, strContentEncoding) {
  2662. h.contentEncoding = append(h.contentEncoding[:0], s.value...)
  2663. continue
  2664. }
  2665. if caseInsensitiveCompare(s.key, strContentLength) {
  2666. if h.contentLength != -1 {
  2667. if h.contentLength, err = parseContentLength(s.value); err != nil {
  2668. h.contentLength = -2
  2669. } else {
  2670. h.contentLengthBytes = append(h.contentLengthBytes[:0], s.value...)
  2671. }
  2672. }
  2673. continue
  2674. }
  2675. if caseInsensitiveCompare(s.key, strConnection) {
  2676. if bytes.Equal(s.value, strClose) {
  2677. h.connectionClose = true
  2678. } else {
  2679. h.connectionClose = false
  2680. h.h = appendArgBytes(h.h, s.key, s.value, argsHasValue)
  2681. }
  2682. continue
  2683. }
  2684. case 's':
  2685. if caseInsensitiveCompare(s.key, strServer) {
  2686. h.server = append(h.server[:0], s.value...)
  2687. continue
  2688. }
  2689. if caseInsensitiveCompare(s.key, strSetCookie) {
  2690. h.cookies, kv = allocArg(h.cookies)
  2691. kv.key = getCookieKey(kv.key, s.value)
  2692. kv.value = append(kv.value[:0], s.value...)
  2693. continue
  2694. }
  2695. case 't':
  2696. if caseInsensitiveCompare(s.key, strTransferEncoding) {
  2697. if len(s.value) > 0 && !bytes.Equal(s.value, strIdentity) {
  2698. h.contentLength = -1
  2699. h.h = setArgBytes(h.h, strTransferEncoding, strChunked, argsHasValue)
  2700. }
  2701. continue
  2702. }
  2703. if caseInsensitiveCompare(s.key, strTrailer) {
  2704. err = h.SetTrailerBytes(s.value)
  2705. continue
  2706. }
  2707. }
  2708. h.h = appendArgBytes(h.h, s.key, s.value, argsHasValue)
  2709. }
  2710. }
  2711. if s.err != nil {
  2712. h.connectionClose = true
  2713. return 0, s.err
  2714. }
  2715. if h.contentLength < 0 {
  2716. h.contentLengthBytes = h.contentLengthBytes[:0]
  2717. }
  2718. if h.contentLength == -2 && !h.ConnectionUpgrade() && !h.mustSkipContentLength() {
  2719. h.h = setArgBytes(h.h, strTransferEncoding, strIdentity, argsHasValue)
  2720. h.connectionClose = true
  2721. }
  2722. if h.noHTTP11 && !h.connectionClose {
  2723. // close connection for non-http/1.1 response unless 'Connection: keep-alive' is set.
  2724. v := peekArgBytes(h.h, strConnection)
  2725. h.connectionClose = !hasHeaderValue(v, strKeepAlive)
  2726. }
  2727. return len(buf) - len(s.b), err
  2728. }
  2729. func (h *RequestHeader) parseHeaders(buf []byte) (int, error) {
  2730. h.contentLength = -2
  2731. contentLengthSeen := false
  2732. var s headerScanner
  2733. s.b = buf
  2734. s.disableNormalizing = h.disableNormalizing
  2735. var err error
  2736. outer:
  2737. for s.next() {
  2738. if len(s.key) > 0 {
  2739. for _, ch := range s.key {
  2740. if !validHeaderFieldByte(ch) {
  2741. err = fmt.Errorf("invalid header key %q", s.key)
  2742. continue outer
  2743. }
  2744. }
  2745. if h.disableSpecialHeader {
  2746. h.h = appendArgBytes(h.h, s.key, s.value, argsHasValue)
  2747. continue
  2748. }
  2749. switch s.key[0] | 0x20 {
  2750. case 'h':
  2751. if caseInsensitiveCompare(s.key, strHost) {
  2752. h.host = append(h.host[:0], s.value...)
  2753. continue
  2754. }
  2755. case 'u':
  2756. if caseInsensitiveCompare(s.key, strUserAgent) {
  2757. h.userAgent = append(h.userAgent[:0], s.value...)
  2758. continue
  2759. }
  2760. case 'c':
  2761. if caseInsensitiveCompare(s.key, strContentType) {
  2762. h.contentType = append(h.contentType[:0], s.value...)
  2763. continue
  2764. }
  2765. if caseInsensitiveCompare(s.key, strContentLength) {
  2766. if contentLengthSeen {
  2767. return 0, errors.New("duplicate Content-Length header")
  2768. }
  2769. contentLengthSeen = true
  2770. if h.contentLength != -1 {
  2771. var nerr error
  2772. if h.contentLength, nerr = parseContentLength(s.value); nerr != nil {
  2773. if err == nil {
  2774. err = nerr
  2775. }
  2776. h.contentLength = -2
  2777. } else {
  2778. h.contentLengthBytes = append(h.contentLengthBytes[:0], s.value...)
  2779. }
  2780. }
  2781. continue
  2782. }
  2783. if caseInsensitiveCompare(s.key, strConnection) {
  2784. if bytes.Equal(s.value, strClose) {
  2785. h.connectionClose = true
  2786. } else {
  2787. h.connectionClose = false
  2788. h.h = appendArgBytes(h.h, s.key, s.value, argsHasValue)
  2789. }
  2790. continue
  2791. }
  2792. case 't':
  2793. if caseInsensitiveCompare(s.key, strTransferEncoding) {
  2794. isIdentity := caseInsensitiveCompare(s.value, strIdentity)
  2795. isChunked := caseInsensitiveCompare(s.value, strChunked)
  2796. if !isIdentity && !isChunked {
  2797. if h.secureErrorLogMessage {
  2798. return 0, errors.New("unsupported Transfer-Encoding")
  2799. }
  2800. return 0, fmt.Errorf("unsupported Transfer-Encoding: %q", s.value)
  2801. }
  2802. if isChunked {
  2803. h.contentLength = -1
  2804. h.h = setArgBytes(h.h, strTransferEncoding, strChunked, argsHasValue)
  2805. }
  2806. continue
  2807. }
  2808. if caseInsensitiveCompare(s.key, strTrailer) {
  2809. if nerr := h.SetTrailerBytes(s.value); nerr != nil {
  2810. if err == nil {
  2811. err = nerr
  2812. }
  2813. }
  2814. continue
  2815. }
  2816. }
  2817. }
  2818. h.h = appendArgBytes(h.h, s.key, s.value, argsHasValue)
  2819. }
  2820. if s.err != nil && err == nil {
  2821. err = s.err
  2822. }
  2823. if err != nil {
  2824. h.connectionClose = true
  2825. return 0, err
  2826. }
  2827. if h.contentLength < 0 {
  2828. h.contentLengthBytes = h.contentLengthBytes[:0]
  2829. }
  2830. if h.noHTTP11 && !h.connectionClose {
  2831. // close connection for non-http/1.1 request unless 'Connection: keep-alive' is set.
  2832. v := peekArgBytes(h.h, strConnection)
  2833. h.connectionClose = !hasHeaderValue(v, strKeepAlive)
  2834. }
  2835. return s.hLen, nil
  2836. }
  2837. func (h *RequestHeader) collectCookies() {
  2838. if h.cookiesCollected {
  2839. return
  2840. }
  2841. for i, n := 0, len(h.h); i < n; i++ {
  2842. kv := &h.h[i]
  2843. if caseInsensitiveCompare(kv.key, strCookie) {
  2844. h.cookies = parseRequestCookies(h.cookies, kv.value)
  2845. tmp := *kv
  2846. copy(h.h[i:], h.h[i+1:])
  2847. n--
  2848. i--
  2849. h.h[n] = tmp
  2850. h.h = h.h[:n]
  2851. }
  2852. }
  2853. h.cookiesCollected = true
  2854. }
  2855. var errNonNumericChars = errors.New("non-numeric chars found")
  2856. func parseContentLength(b []byte) (int, error) {
  2857. v, n, err := parseUintBuf(b)
  2858. if err != nil {
  2859. return -1, fmt.Errorf("cannot parse Content-Length: %w", err)
  2860. }
  2861. if n != len(b) {
  2862. return -1, fmt.Errorf("cannot parse Content-Length: %w", errNonNumericChars)
  2863. }
  2864. return v, nil
  2865. }
  2866. type headerScanner struct {
  2867. b []byte
  2868. key []byte
  2869. value []byte
  2870. err error
  2871. // hLen stores header subslice len
  2872. hLen int
  2873. disableNormalizing bool
  2874. // by checking whether the next line contains a colon or not to tell
  2875. // it's a header entry or a multi line value of current header entry.
  2876. // the side effect of this operation is that we know the index of the
  2877. // next colon and new line, so this can be used during next iteration,
  2878. // instead of find them again.
  2879. nextColon int
  2880. nextNewLine int
  2881. initialized bool
  2882. }
  2883. func (s *headerScanner) next() bool {
  2884. if !s.initialized {
  2885. s.nextColon = -1
  2886. s.nextNewLine = -1
  2887. s.initialized = true
  2888. }
  2889. bLen := len(s.b)
  2890. if bLen >= 2 && s.b[0] == rChar && s.b[1] == nChar {
  2891. s.b = s.b[2:]
  2892. s.hLen += 2
  2893. return false
  2894. }
  2895. if bLen >= 1 && s.b[0] == nChar {
  2896. s.b = s.b[1:]
  2897. s.hLen++
  2898. return false
  2899. }
  2900. var n int
  2901. if s.nextColon >= 0 {
  2902. n = s.nextColon
  2903. s.nextColon = -1
  2904. } else {
  2905. n = bytes.IndexByte(s.b, ':')
  2906. // There can't be a \n inside the header name, check for this.
  2907. x := bytes.IndexByte(s.b, nChar)
  2908. if x < 0 {
  2909. // A header name should always at some point be followed by a \n
  2910. // even if it's the one that terminates the header block.
  2911. s.err = errNeedMore
  2912. return false
  2913. }
  2914. if x < n {
  2915. // There was a \n before the :
  2916. s.err = errInvalidName
  2917. return false
  2918. }
  2919. }
  2920. if n < 0 {
  2921. s.err = errNeedMore
  2922. return false
  2923. }
  2924. s.key = s.b[:n]
  2925. normalizeHeaderKey(s.key, s.disableNormalizing)
  2926. n++
  2927. for len(s.b) > n && s.b[n] == ' ' {
  2928. n++
  2929. // the newline index is a relative index, and lines below trimmed `s.b` by `n`,
  2930. // so the relative newline index also shifted forward. it's safe to decrease
  2931. // to a minus value, it means it's invalid, and will find the newline again.
  2932. s.nextNewLine--
  2933. }
  2934. s.hLen += n
  2935. s.b = s.b[n:]
  2936. if s.nextNewLine >= 0 {
  2937. n = s.nextNewLine
  2938. s.nextNewLine = -1
  2939. } else {
  2940. n = bytes.IndexByte(s.b, nChar)
  2941. }
  2942. if n < 0 {
  2943. s.err = errNeedMore
  2944. return false
  2945. }
  2946. isMultiLineValue := false
  2947. for {
  2948. if n+1 >= len(s.b) {
  2949. break
  2950. }
  2951. if s.b[n+1] != ' ' && s.b[n+1] != '\t' {
  2952. break
  2953. }
  2954. d := bytes.IndexByte(s.b[n+1:], nChar)
  2955. if d <= 0 {
  2956. break
  2957. } else if d == 1 && s.b[n+1] == rChar {
  2958. break
  2959. }
  2960. e := n + d + 1
  2961. if c := bytes.IndexByte(s.b[n+1:e], ':'); c >= 0 {
  2962. s.nextColon = c
  2963. s.nextNewLine = d - c - 1
  2964. break
  2965. }
  2966. isMultiLineValue = true
  2967. n = e
  2968. }
  2969. if n >= len(s.b) {
  2970. s.err = errNeedMore
  2971. return false
  2972. }
  2973. oldB := s.b
  2974. s.value = s.b[:n]
  2975. s.hLen += n + 1
  2976. s.b = s.b[n+1:]
  2977. if n > 0 && s.value[n-1] == rChar {
  2978. n--
  2979. }
  2980. for n > 0 && s.value[n-1] == ' ' {
  2981. n--
  2982. }
  2983. s.value = s.value[:n]
  2984. if isMultiLineValue {
  2985. s.value, s.b, s.hLen = normalizeHeaderValue(s.value, oldB, s.hLen)
  2986. }
  2987. return true
  2988. }
  2989. type headerValueScanner struct {
  2990. b []byte
  2991. value []byte
  2992. }
  2993. func (s *headerValueScanner) next() bool {
  2994. b := s.b
  2995. if len(b) == 0 {
  2996. return false
  2997. }
  2998. n := bytes.IndexByte(b, ',')
  2999. if n < 0 {
  3000. s.value = stripSpace(b)
  3001. s.b = b[len(b):]
  3002. return true
  3003. }
  3004. s.value = stripSpace(b[:n])
  3005. s.b = b[n+1:]
  3006. return true
  3007. }
  3008. func stripSpace(b []byte) []byte {
  3009. for len(b) > 0 && b[0] == ' ' {
  3010. b = b[1:]
  3011. }
  3012. for len(b) > 0 && b[len(b)-1] == ' ' {
  3013. b = b[:len(b)-1]
  3014. }
  3015. return b
  3016. }
  3017. func hasHeaderValue(s, value []byte) bool {
  3018. var vs headerValueScanner
  3019. vs.b = s
  3020. for vs.next() {
  3021. if caseInsensitiveCompare(vs.value, value) {
  3022. return true
  3023. }
  3024. }
  3025. return false
  3026. }
  3027. func nextLine(b []byte) ([]byte, []byte, error) {
  3028. nNext := bytes.IndexByte(b, nChar)
  3029. if nNext < 0 {
  3030. return nil, nil, errNeedMore
  3031. }
  3032. n := nNext
  3033. if n > 0 && b[n-1] == rChar {
  3034. n--
  3035. }
  3036. return b[:n], b[nNext+1:], nil
  3037. }
  3038. func initHeaderKV(kv *argsKV, key, value string, disableNormalizing bool) {
  3039. kv.key = getHeaderKeyBytes(kv, key, disableNormalizing)
  3040. // https://tools.ietf.org/html/rfc7230#section-3.2.4
  3041. kv.value = append(kv.value[:0], value...)
  3042. kv.value = removeNewLines(kv.value)
  3043. }
  3044. func getHeaderKeyBytes(kv *argsKV, key string, disableNormalizing bool) []byte {
  3045. kv.key = append(kv.key[:0], key...)
  3046. normalizeHeaderKey(kv.key, disableNormalizing)
  3047. return kv.key
  3048. }
  3049. func normalizeHeaderValue(ov, ob []byte, headerLength int) (nv, nb []byte, nhl int) {
  3050. nv = ov
  3051. length := len(ov)
  3052. if length <= 0 {
  3053. return
  3054. }
  3055. write := 0
  3056. shrunk := 0
  3057. lineStart := false
  3058. for read := 0; read < length; read++ {
  3059. c := ov[read]
  3060. switch {
  3061. case c == rChar || c == nChar:
  3062. shrunk++
  3063. if c == nChar {
  3064. lineStart = true
  3065. }
  3066. continue
  3067. case lineStart && c == '\t':
  3068. c = ' '
  3069. default:
  3070. lineStart = false
  3071. }
  3072. nv[write] = c
  3073. write++
  3074. }
  3075. nv = nv[:write]
  3076. copy(ob[write:], ob[write+shrunk:])
  3077. // Check if we need to skip \r\n or just \n
  3078. skip := 0
  3079. if ob[write] == rChar {
  3080. if ob[write+1] == nChar {
  3081. skip += 2
  3082. } else {
  3083. skip++
  3084. }
  3085. } else if ob[write] == nChar {
  3086. skip++
  3087. }
  3088. nb = ob[write+skip : len(ob)-shrunk]
  3089. nhl = headerLength - shrunk
  3090. return
  3091. }
  3092. func normalizeHeaderKey(b []byte, disableNormalizing bool) {
  3093. if disableNormalizing {
  3094. return
  3095. }
  3096. n := len(b)
  3097. if n == 0 {
  3098. return
  3099. }
  3100. b[0] = toUpperTable[b[0]]
  3101. for i := 1; i < n; i++ {
  3102. p := &b[i]
  3103. if *p == '-' {
  3104. i++
  3105. if i < n {
  3106. b[i] = toUpperTable[b[i]]
  3107. }
  3108. continue
  3109. }
  3110. *p = toLowerTable[*p]
  3111. }
  3112. }
  3113. // removeNewLines will replace `\r` and `\n` with an empty space.
  3114. func removeNewLines(raw []byte) []byte {
  3115. // check if a `\r` is present and save the position.
  3116. // if no `\r` is found, check if a `\n` is present.
  3117. foundR := bytes.IndexByte(raw, rChar)
  3118. foundN := bytes.IndexByte(raw, nChar)
  3119. start := 0
  3120. switch {
  3121. case foundN != -1:
  3122. if foundR > foundN {
  3123. start = foundN
  3124. } else if foundR != -1 {
  3125. start = foundR
  3126. }
  3127. case foundR != -1:
  3128. start = foundR
  3129. default:
  3130. return raw
  3131. }
  3132. for i := start; i < len(raw); i++ {
  3133. switch raw[i] {
  3134. case rChar, nChar:
  3135. raw[i] = ' '
  3136. default:
  3137. continue
  3138. }
  3139. }
  3140. return raw
  3141. }
  3142. // AppendNormalizedHeaderKey appends normalized header key (name) to dst
  3143. // and returns the resulting dst.
  3144. //
  3145. // Normalized header key starts with uppercase letter. The first letters
  3146. // after dashes are also uppercased. All the other letters are lowercased.
  3147. // Examples:
  3148. //
  3149. // - coNTENT-TYPe -> Content-Type
  3150. // - HOST -> Host
  3151. // - foo-bar-baz -> Foo-Bar-Baz
  3152. func AppendNormalizedHeaderKey(dst []byte, key string) []byte {
  3153. dst = append(dst, key...)
  3154. normalizeHeaderKey(dst[len(dst)-len(key):], false)
  3155. return dst
  3156. }
  3157. // AppendNormalizedHeaderKeyBytes appends normalized header key (name) to dst
  3158. // and returns the resulting dst.
  3159. //
  3160. // Normalized header key starts with uppercase letter. The first letters
  3161. // after dashes are also uppercased. All the other letters are lowercased.
  3162. // Examples:
  3163. //
  3164. // - coNTENT-TYPe -> Content-Type
  3165. // - HOST -> Host
  3166. // - foo-bar-baz -> Foo-Bar-Baz
  3167. func AppendNormalizedHeaderKeyBytes(dst, key []byte) []byte {
  3168. return AppendNormalizedHeaderKey(dst, b2s(key))
  3169. }
  3170. func appendArgsKeyBytes(dst []byte, args []argsKV, sep []byte) []byte {
  3171. for i, n := 0, len(args); i < n; i++ {
  3172. kv := &args[i]
  3173. dst = append(dst, kv.key...)
  3174. if i+1 < n {
  3175. dst = append(dst, sep...)
  3176. }
  3177. }
  3178. return dst
  3179. }
  3180. var (
  3181. errNeedMore = errors.New("need more data: cannot find trailing lf")
  3182. errInvalidName = errors.New("invalid header name")
  3183. errSmallBuffer = errors.New("small read buffer. Increase ReadBufferSize")
  3184. )
  3185. // ErrNothingRead is returned when a keep-alive connection is closed,
  3186. // either because the remote closed it or because of a read timeout.
  3187. type ErrNothingRead struct {
  3188. error
  3189. }
  3190. // ErrSmallBuffer is returned when the provided buffer size is too small
  3191. // for reading request and/or response headers.
  3192. //
  3193. // ReadBufferSize value from Server or clients should reduce the number
  3194. // of such errors.
  3195. type ErrSmallBuffer struct {
  3196. error
  3197. }
  3198. func mustPeekBuffered(r *bufio.Reader) []byte {
  3199. buf, err := r.Peek(r.Buffered())
  3200. if len(buf) == 0 || err != nil {
  3201. panic(fmt.Sprintf("bufio.Reader.Peek() returned unexpected data (%q, %v)", buf, err))
  3202. }
  3203. return buf
  3204. }
  3205. func mustDiscard(r *bufio.Reader, n int) {
  3206. if _, err := r.Discard(n); err != nil {
  3207. panic(fmt.Sprintf("bufio.Reader.Discard(%d) failed: %v", n, err))
  3208. }
  3209. }