client.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. package fiber
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "encoding/json"
  6. "encoding/xml"
  7. "fmt"
  8. "io"
  9. "mime/multipart"
  10. "os"
  11. "path/filepath"
  12. "strconv"
  13. "sync"
  14. "time"
  15. "github.com/gofiber/fiber/v2/utils"
  16. "github.com/valyala/fasthttp"
  17. )
  18. // Request represents HTTP request.
  19. //
  20. // It is forbidden copying Request instances. Create new instances
  21. // and use CopyTo instead.
  22. //
  23. // Request instance MUST NOT be used from concurrently running goroutines.
  24. // Copy from fasthttp
  25. type Request = fasthttp.Request
  26. // Response represents HTTP response.
  27. //
  28. // It is forbidden copying Response instances. Create new instances
  29. // and use CopyTo instead.
  30. //
  31. // Response instance MUST NOT be used from concurrently running goroutines.
  32. // Copy from fasthttp
  33. type Response = fasthttp.Response
  34. // Args represents query arguments.
  35. //
  36. // It is forbidden copying Args instances. Create new instances instead
  37. // and use CopyTo().
  38. //
  39. // Args instance MUST NOT be used from concurrently running goroutines.
  40. // Copy from fasthttp
  41. type Args = fasthttp.Args
  42. // RetryIfFunc signature of retry if function
  43. // Request argument passed to RetryIfFunc, if there are any request errors.
  44. // Copy from fasthttp
  45. type RetryIfFunc = fasthttp.RetryIfFunc
  46. var defaultClient Client
  47. // Client implements http client.
  48. //
  49. // It is safe calling Client methods from concurrently running goroutines.
  50. type Client struct {
  51. mutex sync.RWMutex
  52. // UserAgent is used in User-Agent request header.
  53. UserAgent string
  54. // NoDefaultUserAgentHeader when set to true, causes the default
  55. // User-Agent header to be excluded from the Request.
  56. NoDefaultUserAgentHeader bool
  57. // When set by an external client of Fiber it will use the provided implementation of a
  58. // JSONMarshal
  59. //
  60. // Allowing for flexibility in using another json library for encoding
  61. JSONEncoder utils.JSONMarshal
  62. // When set by an external client of Fiber it will use the provided implementation of a
  63. // JSONUnmarshal
  64. //
  65. // Allowing for flexibility in using another json library for decoding
  66. JSONDecoder utils.JSONUnmarshal
  67. }
  68. // Get returns an agent with http method GET.
  69. func Get(url string) *Agent { return defaultClient.Get(url) }
  70. // Get returns an agent with http method GET.
  71. func (c *Client) Get(url string) *Agent {
  72. return c.createAgent(MethodGet, url)
  73. }
  74. // Head returns an agent with http method HEAD.
  75. func Head(url string) *Agent { return defaultClient.Head(url) }
  76. // Head returns an agent with http method GET.
  77. func (c *Client) Head(url string) *Agent {
  78. return c.createAgent(MethodHead, url)
  79. }
  80. // Post sends POST request to the given URL.
  81. func Post(url string) *Agent { return defaultClient.Post(url) }
  82. // Post sends POST request to the given URL.
  83. func (c *Client) Post(url string) *Agent {
  84. return c.createAgent(MethodPost, url)
  85. }
  86. // Put sends PUT request to the given URL.
  87. func Put(url string) *Agent { return defaultClient.Put(url) }
  88. // Put sends PUT request to the given URL.
  89. func (c *Client) Put(url string) *Agent {
  90. return c.createAgent(MethodPut, url)
  91. }
  92. // Patch sends PATCH request to the given URL.
  93. func Patch(url string) *Agent { return defaultClient.Patch(url) }
  94. // Patch sends PATCH request to the given URL.
  95. func (c *Client) Patch(url string) *Agent {
  96. return c.createAgent(MethodPatch, url)
  97. }
  98. // Delete sends DELETE request to the given URL.
  99. func Delete(url string) *Agent { return defaultClient.Delete(url) }
  100. // Delete sends DELETE request to the given URL.
  101. func (c *Client) Delete(url string) *Agent {
  102. return c.createAgent(MethodDelete, url)
  103. }
  104. func (c *Client) createAgent(method, url string) *Agent {
  105. a := AcquireAgent()
  106. a.req.Header.SetMethod(method)
  107. a.req.SetRequestURI(url)
  108. c.mutex.RLock()
  109. a.Name = c.UserAgent
  110. a.NoDefaultUserAgentHeader = c.NoDefaultUserAgentHeader
  111. a.jsonDecoder = c.JSONDecoder
  112. a.jsonEncoder = c.JSONEncoder
  113. if a.jsonDecoder == nil {
  114. a.jsonDecoder = json.Unmarshal
  115. }
  116. c.mutex.RUnlock()
  117. if err := a.Parse(); err != nil {
  118. a.errs = append(a.errs, err)
  119. }
  120. return a
  121. }
  122. // Agent is an object storing all request data for client.
  123. // Agent instance MUST NOT be used from concurrently running goroutines.
  124. type Agent struct {
  125. // Name is used in User-Agent request header.
  126. Name string
  127. // NoDefaultUserAgentHeader when set to true, causes the default
  128. // User-Agent header to be excluded from the Request.
  129. NoDefaultUserAgentHeader bool
  130. // HostClient is an embedded fasthttp HostClient
  131. *fasthttp.HostClient
  132. req *Request
  133. resp *Response
  134. dest []byte
  135. args *Args
  136. timeout time.Duration
  137. errs []error
  138. formFiles []*FormFile
  139. debugWriter io.Writer
  140. mw multipartWriter
  141. jsonEncoder utils.JSONMarshal
  142. jsonDecoder utils.JSONUnmarshal
  143. maxRedirectsCount int
  144. boundary string
  145. reuse bool
  146. parsed bool
  147. }
  148. // Parse initializes URI and HostClient.
  149. func (a *Agent) Parse() error {
  150. if a.parsed {
  151. return nil
  152. }
  153. a.parsed = true
  154. uri := a.req.URI()
  155. var isTLS bool
  156. scheme := uri.Scheme()
  157. if bytes.Equal(scheme, []byte(schemeHTTPS)) {
  158. isTLS = true
  159. } else if !bytes.Equal(scheme, []byte(schemeHTTP)) {
  160. return fmt.Errorf("unsupported protocol %q. http and https are supported", scheme)
  161. }
  162. name := a.Name
  163. if name == "" && !a.NoDefaultUserAgentHeader {
  164. name = defaultUserAgent
  165. }
  166. a.HostClient = &fasthttp.HostClient{
  167. Addr: fasthttp.AddMissingPort(string(uri.Host()), isTLS),
  168. Name: name,
  169. NoDefaultUserAgentHeader: a.NoDefaultUserAgentHeader,
  170. IsTLS: isTLS,
  171. }
  172. return nil
  173. }
  174. /************************** Header Setting **************************/
  175. // Set sets the given 'key: value' header.
  176. //
  177. // Use Add for setting multiple header values under the same key.
  178. func (a *Agent) Set(k, v string) *Agent {
  179. a.req.Header.Set(k, v)
  180. return a
  181. }
  182. // SetBytesK sets the given 'key: value' header.
  183. //
  184. // Use AddBytesK for setting multiple header values under the same key.
  185. func (a *Agent) SetBytesK(k []byte, v string) *Agent {
  186. a.req.Header.SetBytesK(k, v)
  187. return a
  188. }
  189. // SetBytesV sets the given 'key: value' header.
  190. //
  191. // Use AddBytesV for setting multiple header values under the same key.
  192. func (a *Agent) SetBytesV(k string, v []byte) *Agent {
  193. a.req.Header.SetBytesV(k, v)
  194. return a
  195. }
  196. // SetBytesKV sets the given 'key: value' header.
  197. //
  198. // Use AddBytesKV for setting multiple header values under the same key.
  199. func (a *Agent) SetBytesKV(k, v []byte) *Agent {
  200. a.req.Header.SetBytesKV(k, v)
  201. return a
  202. }
  203. // Add adds the given 'key: value' header.
  204. //
  205. // Multiple headers with the same key may be added with this function.
  206. // Use Set for setting a single header for the given key.
  207. func (a *Agent) Add(k, v string) *Agent {
  208. a.req.Header.Add(k, v)
  209. return a
  210. }
  211. // AddBytesK adds the given 'key: value' header.
  212. //
  213. // Multiple headers with the same key may be added with this function.
  214. // Use SetBytesK for setting a single header for the given key.
  215. func (a *Agent) AddBytesK(k []byte, v string) *Agent {
  216. a.req.Header.AddBytesK(k, v)
  217. return a
  218. }
  219. // AddBytesV adds the given 'key: value' header.
  220. //
  221. // Multiple headers with the same key may be added with this function.
  222. // Use SetBytesV for setting a single header for the given key.
  223. func (a *Agent) AddBytesV(k string, v []byte) *Agent {
  224. a.req.Header.AddBytesV(k, v)
  225. return a
  226. }
  227. // AddBytesKV adds the given 'key: value' header.
  228. //
  229. // Multiple headers with the same key may be added with this function.
  230. // Use SetBytesKV for setting a single header for the given key.
  231. func (a *Agent) AddBytesKV(k, v []byte) *Agent {
  232. a.req.Header.AddBytesKV(k, v)
  233. return a
  234. }
  235. // ConnectionClose sets 'Connection: close' header.
  236. func (a *Agent) ConnectionClose() *Agent {
  237. a.req.Header.SetConnectionClose()
  238. return a
  239. }
  240. // UserAgent sets User-Agent header value.
  241. func (a *Agent) UserAgent(userAgent string) *Agent {
  242. a.req.Header.SetUserAgent(userAgent)
  243. return a
  244. }
  245. // UserAgentBytes sets User-Agent header value.
  246. func (a *Agent) UserAgentBytes(userAgent []byte) *Agent {
  247. a.req.Header.SetUserAgentBytes(userAgent)
  248. return a
  249. }
  250. // Cookie sets one 'key: value' cookie.
  251. func (a *Agent) Cookie(key, value string) *Agent {
  252. a.req.Header.SetCookie(key, value)
  253. return a
  254. }
  255. // CookieBytesK sets one 'key: value' cookie.
  256. func (a *Agent) CookieBytesK(key []byte, value string) *Agent {
  257. a.req.Header.SetCookieBytesK(key, value)
  258. return a
  259. }
  260. // CookieBytesKV sets one 'key: value' cookie.
  261. func (a *Agent) CookieBytesKV(key, value []byte) *Agent {
  262. a.req.Header.SetCookieBytesKV(key, value)
  263. return a
  264. }
  265. // Cookies sets multiple 'key: value' cookies.
  266. func (a *Agent) Cookies(kv ...string) *Agent {
  267. for i := 1; i < len(kv); i += 2 {
  268. a.req.Header.SetCookie(kv[i-1], kv[i])
  269. }
  270. return a
  271. }
  272. // CookiesBytesKV sets multiple 'key: value' cookies.
  273. func (a *Agent) CookiesBytesKV(kv ...[]byte) *Agent {
  274. for i := 1; i < len(kv); i += 2 {
  275. a.req.Header.SetCookieBytesKV(kv[i-1], kv[i])
  276. }
  277. return a
  278. }
  279. // Referer sets Referer header value.
  280. func (a *Agent) Referer(referer string) *Agent {
  281. a.req.Header.SetReferer(referer)
  282. return a
  283. }
  284. // RefererBytes sets Referer header value.
  285. func (a *Agent) RefererBytes(referer []byte) *Agent {
  286. a.req.Header.SetRefererBytes(referer)
  287. return a
  288. }
  289. // ContentType sets Content-Type header value.
  290. func (a *Agent) ContentType(contentType string) *Agent {
  291. a.req.Header.SetContentType(contentType)
  292. return a
  293. }
  294. // ContentTypeBytes sets Content-Type header value.
  295. func (a *Agent) ContentTypeBytes(contentType []byte) *Agent {
  296. a.req.Header.SetContentTypeBytes(contentType)
  297. return a
  298. }
  299. /************************** End Header Setting **************************/
  300. /************************** URI Setting **************************/
  301. // Host sets host for the URI.
  302. func (a *Agent) Host(host string) *Agent {
  303. a.req.URI().SetHost(host)
  304. return a
  305. }
  306. // HostBytes sets host for the URI.
  307. func (a *Agent) HostBytes(host []byte) *Agent {
  308. a.req.URI().SetHostBytes(host)
  309. return a
  310. }
  311. // QueryString sets URI query string.
  312. func (a *Agent) QueryString(queryString string) *Agent {
  313. a.req.URI().SetQueryString(queryString)
  314. return a
  315. }
  316. // QueryStringBytes sets URI query string.
  317. func (a *Agent) QueryStringBytes(queryString []byte) *Agent {
  318. a.req.URI().SetQueryStringBytes(queryString)
  319. return a
  320. }
  321. // BasicAuth sets URI username and password.
  322. func (a *Agent) BasicAuth(username, password string) *Agent {
  323. a.req.URI().SetUsername(username)
  324. a.req.URI().SetPassword(password)
  325. return a
  326. }
  327. // BasicAuthBytes sets URI username and password.
  328. func (a *Agent) BasicAuthBytes(username, password []byte) *Agent {
  329. a.req.URI().SetUsernameBytes(username)
  330. a.req.URI().SetPasswordBytes(password)
  331. return a
  332. }
  333. /************************** End URI Setting **************************/
  334. /************************** Request Setting **************************/
  335. // BodyString sets request body.
  336. func (a *Agent) BodyString(bodyString string) *Agent {
  337. a.req.SetBodyString(bodyString)
  338. return a
  339. }
  340. // Body sets request body.
  341. func (a *Agent) Body(body []byte) *Agent {
  342. a.req.SetBody(body)
  343. return a
  344. }
  345. // BodyStream sets request body stream and, optionally body size.
  346. //
  347. // If bodySize is >= 0, then the bodyStream must provide exactly bodySize bytes
  348. // before returning io.EOF.
  349. //
  350. // If bodySize < 0, then bodyStream is read until io.EOF.
  351. //
  352. // bodyStream.Close() is called after finishing reading all body data
  353. // if it implements io.Closer.
  354. //
  355. // Note that GET and HEAD requests cannot have body.
  356. func (a *Agent) BodyStream(bodyStream io.Reader, bodySize int) *Agent {
  357. a.req.SetBodyStream(bodyStream, bodySize)
  358. return a
  359. }
  360. // JSON sends a JSON request.
  361. func (a *Agent) JSON(v interface{}) *Agent {
  362. if a.jsonEncoder == nil {
  363. a.jsonEncoder = json.Marshal
  364. }
  365. a.req.Header.SetContentType(MIMEApplicationJSON)
  366. if body, err := a.jsonEncoder(v); err != nil {
  367. a.errs = append(a.errs, err)
  368. } else {
  369. a.req.SetBody(body)
  370. }
  371. return a
  372. }
  373. // XML sends an XML request.
  374. func (a *Agent) XML(v interface{}) *Agent {
  375. a.req.Header.SetContentType(MIMEApplicationXML)
  376. if body, err := xml.Marshal(v); err != nil {
  377. a.errs = append(a.errs, err)
  378. } else {
  379. a.req.SetBody(body)
  380. }
  381. return a
  382. }
  383. // Form sends form request with body if args is non-nil.
  384. //
  385. // It is recommended obtaining args via AcquireArgs and release it
  386. // manually in performance-critical code.
  387. func (a *Agent) Form(args *Args) *Agent {
  388. a.req.Header.SetContentType(MIMEApplicationForm)
  389. if args != nil {
  390. a.req.SetBody(args.QueryString())
  391. }
  392. return a
  393. }
  394. // FormFile represents multipart form file
  395. type FormFile struct {
  396. // Fieldname is form file's field name
  397. Fieldname string
  398. // Name is form file's name
  399. Name string
  400. // Content is form file's content
  401. Content []byte
  402. // autoRelease indicates if returns the object
  403. // acquired via AcquireFormFile to the pool.
  404. autoRelease bool
  405. }
  406. // FileData appends files for multipart form request.
  407. //
  408. // It is recommended obtaining formFile via AcquireFormFile and release it
  409. // manually in performance-critical code.
  410. func (a *Agent) FileData(formFiles ...*FormFile) *Agent {
  411. a.formFiles = append(a.formFiles, formFiles...)
  412. return a
  413. }
  414. // SendFile reads file and appends it to multipart form request.
  415. func (a *Agent) SendFile(filename string, fieldname ...string) *Agent {
  416. content, err := os.ReadFile(filepath.Clean(filename))
  417. if err != nil {
  418. a.errs = append(a.errs, err)
  419. return a
  420. }
  421. ff := AcquireFormFile()
  422. if len(fieldname) > 0 && fieldname[0] != "" {
  423. ff.Fieldname = fieldname[0]
  424. } else {
  425. ff.Fieldname = "file" + strconv.Itoa(len(a.formFiles)+1)
  426. }
  427. ff.Name = filepath.Base(filename)
  428. ff.Content = append(ff.Content, content...)
  429. ff.autoRelease = true
  430. a.formFiles = append(a.formFiles, ff)
  431. return a
  432. }
  433. // SendFiles reads files and appends them to multipart form request.
  434. //
  435. // Examples:
  436. //
  437. // SendFile("/path/to/file1", "fieldname1", "/path/to/file2")
  438. func (a *Agent) SendFiles(filenamesAndFieldnames ...string) *Agent {
  439. pairs := len(filenamesAndFieldnames)
  440. if pairs&1 == 1 {
  441. filenamesAndFieldnames = append(filenamesAndFieldnames, "")
  442. }
  443. for i := 0; i < pairs; i += 2 {
  444. a.SendFile(filenamesAndFieldnames[i], filenamesAndFieldnames[i+1])
  445. }
  446. return a
  447. }
  448. // Boundary sets boundary for multipart form request.
  449. func (a *Agent) Boundary(boundary string) *Agent {
  450. a.boundary = boundary
  451. return a
  452. }
  453. // MultipartForm sends multipart form request with k-v and files.
  454. //
  455. // It is recommended obtaining args via AcquireArgs and release it
  456. // manually in performance-critical code.
  457. func (a *Agent) MultipartForm(args *Args) *Agent {
  458. if a.mw == nil {
  459. a.mw = multipart.NewWriter(a.req.BodyWriter())
  460. }
  461. if a.boundary != "" {
  462. if err := a.mw.SetBoundary(a.boundary); err != nil {
  463. a.errs = append(a.errs, err)
  464. return a
  465. }
  466. }
  467. a.req.Header.SetMultipartFormBoundary(a.mw.Boundary())
  468. if args != nil {
  469. args.VisitAll(func(key, value []byte) {
  470. if err := a.mw.WriteField(utils.UnsafeString(key), utils.UnsafeString(value)); err != nil {
  471. a.errs = append(a.errs, err)
  472. }
  473. })
  474. }
  475. for _, ff := range a.formFiles {
  476. w, err := a.mw.CreateFormFile(ff.Fieldname, ff.Name)
  477. if err != nil {
  478. a.errs = append(a.errs, err)
  479. continue
  480. }
  481. if _, err = w.Write(ff.Content); err != nil {
  482. a.errs = append(a.errs, err)
  483. }
  484. }
  485. if err := a.mw.Close(); err != nil {
  486. a.errs = append(a.errs, err)
  487. }
  488. return a
  489. }
  490. /************************** End Request Setting **************************/
  491. /************************** Agent Setting **************************/
  492. // Debug mode enables logging request and response detail
  493. func (a *Agent) Debug(w ...io.Writer) *Agent {
  494. a.debugWriter = os.Stdout
  495. if len(w) > 0 {
  496. a.debugWriter = w[0]
  497. }
  498. return a
  499. }
  500. // Timeout sets request timeout duration.
  501. func (a *Agent) Timeout(timeout time.Duration) *Agent {
  502. a.timeout = timeout
  503. return a
  504. }
  505. // Reuse enables the Agent instance to be used again after one request.
  506. //
  507. // If agent is reusable, then it should be released manually when it is no
  508. // longer used.
  509. func (a *Agent) Reuse() *Agent {
  510. a.reuse = true
  511. return a
  512. }
  513. // InsecureSkipVerify controls whether the Agent verifies the server
  514. // certificate chain and host name.
  515. func (a *Agent) InsecureSkipVerify() *Agent {
  516. if a.HostClient.TLSConfig == nil {
  517. a.HostClient.TLSConfig = &tls.Config{InsecureSkipVerify: true} //nolint:gosec // We explicitly let the user set insecure mode here
  518. } else {
  519. a.HostClient.TLSConfig.InsecureSkipVerify = true
  520. }
  521. return a
  522. }
  523. // TLSConfig sets tls config.
  524. func (a *Agent) TLSConfig(config *tls.Config) *Agent {
  525. a.HostClient.TLSConfig = config
  526. return a
  527. }
  528. // MaxRedirectsCount sets max redirect count for GET and HEAD.
  529. func (a *Agent) MaxRedirectsCount(count int) *Agent {
  530. a.maxRedirectsCount = count
  531. return a
  532. }
  533. // JSONEncoder sets custom json encoder.
  534. func (a *Agent) JSONEncoder(jsonEncoder utils.JSONMarshal) *Agent {
  535. a.jsonEncoder = jsonEncoder
  536. return a
  537. }
  538. // JSONDecoder sets custom json decoder.
  539. func (a *Agent) JSONDecoder(jsonDecoder utils.JSONUnmarshal) *Agent {
  540. a.jsonDecoder = jsonDecoder
  541. return a
  542. }
  543. // Request returns Agent request instance.
  544. func (a *Agent) Request() *Request {
  545. return a.req
  546. }
  547. // SetResponse sets custom response for the Agent instance.
  548. //
  549. // It is recommended obtaining custom response via AcquireResponse and release it
  550. // manually in performance-critical code.
  551. func (a *Agent) SetResponse(customResp *Response) *Agent {
  552. a.resp = customResp
  553. return a
  554. }
  555. // Dest sets custom dest.
  556. //
  557. // The contents of dest will be replaced by the response body, if the dest
  558. // is too small a new slice will be allocated.
  559. func (a *Agent) Dest(dest []byte) *Agent {
  560. a.dest = dest
  561. return a
  562. }
  563. // RetryIf controls whether a retry should be attempted after an error.
  564. //
  565. // By default, will use isIdempotent function from fasthttp
  566. func (a *Agent) RetryIf(retryIf RetryIfFunc) *Agent {
  567. a.HostClient.RetryIf = retryIf
  568. return a
  569. }
  570. /************************** End Agent Setting **************************/
  571. // Bytes returns the status code, bytes body and errors of url.
  572. //
  573. // it's not safe to use Agent after calling [Agent.Bytes]
  574. func (a *Agent) Bytes() (int, []byte, []error) {
  575. defer a.release()
  576. return a.bytes()
  577. }
  578. func (a *Agent) bytes() (code int, body []byte, errs []error) { //nolint:nonamedreturns,revive // We want to overwrite the body in a deferred func. TODO: Check if we really need to do this. We eventually want to get rid of all named returns.
  579. if errs = append(errs, a.errs...); len(errs) > 0 {
  580. return code, body, errs
  581. }
  582. var (
  583. req = a.req
  584. resp *Response
  585. nilResp bool
  586. )
  587. if a.resp == nil {
  588. resp = AcquireResponse()
  589. nilResp = true
  590. } else {
  591. resp = a.resp
  592. }
  593. defer func() {
  594. if a.debugWriter != nil {
  595. printDebugInfo(req, resp, a.debugWriter)
  596. }
  597. if len(errs) == 0 {
  598. code = resp.StatusCode()
  599. }
  600. body = append(a.dest, resp.Body()...) //nolint:gocritic // We want to append to the returned slice here
  601. if nilResp {
  602. ReleaseResponse(resp)
  603. }
  604. }()
  605. if a.timeout > 0 {
  606. if err := a.HostClient.DoTimeout(req, resp, a.timeout); err != nil {
  607. errs = append(errs, err)
  608. return code, body, errs
  609. }
  610. } else if a.maxRedirectsCount > 0 && (string(req.Header.Method()) == MethodGet || string(req.Header.Method()) == MethodHead) {
  611. if err := a.HostClient.DoRedirects(req, resp, a.maxRedirectsCount); err != nil {
  612. errs = append(errs, err)
  613. return code, body, errs
  614. }
  615. } else if err := a.HostClient.Do(req, resp); err != nil {
  616. errs = append(errs, err)
  617. }
  618. return code, body, errs
  619. }
  620. func printDebugInfo(req *Request, resp *Response, w io.Writer) {
  621. msg := fmt.Sprintf("Connected to %s(%s)\r\n\r\n", req.URI().Host(), resp.RemoteAddr())
  622. _, _ = w.Write(utils.UnsafeBytes(msg)) //nolint:errcheck // This will never fail
  623. _, _ = req.WriteTo(w) //nolint:errcheck // This will never fail
  624. _, _ = resp.WriteTo(w) //nolint:errcheck // This will never fail
  625. }
  626. // String returns the status code, string body and errors of url.
  627. //
  628. // it's not safe to use Agent after calling [Agent.String]
  629. func (a *Agent) String() (int, string, []error) {
  630. defer a.release()
  631. code, body, errs := a.bytes()
  632. // TODO: There might be a data race here on body. Maybe use utils.CopyBytes on it?
  633. return code, utils.UnsafeString(body), errs
  634. }
  635. // Struct returns the status code, bytes body and errors of URL.
  636. // And bytes body will be unmarshalled to given v.
  637. //
  638. // it's not safe to use Agent after calling [Agent.Struct]
  639. func (a *Agent) Struct(v interface{}) (int, []byte, []error) {
  640. defer a.release()
  641. code, body, errs := a.bytes()
  642. if len(errs) > 0 {
  643. return code, body, errs
  644. }
  645. // TODO: This should only be done once
  646. if a.jsonDecoder == nil {
  647. a.jsonDecoder = json.Unmarshal
  648. }
  649. if err := a.jsonDecoder(body, v); err != nil {
  650. errs = append(errs, err)
  651. }
  652. return code, body, errs
  653. }
  654. func (a *Agent) release() {
  655. if !a.reuse {
  656. ReleaseAgent(a)
  657. } else {
  658. a.errs = a.errs[:0]
  659. }
  660. }
  661. func (a *Agent) reset() {
  662. a.HostClient = nil
  663. a.req.Reset()
  664. a.resp = nil
  665. a.dest = nil
  666. a.timeout = 0
  667. a.args = nil
  668. a.errs = a.errs[:0]
  669. a.debugWriter = nil
  670. a.mw = nil
  671. a.reuse = false
  672. a.parsed = false
  673. a.maxRedirectsCount = 0
  674. a.boundary = ""
  675. a.Name = ""
  676. a.NoDefaultUserAgentHeader = false
  677. for i, ff := range a.formFiles {
  678. if ff.autoRelease {
  679. ReleaseFormFile(ff)
  680. }
  681. a.formFiles[i] = nil
  682. }
  683. a.formFiles = a.formFiles[:0]
  684. }
  685. var (
  686. clientPool sync.Pool
  687. agentPool = sync.Pool{
  688. New: func() interface{} {
  689. return &Agent{req: &Request{}}
  690. },
  691. }
  692. responsePool sync.Pool
  693. argsPool sync.Pool
  694. formFilePool sync.Pool
  695. )
  696. // AcquireClient returns an empty Client instance from client pool.
  697. //
  698. // The returned Client instance may be passed to ReleaseClient when it is
  699. // no longer needed. This allows Client recycling, reduces GC pressure
  700. // and usually improves performance.
  701. func AcquireClient() *Client {
  702. v := clientPool.Get()
  703. if v == nil {
  704. return &Client{}
  705. }
  706. c, ok := v.(*Client)
  707. if !ok {
  708. panic(fmt.Errorf("failed to type-assert to *Client"))
  709. }
  710. return c
  711. }
  712. // ReleaseClient returns c acquired via AcquireClient to client pool.
  713. //
  714. // It is forbidden accessing req and/or it's members after returning
  715. // it to client pool.
  716. func ReleaseClient(c *Client) {
  717. c.UserAgent = ""
  718. c.NoDefaultUserAgentHeader = false
  719. c.JSONEncoder = nil
  720. c.JSONDecoder = nil
  721. clientPool.Put(c)
  722. }
  723. // AcquireAgent returns an empty Agent instance from Agent pool.
  724. //
  725. // The returned Agent instance may be passed to ReleaseAgent when it is
  726. // no longer needed. This allows Agent recycling, reduces GC pressure
  727. // and usually improves performance.
  728. func AcquireAgent() *Agent {
  729. a, ok := agentPool.Get().(*Agent)
  730. if !ok {
  731. panic(fmt.Errorf("failed to type-assert to *Agent"))
  732. }
  733. return a
  734. }
  735. // ReleaseAgent returns an acquired via AcquireAgent to Agent pool.
  736. //
  737. // It is forbidden accessing req and/or it's members after returning
  738. // it to Agent pool.
  739. func ReleaseAgent(a *Agent) {
  740. a.reset()
  741. agentPool.Put(a)
  742. }
  743. // AcquireResponse returns an empty Response instance from response pool.
  744. //
  745. // The returned Response instance may be passed to ReleaseResponse when it is
  746. // no longer needed. This allows Response recycling, reduces GC pressure
  747. // and usually improves performance.
  748. // Copy from fasthttp
  749. func AcquireResponse() *Response {
  750. v := responsePool.Get()
  751. if v == nil {
  752. return &Response{}
  753. }
  754. r, ok := v.(*Response)
  755. if !ok {
  756. panic(fmt.Errorf("failed to type-assert to *Response"))
  757. }
  758. return r
  759. }
  760. // ReleaseResponse return resp acquired via AcquireResponse to response pool.
  761. //
  762. // It is forbidden accessing resp and/or it's members after returning
  763. // it to response pool.
  764. // Copy from fasthttp
  765. func ReleaseResponse(resp *Response) {
  766. resp.Reset()
  767. responsePool.Put(resp)
  768. }
  769. // AcquireArgs returns an empty Args object from the pool.
  770. //
  771. // The returned Args may be returned to the pool with ReleaseArgs
  772. // when no longer needed. This allows reducing GC load.
  773. // Copy from fasthttp
  774. func AcquireArgs() *Args {
  775. v := argsPool.Get()
  776. if v == nil {
  777. return &Args{}
  778. }
  779. a, ok := v.(*Args)
  780. if !ok {
  781. panic(fmt.Errorf("failed to type-assert to *Args"))
  782. }
  783. return a
  784. }
  785. // ReleaseArgs returns the object acquired via AcquireArgs to the pool.
  786. //
  787. // String not access the released Args object, otherwise data races may occur.
  788. // Copy from fasthttp
  789. func ReleaseArgs(a *Args) {
  790. a.Reset()
  791. argsPool.Put(a)
  792. }
  793. // AcquireFormFile returns an empty FormFile object from the pool.
  794. //
  795. // The returned FormFile may be returned to the pool with ReleaseFormFile
  796. // when no longer needed. This allows reducing GC load.
  797. func AcquireFormFile() *FormFile {
  798. v := formFilePool.Get()
  799. if v == nil {
  800. return &FormFile{}
  801. }
  802. ff, ok := v.(*FormFile)
  803. if !ok {
  804. panic(fmt.Errorf("failed to type-assert to *FormFile"))
  805. }
  806. return ff
  807. }
  808. // ReleaseFormFile returns the object acquired via AcquireFormFile to the pool.
  809. //
  810. // String not access the released FormFile object, otherwise data races may occur.
  811. func ReleaseFormFile(ff *FormFile) {
  812. ff.Fieldname = ""
  813. ff.Name = ""
  814. ff.Content = ff.Content[:0]
  815. ff.autoRelease = false
  816. formFilePool.Put(ff)
  817. }
  818. const (
  819. defaultUserAgent = "fiber"
  820. )
  821. type multipartWriter interface {
  822. Boundary() string
  823. SetBoundary(boundary string) error
  824. CreateFormFile(fieldname, filename string) (io.Writer, error)
  825. WriteField(fieldname, value string) error
  826. Close() error
  827. }