http.go 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200
  1. package fasthttp
  2. import (
  3. "bufio"
  4. "bytes"
  5. "compress/gzip"
  6. "encoding/base64"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "mime/multipart"
  11. "net"
  12. "os"
  13. "sync"
  14. "time"
  15. "github.com/valyala/bytebufferpool"
  16. )
  17. // Request represents HTTP request.
  18. //
  19. // It is forbidden copying Request instances. Create new instances
  20. // and use CopyTo instead.
  21. //
  22. // Request instance MUST NOT be used from concurrently running goroutines.
  23. type Request struct {
  24. noCopy noCopy //nolint:unused,structcheck
  25. // Request header
  26. //
  27. // Copying Header by value is forbidden. Use pointer to Header instead.
  28. Header RequestHeader
  29. uri URI
  30. postArgs Args
  31. bodyStream io.Reader
  32. w requestBodyWriter
  33. body *bytebufferpool.ByteBuffer
  34. bodyRaw []byte
  35. multipartForm *multipart.Form
  36. multipartFormBoundary string
  37. secureErrorLogMessage bool
  38. // Group bool members in order to reduce Request object size.
  39. parsedURI bool
  40. parsedPostArgs bool
  41. keepBodyBuffer bool
  42. // Used by Server to indicate the request was received on a HTTPS endpoint.
  43. // Client/HostClient shouldn't use this field but should depend on the uri.scheme instead.
  44. isTLS bool
  45. // Request timeout. Usually set by DoDeadline or DoTimeout
  46. // if <= 0, means not set
  47. timeout time.Duration
  48. // Use Host header (request.Header.SetHost) instead of the host from SetRequestURI, SetHost, or URI().SetHost
  49. UseHostHeader bool
  50. }
  51. // Response represents HTTP response.
  52. //
  53. // It is forbidden copying Response instances. Create new instances
  54. // and use CopyTo instead.
  55. //
  56. // Response instance MUST NOT be used from concurrently running goroutines.
  57. type Response struct {
  58. noCopy noCopy //nolint:unused,structcheck
  59. // Response header
  60. //
  61. // Copying Header by value is forbidden. Use pointer to Header instead.
  62. Header ResponseHeader
  63. // Flush headers as soon as possible without waiting for first body bytes.
  64. // Relevant for bodyStream only.
  65. ImmediateHeaderFlush bool
  66. bodyStream io.Reader
  67. w responseBodyWriter
  68. body *bytebufferpool.ByteBuffer
  69. bodyRaw []byte
  70. // Response.Read() skips reading body if set to true.
  71. // Use it for reading HEAD responses.
  72. //
  73. // Response.Write() skips writing body if set to true.
  74. // Use it for writing HEAD responses.
  75. SkipBody bool
  76. keepBodyBuffer bool
  77. secureErrorLogMessage bool
  78. // Remote TCPAddr from concurrently net.Conn
  79. raddr net.Addr
  80. // Local TCPAddr from concurrently net.Conn
  81. laddr net.Addr
  82. }
  83. // SetHost sets host for the request.
  84. func (req *Request) SetHost(host string) {
  85. req.URI().SetHost(host)
  86. }
  87. // SetHostBytes sets host for the request.
  88. func (req *Request) SetHostBytes(host []byte) {
  89. req.URI().SetHostBytes(host)
  90. }
  91. // Host returns the host for the given request.
  92. func (req *Request) Host() []byte {
  93. return req.URI().Host()
  94. }
  95. // SetRequestURI sets RequestURI.
  96. func (req *Request) SetRequestURI(requestURI string) {
  97. req.Header.SetRequestURI(requestURI)
  98. req.parsedURI = false
  99. }
  100. // SetRequestURIBytes sets RequestURI.
  101. func (req *Request) SetRequestURIBytes(requestURI []byte) {
  102. req.Header.SetRequestURIBytes(requestURI)
  103. req.parsedURI = false
  104. }
  105. // RequestURI returns request's URI.
  106. func (req *Request) RequestURI() []byte {
  107. if req.parsedURI {
  108. requestURI := req.uri.RequestURI()
  109. req.SetRequestURIBytes(requestURI)
  110. }
  111. return req.Header.RequestURI()
  112. }
  113. // StatusCode returns response status code.
  114. func (resp *Response) StatusCode() int {
  115. return resp.Header.StatusCode()
  116. }
  117. // SetStatusCode sets response status code.
  118. func (resp *Response) SetStatusCode(statusCode int) {
  119. resp.Header.SetStatusCode(statusCode)
  120. }
  121. // ConnectionClose returns true if 'Connection: close' header is set.
  122. func (resp *Response) ConnectionClose() bool {
  123. return resp.Header.ConnectionClose()
  124. }
  125. // SetConnectionClose sets 'Connection: close' header.
  126. func (resp *Response) SetConnectionClose() {
  127. resp.Header.SetConnectionClose()
  128. }
  129. // ConnectionClose returns true if 'Connection: close' header is set.
  130. func (req *Request) ConnectionClose() bool {
  131. return req.Header.ConnectionClose()
  132. }
  133. // SetConnectionClose sets 'Connection: close' header.
  134. func (req *Request) SetConnectionClose() {
  135. req.Header.SetConnectionClose()
  136. }
  137. // SendFile registers file on the given path to be used as response body
  138. // when Write is called.
  139. //
  140. // Note that SendFile doesn't set Content-Type, so set it yourself
  141. // with Header.SetContentType.
  142. func (resp *Response) SendFile(path string) error {
  143. f, err := os.Open(path)
  144. if err != nil {
  145. return err
  146. }
  147. fileInfo, err := f.Stat()
  148. if err != nil {
  149. f.Close()
  150. return err
  151. }
  152. size64 := fileInfo.Size()
  153. size := int(size64)
  154. if int64(size) != size64 {
  155. size = -1
  156. }
  157. resp.Header.SetLastModified(fileInfo.ModTime())
  158. resp.SetBodyStream(f, size)
  159. return nil
  160. }
  161. // SetBodyStream sets request body stream and, optionally body size.
  162. //
  163. // If bodySize is >= 0, then the bodyStream must provide exactly bodySize bytes
  164. // before returning io.EOF.
  165. //
  166. // If bodySize < 0, then bodyStream is read until io.EOF.
  167. //
  168. // bodyStream.Close() is called after finishing reading all body data
  169. // if it implements io.Closer.
  170. //
  171. // Note that GET and HEAD requests cannot have body.
  172. //
  173. // See also SetBodyStreamWriter.
  174. func (req *Request) SetBodyStream(bodyStream io.Reader, bodySize int) {
  175. req.ResetBody()
  176. req.bodyStream = bodyStream
  177. req.Header.SetContentLength(bodySize)
  178. }
  179. // SetBodyStream sets response body stream and, optionally body size.
  180. //
  181. // If bodySize is >= 0, then the bodyStream must provide exactly bodySize bytes
  182. // before returning io.EOF.
  183. //
  184. // If bodySize < 0, then bodyStream is read until io.EOF.
  185. //
  186. // bodyStream.Close() is called after finishing reading all body data
  187. // if it implements io.Closer.
  188. //
  189. // See also SetBodyStreamWriter.
  190. func (resp *Response) SetBodyStream(bodyStream io.Reader, bodySize int) {
  191. resp.ResetBody()
  192. resp.bodyStream = bodyStream
  193. resp.Header.SetContentLength(bodySize)
  194. }
  195. // IsBodyStream returns true if body is set via SetBodyStream*
  196. func (req *Request) IsBodyStream() bool {
  197. return req.bodyStream != nil
  198. }
  199. // IsBodyStream returns true if body is set via SetBodyStream*
  200. func (resp *Response) IsBodyStream() bool {
  201. return resp.bodyStream != nil
  202. }
  203. // SetBodyStreamWriter registers the given sw for populating request body.
  204. //
  205. // This function may be used in the following cases:
  206. //
  207. // * if request body is too big (more than 10MB).
  208. // * if request body is streamed from slow external sources.
  209. // * if request body must be streamed to the server in chunks
  210. // (aka `http client push` or `chunked transfer-encoding`).
  211. //
  212. // Note that GET and HEAD requests cannot have body.
  213. //
  214. /// See also SetBodyStream.
  215. func (req *Request) SetBodyStreamWriter(sw StreamWriter) {
  216. sr := NewStreamReader(sw)
  217. req.SetBodyStream(sr, -1)
  218. }
  219. // SetBodyStreamWriter registers the given sw for populating response body.
  220. //
  221. // This function may be used in the following cases:
  222. //
  223. // * if response body is too big (more than 10MB).
  224. // * if response body is streamed from slow external sources.
  225. // * if response body must be streamed to the client in chunks
  226. // (aka `http server push` or `chunked transfer-encoding`).
  227. //
  228. // See also SetBodyStream.
  229. func (resp *Response) SetBodyStreamWriter(sw StreamWriter) {
  230. sr := NewStreamReader(sw)
  231. resp.SetBodyStream(sr, -1)
  232. }
  233. // BodyWriter returns writer for populating response body.
  234. //
  235. // If used inside RequestHandler, the returned writer must not be used
  236. // after returning from RequestHandler. Use RequestCtx.Write
  237. // or SetBodyStreamWriter in this case.
  238. func (resp *Response) BodyWriter() io.Writer {
  239. resp.w.r = resp
  240. return &resp.w
  241. }
  242. // BodyWriter returns writer for populating request body.
  243. func (req *Request) BodyWriter() io.Writer {
  244. req.w.r = req
  245. return &req.w
  246. }
  247. type responseBodyWriter struct {
  248. r *Response
  249. }
  250. func (w *responseBodyWriter) Write(p []byte) (int, error) {
  251. w.r.AppendBody(p)
  252. return len(p), nil
  253. }
  254. type requestBodyWriter struct {
  255. r *Request
  256. }
  257. func (w *requestBodyWriter) Write(p []byte) (int, error) {
  258. w.r.AppendBody(p)
  259. return len(p), nil
  260. }
  261. func (resp *Response) parseNetConn(conn net.Conn) {
  262. resp.raddr = conn.RemoteAddr()
  263. resp.laddr = conn.LocalAddr()
  264. }
  265. // RemoteAddr returns the remote network address. The Addr returned is shared
  266. // by all invocations of RemoteAddr, so do not modify it.
  267. func (resp *Response) RemoteAddr() net.Addr {
  268. return resp.raddr
  269. }
  270. // LocalAddr returns the local network address. The Addr returned is shared
  271. // by all invocations of LocalAddr, so do not modify it.
  272. func (resp *Response) LocalAddr() net.Addr {
  273. return resp.laddr
  274. }
  275. // Body returns response body.
  276. //
  277. // The returned value is valid until the response is released,
  278. // either though ReleaseResponse or your request handler returning.
  279. // Do not store references to returned value. Make copies instead.
  280. func (resp *Response) Body() []byte {
  281. if resp.bodyStream != nil {
  282. bodyBuf := resp.bodyBuffer()
  283. bodyBuf.Reset()
  284. _, err := copyZeroAlloc(bodyBuf, resp.bodyStream)
  285. resp.closeBodyStream() //nolint:errcheck
  286. if err != nil {
  287. bodyBuf.SetString(err.Error())
  288. }
  289. }
  290. return resp.bodyBytes()
  291. }
  292. func (resp *Response) bodyBytes() []byte {
  293. if resp.bodyRaw != nil {
  294. return resp.bodyRaw
  295. }
  296. if resp.body == nil {
  297. return nil
  298. }
  299. return resp.body.B
  300. }
  301. func (req *Request) bodyBytes() []byte {
  302. if req.bodyRaw != nil {
  303. return req.bodyRaw
  304. }
  305. if req.bodyStream != nil {
  306. bodyBuf := req.bodyBuffer()
  307. bodyBuf.Reset()
  308. _, err := copyZeroAlloc(bodyBuf, req.bodyStream)
  309. req.closeBodyStream() //nolint:errcheck
  310. if err != nil {
  311. bodyBuf.SetString(err.Error())
  312. }
  313. }
  314. if req.body == nil {
  315. return nil
  316. }
  317. return req.body.B
  318. }
  319. func (resp *Response) bodyBuffer() *bytebufferpool.ByteBuffer {
  320. if resp.body == nil {
  321. resp.body = responseBodyPool.Get()
  322. }
  323. resp.bodyRaw = nil
  324. return resp.body
  325. }
  326. func (req *Request) bodyBuffer() *bytebufferpool.ByteBuffer {
  327. if req.body == nil {
  328. req.body = requestBodyPool.Get()
  329. }
  330. req.bodyRaw = nil
  331. return req.body
  332. }
  333. var (
  334. responseBodyPool bytebufferpool.Pool
  335. requestBodyPool bytebufferpool.Pool
  336. )
  337. // BodyGunzip returns un-gzipped body data.
  338. //
  339. // This method may be used if the request header contains
  340. // 'Content-Encoding: gzip' for reading un-gzipped body.
  341. // Use Body for reading gzipped request body.
  342. func (req *Request) BodyGunzip() ([]byte, error) {
  343. return gunzipData(req.Body())
  344. }
  345. // BodyGunzip returns un-gzipped body data.
  346. //
  347. // This method may be used if the response header contains
  348. // 'Content-Encoding: gzip' for reading un-gzipped body.
  349. // Use Body for reading gzipped response body.
  350. func (resp *Response) BodyGunzip() ([]byte, error) {
  351. return gunzipData(resp.Body())
  352. }
  353. func gunzipData(p []byte) ([]byte, error) {
  354. var bb bytebufferpool.ByteBuffer
  355. _, err := WriteGunzip(&bb, p)
  356. if err != nil {
  357. return nil, err
  358. }
  359. return bb.B, nil
  360. }
  361. // BodyUnbrotli returns un-brotlied body data.
  362. //
  363. // This method may be used if the request header contains
  364. // 'Content-Encoding: br' for reading un-brotlied body.
  365. // Use Body for reading brotlied request body.
  366. func (req *Request) BodyUnbrotli() ([]byte, error) {
  367. return unBrotliData(req.Body())
  368. }
  369. // BodyUnbrotli returns un-brotlied body data.
  370. //
  371. // This method may be used if the response header contains
  372. // 'Content-Encoding: br' for reading un-brotlied body.
  373. // Use Body for reading brotlied response body.
  374. func (resp *Response) BodyUnbrotli() ([]byte, error) {
  375. return unBrotliData(resp.Body())
  376. }
  377. func unBrotliData(p []byte) ([]byte, error) {
  378. var bb bytebufferpool.ByteBuffer
  379. _, err := WriteUnbrotli(&bb, p)
  380. if err != nil {
  381. return nil, err
  382. }
  383. return bb.B, nil
  384. }
  385. // BodyInflate returns inflated body data.
  386. //
  387. // This method may be used if the response header contains
  388. // 'Content-Encoding: deflate' for reading inflated request body.
  389. // Use Body for reading deflated request body.
  390. func (req *Request) BodyInflate() ([]byte, error) {
  391. return inflateData(req.Body())
  392. }
  393. // BodyInflate returns inflated body data.
  394. //
  395. // This method may be used if the response header contains
  396. // 'Content-Encoding: deflate' for reading inflated response body.
  397. // Use Body for reading deflated response body.
  398. func (resp *Response) BodyInflate() ([]byte, error) {
  399. return inflateData(resp.Body())
  400. }
  401. func (ctx *RequestCtx) RequestBodyStream() io.Reader {
  402. return ctx.Request.bodyStream
  403. }
  404. func inflateData(p []byte) ([]byte, error) {
  405. var bb bytebufferpool.ByteBuffer
  406. _, err := WriteInflate(&bb, p)
  407. if err != nil {
  408. return nil, err
  409. }
  410. return bb.B, nil
  411. }
  412. // BodyWriteTo writes request body to w.
  413. func (req *Request) BodyWriteTo(w io.Writer) error {
  414. if req.bodyStream != nil {
  415. _, err := copyZeroAlloc(w, req.bodyStream)
  416. req.closeBodyStream() //nolint:errcheck
  417. return err
  418. }
  419. if req.onlyMultipartForm() {
  420. return WriteMultipartForm(w, req.multipartForm, req.multipartFormBoundary)
  421. }
  422. _, err := w.Write(req.bodyBytes())
  423. return err
  424. }
  425. // BodyWriteTo writes response body to w.
  426. func (resp *Response) BodyWriteTo(w io.Writer) error {
  427. if resp.bodyStream != nil {
  428. _, err := copyZeroAlloc(w, resp.bodyStream)
  429. resp.closeBodyStream() //nolint:errcheck
  430. return err
  431. }
  432. _, err := w.Write(resp.bodyBytes())
  433. return err
  434. }
  435. // AppendBody appends p to response body.
  436. //
  437. // It is safe re-using p after the function returns.
  438. func (resp *Response) AppendBody(p []byte) {
  439. resp.closeBodyStream() //nolint:errcheck
  440. resp.bodyBuffer().Write(p) //nolint:errcheck
  441. }
  442. // AppendBodyString appends s to response body.
  443. func (resp *Response) AppendBodyString(s string) {
  444. resp.closeBodyStream() //nolint:errcheck
  445. resp.bodyBuffer().WriteString(s) //nolint:errcheck
  446. }
  447. // SetBody sets response body.
  448. //
  449. // It is safe re-using body argument after the function returns.
  450. func (resp *Response) SetBody(body []byte) {
  451. resp.closeBodyStream() //nolint:errcheck
  452. bodyBuf := resp.bodyBuffer()
  453. bodyBuf.Reset()
  454. bodyBuf.Write(body) //nolint:errcheck
  455. }
  456. // SetBodyString sets response body.
  457. func (resp *Response) SetBodyString(body string) {
  458. resp.closeBodyStream() //nolint:errcheck
  459. bodyBuf := resp.bodyBuffer()
  460. bodyBuf.Reset()
  461. bodyBuf.WriteString(body) //nolint:errcheck
  462. }
  463. // ResetBody resets response body.
  464. func (resp *Response) ResetBody() {
  465. resp.bodyRaw = nil
  466. resp.closeBodyStream() //nolint:errcheck
  467. if resp.body != nil {
  468. if resp.keepBodyBuffer {
  469. resp.body.Reset()
  470. } else {
  471. responseBodyPool.Put(resp.body)
  472. resp.body = nil
  473. }
  474. }
  475. }
  476. // SetBodyRaw sets response body, but without copying it.
  477. //
  478. // From this point onward the body argument must not be changed.
  479. func (resp *Response) SetBodyRaw(body []byte) {
  480. resp.ResetBody()
  481. resp.bodyRaw = body
  482. }
  483. // SetBodyRaw sets response body, but without copying it.
  484. //
  485. // From this point onward the body argument must not be changed.
  486. func (req *Request) SetBodyRaw(body []byte) {
  487. req.ResetBody()
  488. req.bodyRaw = body
  489. }
  490. // ReleaseBody retires the response body if it is greater than "size" bytes.
  491. //
  492. // This permits GC to reclaim the large buffer. If used, must be before
  493. // ReleaseResponse.
  494. //
  495. // Use this method only if you really understand how it works.
  496. // The majority of workloads don't need this method.
  497. func (resp *Response) ReleaseBody(size int) {
  498. resp.bodyRaw = nil
  499. if cap(resp.body.B) > size {
  500. resp.closeBodyStream() //nolint:errcheck
  501. resp.body = nil
  502. }
  503. }
  504. // ReleaseBody retires the request body if it is greater than "size" bytes.
  505. //
  506. // This permits GC to reclaim the large buffer. If used, must be before
  507. // ReleaseRequest.
  508. //
  509. // Use this method only if you really understand how it works.
  510. // The majority of workloads don't need this method.
  511. func (req *Request) ReleaseBody(size int) {
  512. req.bodyRaw = nil
  513. if cap(req.body.B) > size {
  514. req.closeBodyStream() //nolint:errcheck
  515. req.body = nil
  516. }
  517. }
  518. // SwapBody swaps response body with the given body and returns
  519. // the previous response body.
  520. //
  521. // It is forbidden to use the body passed to SwapBody after
  522. // the function returns.
  523. func (resp *Response) SwapBody(body []byte) []byte {
  524. bb := resp.bodyBuffer()
  525. if resp.bodyStream != nil {
  526. bb.Reset()
  527. _, err := copyZeroAlloc(bb, resp.bodyStream)
  528. resp.closeBodyStream() //nolint:errcheck
  529. if err != nil {
  530. bb.Reset()
  531. bb.SetString(err.Error())
  532. }
  533. }
  534. resp.bodyRaw = nil
  535. oldBody := bb.B
  536. bb.B = body
  537. return oldBody
  538. }
  539. // SwapBody swaps request body with the given body and returns
  540. // the previous request body.
  541. //
  542. // It is forbidden to use the body passed to SwapBody after
  543. // the function returns.
  544. func (req *Request) SwapBody(body []byte) []byte {
  545. bb := req.bodyBuffer()
  546. if req.bodyStream != nil {
  547. bb.Reset()
  548. _, err := copyZeroAlloc(bb, req.bodyStream)
  549. req.closeBodyStream() //nolint:errcheck
  550. if err != nil {
  551. bb.Reset()
  552. bb.SetString(err.Error())
  553. }
  554. }
  555. req.bodyRaw = nil
  556. oldBody := bb.B
  557. bb.B = body
  558. return oldBody
  559. }
  560. // Body returns request body.
  561. //
  562. // The returned value is valid until the request is released,
  563. // either though ReleaseRequest or your request handler returning.
  564. // Do not store references to returned value. Make copies instead.
  565. func (req *Request) Body() []byte {
  566. if req.bodyRaw != nil {
  567. return req.bodyRaw
  568. } else if req.onlyMultipartForm() {
  569. body, err := marshalMultipartForm(req.multipartForm, req.multipartFormBoundary)
  570. if err != nil {
  571. return []byte(err.Error())
  572. }
  573. return body
  574. }
  575. return req.bodyBytes()
  576. }
  577. // AppendBody appends p to request body.
  578. //
  579. // It is safe re-using p after the function returns.
  580. func (req *Request) AppendBody(p []byte) {
  581. req.RemoveMultipartFormFiles()
  582. req.closeBodyStream() //nolint:errcheck
  583. req.bodyBuffer().Write(p) //nolint:errcheck
  584. }
  585. // AppendBodyString appends s to request body.
  586. func (req *Request) AppendBodyString(s string) {
  587. req.RemoveMultipartFormFiles()
  588. req.closeBodyStream() //nolint:errcheck
  589. req.bodyBuffer().WriteString(s) //nolint:errcheck
  590. }
  591. // SetBody sets request body.
  592. //
  593. // It is safe re-using body argument after the function returns.
  594. func (req *Request) SetBody(body []byte) {
  595. req.RemoveMultipartFormFiles()
  596. req.closeBodyStream() //nolint:errcheck
  597. req.bodyBuffer().Set(body)
  598. }
  599. // SetBodyString sets request body.
  600. func (req *Request) SetBodyString(body string) {
  601. req.RemoveMultipartFormFiles()
  602. req.closeBodyStream() //nolint:errcheck
  603. req.bodyBuffer().SetString(body)
  604. }
  605. // ResetBody resets request body.
  606. func (req *Request) ResetBody() {
  607. req.bodyRaw = nil
  608. req.RemoveMultipartFormFiles()
  609. req.closeBodyStream() //nolint:errcheck
  610. if req.body != nil {
  611. if req.keepBodyBuffer {
  612. req.body.Reset()
  613. } else {
  614. requestBodyPool.Put(req.body)
  615. req.body = nil
  616. }
  617. }
  618. }
  619. // CopyTo copies req contents to dst except of body stream.
  620. func (req *Request) CopyTo(dst *Request) {
  621. req.copyToSkipBody(dst)
  622. if req.bodyRaw != nil {
  623. dst.bodyRaw = req.bodyRaw
  624. if dst.body != nil {
  625. dst.body.Reset()
  626. }
  627. } else if req.body != nil {
  628. dst.bodyBuffer().Set(req.body.B)
  629. } else if dst.body != nil {
  630. dst.body.Reset()
  631. }
  632. }
  633. func (req *Request) copyToSkipBody(dst *Request) {
  634. dst.Reset()
  635. req.Header.CopyTo(&dst.Header)
  636. req.uri.CopyTo(&dst.uri)
  637. dst.parsedURI = req.parsedURI
  638. req.postArgs.CopyTo(&dst.postArgs)
  639. dst.parsedPostArgs = req.parsedPostArgs
  640. dst.isTLS = req.isTLS
  641. dst.UseHostHeader = req.UseHostHeader
  642. // do not copy multipartForm - it will be automatically
  643. // re-created on the first call to MultipartForm.
  644. }
  645. // CopyTo copies resp contents to dst except of body stream.
  646. func (resp *Response) CopyTo(dst *Response) {
  647. resp.copyToSkipBody(dst)
  648. if resp.bodyRaw != nil {
  649. dst.bodyRaw = resp.bodyRaw
  650. if dst.body != nil {
  651. dst.body.Reset()
  652. }
  653. } else if resp.body != nil {
  654. dst.bodyBuffer().Set(resp.body.B)
  655. } else if dst.body != nil {
  656. dst.body.Reset()
  657. }
  658. }
  659. func (resp *Response) copyToSkipBody(dst *Response) {
  660. dst.Reset()
  661. resp.Header.CopyTo(&dst.Header)
  662. dst.SkipBody = resp.SkipBody
  663. dst.raddr = resp.raddr
  664. dst.laddr = resp.laddr
  665. }
  666. func swapRequestBody(a, b *Request) {
  667. a.body, b.body = b.body, a.body
  668. a.bodyRaw, b.bodyRaw = b.bodyRaw, a.bodyRaw
  669. a.bodyStream, b.bodyStream = b.bodyStream, a.bodyStream
  670. }
  671. func swapResponseBody(a, b *Response) {
  672. a.body, b.body = b.body, a.body
  673. a.bodyRaw, b.bodyRaw = b.bodyRaw, a.bodyRaw
  674. a.bodyStream, b.bodyStream = b.bodyStream, a.bodyStream
  675. }
  676. // URI returns request URI
  677. func (req *Request) URI() *URI {
  678. req.parseURI() //nolint:errcheck
  679. return &req.uri
  680. }
  681. // SetURI initializes request URI
  682. // Use this method if a single URI may be reused across multiple requests.
  683. // Otherwise, you can just use SetRequestURI() and it will be parsed as new URI.
  684. // The URI is copied and can be safely modified later.
  685. func (req *Request) SetURI(newUri *URI) {
  686. if newUri != nil {
  687. newUri.CopyTo(&req.uri)
  688. req.parsedURI = true
  689. return
  690. }
  691. req.uri.Reset()
  692. req.parsedURI = false
  693. }
  694. func (req *Request) parseURI() error {
  695. if req.parsedURI {
  696. return nil
  697. }
  698. req.parsedURI = true
  699. return req.uri.parse(req.Header.Host(), req.Header.RequestURI(), req.isTLS)
  700. }
  701. // PostArgs returns POST arguments.
  702. func (req *Request) PostArgs() *Args {
  703. req.parsePostArgs()
  704. return &req.postArgs
  705. }
  706. func (req *Request) parsePostArgs() {
  707. if req.parsedPostArgs {
  708. return
  709. }
  710. req.parsedPostArgs = true
  711. if !bytes.HasPrefix(req.Header.ContentType(), strPostArgsContentType) {
  712. return
  713. }
  714. req.postArgs.ParseBytes(req.bodyBytes())
  715. }
  716. // ErrNoMultipartForm means that the request's Content-Type
  717. // isn't 'multipart/form-data'.
  718. var ErrNoMultipartForm = errors.New("request has no multipart/form-data Content-Type")
  719. // MultipartForm returns requests's multipart form.
  720. //
  721. // Returns ErrNoMultipartForm if request's Content-Type
  722. // isn't 'multipart/form-data'.
  723. //
  724. // RemoveMultipartFormFiles must be called after returned multipart form
  725. // is processed.
  726. func (req *Request) MultipartForm() (*multipart.Form, error) {
  727. if req.multipartForm != nil {
  728. return req.multipartForm, nil
  729. }
  730. req.multipartFormBoundary = string(req.Header.MultipartFormBoundary())
  731. if len(req.multipartFormBoundary) == 0 {
  732. return nil, ErrNoMultipartForm
  733. }
  734. var err error
  735. ce := req.Header.peek(strContentEncoding)
  736. if req.bodyStream != nil {
  737. bodyStream := req.bodyStream
  738. if bytes.Equal(ce, strGzip) {
  739. // Do not care about memory usage here.
  740. if bodyStream, err = gzip.NewReader(bodyStream); err != nil {
  741. return nil, fmt.Errorf("cannot gunzip request body: %w", err)
  742. }
  743. } else if len(ce) > 0 {
  744. return nil, fmt.Errorf("unsupported Content-Encoding: %q", ce)
  745. }
  746. mr := multipart.NewReader(bodyStream, req.multipartFormBoundary)
  747. req.multipartForm, err = mr.ReadForm(8 * 1024)
  748. if err != nil {
  749. return nil, fmt.Errorf("cannot read multipart/form-data body: %w", err)
  750. }
  751. } else {
  752. body := req.bodyBytes()
  753. if bytes.Equal(ce, strGzip) {
  754. // Do not care about memory usage here.
  755. if body, err = AppendGunzipBytes(nil, body); err != nil {
  756. return nil, fmt.Errorf("cannot gunzip request body: %w", err)
  757. }
  758. } else if len(ce) > 0 {
  759. return nil, fmt.Errorf("unsupported Content-Encoding: %q", ce)
  760. }
  761. req.multipartForm, err = readMultipartForm(bytes.NewReader(body), req.multipartFormBoundary, len(body), len(body))
  762. if err != nil {
  763. return nil, err
  764. }
  765. }
  766. return req.multipartForm, nil
  767. }
  768. func marshalMultipartForm(f *multipart.Form, boundary string) ([]byte, error) {
  769. var buf bytebufferpool.ByteBuffer
  770. if err := WriteMultipartForm(&buf, f, boundary); err != nil {
  771. return nil, err
  772. }
  773. return buf.B, nil
  774. }
  775. // WriteMultipartForm writes the given multipart form f with the given
  776. // boundary to w.
  777. func WriteMultipartForm(w io.Writer, f *multipart.Form, boundary string) error {
  778. // Do not care about memory allocations here, since multipart
  779. // form processing is slow.
  780. if len(boundary) == 0 {
  781. panic("BUG: form boundary cannot be empty")
  782. }
  783. mw := multipart.NewWriter(w)
  784. if err := mw.SetBoundary(boundary); err != nil {
  785. return fmt.Errorf("cannot use form boundary %q: %w", boundary, err)
  786. }
  787. // marshal values
  788. for k, vv := range f.Value {
  789. for _, v := range vv {
  790. if err := mw.WriteField(k, v); err != nil {
  791. return fmt.Errorf("cannot write form field %q value %q: %w", k, v, err)
  792. }
  793. }
  794. }
  795. // marshal files
  796. for k, fvv := range f.File {
  797. for _, fv := range fvv {
  798. vw, err := mw.CreatePart(fv.Header)
  799. if err != nil {
  800. return fmt.Errorf("cannot create form file %q (%q): %w", k, fv.Filename, err)
  801. }
  802. fh, err := fv.Open()
  803. if err != nil {
  804. return fmt.Errorf("cannot open form file %q (%q): %s", k, fv.Filename, err)
  805. }
  806. if _, err = copyZeroAlloc(vw, fh); err != nil {
  807. return fmt.Errorf("error when copying form file %q (%q): %w", k, fv.Filename, err)
  808. }
  809. if err = fh.Close(); err != nil {
  810. return fmt.Errorf("cannot close form file %q (%q): %w", k, fv.Filename, err)
  811. }
  812. }
  813. }
  814. if err := mw.Close(); err != nil {
  815. return fmt.Errorf("error when closing multipart form writer: %w", err)
  816. }
  817. return nil
  818. }
  819. func readMultipartForm(r io.Reader, boundary string, size, maxInMemoryFileSize int) (*multipart.Form, error) {
  820. // Do not care about memory allocations here, since they are tiny
  821. // compared to multipart data (aka multi-MB files) usually sent
  822. // in multipart/form-data requests.
  823. if size <= 0 {
  824. return nil, fmt.Errorf("form size must be greater than 0. Given %d", size)
  825. }
  826. lr := io.LimitReader(r, int64(size))
  827. mr := multipart.NewReader(lr, boundary)
  828. f, err := mr.ReadForm(int64(maxInMemoryFileSize))
  829. if err != nil {
  830. return nil, fmt.Errorf("cannot read multipart/form-data body: %w", err)
  831. }
  832. return f, nil
  833. }
  834. // Reset clears request contents.
  835. func (req *Request) Reset() {
  836. req.Header.Reset()
  837. req.resetSkipHeader()
  838. req.timeout = 0
  839. req.UseHostHeader = false
  840. }
  841. func (req *Request) resetSkipHeader() {
  842. req.ResetBody()
  843. req.uri.Reset()
  844. req.parsedURI = false
  845. req.postArgs.Reset()
  846. req.parsedPostArgs = false
  847. req.isTLS = false
  848. }
  849. // RemoveMultipartFormFiles removes multipart/form-data temporary files
  850. // associated with the request.
  851. func (req *Request) RemoveMultipartFormFiles() {
  852. if req.multipartForm != nil {
  853. // Do not check for error, since these files may be deleted or moved
  854. // to new places by user code.
  855. req.multipartForm.RemoveAll() //nolint:errcheck
  856. req.multipartForm = nil
  857. }
  858. req.multipartFormBoundary = ""
  859. }
  860. // Reset clears response contents.
  861. func (resp *Response) Reset() {
  862. resp.Header.Reset()
  863. resp.resetSkipHeader()
  864. resp.SkipBody = false
  865. resp.raddr = nil
  866. resp.laddr = nil
  867. resp.ImmediateHeaderFlush = false
  868. }
  869. func (resp *Response) resetSkipHeader() {
  870. resp.ResetBody()
  871. }
  872. // Read reads request (including body) from the given r.
  873. //
  874. // RemoveMultipartFormFiles or Reset must be called after
  875. // reading multipart/form-data request in order to delete temporarily
  876. // uploaded files.
  877. //
  878. // If MayContinue returns true, the caller must:
  879. //
  880. // - Either send StatusExpectationFailed response if request headers don't
  881. // satisfy the caller.
  882. // - Or send StatusContinue response before reading request body
  883. // with ContinueReadBody.
  884. // - Or close the connection.
  885. //
  886. // io.EOF is returned if r is closed before reading the first header byte.
  887. func (req *Request) Read(r *bufio.Reader) error {
  888. return req.ReadLimitBody(r, 0)
  889. }
  890. const defaultMaxInMemoryFileSize = 16 * 1024 * 1024
  891. // ErrGetOnly is returned when server expects only GET requests,
  892. // but some other type of request came (Server.GetOnly option is true).
  893. var ErrGetOnly = errors.New("non-GET request received")
  894. // ReadLimitBody reads request from the given r, limiting the body size.
  895. //
  896. // If maxBodySize > 0 and the body size exceeds maxBodySize,
  897. // then ErrBodyTooLarge is returned.
  898. //
  899. // RemoveMultipartFormFiles or Reset must be called after
  900. // reading multipart/form-data request in order to delete temporarily
  901. // uploaded files.
  902. //
  903. // If MayContinue returns true, the caller must:
  904. //
  905. // - Either send StatusExpectationFailed response if request headers don't
  906. // satisfy the caller.
  907. // - Or send StatusContinue response before reading request body
  908. // with ContinueReadBody.
  909. // - Or close the connection.
  910. //
  911. // io.EOF is returned if r is closed before reading the first header byte.
  912. func (req *Request) ReadLimitBody(r *bufio.Reader, maxBodySize int) error {
  913. req.resetSkipHeader()
  914. if err := req.Header.Read(r); err != nil {
  915. return err
  916. }
  917. return req.readLimitBody(r, maxBodySize, false, true)
  918. }
  919. func (req *Request) readLimitBody(r *bufio.Reader, maxBodySize int, getOnly bool, preParseMultipartForm bool) error {
  920. // Do not reset the request here - the caller must reset it before
  921. // calling this method.
  922. if getOnly && !req.Header.IsGet() {
  923. return ErrGetOnly
  924. }
  925. if req.MayContinue() {
  926. // 'Expect: 100-continue' header found. Let the caller deciding
  927. // whether to read request body or
  928. // to return StatusExpectationFailed.
  929. return nil
  930. }
  931. return req.ContinueReadBody(r, maxBodySize, preParseMultipartForm)
  932. }
  933. func (req *Request) readBodyStream(r *bufio.Reader, maxBodySize int, getOnly bool, preParseMultipartForm bool) error {
  934. // Do not reset the request here - the caller must reset it before
  935. // calling this method.
  936. if getOnly && !req.Header.IsGet() {
  937. return ErrGetOnly
  938. }
  939. if req.MayContinue() {
  940. // 'Expect: 100-continue' header found. Let the caller deciding
  941. // whether to read request body or
  942. // to return StatusExpectationFailed.
  943. return nil
  944. }
  945. return req.ContinueReadBodyStream(r, maxBodySize, preParseMultipartForm)
  946. }
  947. // MayContinue returns true if the request contains
  948. // 'Expect: 100-continue' header.
  949. //
  950. // The caller must do one of the following actions if MayContinue returns true:
  951. //
  952. // - Either send StatusExpectationFailed response if request headers don't
  953. // satisfy the caller.
  954. // - Or send StatusContinue response before reading request body
  955. // with ContinueReadBody.
  956. // - Or close the connection.
  957. func (req *Request) MayContinue() bool {
  958. return bytes.Equal(req.Header.peek(strExpect), str100Continue)
  959. }
  960. // ContinueReadBody reads request body if request header contains
  961. // 'Expect: 100-continue'.
  962. //
  963. // The caller must send StatusContinue response before calling this method.
  964. //
  965. // If maxBodySize > 0 and the body size exceeds maxBodySize,
  966. // then ErrBodyTooLarge is returned.
  967. func (req *Request) ContinueReadBody(r *bufio.Reader, maxBodySize int, preParseMultipartForm ...bool) error {
  968. var err error
  969. contentLength := req.Header.realContentLength()
  970. if contentLength > 0 {
  971. if maxBodySize > 0 && contentLength > maxBodySize {
  972. return ErrBodyTooLarge
  973. }
  974. if len(preParseMultipartForm) == 0 || preParseMultipartForm[0] {
  975. // Pre-read multipart form data of known length.
  976. // This way we limit memory usage for large file uploads, since their contents
  977. // is streamed into temporary files if file size exceeds defaultMaxInMemoryFileSize.
  978. req.multipartFormBoundary = string(req.Header.MultipartFormBoundary())
  979. if len(req.multipartFormBoundary) > 0 && len(req.Header.peek(strContentEncoding)) == 0 {
  980. req.multipartForm, err = readMultipartForm(r, req.multipartFormBoundary, contentLength, defaultMaxInMemoryFileSize)
  981. if err != nil {
  982. req.Reset()
  983. }
  984. return err
  985. }
  986. }
  987. }
  988. if contentLength == -2 {
  989. // identity body has no sense for http requests, since
  990. // the end of body is determined by connection close.
  991. // So just ignore request body for requests without
  992. // 'Content-Length' and 'Transfer-Encoding' headers.
  993. // refer to https://tools.ietf.org/html/rfc7230#section-3.3.2
  994. if !req.Header.ignoreBody() {
  995. req.Header.SetContentLength(0)
  996. }
  997. return nil
  998. }
  999. if err = req.ReadBody(r, contentLength, maxBodySize); err != nil {
  1000. return err
  1001. }
  1002. if req.Header.ContentLength() == -1 {
  1003. err = req.Header.ReadTrailer(r)
  1004. if err != nil && err != io.EOF {
  1005. return err
  1006. }
  1007. }
  1008. return nil
  1009. }
  1010. // ReadBody reads request body from the given r, limiting the body size.
  1011. //
  1012. // If maxBodySize > 0 and the body size exceeds maxBodySize,
  1013. // then ErrBodyTooLarge is returned.
  1014. func (req *Request) ReadBody(r *bufio.Reader, contentLength int, maxBodySize int) (err error) {
  1015. bodyBuf := req.bodyBuffer()
  1016. bodyBuf.Reset()
  1017. if contentLength >= 0 {
  1018. bodyBuf.B, err = readBody(r, contentLength, maxBodySize, bodyBuf.B)
  1019. } else if contentLength == -1 {
  1020. bodyBuf.B, err = readBodyChunked(r, maxBodySize, bodyBuf.B)
  1021. } else {
  1022. bodyBuf.B, err = readBodyIdentity(r, maxBodySize, bodyBuf.B)
  1023. req.Header.SetContentLength(len(bodyBuf.B))
  1024. }
  1025. if err != nil {
  1026. req.Reset()
  1027. return err
  1028. }
  1029. return nil
  1030. }
  1031. // ContinueReadBodyStream reads request body if request header contains
  1032. // 'Expect: 100-continue'.
  1033. //
  1034. // The caller must send StatusContinue response before calling this method.
  1035. //
  1036. // If maxBodySize > 0 and the body size exceeds maxBodySize,
  1037. // then ErrBodyTooLarge is returned.
  1038. func (req *Request) ContinueReadBodyStream(r *bufio.Reader, maxBodySize int, preParseMultipartForm ...bool) error {
  1039. var err error
  1040. contentLength := req.Header.realContentLength()
  1041. if contentLength > 0 {
  1042. if len(preParseMultipartForm) == 0 || preParseMultipartForm[0] {
  1043. // Pre-read multipart form data of known length.
  1044. // This way we limit memory usage for large file uploads, since their contents
  1045. // is streamed into temporary files if file size exceeds defaultMaxInMemoryFileSize.
  1046. req.multipartFormBoundary = b2s(req.Header.MultipartFormBoundary())
  1047. if len(req.multipartFormBoundary) > 0 && len(req.Header.peek(strContentEncoding)) == 0 {
  1048. req.multipartForm, err = readMultipartForm(r, req.multipartFormBoundary, contentLength, defaultMaxInMemoryFileSize)
  1049. if err != nil {
  1050. req.Reset()
  1051. }
  1052. return err
  1053. }
  1054. }
  1055. }
  1056. if contentLength == -2 {
  1057. // identity body has no sense for http requests, since
  1058. // the end of body is determined by connection close.
  1059. // So just ignore request body for requests without
  1060. // 'Content-Length' and 'Transfer-Encoding' headers.
  1061. req.Header.SetContentLength(0)
  1062. return nil
  1063. }
  1064. bodyBuf := req.bodyBuffer()
  1065. bodyBuf.Reset()
  1066. bodyBuf.B, err = readBodyWithStreaming(r, contentLength, maxBodySize, bodyBuf.B)
  1067. if err != nil {
  1068. if err == ErrBodyTooLarge {
  1069. req.Header.SetContentLength(contentLength)
  1070. req.body = bodyBuf
  1071. req.bodyStream = acquireRequestStream(bodyBuf, r, &req.Header)
  1072. return nil
  1073. }
  1074. if err == errChunkedStream {
  1075. req.body = bodyBuf
  1076. req.bodyStream = acquireRequestStream(bodyBuf, r, &req.Header)
  1077. return nil
  1078. }
  1079. req.Reset()
  1080. return err
  1081. }
  1082. req.body = bodyBuf
  1083. req.bodyStream = acquireRequestStream(bodyBuf, r, &req.Header)
  1084. req.Header.SetContentLength(contentLength)
  1085. return nil
  1086. }
  1087. // Read reads response (including body) from the given r.
  1088. //
  1089. // io.EOF is returned if r is closed before reading the first header byte.
  1090. func (resp *Response) Read(r *bufio.Reader) error {
  1091. return resp.ReadLimitBody(r, 0)
  1092. }
  1093. // ReadLimitBody reads response headers from the given r,
  1094. // then reads the body using the ReadBody function and limiting the body size.
  1095. //
  1096. // If resp.SkipBody is true then it skips reading the response body.
  1097. //
  1098. // If maxBodySize > 0 and the body size exceeds maxBodySize,
  1099. // then ErrBodyTooLarge is returned.
  1100. //
  1101. // io.EOF is returned if r is closed before reading the first header byte.
  1102. func (resp *Response) ReadLimitBody(r *bufio.Reader, maxBodySize int) error {
  1103. resp.resetSkipHeader()
  1104. err := resp.Header.Read(r)
  1105. if err != nil {
  1106. return err
  1107. }
  1108. if resp.Header.StatusCode() == StatusContinue {
  1109. // Read the next response according to http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html .
  1110. if err = resp.Header.Read(r); err != nil {
  1111. return err
  1112. }
  1113. }
  1114. if !resp.mustSkipBody() {
  1115. err = resp.ReadBody(r, maxBodySize)
  1116. if err != nil {
  1117. return err
  1118. }
  1119. }
  1120. if resp.Header.ContentLength() == -1 {
  1121. err = resp.Header.ReadTrailer(r)
  1122. if err != nil && err != io.EOF {
  1123. return err
  1124. }
  1125. }
  1126. return nil
  1127. }
  1128. // ReadBody reads response body from the given r, limiting the body size.
  1129. //
  1130. // If maxBodySize > 0 and the body size exceeds maxBodySize,
  1131. // then ErrBodyTooLarge is returned.
  1132. func (resp *Response) ReadBody(r *bufio.Reader, maxBodySize int) (err error) {
  1133. bodyBuf := resp.bodyBuffer()
  1134. bodyBuf.Reset()
  1135. contentLength := resp.Header.ContentLength()
  1136. if contentLength >= 0 {
  1137. bodyBuf.B, err = readBody(r, contentLength, maxBodySize, bodyBuf.B)
  1138. } else if contentLength == -1 {
  1139. bodyBuf.B, err = readBodyChunked(r, maxBodySize, bodyBuf.B)
  1140. } else {
  1141. bodyBuf.B, err = readBodyIdentity(r, maxBodySize, bodyBuf.B)
  1142. resp.Header.SetContentLength(len(bodyBuf.B))
  1143. }
  1144. return err
  1145. }
  1146. func (resp *Response) mustSkipBody() bool {
  1147. return resp.SkipBody || resp.Header.mustSkipContentLength()
  1148. }
  1149. var errRequestHostRequired = errors.New("missing required Host header in request")
  1150. // WriteTo writes request to w. It implements io.WriterTo.
  1151. func (req *Request) WriteTo(w io.Writer) (int64, error) {
  1152. return writeBufio(req, w)
  1153. }
  1154. // WriteTo writes response to w. It implements io.WriterTo.
  1155. func (resp *Response) WriteTo(w io.Writer) (int64, error) {
  1156. return writeBufio(resp, w)
  1157. }
  1158. func writeBufio(hw httpWriter, w io.Writer) (int64, error) {
  1159. sw := acquireStatsWriter(w)
  1160. bw := acquireBufioWriter(sw)
  1161. err1 := hw.Write(bw)
  1162. err2 := bw.Flush()
  1163. releaseBufioWriter(bw)
  1164. n := sw.bytesWritten
  1165. releaseStatsWriter(sw)
  1166. err := err1
  1167. if err == nil {
  1168. err = err2
  1169. }
  1170. return n, err
  1171. }
  1172. type statsWriter struct {
  1173. w io.Writer
  1174. bytesWritten int64
  1175. }
  1176. func (w *statsWriter) Write(p []byte) (int, error) {
  1177. n, err := w.w.Write(p)
  1178. w.bytesWritten += int64(n)
  1179. return n, err
  1180. }
  1181. func acquireStatsWriter(w io.Writer) *statsWriter {
  1182. v := statsWriterPool.Get()
  1183. if v == nil {
  1184. return &statsWriter{
  1185. w: w,
  1186. }
  1187. }
  1188. sw := v.(*statsWriter)
  1189. sw.w = w
  1190. return sw
  1191. }
  1192. func releaseStatsWriter(sw *statsWriter) {
  1193. sw.w = nil
  1194. sw.bytesWritten = 0
  1195. statsWriterPool.Put(sw)
  1196. }
  1197. var statsWriterPool sync.Pool
  1198. func acquireBufioWriter(w io.Writer) *bufio.Writer {
  1199. v := bufioWriterPool.Get()
  1200. if v == nil {
  1201. return bufio.NewWriter(w)
  1202. }
  1203. bw := v.(*bufio.Writer)
  1204. bw.Reset(w)
  1205. return bw
  1206. }
  1207. func releaseBufioWriter(bw *bufio.Writer) {
  1208. bufioWriterPool.Put(bw)
  1209. }
  1210. var bufioWriterPool sync.Pool
  1211. func (req *Request) onlyMultipartForm() bool {
  1212. return req.multipartForm != nil && (req.body == nil || len(req.body.B) == 0)
  1213. }
  1214. // Write writes request to w.
  1215. //
  1216. // Write doesn't flush request to w for performance reasons.
  1217. //
  1218. // See also WriteTo.
  1219. func (req *Request) Write(w *bufio.Writer) error {
  1220. if len(req.Header.Host()) == 0 || req.parsedURI {
  1221. uri := req.URI()
  1222. host := uri.Host()
  1223. if len(req.Header.Host()) == 0 {
  1224. if len(host) == 0 {
  1225. return errRequestHostRequired
  1226. } else {
  1227. req.Header.SetHostBytes(host)
  1228. }
  1229. } else if !req.UseHostHeader {
  1230. req.Header.SetHostBytes(host)
  1231. }
  1232. req.Header.SetRequestURIBytes(uri.RequestURI())
  1233. if len(uri.username) > 0 {
  1234. // RequestHeader.SetBytesKV only uses RequestHeader.bufKV.key
  1235. // So we are free to use RequestHeader.bufKV.value as a scratch pad for
  1236. // the base64 encoding.
  1237. nl := len(uri.username) + len(uri.password) + 1
  1238. nb := nl + len(strBasicSpace)
  1239. tl := nb + base64.StdEncoding.EncodedLen(nl)
  1240. if tl > cap(req.Header.bufKV.value) {
  1241. req.Header.bufKV.value = make([]byte, 0, tl)
  1242. }
  1243. buf := req.Header.bufKV.value[:0]
  1244. buf = append(buf, uri.username...)
  1245. buf = append(buf, strColon...)
  1246. buf = append(buf, uri.password...)
  1247. buf = append(buf, strBasicSpace...)
  1248. base64.StdEncoding.Encode(buf[nb:tl], buf[:nl])
  1249. req.Header.SetBytesKV(strAuthorization, buf[nl:tl])
  1250. }
  1251. }
  1252. if req.bodyStream != nil {
  1253. return req.writeBodyStream(w)
  1254. }
  1255. body := req.bodyBytes()
  1256. var err error
  1257. if req.onlyMultipartForm() {
  1258. body, err = marshalMultipartForm(req.multipartForm, req.multipartFormBoundary)
  1259. if err != nil {
  1260. return fmt.Errorf("error when marshaling multipart form: %w", err)
  1261. }
  1262. req.Header.SetMultipartFormBoundary(req.multipartFormBoundary)
  1263. }
  1264. hasBody := false
  1265. if len(body) == 0 {
  1266. body = req.postArgs.QueryString()
  1267. }
  1268. if len(body) != 0 || !req.Header.ignoreBody() {
  1269. hasBody = true
  1270. req.Header.SetContentLength(len(body))
  1271. }
  1272. if err = req.Header.Write(w); err != nil {
  1273. return err
  1274. }
  1275. if hasBody {
  1276. _, err = w.Write(body)
  1277. } else if len(body) > 0 {
  1278. if req.secureErrorLogMessage {
  1279. return fmt.Errorf("non-zero body for non-POST request")
  1280. }
  1281. return fmt.Errorf("non-zero body for non-POST request. body=%q", body)
  1282. }
  1283. return err
  1284. }
  1285. // WriteGzip writes response with gzipped body to w.
  1286. //
  1287. // The method gzips response body and sets 'Content-Encoding: gzip'
  1288. // header before writing response to w.
  1289. //
  1290. // WriteGzip doesn't flush response to w for performance reasons.
  1291. func (resp *Response) WriteGzip(w *bufio.Writer) error {
  1292. return resp.WriteGzipLevel(w, CompressDefaultCompression)
  1293. }
  1294. // WriteGzipLevel writes response with gzipped body to w.
  1295. //
  1296. // Level is the desired compression level:
  1297. //
  1298. // * CompressNoCompression
  1299. // * CompressBestSpeed
  1300. // * CompressBestCompression
  1301. // * CompressDefaultCompression
  1302. // * CompressHuffmanOnly
  1303. //
  1304. // The method gzips response body and sets 'Content-Encoding: gzip'
  1305. // header before writing response to w.
  1306. //
  1307. // WriteGzipLevel doesn't flush response to w for performance reasons.
  1308. func (resp *Response) WriteGzipLevel(w *bufio.Writer, level int) error {
  1309. if err := resp.gzipBody(level); err != nil {
  1310. return err
  1311. }
  1312. return resp.Write(w)
  1313. }
  1314. // WriteDeflate writes response with deflated body to w.
  1315. //
  1316. // The method deflates response body and sets 'Content-Encoding: deflate'
  1317. // header before writing response to w.
  1318. //
  1319. // WriteDeflate doesn't flush response to w for performance reasons.
  1320. func (resp *Response) WriteDeflate(w *bufio.Writer) error {
  1321. return resp.WriteDeflateLevel(w, CompressDefaultCompression)
  1322. }
  1323. // WriteDeflateLevel writes response with deflated body to w.
  1324. //
  1325. // Level is the desired compression level:
  1326. //
  1327. // * CompressNoCompression
  1328. // * CompressBestSpeed
  1329. // * CompressBestCompression
  1330. // * CompressDefaultCompression
  1331. // * CompressHuffmanOnly
  1332. //
  1333. // The method deflates response body and sets 'Content-Encoding: deflate'
  1334. // header before writing response to w.
  1335. //
  1336. // WriteDeflateLevel doesn't flush response to w for performance reasons.
  1337. func (resp *Response) WriteDeflateLevel(w *bufio.Writer, level int) error {
  1338. if err := resp.deflateBody(level); err != nil {
  1339. return err
  1340. }
  1341. return resp.Write(w)
  1342. }
  1343. func (resp *Response) brotliBody(level int) error {
  1344. if len(resp.Header.peek(strContentEncoding)) > 0 {
  1345. // It looks like the body is already compressed.
  1346. // Do not compress it again.
  1347. return nil
  1348. }
  1349. if !resp.Header.isCompressibleContentType() {
  1350. // The content-type cannot be compressed.
  1351. return nil
  1352. }
  1353. if resp.bodyStream != nil {
  1354. // Reset Content-Length to -1, since it is impossible
  1355. // to determine body size beforehand of streamed compression.
  1356. // For https://github.com/valyala/fasthttp/issues/176 .
  1357. resp.Header.SetContentLength(-1)
  1358. // Do not care about memory allocations here, since brotli is slow
  1359. // and allocates a lot of memory by itself.
  1360. bs := resp.bodyStream
  1361. resp.bodyStream = NewStreamReader(func(sw *bufio.Writer) {
  1362. zw := acquireStacklessBrotliWriter(sw, level)
  1363. fw := &flushWriter{
  1364. wf: zw,
  1365. bw: sw,
  1366. }
  1367. copyZeroAlloc(fw, bs) //nolint:errcheck
  1368. releaseStacklessBrotliWriter(zw, level)
  1369. if bsc, ok := bs.(io.Closer); ok {
  1370. bsc.Close()
  1371. }
  1372. })
  1373. } else {
  1374. bodyBytes := resp.bodyBytes()
  1375. if len(bodyBytes) < minCompressLen {
  1376. // There is no sense in spending CPU time on small body compression,
  1377. // since there is a very high probability that the compressed
  1378. // body size will be bigger than the original body size.
  1379. return nil
  1380. }
  1381. w := responseBodyPool.Get()
  1382. w.B = AppendBrotliBytesLevel(w.B, bodyBytes, level)
  1383. // Hack: swap resp.body with w.
  1384. if resp.body != nil {
  1385. responseBodyPool.Put(resp.body)
  1386. }
  1387. resp.body = w
  1388. resp.bodyRaw = nil
  1389. }
  1390. resp.Header.SetCanonical(strContentEncoding, strBr)
  1391. return nil
  1392. }
  1393. func (resp *Response) gzipBody(level int) error {
  1394. if len(resp.Header.peek(strContentEncoding)) > 0 {
  1395. // It looks like the body is already compressed.
  1396. // Do not compress it again.
  1397. return nil
  1398. }
  1399. if !resp.Header.isCompressibleContentType() {
  1400. // The content-type cannot be compressed.
  1401. return nil
  1402. }
  1403. if resp.bodyStream != nil {
  1404. // Reset Content-Length to -1, since it is impossible
  1405. // to determine body size beforehand of streamed compression.
  1406. // For https://github.com/valyala/fasthttp/issues/176 .
  1407. resp.Header.SetContentLength(-1)
  1408. // Do not care about memory allocations here, since gzip is slow
  1409. // and allocates a lot of memory by itself.
  1410. bs := resp.bodyStream
  1411. resp.bodyStream = NewStreamReader(func(sw *bufio.Writer) {
  1412. zw := acquireStacklessGzipWriter(sw, level)
  1413. fw := &flushWriter{
  1414. wf: zw,
  1415. bw: sw,
  1416. }
  1417. copyZeroAlloc(fw, bs) //nolint:errcheck
  1418. releaseStacklessGzipWriter(zw, level)
  1419. if bsc, ok := bs.(io.Closer); ok {
  1420. bsc.Close()
  1421. }
  1422. })
  1423. } else {
  1424. bodyBytes := resp.bodyBytes()
  1425. if len(bodyBytes) < minCompressLen {
  1426. // There is no sense in spending CPU time on small body compression,
  1427. // since there is a very high probability that the compressed
  1428. // body size will be bigger than the original body size.
  1429. return nil
  1430. }
  1431. w := responseBodyPool.Get()
  1432. w.B = AppendGzipBytesLevel(w.B, bodyBytes, level)
  1433. // Hack: swap resp.body with w.
  1434. if resp.body != nil {
  1435. responseBodyPool.Put(resp.body)
  1436. }
  1437. resp.body = w
  1438. resp.bodyRaw = nil
  1439. }
  1440. resp.Header.SetCanonical(strContentEncoding, strGzip)
  1441. return nil
  1442. }
  1443. func (resp *Response) deflateBody(level int) error {
  1444. if len(resp.Header.peek(strContentEncoding)) > 0 {
  1445. // It looks like the body is already compressed.
  1446. // Do not compress it again.
  1447. return nil
  1448. }
  1449. if !resp.Header.isCompressibleContentType() {
  1450. // The content-type cannot be compressed.
  1451. return nil
  1452. }
  1453. if resp.bodyStream != nil {
  1454. // Reset Content-Length to -1, since it is impossible
  1455. // to determine body size beforehand of streamed compression.
  1456. // For https://github.com/valyala/fasthttp/issues/176 .
  1457. resp.Header.SetContentLength(-1)
  1458. // Do not care about memory allocations here, since flate is slow
  1459. // and allocates a lot of memory by itself.
  1460. bs := resp.bodyStream
  1461. resp.bodyStream = NewStreamReader(func(sw *bufio.Writer) {
  1462. zw := acquireStacklessDeflateWriter(sw, level)
  1463. fw := &flushWriter{
  1464. wf: zw,
  1465. bw: sw,
  1466. }
  1467. copyZeroAlloc(fw, bs) //nolint:errcheck
  1468. releaseStacklessDeflateWriter(zw, level)
  1469. if bsc, ok := bs.(io.Closer); ok {
  1470. bsc.Close()
  1471. }
  1472. })
  1473. } else {
  1474. bodyBytes := resp.bodyBytes()
  1475. if len(bodyBytes) < minCompressLen {
  1476. // There is no sense in spending CPU time on small body compression,
  1477. // since there is a very high probability that the compressed
  1478. // body size will be bigger than the original body size.
  1479. return nil
  1480. }
  1481. w := responseBodyPool.Get()
  1482. w.B = AppendDeflateBytesLevel(w.B, bodyBytes, level)
  1483. // Hack: swap resp.body with w.
  1484. if resp.body != nil {
  1485. responseBodyPool.Put(resp.body)
  1486. }
  1487. resp.body = w
  1488. resp.bodyRaw = nil
  1489. }
  1490. resp.Header.SetCanonical(strContentEncoding, strDeflate)
  1491. return nil
  1492. }
  1493. // Bodies with sizes smaller than minCompressLen aren't compressed at all
  1494. const minCompressLen = 200
  1495. type writeFlusher interface {
  1496. io.Writer
  1497. Flush() error
  1498. }
  1499. type flushWriter struct {
  1500. wf writeFlusher
  1501. bw *bufio.Writer
  1502. }
  1503. func (w *flushWriter) Write(p []byte) (int, error) {
  1504. n, err := w.wf.Write(p)
  1505. if err != nil {
  1506. return 0, err
  1507. }
  1508. if err = w.wf.Flush(); err != nil {
  1509. return 0, err
  1510. }
  1511. if err = w.bw.Flush(); err != nil {
  1512. return 0, err
  1513. }
  1514. return n, nil
  1515. }
  1516. // Write writes response to w.
  1517. //
  1518. // Write doesn't flush response to w for performance reasons.
  1519. //
  1520. // See also WriteTo.
  1521. func (resp *Response) Write(w *bufio.Writer) error {
  1522. sendBody := !resp.mustSkipBody()
  1523. if resp.bodyStream != nil {
  1524. return resp.writeBodyStream(w, sendBody)
  1525. }
  1526. body := resp.bodyBytes()
  1527. bodyLen := len(body)
  1528. if sendBody || bodyLen > 0 {
  1529. resp.Header.SetContentLength(bodyLen)
  1530. }
  1531. if err := resp.Header.Write(w); err != nil {
  1532. return err
  1533. }
  1534. if sendBody {
  1535. if _, err := w.Write(body); err != nil {
  1536. return err
  1537. }
  1538. }
  1539. return nil
  1540. }
  1541. func (req *Request) writeBodyStream(w *bufio.Writer) error {
  1542. var err error
  1543. contentLength := req.Header.ContentLength()
  1544. if contentLength < 0 {
  1545. lrSize := limitedReaderSize(req.bodyStream)
  1546. if lrSize >= 0 {
  1547. contentLength = int(lrSize)
  1548. if int64(contentLength) != lrSize {
  1549. contentLength = -1
  1550. }
  1551. if contentLength >= 0 {
  1552. req.Header.SetContentLength(contentLength)
  1553. }
  1554. }
  1555. }
  1556. if contentLength >= 0 {
  1557. if err = req.Header.Write(w); err == nil {
  1558. err = writeBodyFixedSize(w, req.bodyStream, int64(contentLength))
  1559. }
  1560. } else {
  1561. req.Header.SetContentLength(-1)
  1562. err = req.Header.Write(w)
  1563. if err == nil {
  1564. err = writeBodyChunked(w, req.bodyStream)
  1565. }
  1566. if err == nil {
  1567. err = req.Header.writeTrailer(w)
  1568. }
  1569. }
  1570. err1 := req.closeBodyStream()
  1571. if err == nil {
  1572. err = err1
  1573. }
  1574. return err
  1575. }
  1576. // ErrBodyStreamWritePanic is returned when panic happens during writing body stream.
  1577. type ErrBodyStreamWritePanic struct {
  1578. error
  1579. }
  1580. func (resp *Response) writeBodyStream(w *bufio.Writer, sendBody bool) (err error) {
  1581. defer func() {
  1582. if r := recover(); r != nil {
  1583. err = &ErrBodyStreamWritePanic{
  1584. error: fmt.Errorf("panic while writing body stream: %+v", r),
  1585. }
  1586. }
  1587. }()
  1588. contentLength := resp.Header.ContentLength()
  1589. if contentLength < 0 {
  1590. lrSize := limitedReaderSize(resp.bodyStream)
  1591. if lrSize >= 0 {
  1592. contentLength = int(lrSize)
  1593. if int64(contentLength) != lrSize {
  1594. contentLength = -1
  1595. }
  1596. if contentLength >= 0 {
  1597. resp.Header.SetContentLength(contentLength)
  1598. }
  1599. }
  1600. }
  1601. if contentLength >= 0 {
  1602. if err = resp.Header.Write(w); err == nil {
  1603. if resp.ImmediateHeaderFlush {
  1604. err = w.Flush()
  1605. }
  1606. if err == nil && sendBody {
  1607. err = writeBodyFixedSize(w, resp.bodyStream, int64(contentLength))
  1608. }
  1609. }
  1610. } else {
  1611. resp.Header.SetContentLength(-1)
  1612. if err = resp.Header.Write(w); err == nil {
  1613. if resp.ImmediateHeaderFlush {
  1614. err = w.Flush()
  1615. }
  1616. if err == nil && sendBody {
  1617. err = writeBodyChunked(w, resp.bodyStream)
  1618. }
  1619. if err == nil {
  1620. err = resp.Header.writeTrailer(w)
  1621. }
  1622. }
  1623. }
  1624. err1 := resp.closeBodyStream()
  1625. if err == nil {
  1626. err = err1
  1627. }
  1628. return err
  1629. }
  1630. func (req *Request) closeBodyStream() error {
  1631. if req.bodyStream == nil {
  1632. return nil
  1633. }
  1634. var err error
  1635. if bsc, ok := req.bodyStream.(io.Closer); ok {
  1636. err = bsc.Close()
  1637. }
  1638. req.bodyStream = nil
  1639. return err
  1640. }
  1641. func (resp *Response) closeBodyStream() error {
  1642. if resp.bodyStream == nil {
  1643. return nil
  1644. }
  1645. var err error
  1646. if bsc, ok := resp.bodyStream.(io.Closer); ok {
  1647. err = bsc.Close()
  1648. }
  1649. resp.bodyStream = nil
  1650. return err
  1651. }
  1652. // String returns request representation.
  1653. //
  1654. // Returns error message instead of request representation on error.
  1655. //
  1656. // Use Write instead of String for performance-critical code.
  1657. func (req *Request) String() string {
  1658. return getHTTPString(req)
  1659. }
  1660. // String returns response representation.
  1661. //
  1662. // Returns error message instead of response representation on error.
  1663. //
  1664. // Use Write instead of String for performance-critical code.
  1665. func (resp *Response) String() string {
  1666. return getHTTPString(resp)
  1667. }
  1668. func getHTTPString(hw httpWriter) string {
  1669. w := bytebufferpool.Get()
  1670. bw := bufio.NewWriter(w)
  1671. if err := hw.Write(bw); err != nil {
  1672. return err.Error()
  1673. }
  1674. if err := bw.Flush(); err != nil {
  1675. return err.Error()
  1676. }
  1677. s := string(w.B)
  1678. bytebufferpool.Put(w)
  1679. return s
  1680. }
  1681. type httpWriter interface {
  1682. Write(w *bufio.Writer) error
  1683. }
  1684. func writeBodyChunked(w *bufio.Writer, r io.Reader) error {
  1685. vbuf := copyBufPool.Get()
  1686. buf := vbuf.([]byte)
  1687. var err error
  1688. var n int
  1689. for {
  1690. n, err = r.Read(buf)
  1691. if n == 0 {
  1692. if err == nil {
  1693. panic("BUG: io.Reader returned 0, nil")
  1694. }
  1695. if err == io.EOF {
  1696. if err = writeChunk(w, buf[:0]); err != nil {
  1697. break
  1698. }
  1699. err = nil
  1700. }
  1701. break
  1702. }
  1703. if err = writeChunk(w, buf[:n]); err != nil {
  1704. break
  1705. }
  1706. }
  1707. copyBufPool.Put(vbuf)
  1708. return err
  1709. }
  1710. func limitedReaderSize(r io.Reader) int64 {
  1711. lr, ok := r.(*io.LimitedReader)
  1712. if !ok {
  1713. return -1
  1714. }
  1715. return lr.N
  1716. }
  1717. func writeBodyFixedSize(w *bufio.Writer, r io.Reader, size int64) error {
  1718. if size > maxSmallFileSize {
  1719. // w buffer must be empty for triggering
  1720. // sendfile path in bufio.Writer.ReadFrom.
  1721. if err := w.Flush(); err != nil {
  1722. return err
  1723. }
  1724. }
  1725. n, err := copyZeroAlloc(w, r)
  1726. if n != size && err == nil {
  1727. err = fmt.Errorf("copied %d bytes from body stream instead of %d bytes", n, size)
  1728. }
  1729. return err
  1730. }
  1731. func copyZeroAlloc(w io.Writer, r io.Reader) (int64, error) {
  1732. vbuf := copyBufPool.Get()
  1733. buf := vbuf.([]byte)
  1734. n, err := io.CopyBuffer(w, r, buf)
  1735. copyBufPool.Put(vbuf)
  1736. return n, err
  1737. }
  1738. var copyBufPool = sync.Pool{
  1739. New: func() interface{} {
  1740. return make([]byte, 4096)
  1741. },
  1742. }
  1743. func writeChunk(w *bufio.Writer, b []byte) error {
  1744. n := len(b)
  1745. if err := writeHexInt(w, n); err != nil {
  1746. return err
  1747. }
  1748. if _, err := w.Write(strCRLF); err != nil {
  1749. return err
  1750. }
  1751. if _, err := w.Write(b); err != nil {
  1752. return err
  1753. }
  1754. // If is end chunk, write CRLF after writing trailer
  1755. if n > 0 {
  1756. if _, err := w.Write(strCRLF); err != nil {
  1757. return err
  1758. }
  1759. }
  1760. return w.Flush()
  1761. }
  1762. // ErrBodyTooLarge is returned if either request or response body exceeds
  1763. // the given limit.
  1764. var ErrBodyTooLarge = errors.New("body size exceeds the given limit")
  1765. func readBody(r *bufio.Reader, contentLength int, maxBodySize int, dst []byte) ([]byte, error) {
  1766. if maxBodySize > 0 && contentLength > maxBodySize {
  1767. return dst, ErrBodyTooLarge
  1768. }
  1769. return appendBodyFixedSize(r, dst, contentLength)
  1770. }
  1771. var errChunkedStream = errors.New("chunked stream")
  1772. func readBodyWithStreaming(r *bufio.Reader, contentLength int, maxBodySize int, dst []byte) (b []byte, err error) {
  1773. if contentLength == -1 {
  1774. // handled in requestStream.Read()
  1775. return b, errChunkedStream
  1776. }
  1777. dst = dst[:0]
  1778. readN := maxBodySize
  1779. if readN > contentLength {
  1780. readN = contentLength
  1781. }
  1782. if readN > 8*1024 {
  1783. readN = 8 * 1024
  1784. }
  1785. if contentLength >= 0 && maxBodySize >= contentLength {
  1786. b, err = appendBodyFixedSize(r, dst, readN)
  1787. } else {
  1788. b, err = readBodyIdentity(r, readN, dst)
  1789. }
  1790. if err != nil {
  1791. return b, err
  1792. }
  1793. if contentLength > maxBodySize {
  1794. return b, ErrBodyTooLarge
  1795. }
  1796. return b, nil
  1797. }
  1798. func readBodyIdentity(r *bufio.Reader, maxBodySize int, dst []byte) ([]byte, error) {
  1799. dst = dst[:cap(dst)]
  1800. if len(dst) == 0 {
  1801. dst = make([]byte, 1024)
  1802. }
  1803. offset := 0
  1804. for {
  1805. nn, err := r.Read(dst[offset:])
  1806. if nn <= 0 {
  1807. if err != nil {
  1808. if err == io.EOF {
  1809. return dst[:offset], nil
  1810. }
  1811. return dst[:offset], err
  1812. }
  1813. panic(fmt.Sprintf("BUG: bufio.Read() returned (%d, nil)", nn))
  1814. }
  1815. offset += nn
  1816. if maxBodySize > 0 && offset > maxBodySize {
  1817. return dst[:offset], ErrBodyTooLarge
  1818. }
  1819. if len(dst) == offset {
  1820. n := round2(2 * offset)
  1821. if maxBodySize > 0 && n > maxBodySize {
  1822. n = maxBodySize + 1
  1823. }
  1824. b := make([]byte, n)
  1825. copy(b, dst)
  1826. dst = b
  1827. }
  1828. }
  1829. }
  1830. func appendBodyFixedSize(r *bufio.Reader, dst []byte, n int) ([]byte, error) {
  1831. if n == 0 {
  1832. return dst, nil
  1833. }
  1834. offset := len(dst)
  1835. dstLen := offset + n
  1836. if cap(dst) < dstLen {
  1837. b := make([]byte, round2(dstLen))
  1838. copy(b, dst)
  1839. dst = b
  1840. }
  1841. dst = dst[:dstLen]
  1842. for {
  1843. nn, err := r.Read(dst[offset:])
  1844. if nn <= 0 {
  1845. if err != nil {
  1846. if err == io.EOF {
  1847. err = io.ErrUnexpectedEOF
  1848. }
  1849. return dst[:offset], err
  1850. }
  1851. panic(fmt.Sprintf("BUG: bufio.Read() returned (%d, nil)", nn))
  1852. }
  1853. offset += nn
  1854. if offset == dstLen {
  1855. return dst, nil
  1856. }
  1857. }
  1858. }
  1859. // ErrBrokenChunk is returned when server receives a broken chunked body (Transfer-Encoding: chunked).
  1860. type ErrBrokenChunk struct {
  1861. error
  1862. }
  1863. func readBodyChunked(r *bufio.Reader, maxBodySize int, dst []byte) ([]byte, error) {
  1864. if len(dst) > 0 {
  1865. panic("BUG: expected zero-length buffer")
  1866. }
  1867. strCRLFLen := len(strCRLF)
  1868. for {
  1869. chunkSize, err := parseChunkSize(r)
  1870. if err != nil {
  1871. return dst, err
  1872. }
  1873. if chunkSize == 0 {
  1874. return dst, err
  1875. }
  1876. if maxBodySize > 0 && len(dst)+chunkSize > maxBodySize {
  1877. return dst, ErrBodyTooLarge
  1878. }
  1879. dst, err = appendBodyFixedSize(r, dst, chunkSize+strCRLFLen)
  1880. if err != nil {
  1881. return dst, err
  1882. }
  1883. if !bytes.Equal(dst[len(dst)-strCRLFLen:], strCRLF) {
  1884. return dst, ErrBrokenChunk{
  1885. error: fmt.Errorf("cannot find crlf at the end of chunk"),
  1886. }
  1887. }
  1888. dst = dst[:len(dst)-strCRLFLen]
  1889. }
  1890. }
  1891. func parseChunkSize(r *bufio.Reader) (int, error) {
  1892. n, err := readHexInt(r)
  1893. if err != nil {
  1894. return -1, err
  1895. }
  1896. for {
  1897. c, err := r.ReadByte()
  1898. if err != nil {
  1899. return -1, ErrBrokenChunk{
  1900. error: fmt.Errorf("cannot read '\r' char at the end of chunk size: %s", err),
  1901. }
  1902. }
  1903. // Skip chunk extension after chunk size.
  1904. // Add support later if anyone needs it.
  1905. if c != '\r' {
  1906. continue
  1907. }
  1908. if err := r.UnreadByte(); err != nil {
  1909. return -1, ErrBrokenChunk{
  1910. error: fmt.Errorf("cannot unread '\r' char at the end of chunk size: %w", err),
  1911. }
  1912. }
  1913. break
  1914. }
  1915. err = readCrLf(r)
  1916. if err != nil {
  1917. return -1, err
  1918. }
  1919. return n, nil
  1920. }
  1921. func readCrLf(r *bufio.Reader) error {
  1922. for _, exp := range []byte{'\r', '\n'} {
  1923. c, err := r.ReadByte()
  1924. if err != nil {
  1925. return ErrBrokenChunk{
  1926. error: fmt.Errorf("cannot read %q char at the end of chunk size: %w", exp, err),
  1927. }
  1928. }
  1929. if c != exp {
  1930. return ErrBrokenChunk{
  1931. error: fmt.Errorf("unexpected char %q at the end of chunk size. Expected %q", c, exp),
  1932. }
  1933. }
  1934. }
  1935. return nil
  1936. }
  1937. func round2(n int) int {
  1938. if n <= 0 {
  1939. return 0
  1940. }
  1941. x := uint32(n - 1)
  1942. x |= x >> 1
  1943. x |= x >> 2
  1944. x |= x >> 4
  1945. x |= x >> 8
  1946. x |= x >> 16
  1947. return int(x + 1)
  1948. }