| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494 |
- package fasthttp
- import (
- "bufio"
- "bytes"
- "errors"
- "fmt"
- "io"
- "iter"
- "log/slog"
- "sync"
- "sync/atomic"
- "time"
- )
- const (
- rChar = byte('\r')
- nChar = byte('\n')
- )
- type header struct {
- h []argsKV
- cookies []argsKV
- bufK []byte
- bufV []byte
- contentLengthBytes []byte
- contentType []byte
- protocol []byte
- mulHeader [][]byte
- trailer [][]byte
- contentLength int
- disableNormalizing bool
- secureErrorLogMessage bool
- noHTTP11 bool
- connectionClose bool
- noDefaultContentType bool
- }
- // ResponseHeader represents HTTP response header.
- //
- // It is forbidden copying ResponseHeader instances.
- // Create new instances instead and use CopyTo.
- //
- // ResponseHeader instance MUST NOT be used from concurrently running
- // goroutines.
- type ResponseHeader struct {
- noCopy noCopy
- header
- statusMessage []byte
- contentEncoding []byte
- server []byte
- statusCode int
- noDefaultDate bool
- }
- // RequestHeader represents HTTP request header.
- //
- // It is forbidden copying RequestHeader instances.
- // Create new instances instead and use CopyTo.
- //
- // RequestHeader instance MUST NOT be used from concurrently running
- // goroutines.
- type RequestHeader struct {
- noCopy noCopy
- header
- method []byte
- requestURI []byte
- host []byte
- userAgent []byte
- // stores an immutable copy of headers as they were received from the
- // wire.
- rawHeaders []byte
- disableSpecialHeader bool
- cookiesCollected bool
- }
- // SetContentRange sets 'Content-Range: bytes startPos-endPos/contentLength'
- // header.
- func (h *ResponseHeader) SetContentRange(startPos, endPos, contentLength int) {
- b := h.bufV[:0]
- b = append(b, strBytes...)
- b = append(b, ' ')
- b = AppendUint(b, startPos)
- b = append(b, '-')
- b = AppendUint(b, endPos)
- b = append(b, '/')
- b = AppendUint(b, contentLength)
- h.bufV = b
- h.setNonSpecial(strContentRange, h.bufV)
- }
- // SetByteRange sets 'Range: bytes=startPos-endPos' header.
- //
- // - If startPos is negative, then 'bytes=-startPos' value is set.
- // - If endPos is negative, then 'bytes=startPos-' value is set.
- func (h *RequestHeader) SetByteRange(startPos, endPos int) {
- b := h.bufV[:0]
- b = append(b, strBytes...)
- b = append(b, '=')
- if startPos >= 0 {
- b = AppendUint(b, startPos)
- } else {
- endPos = -startPos
- }
- b = append(b, '-')
- if endPos >= 0 {
- b = AppendUint(b, endPos)
- }
- h.bufV = b
- h.setNonSpecial(strRange, h.bufV)
- }
- // StatusCode returns response status code.
- func (h *ResponseHeader) StatusCode() int {
- if h.statusCode == 0 {
- return StatusOK
- }
- return h.statusCode
- }
- // SetStatusCode sets response status code.
- func (h *ResponseHeader) SetStatusCode(statusCode int) {
- h.statusCode = statusCode
- }
- // StatusMessage returns response status message.
- func (h *ResponseHeader) StatusMessage() []byte {
- return h.statusMessage
- }
- // SetStatusMessage sets response status message bytes.
- func (h *ResponseHeader) SetStatusMessage(statusMessage []byte) {
- h.statusMessage = append(h.statusMessage[:0], statusMessage...)
- }
- // SetProtocol sets response protocol bytes.
- func (h *ResponseHeader) SetProtocol(protocol []byte) {
- h.protocol = append(h.protocol[:0], protocol...)
- }
- // SetLastModified sets 'Last-Modified' header to the given value.
- func (h *ResponseHeader) SetLastModified(t time.Time) {
- h.bufV = AppendHTTPDate(h.bufV[:0], t)
- h.setNonSpecial(strLastModified, h.bufV)
- }
- // ConnectionClose returns true if 'Connection: close' header is set.
- func (h *header) ConnectionClose() bool {
- return h.connectionClose
- }
- // SetConnectionClose sets 'Connection: close' header.
- func (h *header) SetConnectionClose() {
- h.connectionClose = true
- }
- // ResetConnectionClose clears 'Connection: close' header if it exists.
- func (h *header) ResetConnectionClose() {
- if h.connectionClose {
- h.connectionClose = false
- h.h = delAllArgs(h.h, HeaderConnection)
- }
- }
- // ConnectionUpgrade returns true if 'Connection: Upgrade' header is set.
- func (h *ResponseHeader) ConnectionUpgrade() bool {
- return hasHeaderValue(h.Peek(HeaderConnection), strUpgrade)
- }
- // ConnectionUpgrade returns true if 'Connection: Upgrade' header is set.
- func (h *RequestHeader) ConnectionUpgrade() bool {
- return hasHeaderValue(h.Peek(HeaderConnection), strUpgrade)
- }
- // PeekCookie is able to returns cookie by a given key from response.
- func (h *ResponseHeader) PeekCookie(key string) []byte {
- return peekArgStr(h.cookies, key)
- }
- // ContentLength returns Content-Length header value.
- //
- // It may be negative:
- // -1 means Transfer-Encoding: chunked.
- // -2 means Transfer-Encoding: identity.
- func (h *header) ContentLength() int {
- return h.contentLength
- }
- // SetContentLength sets Content-Length header value.
- //
- // Content-Length may be negative:
- // -1 means Transfer-Encoding: chunked.
- // -2 means Transfer-Encoding: identity.
- func (h *ResponseHeader) SetContentLength(contentLength int) {
- if h.mustSkipContentLength() {
- return
- }
- h.contentLength = contentLength
- if contentLength >= 0 {
- h.contentLengthBytes = AppendUint(h.contentLengthBytes[:0], contentLength)
- h.h = delAllArgs(h.h, HeaderTransferEncoding)
- return
- } else if contentLength == -1 {
- h.contentLengthBytes = h.contentLengthBytes[:0]
- h.h = setArgBytes(h.h, strTransferEncoding, strChunked, argsHasValue)
- return
- }
- h.SetConnectionClose()
- }
- func (h *ResponseHeader) mustSkipContentLength() bool {
- // From http/1.1 specs:
- // All 1xx (informational), 204 (no content), and 304 (not modified) responses MUST NOT include a message-body
- statusCode := h.StatusCode()
- // Fast path.
- if statusCode < 100 || statusCode == StatusOK {
- return false
- }
- // Slow path.
- return statusCode == StatusNotModified || statusCode == StatusNoContent || statusCode < 200
- }
- // SetContentLength sets Content-Length header value.
- //
- // Negative content-length sets 'Transfer-Encoding: chunked' header.
- func (h *RequestHeader) SetContentLength(contentLength int) {
- h.contentLength = contentLength
- if contentLength >= 0 {
- h.contentLengthBytes = AppendUint(h.contentLengthBytes[:0], contentLength)
- h.h = delAllArgs(h.h, HeaderTransferEncoding)
- } else {
- h.contentLengthBytes = h.contentLengthBytes[:0]
- h.h = setArgBytes(h.h, strTransferEncoding, strChunked, argsHasValue)
- }
- }
- func (h *ResponseHeader) isCompressibleContentType() bool {
- contentType := h.ContentType()
- return bytes.HasPrefix(contentType, strTextSlash) ||
- bytes.HasPrefix(contentType, strApplicationSlash) ||
- bytes.HasPrefix(contentType, strImageSVG) ||
- bytes.HasPrefix(contentType, strImageIcon) ||
- bytes.HasPrefix(contentType, strFontSlash) ||
- bytes.HasPrefix(contentType, strMultipartSlash)
- }
- // ContentType returns Content-Type header value.
- func (h *ResponseHeader) ContentType() []byte {
- contentType := h.contentType
- if !h.noDefaultContentType && len(h.contentType) == 0 {
- contentType = defaultContentType
- }
- return contentType
- }
- // SetContentType sets Content-Type header value.
- func (h *header) SetContentType(contentType string) {
- h.contentType = append(h.contentType[:0], contentType...)
- }
- // SetContentTypeBytes sets Content-Type header value.
- func (h *header) SetContentTypeBytes(contentType []byte) {
- h.contentType = append(h.contentType[:0], contentType...)
- }
- // ContentEncoding returns Content-Encoding header value.
- func (h *ResponseHeader) ContentEncoding() []byte {
- return h.contentEncoding
- }
- // SetContentEncoding sets Content-Encoding header value.
- func (h *ResponseHeader) SetContentEncoding(contentEncoding string) {
- h.contentEncoding = append(h.contentEncoding[:0], contentEncoding...)
- }
- // SetContentEncodingBytes sets Content-Encoding header value.
- func (h *ResponseHeader) SetContentEncodingBytes(contentEncoding []byte) {
- h.contentEncoding = append(h.contentEncoding[:0], contentEncoding...)
- }
- // addVaryBytes add value to the 'Vary' header if it's not included.
- func (h *ResponseHeader) addVaryBytes(value []byte) {
- v := h.peek(strVary)
- if len(v) == 0 {
- // 'Vary' is not set
- h.SetBytesV(HeaderVary, value)
- } else if !bytes.Contains(v, value) {
- // 'Vary' is set and not contains target value
- h.SetBytesV(HeaderVary, append(append(v, ','), value...))
- } // else: 'Vary' is set and contains target value
- }
- // Server returns Server header value.
- func (h *ResponseHeader) Server() []byte {
- return h.server
- }
- // SetServer sets Server header value.
- func (h *ResponseHeader) SetServer(server string) {
- h.server = append(h.server[:0], server...)
- }
- // SetServerBytes sets Server header value.
- func (h *ResponseHeader) SetServerBytes(server []byte) {
- h.server = append(h.server[:0], server...)
- }
- // ContentType returns Content-Type header value.
- func (h *RequestHeader) ContentType() []byte {
- if h.disableSpecialHeader {
- return peekArgBytes(h.h, []byte(HeaderContentType))
- }
- return h.contentType
- }
- // ContentEncoding returns Content-Encoding header value.
- func (h *RequestHeader) ContentEncoding() []byte {
- return peekArgBytes(h.h, strContentEncoding)
- }
- // SetContentEncoding sets Content-Encoding header value.
- func (h *RequestHeader) SetContentEncoding(contentEncoding string) {
- h.SetBytesK(strContentEncoding, contentEncoding)
- }
- // SetContentEncodingBytes sets Content-Encoding header value.
- func (h *RequestHeader) SetContentEncodingBytes(contentEncoding []byte) {
- h.setNonSpecial(strContentEncoding, contentEncoding)
- }
- // SetMultipartFormBoundary sets the following Content-Type:
- // 'multipart/form-data; boundary=...'
- // where ... is substituted by the given boundary.
- func (h *RequestHeader) SetMultipartFormBoundary(boundary string) {
- b := h.bufV[:0]
- b = append(b, strMultipartFormData...)
- b = append(b, ';', ' ')
- b = append(b, strBoundary...)
- b = append(b, '=')
- b = append(b, boundary...)
- h.bufV = b
- h.SetContentTypeBytes(h.bufV)
- }
- // SetMultipartFormBoundaryBytes sets the following Content-Type:
- // 'multipart/form-data; boundary=...'
- // where ... is substituted by the given boundary.
- func (h *RequestHeader) SetMultipartFormBoundaryBytes(boundary []byte) {
- b := h.bufV[:0]
- b = append(b, strMultipartFormData...)
- b = append(b, ';', ' ')
- b = append(b, strBoundary...)
- b = append(b, '=')
- b = append(b, boundary...)
- h.bufV = b
- h.SetContentTypeBytes(h.bufV)
- }
- // SetTrailer sets header Trailer value for chunked response
- // to indicate which headers will be sent after the body.
- //
- // Use Set to set the trailer header later.
- //
- // Trailers are only supported with chunked transfer.
- // Trailers allow the sender to include additional headers at the end of chunked messages.
- //
- // The following trailers are forbidden:
- // 1. necessary for message framing (e.g., Transfer-Encoding and Content-Length),
- // 2. routing (e.g., Host),
- // 3. request modifiers (e.g., controls and conditionals in Section 5 of [RFC7231]),
- // 4. authentication (e.g., see [RFC7235] and [RFC6265]),
- // 5. response control data (e.g., see Section 7.1 of [RFC7231]),
- // 6. determining how to process the payload (e.g., Content-Encoding, Content-Type, Content-Range, and Trailer)
- //
- // Return ErrBadTrailer if contain any forbidden trailers.
- func (h *header) SetTrailer(trailer string) error {
- return h.SetTrailerBytes(s2b(trailer))
- }
- // SetTrailerBytes sets Trailer header value for chunked response
- // to indicate which headers will be sent after the body.
- //
- // Use Set to set the trailer header later.
- //
- // Trailers are only supported with chunked transfer.
- // Trailers allow the sender to include additional headers at the end of chunked messages.
- //
- // The following trailers are forbidden:
- // 1. necessary for message framing (e.g., Transfer-Encoding and Content-Length),
- // 2. routing (e.g., Host),
- // 3. request modifiers (e.g., controls and conditionals in Section 5 of [RFC7231]),
- // 4. authentication (e.g., see [RFC7235] and [RFC6265]),
- // 5. response control data (e.g., see Section 7.1 of [RFC7231]),
- // 6. determining how to process the payload (e.g., Content-Encoding, Content-Type, Content-Range, and Trailer)
- //
- // Return ErrBadTrailer if contain any forbidden trailers.
- func (h *header) SetTrailerBytes(trailer []byte) error {
- h.trailer = h.trailer[:0]
- return h.AddTrailerBytes(trailer)
- }
- // AddTrailer add Trailer header value for chunked response
- // to indicate which headers will be sent after the body.
- //
- // Use Set to set the trailer header later.
- //
- // Trailers are only supported with chunked transfer.
- // Trailers allow the sender to include additional headers at the end of chunked messages.
- //
- // The following trailers are forbidden:
- // 1. necessary for message framing (e.g., Transfer-Encoding and Content-Length),
- // 2. routing (e.g., Host),
- // 3. request modifiers (e.g., controls and conditionals in Section 5 of [RFC7231]),
- // 4. authentication (e.g., see [RFC7235] and [RFC6265]),
- // 5. response control data (e.g., see Section 7.1 of [RFC7231]),
- // 6. determining how to process the payload (e.g., Content-Encoding, Content-Type, Content-Range, and Trailer)
- //
- // Return ErrBadTrailer if contain any forbidden trailers.
- func (h *header) AddTrailer(trailer string) error {
- return h.AddTrailerBytes(s2b(trailer))
- }
- var ErrBadTrailer = errors.New("contain forbidden trailer")
- // AddTrailerBytes add Trailer header value for chunked response
- // to indicate which headers will be sent after the body.
- //
- // Use Set to set the trailer header later.
- //
- // Trailers are only supported with chunked transfer.
- // Trailers allow the sender to include additional headers at the end of chunked messages.
- //
- // The following trailers are forbidden:
- // 1. necessary for message framing (e.g., Transfer-Encoding and Content-Length),
- // 2. routing (e.g., Host),
- // 3. request modifiers (e.g., controls and conditionals in Section 5 of [RFC7231]),
- // 4. authentication (e.g., see [RFC7235] and [RFC6265]),
- // 5. response control data (e.g., see Section 7.1 of [RFC7231]),
- // 6. determining how to process the payload (e.g., Content-Encoding, Content-Type, Content-Range, and Trailer)
- //
- // Return ErrBadTrailer if contain any forbidden trailers.
- func (h *header) AddTrailerBytes(trailer []byte) (err error) {
- for i := -1; i+1 < len(trailer); {
- trailer = trailer[i+1:]
- i = bytes.IndexByte(trailer, ',')
- if i < 0 {
- i = len(trailer)
- }
- key := trailer[:i]
- for len(key) > 0 && key[0] == ' ' {
- key = key[1:]
- }
- for len(key) > 0 && key[len(key)-1] == ' ' {
- key = key[:len(key)-1]
- }
- // Forbidden by RFC 7230, section 4.1.2
- if isBadTrailer(key) {
- err = ErrBadTrailer
- continue
- }
- h.bufK = append(h.bufK[:0], key...)
- normalizeHeaderKey(h.bufK, h.disableNormalizing || bytes.IndexByte(h.bufK, ' ') != -1)
- if cap(h.trailer) > len(h.trailer) {
- h.trailer = h.trailer[:len(h.trailer)+1]
- h.trailer[len(h.trailer)-1] = append(h.trailer[len(h.trailer)-1][:0], h.bufK...)
- } else {
- key = make([]byte, len(h.bufK))
- copy(key, h.bufK)
- h.trailer = append(h.trailer, key)
- }
- }
- return err
- }
- // validHeaderFieldByte returns true if c valid header field byte
- // as defined by RFC 7230.
- func validHeaderFieldByte(c byte) bool {
- return c < 128 && validHeaderFieldByteTable[c] == 1
- }
- // validHeaderValueByte returns true if c valid header value byte
- // as defined by RFC 7230.
- func validHeaderValueByte(c byte) bool {
- return validHeaderValueByteTable[c] == 1
- }
- // VisitHeaderParams calls f for each parameter in the given header bytes.
- // It stops processing when f returns false or an invalid parameter is found.
- // Parameter values may be quoted, in which case \ is treated as an escape
- // character, and the value is unquoted before being passed to value.
- // See: https://www.rfc-editor.org/rfc/rfc9110#section-5.6.6
- //
- // f must not retain references to key and/or value after returning.
- // Copy key and/or value contents before returning if you need retaining them.
- func VisitHeaderParams(b []byte, f func(key, value []byte) bool) {
- for len(b) > 0 {
- idxSemi := 0
- for idxSemi < len(b) && b[idxSemi] != ';' {
- idxSemi++
- }
- if idxSemi >= len(b) {
- return
- }
- b = b[idxSemi+1:]
- for len(b) > 0 && b[0] == ' ' {
- b = b[1:]
- }
- n := 0
- if len(b) == 0 || !validHeaderFieldByte(b[n]) {
- return
- }
- n++
- for n < len(b) && validHeaderFieldByte(b[n]) {
- n++
- }
- if n >= len(b)-1 || b[n] != '=' {
- return
- }
- param := b[:n]
- n++
- switch {
- case validHeaderFieldByte(b[n]):
- m := n
- n++
- for n < len(b) && validHeaderFieldByte(b[n]) {
- n++
- }
- if !f(param, b[m:n]) {
- return
- }
- case b[n] == '"':
- foundEndQuote := false
- escaping := false
- n++
- m := n
- for ; n < len(b); n++ {
- if b[n] == '"' && !escaping {
- foundEndQuote = true
- break
- }
- escaping = (b[n] == '\\' && !escaping)
- }
- if !foundEndQuote {
- return
- }
- if !f(param, b[m:n]) {
- return
- }
- n++
- default:
- return
- }
- b = b[n:]
- }
- }
- // MultipartFormBoundary returns boundary part
- // from 'multipart/form-data; boundary=...' Content-Type.
- func (h *RequestHeader) MultipartFormBoundary() []byte {
- b := h.ContentType()
- if !bytes.HasPrefix(b, strMultipartFormData) {
- return nil
- }
- b = b[len(strMultipartFormData):]
- if len(b) == 0 || b[0] != ';' {
- return nil
- }
- var n int
- for len(b) > 0 {
- n++
- for len(b) > n && b[n] == ' ' {
- n++
- }
- b = b[n:]
- if !bytes.HasPrefix(b, strBoundary) {
- if n = bytes.IndexByte(b, ';'); n < 0 {
- return nil
- }
- continue
- }
- b = b[len(strBoundary):]
- if len(b) == 0 || b[0] != '=' {
- return nil
- }
- b = b[1:]
- if n = bytes.IndexByte(b, ';'); n >= 0 {
- b = b[:n]
- }
- if len(b) > 1 && b[0] == '"' && b[len(b)-1] == '"' {
- b = b[1 : len(b)-1]
- }
- return b
- }
- return nil
- }
- // Host returns Host header value.
- func (h *RequestHeader) Host() []byte {
- if h.disableSpecialHeader {
- return peekArgBytes(h.h, []byte(HeaderHost))
- }
- return h.host
- }
- // SetHost sets Host header value.
- func (h *RequestHeader) SetHost(host string) {
- h.host = append(h.host[:0], host...)
- }
- // SetHostBytes sets Host header value.
- func (h *RequestHeader) SetHostBytes(host []byte) {
- h.host = append(h.host[:0], host...)
- }
- // UserAgent returns User-Agent header value.
- func (h *RequestHeader) UserAgent() []byte {
- if h.disableSpecialHeader {
- return peekArgBytes(h.h, []byte(HeaderUserAgent))
- }
- return h.userAgent
- }
- // SetUserAgent sets User-Agent header value.
- func (h *RequestHeader) SetUserAgent(userAgent string) {
- h.userAgent = append(h.userAgent[:0], userAgent...)
- }
- // SetUserAgentBytes sets User-Agent header value.
- func (h *RequestHeader) SetUserAgentBytes(userAgent []byte) {
- h.userAgent = append(h.userAgent[:0], userAgent...)
- }
- // Referer returns Referer header value.
- func (h *RequestHeader) Referer() []byte {
- return peekArgBytes(h.h, strReferer)
- }
- // SetReferer sets Referer header value.
- func (h *RequestHeader) SetReferer(referer string) {
- h.SetBytesK(strReferer, referer)
- }
- // SetRefererBytes sets Referer header value.
- func (h *RequestHeader) SetRefererBytes(referer []byte) {
- h.setNonSpecial(strReferer, referer)
- }
- // Method returns HTTP request method.
- func (h *RequestHeader) Method() []byte {
- if len(h.method) == 0 {
- return []byte(MethodGet)
- }
- return h.method
- }
- // SetMethod sets HTTP request method.
- func (h *RequestHeader) SetMethod(method string) {
- h.method = append(h.method[:0], method...)
- }
- // SetMethodBytes sets HTTP request method.
- func (h *RequestHeader) SetMethodBytes(method []byte) {
- h.method = append(h.method[:0], method...)
- }
- // Protocol returns HTTP protocol.
- func (h *header) Protocol() []byte {
- if len(h.protocol) == 0 {
- return strHTTP11
- }
- return h.protocol
- }
- // SetProtocol sets HTTP request protocol.
- func (h *RequestHeader) SetProtocol(protocol string) {
- h.protocol = append(h.protocol[:0], protocol...)
- h.noHTTP11 = !bytes.Equal(h.protocol, strHTTP11)
- }
- // SetProtocolBytes sets HTTP request protocol.
- func (h *RequestHeader) SetProtocolBytes(protocol []byte) {
- h.protocol = append(h.protocol[:0], protocol...)
- h.noHTTP11 = !bytes.Equal(h.protocol, strHTTP11)
- }
- // RequestURI returns RequestURI from the first HTTP request line.
- func (h *RequestHeader) RequestURI() []byte {
- requestURI := h.requestURI
- if len(requestURI) == 0 {
- requestURI = strSlash
- }
- return requestURI
- }
- // SetRequestURI sets RequestURI for the first HTTP request line.
- // RequestURI must be properly encoded.
- // Use URI.RequestURI for constructing proper RequestURI if unsure.
- func (h *RequestHeader) SetRequestURI(requestURI string) {
- h.requestURI = append(h.requestURI[:0], requestURI...)
- }
- // SetRequestURIBytes sets RequestURI for the first HTTP request line.
- // RequestURI must be properly encoded.
- // Use URI.RequestURI for constructing proper RequestURI if unsure.
- func (h *RequestHeader) SetRequestURIBytes(requestURI []byte) {
- h.requestURI = append(h.requestURI[:0], requestURI...)
- }
- // IsGet returns true if request method is GET.
- func (h *RequestHeader) IsGet() bool {
- return string(h.Method()) == MethodGet
- }
- // IsPost returns true if request method is POST.
- func (h *RequestHeader) IsPost() bool {
- return string(h.Method()) == MethodPost
- }
- // IsPut returns true if request method is PUT.
- func (h *RequestHeader) IsPut() bool {
- return string(h.Method()) == MethodPut
- }
- // IsHead returns true if request method is HEAD.
- func (h *RequestHeader) IsHead() bool {
- return string(h.Method()) == MethodHead
- }
- // IsDelete returns true if request method is DELETE.
- func (h *RequestHeader) IsDelete() bool {
- return string(h.Method()) == MethodDelete
- }
- // IsConnect returns true if request method is CONNECT.
- func (h *RequestHeader) IsConnect() bool {
- return string(h.Method()) == MethodConnect
- }
- // IsOptions returns true if request method is OPTIONS.
- func (h *RequestHeader) IsOptions() bool {
- return string(h.Method()) == MethodOptions
- }
- // IsTrace returns true if request method is TRACE.
- func (h *RequestHeader) IsTrace() bool {
- return string(h.Method()) == MethodTrace
- }
- // IsPatch returns true if request method is PATCH.
- func (h *RequestHeader) IsPatch() bool {
- return string(h.Method()) == MethodPatch
- }
- // IsHTTP11 returns true if the header is HTTP/1.1.
- func (h *header) IsHTTP11() bool {
- return !h.noHTTP11
- }
- // HasAcceptEncoding returns true if the header contains
- // the given Accept-Encoding value.
- func (h *RequestHeader) HasAcceptEncoding(acceptEncoding string) bool {
- h.bufV = append(h.bufV[:0], acceptEncoding...)
- return h.HasAcceptEncodingBytes(h.bufV)
- }
- // HasAcceptEncodingBytes returns true if the header contains
- // the given Accept-Encoding value.
- func (h *RequestHeader) HasAcceptEncodingBytes(acceptEncoding []byte) bool {
- ae := h.peek(strAcceptEncoding)
- n := bytes.Index(ae, acceptEncoding)
- if n < 0 {
- return false
- }
- b := ae[n+len(acceptEncoding):]
- if len(b) > 0 && b[0] != ',' {
- return false
- }
- if n == 0 {
- return true
- }
- return ae[n-1] == ' '
- }
- // Len returns the number of headers set,
- // i.e. the number of times f is called in VisitAll.
- func (h *ResponseHeader) Len() int {
- n := 0
- for range h.All() {
- n++
- }
- return n
- }
- // Len returns the number of headers set,
- // i.e. the number of times f is called in VisitAll.
- func (h *RequestHeader) Len() int {
- n := 0
- for range h.All() {
- n++
- }
- return n
- }
- // DisableSpecialHeader disables special header processing.
- // fasthttp will not set any special headers for you, such as Host, Content-Type, User-Agent, etc.
- // You must set everything yourself.
- // If RequestHeader.Read() is called, special headers will be ignored.
- // This can be used to control case and order of special headers.
- // This is generally not recommended.
- func (h *RequestHeader) DisableSpecialHeader() {
- h.disableSpecialHeader = true
- }
- // EnableSpecialHeader enables special header processing.
- // fasthttp will send Host, Content-Type, User-Agent, etc headers for you.
- // This is suggested and enabled by default.
- func (h *RequestHeader) EnableSpecialHeader() {
- h.disableSpecialHeader = false
- }
- // DisableNormalizing disables header names' normalization.
- //
- // By default all the header names are normalized by uppercasing
- // the first letter and all the first letters following dashes,
- // while lowercasing all the other letters.
- // Examples:
- //
- // - CONNECTION -> Connection
- // - conteNT-tYPE -> Content-Type
- // - foo-bar-baz -> Foo-Bar-Baz
- //
- // Disable header names' normalization only if know what are you doing.
- func (h *header) DisableNormalizing() {
- h.disableNormalizing = true
- }
- // EnableNormalizing enables header names' normalization.
- //
- // Header names are normalized by uppercasing the first letter and
- // all the first letters following dashes, while lowercasing all
- // the other letters.
- // Examples:
- //
- // - CONNECTION -> Connection
- // - conteNT-tYPE -> Content-Type
- // - foo-bar-baz -> Foo-Bar-Baz
- //
- // This is enabled by default unless disabled using DisableNormalizing().
- func (h *header) EnableNormalizing() {
- h.disableNormalizing = false
- }
- // SetNoDefaultContentType allows you to control if a default Content-Type header will be set (false) or not (true).
- func (h *header) SetNoDefaultContentType(noDefaultContentType bool) {
- h.noDefaultContentType = noDefaultContentType
- }
- // Reset clears response header.
- func (h *ResponseHeader) Reset() {
- h.disableNormalizing = false
- h.SetNoDefaultContentType(false)
- h.noDefaultDate = false
- h.resetSkipNormalize()
- }
- func (h *ResponseHeader) resetSkipNormalize() {
- h.noHTTP11 = false
- h.connectionClose = false
- h.statusCode = 0
- h.statusMessage = h.statusMessage[:0]
- h.protocol = h.protocol[:0]
- h.contentLength = 0
- h.contentLengthBytes = h.contentLengthBytes[:0]
- h.contentType = h.contentType[:0]
- h.contentEncoding = h.contentEncoding[:0]
- h.server = h.server[:0]
- h.h = h.h[:0]
- h.cookies = h.cookies[:0]
- h.trailer = h.trailer[:0]
- h.mulHeader = h.mulHeader[:0]
- }
- // Reset clears request header.
- func (h *RequestHeader) Reset() {
- h.disableSpecialHeader = false
- h.disableNormalizing = false
- h.SetNoDefaultContentType(false)
- h.resetSkipNormalize()
- }
- func (h *RequestHeader) resetSkipNormalize() {
- h.noHTTP11 = false
- h.connectionClose = false
- h.contentLength = 0
- h.contentLengthBytes = h.contentLengthBytes[:0]
- h.method = h.method[:0]
- h.protocol = h.protocol[:0]
- h.requestURI = h.requestURI[:0]
- h.host = h.host[:0]
- h.contentType = h.contentType[:0]
- h.userAgent = h.userAgent[:0]
- h.trailer = h.trailer[:0]
- h.mulHeader = h.mulHeader[:0]
- h.h = h.h[:0]
- h.cookies = h.cookies[:0]
- h.cookiesCollected = false
- h.rawHeaders = h.rawHeaders[:0]
- }
- func (h *header) copyTo(dst *header) {
- dst.disableNormalizing = h.disableNormalizing
- dst.noHTTP11 = h.noHTTP11
- dst.connectionClose = h.connectionClose
- dst.noDefaultContentType = h.noDefaultContentType
- dst.contentLength = h.contentLength
- dst.contentLengthBytes = append(dst.contentLengthBytes, h.contentLengthBytes...)
- dst.protocol = append(dst.protocol, h.protocol...)
- dst.contentType = append(dst.contentType, h.contentType...)
- dst.trailer = copyTrailer(dst.trailer, h.trailer)
- dst.cookies = copyArgs(dst.cookies, h.cookies)
- dst.h = copyArgs(dst.h, h.h)
- }
- // CopyTo copies all the headers to dst.
- func (h *ResponseHeader) CopyTo(dst *ResponseHeader) {
- dst.Reset()
- h.copyTo(&dst.header)
- dst.noDefaultDate = h.noDefaultDate
- dst.statusCode = h.statusCode
- dst.statusMessage = append(dst.statusMessage, h.statusMessage...)
- dst.contentEncoding = append(dst.contentEncoding, h.contentEncoding...)
- dst.server = append(dst.server, h.server...)
- }
- // CopyTo copies all the headers to dst.
- func (h *RequestHeader) CopyTo(dst *RequestHeader) {
- dst.Reset()
- h.copyTo(&dst.header)
- dst.method = append(dst.method, h.method...)
- dst.requestURI = append(dst.requestURI, h.requestURI...)
- dst.host = append(dst.host, h.host...)
- dst.userAgent = append(dst.userAgent, h.userAgent...)
- dst.cookiesCollected = h.cookiesCollected
- dst.rawHeaders = append(dst.rawHeaders, h.rawHeaders...)
- }
- // All returns an iterator over key-value pairs in h.
- //
- // The key and value may invalid outside the iteration loop.
- // Copy key and/or value contents for each iteration if you need retaining
- // them.
- func (h *ResponseHeader) All() iter.Seq2[[]byte, []byte] {
- return func(yield func([]byte, []byte) bool) {
- if len(h.contentLengthBytes) > 0 && !yield(strContentLength, h.contentLengthBytes) {
- return
- }
- if contentType := h.ContentType(); len(contentType) > 0 && !yield(strContentType, contentType) {
- return
- }
- if contentEncoding := h.ContentEncoding(); len(contentEncoding) > 0 && !yield(strContentEncoding, contentEncoding) {
- return
- }
- if server := h.Server(); len(server) > 0 && !yield(strServer, server) {
- return
- }
- for i := range h.cookies {
- if !yield(strSetCookie, h.cookies[i].value) {
- return
- }
- }
- if len(h.trailer) > 0 && !yield(strTrailer, appendTrailerBytes(nil, h.trailer, strCommaSpace)) {
- return
- }
- for i := range h.h {
- if !yield(h.h[i].key, h.h[i].value) {
- return
- }
- }
- if h.ConnectionClose() && !yield(strConnection, strClose) {
- return
- }
- }
- }
- // VisitAll calls f for each header.
- //
- // f must not retain references to key and/or value after returning.
- // Copy key and/or value contents before returning if you need retaining them.
- //
- // Deprecated: Use All instead.
- func (h *ResponseHeader) VisitAll(f func(key, value []byte)) {
- h.All()(func(key, value []byte) bool {
- f(key, value)
- return true
- })
- }
- // Trailers returns an iterator over trailers in h.
- //
- // The value of trailer may invalid outside the iteration loop.
- func (h *header) Trailers() iter.Seq[[]byte] {
- return func(yield func([]byte) bool) {
- for i := range h.trailer {
- if !yield(h.trailer[i]) {
- break
- }
- }
- }
- }
- // VisitAllTrailer calls f for each response Trailer.
- //
- // f must not retain references to value after returning.
- //
- // Deprecated: Use Trailers instead.
- func (h *header) VisitAllTrailer(f func(value []byte)) {
- h.Trailers()(func(v []byte) bool {
- f(v)
- return true
- })
- }
- // Cookies returns an iterator over key-value paired response cookie in h.
- //
- // Cookie name is passed in key and the whole Set-Cookie header value
- // is passed in value for each iteration. Value may be parsed with
- // Cookie.ParseBytes().
- //
- // The key and value may invalid outside the iteration loop.
- // Copy key and/or value contents for each iteration if you need retaining
- // them.
- func (h *ResponseHeader) Cookies() iter.Seq2[[]byte, []byte] {
- return func(yield func([]byte, []byte) bool) {
- for i := range h.cookies {
- if !yield(h.cookies[i].key, h.cookies[i].value) {
- break
- }
- }
- }
- }
- // VisitAllCookie calls f for each response cookie.
- //
- // Cookie name is passed in key and the whole Set-Cookie header value
- // is passed in value on each f invocation. Value may be parsed
- // with Cookie.ParseBytes().
- //
- // f must not retain references to key and/or value after returning.
- //
- // Deprecated: Use Cookies instead.
- func (h *ResponseHeader) VisitAllCookie(f func(key, value []byte)) {
- h.Cookies()(func(key, value []byte) bool {
- f(key, value)
- return true
- })
- }
- // Cookies returns an iterator over key-value pairs request cookie in h.
- //
- // The key and value may invalid outside the iteration loop.
- // Copy key and/or value contents for each iteration if you need retaining
- // them.
- func (h *RequestHeader) Cookies() iter.Seq2[[]byte, []byte] {
- return func(yield func([]byte, []byte) bool) {
- h.collectCookies()
- for i := range h.cookies {
- if !yield(h.cookies[i].key, h.cookies[i].value) {
- break
- }
- }
- }
- }
- // VisitAllCookie calls f for each request cookie.
- //
- // f must not retain references to key and/or value after returning.
- //
- // Deprecated: Use Cookies instead.
- func (h *RequestHeader) VisitAllCookie(f func(key, value []byte)) {
- h.Cookies()(func(key, value []byte) bool {
- f(key, value)
- return true
- })
- }
- // All returns an iterator over key-value pairs in h.
- //
- // The key and value may invalid outside the iteration loop.
- // Copy key and/or value contents for each iteration if you need retaining
- // them.
- //
- // To get the headers in order they were received use AllInOrder.
- func (h *RequestHeader) All() iter.Seq2[[]byte, []byte] {
- return func(yield func([]byte, []byte) bool) {
- if host := h.Host(); len(host) > 0 && !yield(strHost, host) {
- return
- }
- if len(h.contentLengthBytes) > 0 && !yield(strContentLength, h.contentLengthBytes) {
- return
- }
- if contentType := h.ContentType(); len(contentType) > 0 && !yield(strContentType, contentType) {
- return
- }
- if userAgent := h.UserAgent(); len(userAgent) > 0 && !yield(strUserAgent, userAgent) {
- return
- }
- if len(h.trailer) > 0 && !yield(strTrailer, appendTrailerBytes(nil, h.trailer, strCommaSpace)) {
- return
- }
- h.collectCookies()
- if len(h.cookies) > 0 {
- h.bufV = appendRequestCookieBytes(h.bufV[:0], h.cookies)
- if !yield(strCookie, h.bufV) {
- return
- }
- }
- for i := range h.h {
- if !yield(h.h[i].key, h.h[i].value) {
- return
- }
- }
- if h.ConnectionClose() && !yield(strConnection, strClose) {
- return
- }
- }
- }
- // VisitAll calls f for each header.
- //
- // f must not retain references to key and/or value after returning.
- // Copy key and/or value contents before returning if you need retaining them.
- //
- // To get the headers in order they were received use VisitAllInOrder.
- //
- // Deprecated: Use All instead.
- func (h *RequestHeader) VisitAll(f func(key, value []byte)) {
- h.All()(func(key, value []byte) bool {
- f(key, value)
- return true
- })
- }
- // AllInOrder returns an iterator over key-value pairs in h in the order they
- // were received.
- //
- // The key and value may invalid outside the iteration loop.
- // Copy key and/or value contents for each iteration if you need retaining
- // them.
- //
- // The returned iterator is slightly slower than All because it has to reparse
- // the raw headers to get the order.
- func (h *RequestHeader) AllInOrder() iter.Seq2[[]byte, []byte] {
- return func(yield func([]byte, []byte) bool) {
- var s headerScanner
- s.b = h.rawHeaders
- for s.next() {
- normalizeHeaderKey(s.key, h.disableNormalizing || bytes.IndexByte(s.key, ' ') != -1)
- if len(s.key) > 0 {
- if !yield(s.key, s.value) {
- break
- }
- }
- }
- }
- }
- // VisitAllInOrder calls f for each header in the order they were received.
- //
- // f must not retain references to key and/or value after returning.
- // Copy key and/or value contents before returning if you need retaining them.
- //
- // This function is slightly slower than VisitAll because it has to reparse the
- // raw headers to get the order.
- //
- // Deprecated: Use AllInOrder instead.
- func (h *RequestHeader) VisitAllInOrder(f func(key, value []byte)) {
- h.AllInOrder()(func(key, value []byte) bool {
- f(key, value)
- return true
- })
- }
- // Del deletes header with the given key.
- func (h *ResponseHeader) Del(key string) {
- h.bufK = getHeaderKeyBytes(h.bufK, key, h.disableNormalizing)
- h.del(h.bufK)
- }
- // DelBytes deletes header with the given key.
- func (h *ResponseHeader) DelBytes(key []byte) {
- h.bufK = append(h.bufK[:0], key...)
- normalizeHeaderKey(h.bufK, h.disableNormalizing || bytes.IndexByte(key, ' ') != -1)
- h.del(h.bufK)
- }
- func (h *ResponseHeader) del(key []byte) {
- switch string(key) {
- case HeaderContentType:
- h.contentType = h.contentType[:0]
- case HeaderContentEncoding:
- h.contentEncoding = h.contentEncoding[:0]
- case HeaderServer:
- h.server = h.server[:0]
- case HeaderSetCookie:
- h.cookies = h.cookies[:0]
- case HeaderContentLength:
- h.contentLength = 0
- h.contentLengthBytes = h.contentLengthBytes[:0]
- case HeaderConnection:
- h.connectionClose = false
- case HeaderTrailer:
- h.trailer = h.trailer[:0]
- }
- h.h = delAllArgs(h.h, b2s(key))
- }
- // Del deletes header with the given key.
- func (h *RequestHeader) Del(key string) {
- h.bufK = getHeaderKeyBytes(h.bufK, key, h.disableNormalizing)
- h.del(h.bufK)
- }
- // DelBytes deletes header with the given key.
- func (h *RequestHeader) DelBytes(key []byte) {
- h.bufK = append(h.bufK[:0], key...)
- normalizeHeaderKey(h.bufK, h.disableNormalizing || bytes.IndexByte(key, ' ') != -1)
- h.del(h.bufK)
- }
- func (h *RequestHeader) del(key []byte) {
- switch string(key) {
- case HeaderHost:
- h.host = h.host[:0]
- case HeaderContentType:
- h.contentType = h.contentType[:0]
- case HeaderUserAgent:
- h.userAgent = h.userAgent[:0]
- case HeaderCookie:
- h.cookies = h.cookies[:0]
- case HeaderContentLength:
- h.contentLength = 0
- h.contentLengthBytes = h.contentLengthBytes[:0]
- case HeaderConnection:
- h.connectionClose = false
- case HeaderTrailer:
- h.trailer = h.trailer[:0]
- }
- h.h = delAllArgs(h.h, b2s(key))
- }
- // setSpecialHeader handles special headers and return true when a header is processed.
- func (h *ResponseHeader) setSpecialHeader(key, value []byte) bool {
- if len(key) == 0 {
- return false
- }
- switch key[0] | 0x20 {
- case 'c':
- switch {
- case caseInsensitiveCompare(strContentType, key):
- h.SetContentTypeBytes(value)
- return true
- case caseInsensitiveCompare(strContentLength, key):
- if contentLength, err := parseContentLength(value); err == nil {
- h.contentLength = contentLength
- h.contentLengthBytes = append(h.contentLengthBytes[:0], value...)
- }
- return true
- case caseInsensitiveCompare(strContentEncoding, key):
- h.SetContentEncodingBytes(value)
- return true
- case caseInsensitiveCompare(strConnection, key):
- if bytes.Equal(strClose, value) {
- h.SetConnectionClose()
- } else {
- h.ResetConnectionClose()
- h.setNonSpecial(key, value)
- }
- return true
- }
- case 's':
- if caseInsensitiveCompare(strServer, key) {
- h.SetServerBytes(value)
- return true
- } else if caseInsensitiveCompare(strSetCookie, key) {
- var kv *argsKV
- h.cookies, kv = allocArg(h.cookies)
- kv.key = getCookieKey(kv.key, value)
- kv.value = append(kv.value[:0], value...)
- return true
- }
- case 't':
- if caseInsensitiveCompare(strTransferEncoding, key) {
- // Transfer-Encoding is managed automatically.
- return true
- } else if caseInsensitiveCompare(strTrailer, key) {
- _ = h.SetTrailerBytes(value)
- return true
- }
- case 'd':
- if caseInsensitiveCompare(strDate, key) {
- // Date is managed automatically.
- return true
- }
- }
- return false
- }
- // setNonSpecial directly put into map i.e. not a basic header.
- func (h *header) setNonSpecial(key, value []byte) {
- h.h = setArgBytes(h.h, key, value, argsHasValue)
- }
- // setSpecialHeader handles special headers and return true when a header is processed.
- func (h *RequestHeader) setSpecialHeader(key, value []byte) bool {
- if len(key) == 0 || h.disableSpecialHeader {
- return false
- }
- switch key[0] | 0x20 {
- case 'c':
- switch {
- case caseInsensitiveCompare(strContentType, key):
- h.SetContentTypeBytes(value)
- return true
- case caseInsensitiveCompare(strContentLength, key):
- if contentLength, err := parseContentLength(value); err == nil {
- h.contentLength = contentLength
- h.contentLengthBytes = append(h.contentLengthBytes[:0], value...)
- }
- return true
- case caseInsensitiveCompare(strConnection, key):
- if bytes.Equal(strClose, value) {
- h.SetConnectionClose()
- } else {
- h.ResetConnectionClose()
- h.setNonSpecial(key, value)
- }
- return true
- case caseInsensitiveCompare(strCookie, key):
- h.collectCookies()
- h.cookies = parseRequestCookies(h.cookies, value)
- return true
- }
- case 't':
- if caseInsensitiveCompare(strTransferEncoding, key) {
- // Transfer-Encoding is managed automatically.
- return true
- } else if caseInsensitiveCompare(strTrailer, key) {
- _ = h.SetTrailerBytes(value)
- return true
- }
- case 'h':
- if caseInsensitiveCompare(strHost, key) {
- h.SetHostBytes(value)
- return true
- }
- case 'u':
- if caseInsensitiveCompare(strUserAgent, key) {
- h.SetUserAgentBytes(value)
- return true
- }
- }
- return false
- }
- // Add adds the given 'key: value' header.
- //
- // Multiple headers with the same key may be added with this function.
- // Use Set for setting a single header for the given key.
- //
- // the Content-Type, Content-Length, Connection, Server, Transfer-Encoding
- // and Date headers can only be set once and will overwrite the previous value,
- // while Set-Cookie will not clear previous cookies.
- //
- // If the header is set as a Trailer (forbidden trailers will not be set, see AddTrailer for more details),
- // it will be sent after the chunked response body.
- func (h *ResponseHeader) Add(key, value string) {
- h.AddBytesKV(s2b(key), s2b(value))
- }
- // AddBytesK adds the given 'key: value' header.
- //
- // Multiple headers with the same key may be added with this function.
- // Use SetBytesK for setting a single header for the given key.
- //
- // the Content-Type, Content-Length, Connection, Server, Transfer-Encoding
- // and Date headers can only be set once and will overwrite the previous value,
- // while Set-Cookie will not clear previous cookies.
- //
- // If the header is set as a Trailer (forbidden trailers will not be set, see AddTrailer for more details),
- // it will be sent after the chunked response body.
- func (h *ResponseHeader) AddBytesK(key []byte, value string) {
- h.AddBytesKV(key, s2b(value))
- }
- // AddBytesV adds the given 'key: value' header.
- //
- // Multiple headers with the same key may be added with this function.
- // Use SetBytesV for setting a single header for the given key.
- //
- // the Content-Type, Content-Length, Connection, Server, Transfer-Encoding
- // and Date headers can only be set once and will overwrite the previous value,
- // while Set-Cookie will not clear previous cookies.
- //
- // If the header is set as a Trailer (forbidden trailers will not be set, see AddTrailer for more details),
- // it will be sent after the chunked response body.
- func (h *ResponseHeader) AddBytesV(key string, value []byte) {
- h.AddBytesKV(s2b(key), value)
- }
- // AddBytesKV adds the given 'key: value' header.
- //
- // Multiple headers with the same key may be added with this function.
- // Use SetBytesKV for setting a single header for the given key.
- //
- // the Content-Type, Content-Length, Connection, Server, Transfer-Encoding
- // and Date headers can only be set once and will overwrite the previous value,
- // while the Set-Cookie header will not clear previous cookies.
- //
- // If the header is set as a Trailer (forbidden trailers will not be set, see AddTrailer for more details),
- // it will be sent after the chunked response body.
- func (h *ResponseHeader) AddBytesKV(key, value []byte) {
- if h.setSpecialHeader(key, value) {
- return
- }
- h.bufK = getHeaderKeyBytes(h.bufK, b2s(key), h.disableNormalizing)
- h.h = appendArgBytes(h.h, h.bufK, value, argsHasValue)
- }
- // Set sets the given 'key: value' header.
- //
- // Please note that the Set-Cookie header will not clear previous cookies,
- // use SetCookie instead to reset cookies.
- //
- // If the header is set as a Trailer (forbidden trailers will not be set, see SetTrailer for more details),
- // it will be sent after the chunked response body.
- //
- // Use Add for setting multiple header values under the same key.
- func (h *ResponseHeader) Set(key, value string) {
- h.bufK, h.bufV = initHeaderKV(h.bufK, h.bufV, key, value, h.disableNormalizing)
- h.SetCanonical(h.bufK, h.bufV)
- }
- // SetBytesK sets the given 'key: value' header.
- //
- // Please note that the Set-Cookie header will not clear previous cookies,
- // use SetCookie instead to reset cookies.
- //
- // If the header is set as a Trailer (forbidden trailers will not be set, see SetTrailer for more details),
- // it will be sent after the chunked response body.
- //
- // Use AddBytesK for setting multiple header values under the same key.
- func (h *ResponseHeader) SetBytesK(key []byte, value string) {
- h.bufV = append(h.bufV[:0], value...)
- h.SetBytesKV(key, h.bufV)
- }
- // SetBytesV sets the given 'key: value' header.
- //
- // Please note that the Set-Cookie header will not clear previous cookies,
- // use SetCookie instead to reset cookies.
- //
- // If the header is set as a Trailer (forbidden trailers will not be set, see SetTrailer for more details),
- // it will be sent after the chunked response body.
- //
- // Use AddBytesV for setting multiple header values under the same key.
- func (h *ResponseHeader) SetBytesV(key string, value []byte) {
- h.bufK = getHeaderKeyBytes(h.bufK, key, h.disableNormalizing)
- h.SetCanonical(h.bufK, value)
- }
- // SetBytesKV sets the given 'key: value' header.
- //
- // Please note that the Set-Cookie header will not clear previous cookies,
- // use SetCookie instead to reset cookies.
- //
- // If the header is set as a Trailer (forbidden trailers will not be set, see SetTrailer for more details),
- // it will be sent after the chunked response body.
- //
- // Use AddBytesKV for setting multiple header values under the same key.
- func (h *ResponseHeader) SetBytesKV(key, value []byte) {
- h.bufK = append(h.bufK[:0], key...)
- normalizeHeaderKey(h.bufK, h.disableNormalizing || bytes.IndexByte(key, ' ') != -1)
- h.SetCanonical(h.bufK, value)
- }
- // SetCanonical sets the given 'key: value' header assuming that
- // key is in canonical form.
- //
- // Please note that the Set-Cookie header will not clear previous cookies,
- // use SetCookie instead to reset cookies.
- //
- // If the header is set as a Trailer (forbidden trailers will not be set, see SetTrailer for more details),
- // it will be sent after the chunked response body.
- func (h *ResponseHeader) SetCanonical(key, value []byte) {
- if h.setSpecialHeader(key, value) {
- return
- }
- h.setNonSpecial(key, value)
- }
- // SetCookie sets the given response cookie.
- //
- // It is safe re-using the cookie after the function returns.
- func (h *ResponseHeader) SetCookie(cookie *Cookie) {
- h.cookies = setArgBytes(h.cookies, cookie.Key(), cookie.Cookie(), argsHasValue)
- }
- // SetCookie sets 'key: value' cookies.
- func (h *RequestHeader) SetCookie(key, value string) {
- h.collectCookies()
- h.cookies = setArg(h.cookies, key, value, argsHasValue)
- }
- // SetCookieBytesK sets 'key: value' cookies.
- func (h *RequestHeader) SetCookieBytesK(key []byte, value string) {
- h.SetCookie(b2s(key), value)
- }
- // SetCookieBytesKV sets 'key: value' cookies.
- func (h *RequestHeader) SetCookieBytesKV(key, value []byte) {
- h.SetCookie(b2s(key), b2s(value))
- }
- // DelClientCookie instructs the client to remove the given cookie.
- // This doesn't work for a cookie with specific domain or path,
- // you should delete it manually like:
- //
- // c := AcquireCookie()
- // c.SetKey(key)
- // c.SetDomain("example.com")
- // c.SetPath("/path")
- // c.SetExpire(CookieExpireDelete)
- // h.SetCookie(c)
- // ReleaseCookie(c)
- //
- // Use DelCookie if you want just removing the cookie from response header.
- func (h *ResponseHeader) DelClientCookie(key string) {
- h.DelCookie(key)
- c := AcquireCookie()
- c.SetKey(key)
- c.SetExpire(CookieExpireDelete)
- h.SetCookie(c)
- ReleaseCookie(c)
- }
- // DelClientCookieBytes instructs the client to remove the given cookie.
- // This doesn't work for a cookie with specific domain or path,
- // you should delete it manually like:
- //
- // c := AcquireCookie()
- // c.SetKey(key)
- // c.SetDomain("example.com")
- // c.SetPath("/path")
- // c.SetExpire(CookieExpireDelete)
- // h.SetCookie(c)
- // ReleaseCookie(c)
- //
- // Use DelCookieBytes if you want just removing the cookie from response header.
- func (h *ResponseHeader) DelClientCookieBytes(key []byte) {
- h.DelClientCookie(b2s(key))
- }
- // DelCookie removes cookie under the given key from response header.
- //
- // Note that DelCookie doesn't remove the cookie from the client.
- // Use DelClientCookie instead.
- func (h *ResponseHeader) DelCookie(key string) {
- h.cookies = delAllArgs(h.cookies, key)
- }
- // DelCookieBytes removes cookie under the given key from response header.
- //
- // Note that DelCookieBytes doesn't remove the cookie from the client.
- // Use DelClientCookieBytes instead.
- func (h *ResponseHeader) DelCookieBytes(key []byte) {
- h.DelCookie(b2s(key))
- }
- // DelCookie removes cookie under the given key.
- func (h *RequestHeader) DelCookie(key string) {
- h.collectCookies()
- h.cookies = delAllArgs(h.cookies, key)
- }
- // DelCookieBytes removes cookie under the given key.
- func (h *RequestHeader) DelCookieBytes(key []byte) {
- h.DelCookie(b2s(key))
- }
- // DelAllCookies removes all the cookies from response headers.
- func (h *ResponseHeader) DelAllCookies() {
- h.cookies = h.cookies[:0]
- }
- // DelAllCookies removes all the cookies from request headers.
- func (h *RequestHeader) DelAllCookies() {
- h.collectCookies()
- h.cookies = h.cookies[:0]
- }
- // Add adds the given 'key: value' header.
- //
- // Multiple headers with the same key may be added with this function.
- // Use Set for setting a single header for the given key.
- //
- // If the header is set as a Trailer (forbidden trailers will not be set, see AddTrailer for more details),
- // it will be sent after the chunked request body.
- func (h *RequestHeader) Add(key, value string) {
- h.AddBytesKV(s2b(key), s2b(value))
- }
- // AddBytesK adds the given 'key: value' header.
- //
- // Multiple headers with the same key may be added with this function.
- // Use SetBytesK for setting a single header for the given key.
- //
- // If the header is set as a Trailer (forbidden trailers will not be set, see AddTrailer for more details),
- // it will be sent after the chunked request body.
- func (h *RequestHeader) AddBytesK(key []byte, value string) {
- h.AddBytesKV(key, s2b(value))
- }
- // AddBytesV adds the given 'key: value' header.
- //
- // Multiple headers with the same key may be added with this function.
- // Use SetBytesV for setting a single header for the given key.
- //
- // If the header is set as a Trailer (forbidden trailers will not be set, see AddTrailer for more details),
- // it will be sent after the chunked request body.
- func (h *RequestHeader) AddBytesV(key string, value []byte) {
- h.AddBytesKV(s2b(key), value)
- }
- // AddBytesKV adds the given 'key: value' header.
- //
- // Multiple headers with the same key may be added with this function.
- // Use SetBytesKV for setting a single header for the given key.
- //
- // the Content-Type, Content-Length, Connection, Transfer-Encoding,
- // Host and User-Agent headers can only be set once and will overwrite
- // the previous value, while the Cookie header will not clear previous cookies.
- //
- // If the header is set as a Trailer (forbidden trailers will not be set, see AddTrailer for more details),
- // it will be sent after the chunked request body.
- func (h *RequestHeader) AddBytesKV(key, value []byte) {
- if h.setSpecialHeader(key, value) {
- return
- }
- h.bufK = getHeaderKeyBytes(h.bufK, b2s(key), h.disableNormalizing)
- h.h = appendArgBytes(h.h, h.bufK, value, argsHasValue)
- }
- // Set sets the given 'key: value' header.
- //
- // Please note that the Cookie header will not clear previous cookies,
- // delete cookies before calling in order to reset cookies.
- //
- // If the header is set as a Trailer (forbidden trailers will not be set, see SetTrailer for more details),
- // it will be sent after the chunked request body.
- //
- // Use Add for setting multiple header values under the same key.
- func (h *RequestHeader) Set(key, value string) {
- h.bufK, h.bufV = initHeaderKV(h.bufK, h.bufV, key, value, h.disableNormalizing)
- h.SetCanonical(h.bufK, h.bufV)
- }
- // SetBytesK sets the given 'key: value' header.
- //
- // Please note that the Cookie header will not clear previous cookies,
- // delete cookies before calling in order to reset cookies.
- //
- // If the header is set as a Trailer (forbidden trailers will not be set, see SetTrailer for more details),
- // it will be sent after the chunked request body.
- //
- // Use AddBytesK for setting multiple header values under the same key.
- func (h *RequestHeader) SetBytesK(key []byte, value string) {
- h.bufV = append(h.bufV[:0], value...)
- h.SetBytesKV(key, h.bufV)
- }
- // SetBytesV sets the given 'key: value' header.
- //
- // Please note that the Cookie header will not clear previous cookies,
- // delete cookies before calling in order to reset cookies.
- //
- // If the header is set as a Trailer (forbidden trailers will not be set, see SetTrailer for more details),
- // it will be sent after the chunked request body.
- //
- // Use AddBytesV for setting multiple header values under the same key.
- func (h *RequestHeader) SetBytesV(key string, value []byte) {
- h.bufK = getHeaderKeyBytes(h.bufK, key, h.disableNormalizing)
- h.SetCanonical(h.bufK, value)
- }
- // SetBytesKV sets the given 'key: value' header.
- //
- // Please note that the Cookie header will not clear previous cookies,
- // delete cookies before calling in order to reset cookies.
- //
- // If the header is set as a Trailer (forbidden trailers will not be set, see SetTrailer for more details),
- // it will be sent after the chunked request body.
- //
- // Use AddBytesKV for setting multiple header values under the same key.
- func (h *RequestHeader) SetBytesKV(key, value []byte) {
- h.bufK = append(h.bufK[:0], key...)
- normalizeHeaderKey(h.bufK, h.disableNormalizing || bytes.IndexByte(key, ' ') != -1)
- h.SetCanonical(h.bufK, value)
- }
- // SetCanonical sets the given 'key: value' header assuming that
- // key is in canonical form.
- //
- // Please note that the Cookie header will not clear previous cookies,
- // delete cookies before calling in order to reset cookies.
- //
- // If the header is set as a Trailer (forbidden trailers will not be set, see SetTrailer for more details),
- // it will be sent after the chunked request body.
- func (h *RequestHeader) SetCanonical(key, value []byte) {
- if h.setSpecialHeader(key, value) {
- return
- }
- h.setNonSpecial(key, value)
- }
- // Peek returns header value for the given key.
- //
- // The returned value is valid until the response is released,
- // either though ReleaseResponse or your request handler returning.
- // Do not store references to the returned value. Make copies instead.
- func (h *ResponseHeader) Peek(key string) []byte {
- h.bufK = getHeaderKeyBytes(h.bufK, key, h.disableNormalizing)
- return h.peek(h.bufK)
- }
- // PeekBytes returns header value for the given key.
- //
- // The returned value is valid until the response is released,
- // either though ReleaseResponse or your request handler returning.
- // Do not store references to returned value. Make copies instead.
- func (h *ResponseHeader) PeekBytes(key []byte) []byte {
- h.bufK = append(h.bufK[:0], key...)
- normalizeHeaderKey(h.bufK, h.disableNormalizing || bytes.IndexByte(key, ' ') != -1)
- return h.peek(h.bufK)
- }
- // Peek returns header value for the given key.
- //
- // The returned value is valid until the request is released,
- // either though ReleaseRequest or your request handler returning.
- // Do not store references to returned value. Make copies instead.
- func (h *RequestHeader) Peek(key string) []byte {
- h.bufK = getHeaderKeyBytes(h.bufK, key, h.disableNormalizing)
- return h.peek(h.bufK)
- }
- // PeekBytes returns header value for the given key.
- //
- // The returned value is valid until the request is released,
- // either though ReleaseRequest or your request handler returning.
- // Do not store references to returned value. Make copies instead.
- func (h *RequestHeader) PeekBytes(key []byte) []byte {
- h.bufK = append(h.bufK[:0], key...)
- normalizeHeaderKey(h.bufK, h.disableNormalizing || bytes.IndexByte(key, ' ') != -1)
- return h.peek(h.bufK)
- }
- func (h *ResponseHeader) peek(key []byte) []byte {
- switch string(key) {
- case HeaderContentType:
- return h.ContentType()
- case HeaderContentEncoding:
- return h.ContentEncoding()
- case HeaderServer:
- return h.Server()
- case HeaderConnection:
- if h.ConnectionClose() {
- return strClose
- }
- return peekArgBytes(h.h, key)
- case HeaderContentLength:
- return h.contentLengthBytes
- case HeaderSetCookie:
- return appendResponseCookieBytes(nil, h.cookies)
- case HeaderTrailer:
- return appendTrailerBytes(nil, h.trailer, strCommaSpace)
- default:
- return peekArgBytes(h.h, key)
- }
- }
- func (h *RequestHeader) peek(key []byte) []byte {
- switch string(key) {
- case HeaderHost:
- return h.Host()
- case HeaderContentType:
- return h.ContentType()
- case HeaderUserAgent:
- return h.UserAgent()
- case HeaderConnection:
- if h.ConnectionClose() {
- return strClose
- }
- return peekArgBytes(h.h, key)
- case HeaderContentLength:
- return h.contentLengthBytes
- case HeaderCookie:
- if h.cookiesCollected {
- return appendRequestCookieBytes(nil, h.cookies)
- }
- return peekArgBytes(h.h, key)
- case HeaderTrailer:
- return appendTrailerBytes(nil, h.trailer, strCommaSpace)
- default:
- return peekArgBytes(h.h, key)
- }
- }
- // PeekAll returns all header value for the given key.
- //
- // The returned value is valid until the request is released,
- // either though ReleaseRequest or your request handler returning.
- // Any future calls to the Peek* will modify the returned value.
- // Do not store references to returned value. Make copies instead.
- func (h *RequestHeader) PeekAll(key string) [][]byte {
- h.bufK = getHeaderKeyBytes(h.bufK, key, h.disableNormalizing)
- return h.peekAll(h.bufK)
- }
- func (h *RequestHeader) peekAll(key []byte) [][]byte {
- h.mulHeader = h.mulHeader[:0]
- switch string(key) {
- case HeaderHost:
- if host := h.Host(); len(host) > 0 {
- h.mulHeader = append(h.mulHeader, host)
- }
- case HeaderContentType:
- if contentType := h.ContentType(); len(contentType) > 0 {
- h.mulHeader = append(h.mulHeader, contentType)
- }
- case HeaderUserAgent:
- if ua := h.UserAgent(); len(ua) > 0 {
- h.mulHeader = append(h.mulHeader, ua)
- }
- case HeaderConnection:
- if h.ConnectionClose() {
- h.mulHeader = append(h.mulHeader, strClose)
- } else {
- h.mulHeader = peekAllArgBytesToDst(h.mulHeader, h.h, key)
- }
- case HeaderContentLength:
- h.mulHeader = append(h.mulHeader, h.contentLengthBytes)
- case HeaderCookie:
- if h.cookiesCollected {
- h.mulHeader = append(h.mulHeader, appendRequestCookieBytes(nil, h.cookies))
- } else {
- h.mulHeader = peekAllArgBytesToDst(h.mulHeader, h.h, key)
- }
- case HeaderTrailer:
- h.mulHeader = append(h.mulHeader, appendTrailerBytes(nil, h.trailer, strCommaSpace))
- default:
- h.mulHeader = peekAllArgBytesToDst(h.mulHeader, h.h, key)
- }
- return h.mulHeader
- }
- // PeekAll returns all header value for the given key.
- //
- // The returned value is valid until the request is released,
- // either though ReleaseResponse or your request handler returning.
- // Any future calls to the Peek* will modify the returned value.
- // Do not store references to returned value. Make copies instead.
- func (h *ResponseHeader) PeekAll(key string) [][]byte {
- h.bufK = getHeaderKeyBytes(h.bufK, key, h.disableNormalizing)
- return h.peekAll(h.bufK)
- }
- func (h *ResponseHeader) peekAll(key []byte) [][]byte {
- h.mulHeader = h.mulHeader[:0]
- switch string(key) {
- case HeaderContentType:
- if contentType := h.ContentType(); len(contentType) > 0 {
- h.mulHeader = append(h.mulHeader, contentType)
- }
- case HeaderContentEncoding:
- if contentEncoding := h.ContentEncoding(); len(contentEncoding) > 0 {
- h.mulHeader = append(h.mulHeader, contentEncoding)
- }
- case HeaderServer:
- if server := h.Server(); len(server) > 0 {
- h.mulHeader = append(h.mulHeader, server)
- }
- case HeaderConnection:
- if h.ConnectionClose() {
- h.mulHeader = append(h.mulHeader, strClose)
- } else {
- h.mulHeader = peekAllArgBytesToDst(h.mulHeader, h.h, key)
- }
- case HeaderContentLength:
- h.mulHeader = append(h.mulHeader, h.contentLengthBytes)
- case HeaderSetCookie:
- h.mulHeader = append(h.mulHeader, appendResponseCookieBytes(nil, h.cookies))
- case HeaderTrailer:
- h.mulHeader = append(h.mulHeader, appendTrailerBytes(nil, h.trailer, strCommaSpace))
- default:
- h.mulHeader = peekAllArgBytesToDst(h.mulHeader, h.h, key)
- }
- return h.mulHeader
- }
- // PeekKeys return all header keys.
- //
- // The returned value is valid until the request is released,
- // either though ReleaseRequest or your request handler returning.
- // Any future calls to the Peek* will modify the returned value.
- // Do not store references to returned value. Make copies instead.
- func (h *header) PeekKeys() [][]byte {
- h.mulHeader = h.mulHeader[:0]
- h.mulHeader = peekArgsKeys(h.mulHeader, h.h)
- return h.mulHeader
- }
- // PeekTrailerKeys return all trailer keys.
- //
- // The returned value is valid until the request is released,
- // either though ReleaseResponse or your request handler returning.
- // Any future calls to the Peek* will modify the returned value.
- // Do not store references to returned value. Make copies instead.
- func (h *header) PeekTrailerKeys() [][]byte {
- h.mulHeader = copyTrailer(h.mulHeader, h.trailer)
- return h.mulHeader
- }
- // Cookie returns cookie for the given key.
- func (h *RequestHeader) Cookie(key string) []byte {
- h.collectCookies()
- return peekArgStr(h.cookies, key)
- }
- // CookieBytes returns cookie for the given key.
- func (h *RequestHeader) CookieBytes(key []byte) []byte {
- h.collectCookies()
- return peekArgBytes(h.cookies, key)
- }
- // Cookie fills cookie for the given cookie.Key.
- //
- // Returns false if cookie with the given cookie.Key is missing.
- func (h *ResponseHeader) Cookie(cookie *Cookie) bool {
- v := peekArgBytes(h.cookies, cookie.Key())
- if v == nil {
- return false
- }
- cookie.ParseBytes(v) //nolint:errcheck
- return true
- }
- // Read reads response header from r.
- //
- // io.EOF is returned if r is closed before reading the first header byte.
- func (h *ResponseHeader) Read(r *bufio.Reader) error {
- n := 1
- for {
- err := h.tryRead(r, n)
- if err == nil {
- return nil
- }
- if err != errNeedMore {
- h.resetSkipNormalize()
- return err
- }
- n = r.Buffered() + 1
- }
- }
- func (h *ResponseHeader) tryRead(r *bufio.Reader, n int) error {
- h.resetSkipNormalize()
- b, err := r.Peek(n)
- if len(b) == 0 {
- // Return ErrTimeout on any timeout.
- if x, ok := err.(interface{ Timeout() bool }); ok && x.Timeout() {
- return ErrTimeout
- }
- // treat all other errors on the first byte read as EOF
- if n == 1 || err == io.EOF {
- return io.EOF
- }
- // This is for go 1.6 bug. See https://github.com/golang/go/issues/14121 .
- if err == bufio.ErrBufferFull {
- if h.secureErrorLogMessage {
- return &ErrSmallBuffer{
- error: errors.New("error when reading response headers"),
- }
- }
- return &ErrSmallBuffer{
- error: fmt.Errorf("error when reading response headers: %w", errSmallBuffer),
- }
- }
- return fmt.Errorf("error when reading response headers: %w", err)
- }
- b = mustPeekBuffered(r)
- headersLen, errParse := h.parse(b)
- if errParse != nil {
- return headerError("response", err, errParse, b, h.secureErrorLogMessage)
- }
- mustDiscard(r, headersLen)
- return nil
- }
- // ReadTrailer reads response trailer header from r.
- //
- // io.EOF is returned if r is closed before reading the first byte.
- func (h *header) ReadTrailer(r *bufio.Reader) error {
- n := 1
- for {
- err := h.tryReadTrailer(r, n)
- if err == nil {
- return nil
- }
- if err != errNeedMore {
- return err
- }
- n = r.Buffered() + 1
- }
- }
- func (h *header) tryReadTrailer(r *bufio.Reader, n int) error {
- b, err := r.Peek(n)
- if len(b) == 0 {
- // Return ErrTimeout on any timeout.
- if x, ok := err.(interface{ Timeout() bool }); ok && x.Timeout() {
- return ErrTimeout
- }
- if n == 1 || err == io.EOF {
- return io.EOF
- }
- // This is for go 1.6 bug. See https://github.com/golang/go/issues/14121 .
- if err == bufio.ErrBufferFull {
- if h.secureErrorLogMessage {
- return &ErrSmallBuffer{
- error: errors.New("error when reading response trailer"),
- }
- }
- return &ErrSmallBuffer{
- error: fmt.Errorf("error when reading response trailer: %w", errSmallBuffer),
- }
- }
- return fmt.Errorf("error when reading response trailer: %w", err)
- }
- b = mustPeekBuffered(r)
- trailers, headersLen, errParse := parseTrailer(b, h.h, h.disableNormalizing)
- h.h = trailers
- if errParse != nil {
- if err == io.EOF {
- return err
- }
- return headerError("response", err, errParse, b, h.secureErrorLogMessage)
- }
- mustDiscard(r, headersLen)
- return nil
- }
- func headerError(typ string, err, errParse error, b []byte, secureErrorLogMessage bool) error {
- if errParse != errNeedMore {
- return headerErrorMsg(typ, errParse, b, secureErrorLogMessage)
- }
- if err == nil {
- return errNeedMore
- }
- // Buggy servers may leave trailing CRLFs after http body.
- // Treat this case as EOF.
- if isOnlyCRLF(b) {
- return io.EOF
- }
- if err != bufio.ErrBufferFull {
- return headerErrorMsg(typ, err, b, secureErrorLogMessage)
- }
- return &ErrSmallBuffer{
- error: headerErrorMsg(typ, errSmallBuffer, b, secureErrorLogMessage),
- }
- }
- func headerErrorMsg(typ string, err error, b []byte, secureErrorLogMessage bool) error {
- if secureErrorLogMessage {
- return fmt.Errorf("error when reading %s headers: %w. Buffer size=%d", typ, err, len(b))
- }
- return fmt.Errorf("error when reading %s headers: %w. Buffer size=%d, contents: %s", typ, err, len(b), bufferSnippet(b))
- }
- // Read reads request header from r.
- //
- // io.EOF is returned if r is closed before reading the first header byte.
- func (h *RequestHeader) Read(r *bufio.Reader) error {
- return h.readLoop(r, true)
- }
- // readLoop reads request header from r optionally loops until it has enough data.
- //
- // io.EOF is returned if r is closed before reading the first header byte.
- func (h *RequestHeader) readLoop(r *bufio.Reader, waitForMore bool) error {
- n := 1
- for {
- err := h.tryRead(r, n)
- if err == nil {
- return nil
- }
- if !waitForMore || err != errNeedMore {
- h.resetSkipNormalize()
- return err
- }
- n = r.Buffered() + 1
- }
- }
- func (h *RequestHeader) tryRead(r *bufio.Reader, n int) error {
- h.resetSkipNormalize()
- b, err := r.Peek(n)
- if len(b) == 0 {
- if err == io.EOF {
- return err
- }
- if err == nil {
- panic("bufio.Reader.Peek() returned nil, nil")
- }
- // This is for go 1.6 bug. See https://github.com/golang/go/issues/14121 .
- if err == bufio.ErrBufferFull {
- return &ErrSmallBuffer{
- error: fmt.Errorf("error when reading request headers: %w (n=%d, r.Buffered()=%d)", errSmallBuffer, n, r.Buffered()),
- }
- }
- // n == 1 on the first read for the request.
- if n == 1 {
- // We didn't read a single byte.
- return ErrNothingRead{error: err}
- }
- return fmt.Errorf("error when reading request headers: %w", err)
- }
- b = mustPeekBuffered(r)
- headersLen, errParse := h.parse(b)
- if errParse != nil {
- return headerError("request", err, errParse, b, h.secureErrorLogMessage)
- }
- mustDiscard(r, headersLen)
- return nil
- }
- func bufferSnippet(b []byte) string {
- n := len(b)
- start := 200
- end := n - start
- if start >= end {
- start = n
- end = n
- }
- bStart, bEnd := b[:start], b[end:]
- if len(bEnd) == 0 {
- return fmt.Sprintf("%q", b)
- }
- return fmt.Sprintf("%q...%q", bStart, bEnd)
- }
- func isOnlyCRLF(b []byte) bool {
- for _, ch := range b {
- if ch != rChar && ch != nChar {
- return false
- }
- }
- return true
- }
- func updateServerDate() {
- refreshServerDate()
- go func() {
- for {
- time.Sleep(time.Second)
- refreshServerDate()
- }
- }()
- }
- var (
- serverDate atomic.Value
- serverDateOnce sync.Once // serverDateOnce.Do(updateServerDate)
- )
- func refreshServerDate() {
- b := AppendHTTPDate(nil, time.Now())
- serverDate.Store(b)
- }
- // Write writes response header to w.
- func (h *ResponseHeader) Write(w *bufio.Writer) error {
- _, err := w.Write(h.Header())
- return err
- }
- // WriteTo writes response header to w.
- //
- // WriteTo implements io.WriterTo interface.
- func (h *ResponseHeader) WriteTo(w io.Writer) (int64, error) {
- n, err := w.Write(h.Header())
- return int64(n), err
- }
- // Header returns response header representation.
- //
- // Headers that set as Trailer will not represent. Use TrailerHeader for trailers.
- //
- // The returned value is valid until the request is released,
- // either though ReleaseRequest or your request handler returning.
- // Do not store references to returned value. Make copies instead.
- func (h *ResponseHeader) Header() []byte {
- h.bufV = h.AppendBytes(h.bufV[:0])
- return h.bufV
- }
- // writeTrailer writes response trailer to w.
- func (h *ResponseHeader) writeTrailer(w *bufio.Writer) error {
- _, err := w.Write(h.TrailerHeader())
- return err
- }
- // TrailerHeader returns response trailer header representation.
- //
- // Trailers will only be received with chunked transfer.
- //
- // The returned value is valid until the request is released,
- // either though ReleaseRequest or your request handler returning.
- // Do not store references to returned value. Make copies instead.
- func (h *ResponseHeader) TrailerHeader() []byte {
- h.bufV = h.bufV[:0]
- for _, t := range h.trailer {
- value := h.peek(t)
- h.bufV = appendHeaderLine(h.bufV, t, value)
- }
- h.bufV = append(h.bufV, strCRLF...)
- return h.bufV
- }
- // String returns response header representation.
- func (h *ResponseHeader) String() string {
- return string(h.Header())
- }
- // appendStatusLine appends the response status line to dst and returns
- // the extended dst.
- func (h *ResponseHeader) appendStatusLine(dst []byte) []byte {
- statusCode := h.StatusCode()
- if statusCode < 0 {
- statusCode = StatusOK
- }
- return formatStatusLine(dst, h.Protocol(), statusCode, h.StatusMessage())
- }
- // AppendBytes appends response header representation to dst and returns
- // the extended dst.
- func (h *ResponseHeader) AppendBytes(dst []byte) []byte {
- dst = h.appendStatusLine(dst[:0])
- server := h.Server()
- if len(server) != 0 {
- dst = appendHeaderLine(dst, strServer, server)
- }
- if !h.noDefaultDate {
- serverDateOnce.Do(updateServerDate)
- dst = appendHeaderLine(dst, strDate, serverDate.Load().([]byte))
- }
- // Append Content-Type only for non-zero responses
- // or if it is explicitly set.
- // See https://github.com/valyala/fasthttp/issues/28 .
- if h.ContentLength() != 0 || len(h.contentType) > 0 {
- contentType := h.ContentType()
- if len(contentType) > 0 {
- dst = appendHeaderLine(dst, strContentType, contentType)
- }
- }
- contentEncoding := h.ContentEncoding()
- if len(contentEncoding) > 0 {
- dst = appendHeaderLine(dst, strContentEncoding, contentEncoding)
- }
- if len(h.contentLengthBytes) > 0 {
- dst = appendHeaderLine(dst, strContentLength, h.contentLengthBytes)
- }
- for i, n := 0, len(h.h); i < n; i++ {
- kv := &h.h[i]
- // Exclude trailer from header
- exclude := false
- for _, t := range h.trailer {
- if bytes.Equal(kv.key, t) {
- exclude = true
- break
- }
- }
- if !exclude && (h.noDefaultDate || !bytes.Equal(kv.key, strDate)) {
- dst = appendHeaderLine(dst, kv.key, kv.value)
- }
- }
- if len(h.trailer) > 0 {
- dst = appendHeaderLine(dst, strTrailer, appendTrailerBytes(nil, h.trailer, strCommaSpace))
- }
- n := len(h.cookies)
- if n > 0 {
- for i := 0; i < n; i++ {
- kv := &h.cookies[i]
- dst = appendHeaderLine(dst, strSetCookie, kv.value)
- }
- }
- if h.ConnectionClose() {
- dst = appendHeaderLine(dst, strConnection, strClose)
- }
- return append(dst, strCRLF...)
- }
- // Write writes request header to w.
- func (h *RequestHeader) Write(w *bufio.Writer) error {
- _, err := w.Write(h.Header())
- return err
- }
- // WriteTo writes request header to w.
- //
- // WriteTo implements io.WriterTo interface.
- func (h *RequestHeader) WriteTo(w io.Writer) (int64, error) {
- n, err := w.Write(h.Header())
- return int64(n), err
- }
- // Header returns request header representation.
- //
- // Headers that set as Trailer will not represent. Use TrailerHeader for trailers.
- //
- // The returned value is valid until the request is released,
- // either though ReleaseRequest or your request handler returning.
- // Do not store references to returned value. Make copies instead.
- func (h *RequestHeader) Header() []byte {
- h.bufV = h.AppendBytes(h.bufV[:0])
- return h.bufV
- }
- // writeTrailer writes request trailer to w.
- func (h *RequestHeader) writeTrailer(w *bufio.Writer) error {
- _, err := w.Write(h.TrailerHeader())
- return err
- }
- // TrailerHeader returns request trailer header representation.
- //
- // Trailers will only be received with chunked transfer.
- //
- // The returned value is valid until the request is released,
- // either though ReleaseRequest or your request handler returning.
- // Do not store references to returned value. Make copies instead.
- func (h *RequestHeader) TrailerHeader() []byte {
- h.bufV = h.bufV[:0]
- for _, t := range h.trailer {
- value := h.peek(t)
- h.bufV = appendHeaderLine(h.bufV, t, value)
- }
- h.bufV = append(h.bufV, strCRLF...)
- return h.bufV
- }
- // RawHeaders returns raw header key/value bytes.
- //
- // Depending on server configuration, header keys may be normalized to
- // capital-case in place.
- //
- // This copy is set aside during parsing, so empty slice is returned for all
- // cases where parsing did not happen. Similarly, request line is not stored
- // during parsing and can not be returned.
- //
- // The slice is not safe to use after the handler returns.
- func (h *RequestHeader) RawHeaders() []byte {
- return h.rawHeaders
- }
- // String returns request header representation.
- func (h *RequestHeader) String() string {
- return string(h.Header())
- }
- // AppendBytes appends request header representation to dst and returns
- // the extended dst.
- func (h *RequestHeader) AppendBytes(dst []byte) []byte {
- dst = append(dst, h.Method()...)
- dst = append(dst, ' ')
- dst = append(dst, h.RequestURI()...)
- dst = append(dst, ' ')
- dst = append(dst, h.Protocol()...)
- dst = append(dst, strCRLF...)
- userAgent := h.UserAgent()
- if len(userAgent) > 0 && !h.disableSpecialHeader {
- dst = appendHeaderLine(dst, strUserAgent, userAgent)
- }
- host := h.Host()
- if len(host) > 0 && !h.disableSpecialHeader {
- dst = appendHeaderLine(dst, strHost, host)
- }
- contentType := h.ContentType()
- if !h.noDefaultContentType && len(contentType) == 0 && !h.ignoreBody() {
- contentType = strDefaultContentType
- }
- if len(contentType) > 0 && !h.disableSpecialHeader {
- dst = appendHeaderLine(dst, strContentType, contentType)
- }
- if len(h.contentLengthBytes) > 0 && !h.disableSpecialHeader {
- dst = appendHeaderLine(dst, strContentLength, h.contentLengthBytes)
- }
- for i, n := 0, len(h.h); i < n; i++ {
- kv := &h.h[i]
- // Exclude trailer from header
- exclude := false
- for _, t := range h.trailer {
- if bytes.Equal(kv.key, t) {
- exclude = true
- break
- }
- }
- if !exclude {
- dst = appendHeaderLine(dst, kv.key, kv.value)
- }
- }
- if len(h.trailer) > 0 {
- dst = appendHeaderLine(dst, strTrailer, appendTrailerBytes(nil, h.trailer, strCommaSpace))
- }
- // there is no need in h.collectCookies() here, since if cookies aren't collected yet,
- // they all are located in h.h.
- n := len(h.cookies)
- if n > 0 && !h.disableSpecialHeader {
- dst = append(dst, strCookie...)
- dst = append(dst, strColonSpace...)
- dst = appendRequestCookieBytes(dst, h.cookies)
- dst = append(dst, strCRLF...)
- }
- if h.ConnectionClose() && !h.disableSpecialHeader {
- dst = appendHeaderLine(dst, strConnection, strClose)
- }
- return append(dst, strCRLF...)
- }
- func appendHeaderLine(dst, key, value []byte) []byte {
- dst = append(dst, key...)
- dst = append(dst, strColonSpace...)
- dst = append(dst, value...)
- return append(dst, strCRLF...)
- }
- func (h *ResponseHeader) parse(buf []byte) (int, error) {
- m, err := h.parseFirstLine(buf)
- if err != nil {
- return 0, err
- }
- n, err := h.parseHeaders(buf[m:])
- if err != nil {
- return 0, err
- }
- return m + n, nil
- }
- func (h *RequestHeader) ignoreBody() bool {
- return h.IsGet() || h.IsHead()
- }
- func (h *RequestHeader) parse(buf []byte) (int, error) {
- m, err := h.parseFirstLine(buf)
- if err != nil {
- return 0, err
- }
- h.rawHeaders, _, err = readRawHeaders(h.rawHeaders[:0], buf[m:])
- if err != nil {
- return 0, err
- }
- var n int
- n, err = h.parseHeaders(buf[m:])
- if err != nil {
- return 0, err
- }
- return m + n, nil
- }
- func parseTrailer(src []byte, dest []argsKV, disableNormalizing bool) ([]argsKV, int, error) {
- // Skip any 0 length chunk.
- if src[0] == '0' {
- skip := len(strCRLF) + 1
- if len(src) < skip {
- return dest, 0, io.EOF
- }
- src = src[skip:]
- }
- var s headerScanner
- s.b = src
- for s.next() {
- if len(s.key) == 0 {
- continue
- }
- disable := disableNormalizing
- for _, ch := range s.key {
- if !validHeaderFieldByte(ch) {
- // We accept invalid headers with a space before the
- // colon, but must not canonicalize them.
- // See: https://github.com/valyala/fasthttp/issues/1917
- if ch == ' ' {
- disable = true
- continue
- }
- return dest, 0, fmt.Errorf("invalid trailer key %q", s.key)
- }
- }
- // Forbidden by RFC 7230, section 4.1.2
- if isBadTrailer(s.key) {
- return dest, 0, fmt.Errorf("forbidden trailer key %q", s.key)
- }
- normalizeHeaderKey(s.key, disable)
- dest = appendArgBytes(dest, s.key, s.value, argsHasValue)
- }
- if s.err != nil {
- return dest, 0, s.err
- }
- return dest, s.hLen, nil
- }
- func isBadTrailer(key []byte) bool {
- if len(key) == 0 {
- return true
- }
- switch key[0] | 0x20 {
- case 'a':
- return caseInsensitiveCompare(key, strAuthorization)
- case 'c':
- if len(key) > len(HeaderContentType) && caseInsensitiveCompare(key[:8], strContentType[:8]) {
- // skip compare prefix 'Content-'
- return caseInsensitiveCompare(key[8:], strContentEncoding[8:]) ||
- caseInsensitiveCompare(key[8:], strContentLength[8:]) ||
- caseInsensitiveCompare(key[8:], strContentType[8:]) ||
- caseInsensitiveCompare(key[8:], strContentRange[8:])
- }
- return caseInsensitiveCompare(key, strConnection)
- case 'e':
- return caseInsensitiveCompare(key, strExpect)
- case 'h':
- return caseInsensitiveCompare(key, strHost)
- case 'k':
- return caseInsensitiveCompare(key, strKeepAlive)
- case 'm':
- return caseInsensitiveCompare(key, strMaxForwards)
- case 'p':
- if len(key) > len(HeaderProxyConnection) && caseInsensitiveCompare(key[:6], strProxyConnection[:6]) {
- // skip compare prefix 'Proxy-'
- return caseInsensitiveCompare(key[6:], strProxyConnection[6:]) ||
- caseInsensitiveCompare(key[6:], strProxyAuthenticate[6:]) ||
- caseInsensitiveCompare(key[6:], strProxyAuthorization[6:])
- }
- case 'r':
- return caseInsensitiveCompare(key, strRange)
- case 't':
- return caseInsensitiveCompare(key, strTE) ||
- caseInsensitiveCompare(key, strTrailer) ||
- caseInsensitiveCompare(key, strTransferEncoding)
- case 'w':
- return caseInsensitiveCompare(key, strWWWAuthenticate)
- }
- return false
- }
- func (h *ResponseHeader) parseFirstLine(buf []byte) (int, error) {
- bNext := buf
- var b []byte
- var err error
- for len(b) == 0 {
- if b, bNext, err = nextLine(bNext); err != nil {
- return 0, err
- }
- }
- // parse protocol
- n := bytes.IndexByte(b, ' ')
- if n < 0 {
- if h.secureErrorLogMessage {
- return 0, errors.New("cannot find whitespace in the first line of response")
- }
- return 0, fmt.Errorf("cannot find whitespace in the first line of response %q", buf)
- }
- h.noHTTP11 = !bytes.Equal(b[:n], strHTTP11)
- b = b[n+1:]
- // parse status code
- h.statusCode, n, err = parseUintBuf(b)
- if err != nil {
- if h.secureErrorLogMessage {
- return 0, fmt.Errorf("cannot parse response status code: %w", err)
- }
- return 0, fmt.Errorf("cannot parse response status code: %w. Response %q", err, buf)
- }
- if len(b) > n && b[n] != ' ' {
- if h.secureErrorLogMessage {
- return 0, errors.New("unexpected char at the end of status code")
- }
- return 0, fmt.Errorf("unexpected char at the end of status code. Response %q", buf)
- }
- if len(b) > n+1 {
- h.SetStatusMessage(b[n+1:])
- }
- return len(buf) - len(bNext), nil
- }
- func isValidMethod(method []byte) bool {
- for _, ch := range method {
- if validMethodValueByteTable[ch] == 0 {
- return false
- }
- }
- return true
- }
- func (h *RequestHeader) parseFirstLine(buf []byte) (int, error) {
- bNext := buf
- var b []byte
- var err error
- for len(b) == 0 {
- if b, bNext, err = nextLine(bNext); err != nil {
- return 0, err
- }
- }
- // parse method
- n := bytes.IndexByte(b, ' ')
- if n <= 0 {
- if h.secureErrorLogMessage {
- return 0, errors.New("cannot find http request method")
- }
- return 0, fmt.Errorf("cannot find http request method in %q", buf)
- }
- h.method = append(h.method[:0], b[:n]...)
- if !isValidMethod(h.method) {
- if h.secureErrorLogMessage {
- return 0, errors.New("unsupported http request method")
- }
- return 0, fmt.Errorf("unsupported http request method %q in %q", h.method, buf)
- }
- b = b[n+1:]
- // parse requestURI
- n = bytes.LastIndexByte(b, ' ')
- if n < 0 {
- return 0, fmt.Errorf("cannot find whitespace in the first line of request %q", buf)
- } else if n == 0 {
- if h.secureErrorLogMessage {
- return 0, errors.New("requestURI cannot be empty")
- }
- return 0, fmt.Errorf("requestURI cannot be empty in %q", buf)
- }
- protoStr := b[n+1:]
- // Follow RFCs 7230 and 9112 and require that HTTP versions match the following pattern: HTTP/[0-9]\.[0-9]
- if len(protoStr) != len(strHTTP11) {
- if h.secureErrorLogMessage {
- return 0, fmt.Errorf("unsupported HTTP version %q", protoStr)
- }
- return 0, fmt.Errorf("unsupported HTTP version %q in %q", protoStr, buf)
- }
- if !bytes.HasPrefix(protoStr, strHTTP11[:5]) {
- if h.secureErrorLogMessage {
- return 0, fmt.Errorf("unsupported HTTP version %q", protoStr)
- }
- return 0, fmt.Errorf("unsupported HTTP version %q in %q", protoStr, buf)
- }
- if protoStr[5] < '0' || protoStr[5] > '9' || protoStr[7] < '0' || protoStr[7] > '9' {
- if h.secureErrorLogMessage {
- return 0, fmt.Errorf("unsupported HTTP version %q", protoStr)
- }
- return 0, fmt.Errorf("unsupported HTTP version %q in %q", protoStr, buf)
- }
- h.noHTTP11 = !bytes.Equal(protoStr, strHTTP11)
- h.protocol = append(h.protocol[:0], protoStr...)
- h.requestURI = append(h.requestURI[:0], b[:n]...)
- return len(buf) - len(bNext), nil
- }
- func readRawHeaders(dst, buf []byte) ([]byte, int, error) {
- n := bytes.IndexByte(buf, nChar)
- if n < 0 {
- return dst[:0], 0, errNeedMore
- }
- if (n == 1 && buf[0] == rChar) || n == 0 {
- // empty headers
- return dst, n + 1, nil
- }
- n++
- b := buf
- m := n
- for {
- b = b[m:]
- m = bytes.IndexByte(b, nChar)
- if m < 0 {
- return dst, 0, errNeedMore
- }
- m++
- n += m
- if (m == 2 && b[0] == rChar) || m == 1 {
- dst = append(dst, buf[:n]...)
- return dst, n, nil
- }
- }
- }
- func (h *ResponseHeader) parseHeaders(buf []byte) (int, error) {
- // 'identity' content-length by default
- h.contentLength = -2
- var s headerScanner
- s.b = buf
- var kv *argsKV
- for s.next() {
- if len(s.key) == 0 {
- h.connectionClose = true
- return 0, fmt.Errorf("invalid header key %q", s.key)
- }
- disableNormalizing := h.disableNormalizing
- for _, ch := range s.key {
- if !validHeaderFieldByte(ch) {
- h.connectionClose = true
- // We accept invalid headers with a space before the
- // colon, but must not canonicalize them.
- // See: https://github.com/valyala/fasthttp/issues/1917
- if ch == ' ' {
- disableNormalizing = true
- continue
- }
- return 0, fmt.Errorf("invalid header key %q", s.key)
- }
- }
- normalizeHeaderKey(s.key, disableNormalizing)
- for _, ch := range s.value {
- if !validHeaderValueByte(ch) {
- h.connectionClose = true
- return 0, fmt.Errorf("invalid header value %q", s.value)
- }
- }
- switch s.key[0] | 0x20 {
- case 'c':
- if caseInsensitiveCompare(s.key, strContentType) {
- h.contentType = append(h.contentType[:0], s.value...)
- continue
- }
- if caseInsensitiveCompare(s.key, strContentEncoding) {
- h.contentEncoding = append(h.contentEncoding[:0], s.value...)
- continue
- }
- if caseInsensitiveCompare(s.key, strContentLength) {
- if h.contentLength != -1 {
- var err error
- h.contentLength, err = parseContentLength(s.value)
- if err != nil {
- h.contentLength = -2
- h.connectionClose = true
- return 0, err
- }
- h.contentLengthBytes = append(h.contentLengthBytes[:0], s.value...)
- }
- continue
- }
- if caseInsensitiveCompare(s.key, strConnection) {
- if bytes.Equal(s.value, strClose) {
- h.connectionClose = true
- } else {
- h.connectionClose = false
- h.h = appendArgBytes(h.h, s.key, s.value, argsHasValue)
- }
- continue
- }
- case 's':
- if caseInsensitiveCompare(s.key, strServer) {
- h.server = append(h.server[:0], s.value...)
- continue
- }
- if caseInsensitiveCompare(s.key, strSetCookie) {
- h.cookies, kv = allocArg(h.cookies)
- kv.key = getCookieKey(kv.key, s.value)
- kv.value = append(kv.value[:0], s.value...)
- continue
- }
- case 't':
- if caseInsensitiveCompare(s.key, strTransferEncoding) {
- if len(s.value) > 0 && !bytes.Equal(s.value, strIdentity) {
- h.contentLength = -1
- h.h = setArgBytes(h.h, strTransferEncoding, strChunked, argsHasValue)
- }
- continue
- }
- if caseInsensitiveCompare(s.key, strTrailer) {
- err := h.SetTrailerBytes(s.value)
- if err != nil {
- h.connectionClose = true
- return 0, err
- }
- continue
- }
- }
- h.h = appendArgBytes(h.h, s.key, s.value, argsHasValue)
- }
- if s.err != nil {
- h.connectionClose = true
- return 0, s.err
- }
- if h.contentLength < 0 {
- h.contentLengthBytes = h.contentLengthBytes[:0]
- }
- if h.contentLength == -2 && !h.ConnectionUpgrade() && !h.mustSkipContentLength() {
- // According to modern HTTP/1.1 specifications (RFC 7230):
- // `identity` as a value for `Transfer-Encoding` was removed
- // in the errata to RFC 2616.
- // Therefore, we do not include `Transfer-Encoding: identity` in the header.
- // See: https://github.com/valyala/fasthttp/issues/1909
- h.connectionClose = true
- }
- if h.noHTTP11 && !h.connectionClose {
- // close connection for non-http/1.1 response unless 'Connection: keep-alive' is set.
- v := peekArgBytes(h.h, strConnection)
- h.connectionClose = !hasHeaderValue(v, strKeepAlive)
- }
- return len(buf) - len(s.b), nil
- }
- func (h *RequestHeader) parseHeaders(buf []byte) (int, error) {
- h.contentLength = -2
- contentLengthSeen := false
- var s headerScanner
- s.b = buf
- for s.next() {
- if len(s.key) == 0 {
- h.connectionClose = true
- return 0, fmt.Errorf("invalid header key %q", s.key)
- }
- disableNormalizing := h.disableNormalizing
- for _, ch := range s.key {
- if !validHeaderFieldByte(ch) {
- if ch == ' ' {
- disableNormalizing = true
- continue
- }
- h.connectionClose = true
- return 0, fmt.Errorf("invalid header key %q", s.key)
- }
- }
- normalizeHeaderKey(s.key, disableNormalizing)
- for _, ch := range s.value {
- if !validHeaderValueByte(ch) {
- h.connectionClose = true
- return 0, fmt.Errorf("invalid header value %q", s.value)
- }
- }
- if h.disableSpecialHeader {
- h.h = appendArgBytes(h.h, s.key, s.value, argsHasValue)
- continue
- }
- switch s.key[0] | 0x20 {
- case 'h':
- if caseInsensitiveCompare(s.key, strHost) {
- h.host = append(h.host[:0], s.value...)
- continue
- }
- case 'u':
- if caseInsensitiveCompare(s.key, strUserAgent) {
- h.userAgent = append(h.userAgent[:0], s.value...)
- continue
- }
- case 'c':
- if caseInsensitiveCompare(s.key, strContentType) {
- h.contentType = append(h.contentType[:0], s.value...)
- continue
- }
- if caseInsensitiveCompare(s.key, strContentLength) {
- if contentLengthSeen {
- h.connectionClose = true
- return 0, errors.New("duplicate Content-Length header")
- }
- contentLengthSeen = true
- if h.contentLength != -1 {
- var err error
- h.contentLength, err = parseContentLength(s.value)
- if err != nil {
- h.contentLength = -2
- h.connectionClose = true
- return 0, err
- }
- h.contentLengthBytes = append(h.contentLengthBytes[:0], s.value...)
- }
- continue
- }
- if caseInsensitiveCompare(s.key, strConnection) {
- if bytes.Equal(s.value, strClose) {
- h.connectionClose = true
- } else {
- h.connectionClose = false
- h.h = appendArgBytes(h.h, s.key, s.value, argsHasValue)
- }
- continue
- }
- case 't':
- if caseInsensitiveCompare(s.key, strTransferEncoding) {
- isIdentity := caseInsensitiveCompare(s.value, strIdentity)
- isChunked := caseInsensitiveCompare(s.value, strChunked)
- if !isIdentity && !isChunked {
- h.connectionClose = true
- if h.secureErrorLogMessage {
- return 0, errors.New("unsupported Transfer-Encoding")
- }
- return 0, fmt.Errorf("unsupported Transfer-Encoding: %q", s.value)
- }
- if isChunked {
- h.contentLength = -1
- h.h = setArgBytes(h.h, strTransferEncoding, strChunked, argsHasValue)
- }
- continue
- }
- if caseInsensitiveCompare(s.key, strTrailer) {
- err := h.SetTrailerBytes(s.value)
- if err != nil {
- h.connectionClose = true
- return 0, err
- }
- continue
- }
- }
- h.h = appendArgBytes(h.h, s.key, s.value, argsHasValue)
- }
- if s.err != nil {
- h.connectionClose = true
- return 0, s.err
- }
- if h.contentLength < 0 {
- h.contentLengthBytes = h.contentLengthBytes[:0]
- }
- if h.noHTTP11 && !h.connectionClose {
- // close connection for non-http/1.1 request unless 'Connection: keep-alive' is set.
- v := peekArgBytes(h.h, strConnection)
- h.connectionClose = !hasHeaderValue(v, strKeepAlive)
- }
- return s.hLen, nil
- }
- func (h *RequestHeader) collectCookies() {
- if h.cookiesCollected {
- return
- }
- for i, n := 0, len(h.h); i < n; i++ {
- kv := &h.h[i]
- if caseInsensitiveCompare(kv.key, strCookie) {
- h.cookies = parseRequestCookies(h.cookies, kv.value)
- tmp := *kv
- copy(h.h[i:], h.h[i+1:])
- n--
- i--
- h.h[n] = tmp
- h.h = h.h[:n]
- }
- }
- h.cookiesCollected = true
- }
- var errNonNumericChars = errors.New("non-numeric chars found")
- func parseContentLength(b []byte) (int, error) {
- v, n, err := parseUintBuf(b)
- if err != nil {
- return -1, fmt.Errorf("cannot parse Content-Length: %w", err)
- }
- if n != len(b) {
- return -1, fmt.Errorf("cannot parse Content-Length: %w", errNonNumericChars)
- }
- return v, nil
- }
- type headerScanner struct {
- err error
- b []byte
- key []byte
- value []byte
- // hLen stores header subslice len
- hLen int
- // by checking whether the next line contains a colon or not to tell
- // it's a header entry or a multi line value of current header entry.
- // the side effect of this operation is that we know the index of the
- // next colon and new line, so this can be used during next iteration,
- // instead of find them again.
- nextColon int
- nextNewLine int
- initialized bool
- // This is only used to print the deprecated newline separator warning.
- // TODO: Remove this again once the newline separator is removed.
- warned bool
- }
- // DeprecatedNewlineIncludeContext is used to control whether the context of the
- // header is included in the warning message about the deprecated newline
- // separator.
- // Warning: this can potentially leak sensitive information such as auth headers.
- var DeprecatedNewlineIncludeContext atomic.Bool
- // TODO: Remove this again once the newline separator is removed.
- var warnedAboutDeprecatedNewlineSeparatorLimiter atomic.Int64
- func (s *headerScanner) next() bool {
- if !s.initialized {
- s.nextColon = -1
- s.nextNewLine = -1
- s.initialized = true
- }
- bLen := len(s.b)
- if bLen >= 2 && s.b[0] == rChar && s.b[1] == nChar {
- s.b = s.b[2:]
- s.hLen += 2
- return false
- }
- if bLen >= 1 && s.b[0] == nChar {
- s.b = s.b[1:]
- s.hLen++
- return false
- }
- var n int
- if s.nextColon >= 0 {
- n = s.nextColon
- s.nextColon = -1
- } else {
- n = bytes.IndexByte(s.b, ':')
- // There can't be a \n inside the header name, check for this.
- x := bytes.IndexByte(s.b, nChar)
- if x < 0 {
- // A header name should always at some point be followed by a \n
- // even if it's the one that terminates the header block.
- s.err = errNeedMore
- return false
- }
- if x < n {
- // There was a \n before the :
- s.err = errInvalidName
- return false
- }
- // If the character before '\n' isn't '\r', print a warning.
- if !s.warned && x > 1 && s.b[x-1] != rChar {
- // Only warn once per second.
- now := time.Now().Unix()
- if warnedAboutDeprecatedNewlineSeparatorLimiter.Load() < now {
- if warnedAboutDeprecatedNewlineSeparatorLimiter.Swap(now) < now {
- if DeprecatedNewlineIncludeContext.Load() {
- // Include 20 characters after the '\n'.
- xx := x + 20
- if len(s.b) < xx {
- xx = len(s.b)
- }
- slog.Error("Deprecated newline only separator found in header", "context", fmt.Sprintf("%q", s.b[:xx]))
- } else {
- slog.Error("Deprecated newline only separator found in header")
- }
- s.warned = true
- }
- }
- }
- }
- if n < 0 {
- s.err = errNeedMore
- return false
- }
- s.key = s.b[:n]
- n++
- for len(s.b) > n && (s.b[n] == ' ' || s.b[n] == '\t') {
- n++
- // the newline index is a relative index, and lines below trimmed `s.b` by `n`,
- // so the relative newline index also shifted forward. it's safe to decrease
- // to a minus value, it means it's invalid, and will find the newline again.
- s.nextNewLine--
- }
- s.hLen += n
- s.b = s.b[n:]
- if s.nextNewLine >= 0 {
- n = s.nextNewLine
- s.nextNewLine = -1
- } else {
- n = bytes.IndexByte(s.b, nChar)
- }
- if n < 0 {
- s.err = errNeedMore
- return false
- }
- for n+1 < len(s.b) {
- if s.b[n+1] != ' ' && s.b[n+1] != '\t' {
- break
- }
- d := bytes.IndexByte(s.b[n+1:], nChar)
- if d <= 0 {
- break
- } else if d == 1 && s.b[n+1] == rChar {
- break
- }
- e := n + d + 1
- if c := bytes.IndexByte(s.b[n+1:e], ':'); c >= 0 {
- s.nextColon = c
- s.nextNewLine = d - c - 1
- break
- }
- n = e
- }
- if n >= len(s.b) {
- s.err = errNeedMore
- return false
- }
- s.value = s.b[:n]
- s.hLen += n + 1
- s.b = s.b[n+1:]
- if n > 0 && s.value[n-1] == rChar {
- n--
- }
- for n > 0 && (s.value[n-1] == ' ' || s.value[n-1] == '\t') {
- n--
- }
- s.value = s.value[:n]
- if bytes.Contains(s.b, strCRLF) {
- s.value = normalizeHeaderValue(s.value)
- }
- return true
- }
- type headerValueScanner struct {
- b []byte
- value []byte
- }
- func (s *headerValueScanner) next() bool {
- b := s.b
- if len(b) == 0 {
- return false
- }
- n := bytes.IndexByte(b, ',')
- if n < 0 {
- s.value = stripSpace(b)
- s.b = b[len(b):]
- return true
- }
- s.value = stripSpace(b[:n])
- s.b = b[n+1:]
- return true
- }
- func stripSpace(b []byte) []byte {
- for len(b) > 0 && b[0] == ' ' {
- b = b[1:]
- }
- for len(b) > 0 && b[len(b)-1] == ' ' {
- b = b[:len(b)-1]
- }
- return b
- }
- func hasHeaderValue(s, value []byte) bool {
- var vs headerValueScanner
- vs.b = s
- for vs.next() {
- if caseInsensitiveCompare(vs.value, value) {
- return true
- }
- }
- return false
- }
- func nextLine(b []byte) ([]byte, []byte, error) {
- nNext := bytes.IndexByte(b, nChar)
- if nNext < 0 {
- return nil, nil, errNeedMore
- }
- n := nNext
- if n > 0 && b[n-1] == rChar {
- n--
- }
- return b[:n], b[nNext+1:], nil
- }
- func initHeaderKV(bufK, bufV []byte, key, value string, disableNormalizing bool) ([]byte, []byte) {
- bufK = getHeaderKeyBytes(bufK, key, disableNormalizing)
- // https://tools.ietf.org/html/rfc7230#section-3.2.4
- bufV = append(bufV[:0], value...)
- bufV = removeNewLines(bufV)
- return bufK, bufV
- }
- func getHeaderKeyBytes(bufK []byte, key string, disableNormalizing bool) []byte {
- bufK = append(bufK[:0], key...)
- normalizeHeaderKey(bufK, disableNormalizing || bytes.IndexByte(bufK, ' ') != -1)
- return bufK
- }
- func normalizeHeaderValue(ov []byte) (nv []byte) {
- nv = ov
- length := len(ov)
- if length <= 0 {
- return
- }
- write := 0
- shrunk := 0
- once := false
- lineStart := false
- for read := 0; read < length; read++ {
- c := ov[read]
- switch {
- case c == rChar || c == nChar:
- shrunk++
- if c == nChar {
- lineStart = true
- once = false
- }
- continue
- case lineStart && (c == '\t' || c == ' '):
- if !once {
- c = ' '
- once = true
- } else {
- shrunk++
- continue
- }
- default:
- lineStart = false
- }
- nv[write] = c
- write++
- }
- nv = nv[:length-shrunk]
- return
- }
- func normalizeHeaderKey(b []byte, disableNormalizing bool) {
- if disableNormalizing {
- return
- }
- n := len(b)
- if n == 0 {
- return
- }
- b[0] = toUpperTable[b[0]]
- for i := 1; i < n; i++ {
- p := &b[i]
- if *p == '-' {
- i++
- if i < n {
- b[i] = toUpperTable[b[i]]
- }
- continue
- }
- *p = toLowerTable[*p]
- }
- }
- // removeNewLines will replace `\r` and `\n` with an empty space.
- func removeNewLines(raw []byte) []byte {
- // check if a `\r` is present and save the position.
- // if no `\r` is found, check if a `\n` is present.
- foundR := bytes.IndexByte(raw, rChar)
- foundN := bytes.IndexByte(raw, nChar)
- start := 0
- switch {
- case foundN != -1:
- if foundR > foundN {
- start = foundN
- } else if foundR != -1 {
- start = foundR
- }
- case foundR != -1:
- start = foundR
- default:
- return raw
- }
- for i := start; i < len(raw); i++ {
- switch raw[i] {
- case rChar, nChar:
- raw[i] = ' '
- default:
- continue
- }
- }
- return raw
- }
- // AppendNormalizedHeaderKey appends normalized header key (name) to dst
- // and returns the resulting dst.
- //
- // Normalized header key starts with uppercase letter. The first letters
- // after dashes are also uppercased. All the other letters are lowercased.
- // Examples:
- //
- // - coNTENT-TYPe -> Content-Type
- // - HOST -> Host
- // - foo-bar-baz -> Foo-Bar-Baz
- func AppendNormalizedHeaderKey(dst []byte, key string) []byte {
- dst = append(dst, key...)
- normalizeHeaderKey(dst[len(dst)-len(key):], false)
- return dst
- }
- // AppendNormalizedHeaderKeyBytes appends normalized header key (name) to dst
- // and returns the resulting dst.
- //
- // Normalized header key starts with uppercase letter. The first letters
- // after dashes are also uppercased. All the other letters are lowercased.
- // Examples:
- //
- // - coNTENT-TYPe -> Content-Type
- // - HOST -> Host
- // - foo-bar-baz -> Foo-Bar-Baz
- func AppendNormalizedHeaderKeyBytes(dst, key []byte) []byte {
- return AppendNormalizedHeaderKey(dst, b2s(key))
- }
- func appendTrailerBytes(dst []byte, trailer [][]byte, sep []byte) []byte {
- for i, n := 0, len(trailer); i < n; i++ {
- dst = append(dst, trailer[i]...)
- if i+1 < n {
- dst = append(dst, sep...)
- }
- }
- return dst
- }
- func copyTrailer(dst, src [][]byte) [][]byte {
- if cap(dst) > len(src) {
- dst = dst[:len(src)]
- } else {
- dst = append(dst[:0], src...)
- }
- for i := range dst {
- dst[i] = make([]byte, len(src[i]))
- copy(dst[i], src[i])
- }
- return dst
- }
- var (
- errNeedMore = errors.New("need more data: cannot find trailing lf")
- errInvalidName = errors.New("invalid header name")
- errSmallBuffer = errors.New("small read buffer. Increase ReadBufferSize")
- )
- // ErrNothingRead is returned when a keep-alive connection is closed,
- // either because the remote closed it or because of a read timeout.
- type ErrNothingRead struct {
- error
- }
- // ErrSmallBuffer is returned when the provided buffer size is too small
- // for reading request and/or response headers.
- //
- // ReadBufferSize value from Server or clients should reduce the number
- // of such errors.
- type ErrSmallBuffer struct {
- error
- }
- func mustPeekBuffered(r *bufio.Reader) []byte {
- buf, err := r.Peek(r.Buffered())
- if len(buf) == 0 || err != nil {
- panic(fmt.Sprintf("bufio.Reader.Peek() returned unexpected data (%q, %v)", buf, err))
- }
- return buf
- }
- func mustDiscard(r *bufio.Reader, n int) {
- if _, err := r.Discard(n); err != nil {
- panic(fmt.Sprintf("bufio.Reader.Discard(%d) failed: %v", n, err))
- }
- }
|