client.go 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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{}, ctype ...string) *Agent {
  362. if a.jsonEncoder == nil {
  363. a.jsonEncoder = json.Marshal
  364. }
  365. if len(ctype) > 0 {
  366. a.req.Header.SetContentType(ctype[0])
  367. } else {
  368. a.req.Header.SetContentType(MIMEApplicationJSON)
  369. }
  370. if body, err := a.jsonEncoder(v); err != nil {
  371. a.errs = append(a.errs, err)
  372. } else {
  373. a.req.SetBody(body)
  374. }
  375. return a
  376. }
  377. // XML sends an XML request.
  378. func (a *Agent) XML(v interface{}) *Agent {
  379. a.req.Header.SetContentType(MIMEApplicationXML)
  380. if body, err := xml.Marshal(v); err != nil {
  381. a.errs = append(a.errs, err)
  382. } else {
  383. a.req.SetBody(body)
  384. }
  385. return a
  386. }
  387. // Form sends form request with body if args is non-nil.
  388. //
  389. // It is recommended obtaining args via AcquireArgs and release it
  390. // manually in performance-critical code.
  391. func (a *Agent) Form(args *Args) *Agent {
  392. a.req.Header.SetContentType(MIMEApplicationForm)
  393. if args != nil {
  394. a.req.SetBody(args.QueryString())
  395. }
  396. return a
  397. }
  398. // FormFile represents multipart form file
  399. type FormFile struct {
  400. // Fieldname is form file's field name
  401. Fieldname string
  402. // Name is form file's name
  403. Name string
  404. // Content is form file's content
  405. Content []byte
  406. // autoRelease indicates if returns the object
  407. // acquired via AcquireFormFile to the pool.
  408. autoRelease bool
  409. }
  410. // FileData appends files for multipart form request.
  411. //
  412. // It is recommended obtaining formFile via AcquireFormFile and release it
  413. // manually in performance-critical code.
  414. func (a *Agent) FileData(formFiles ...*FormFile) *Agent {
  415. a.formFiles = append(a.formFiles, formFiles...)
  416. return a
  417. }
  418. // SendFile reads file and appends it to multipart form request.
  419. func (a *Agent) SendFile(filename string, fieldname ...string) *Agent {
  420. content, err := os.ReadFile(filepath.Clean(filename))
  421. if err != nil {
  422. a.errs = append(a.errs, err)
  423. return a
  424. }
  425. ff := AcquireFormFile()
  426. if len(fieldname) > 0 && fieldname[0] != "" {
  427. ff.Fieldname = fieldname[0]
  428. } else {
  429. ff.Fieldname = "file" + strconv.Itoa(len(a.formFiles)+1)
  430. }
  431. ff.Name = filepath.Base(filename)
  432. ff.Content = append(ff.Content, content...)
  433. ff.autoRelease = true
  434. a.formFiles = append(a.formFiles, ff)
  435. return a
  436. }
  437. // SendFiles reads files and appends them to multipart form request.
  438. //
  439. // Examples:
  440. //
  441. // SendFile("/path/to/file1", "fieldname1", "/path/to/file2")
  442. func (a *Agent) SendFiles(filenamesAndFieldnames ...string) *Agent {
  443. pairs := len(filenamesAndFieldnames)
  444. if pairs&1 == 1 {
  445. filenamesAndFieldnames = append(filenamesAndFieldnames, "")
  446. }
  447. for i := 0; i < pairs; i += 2 {
  448. a.SendFile(filenamesAndFieldnames[i], filenamesAndFieldnames[i+1])
  449. }
  450. return a
  451. }
  452. // Boundary sets boundary for multipart form request.
  453. func (a *Agent) Boundary(boundary string) *Agent {
  454. a.boundary = boundary
  455. return a
  456. }
  457. // MultipartForm sends multipart form request with k-v and files.
  458. //
  459. // It is recommended obtaining args via AcquireArgs and release it
  460. // manually in performance-critical code.
  461. func (a *Agent) MultipartForm(args *Args) *Agent {
  462. if a.mw == nil {
  463. a.mw = multipart.NewWriter(a.req.BodyWriter())
  464. }
  465. if a.boundary != "" {
  466. if err := a.mw.SetBoundary(a.boundary); err != nil {
  467. a.errs = append(a.errs, err)
  468. return a
  469. }
  470. }
  471. a.req.Header.SetMultipartFormBoundary(a.mw.Boundary())
  472. if args != nil {
  473. args.VisitAll(func(key, value []byte) {
  474. if err := a.mw.WriteField(utils.UnsafeString(key), utils.UnsafeString(value)); err != nil {
  475. a.errs = append(a.errs, err)
  476. }
  477. })
  478. }
  479. for _, ff := range a.formFiles {
  480. w, err := a.mw.CreateFormFile(ff.Fieldname, ff.Name)
  481. if err != nil {
  482. a.errs = append(a.errs, err)
  483. continue
  484. }
  485. if _, err = w.Write(ff.Content); err != nil {
  486. a.errs = append(a.errs, err)
  487. }
  488. }
  489. if err := a.mw.Close(); err != nil {
  490. a.errs = append(a.errs, err)
  491. }
  492. return a
  493. }
  494. /************************** End Request Setting **************************/
  495. /************************** Agent Setting **************************/
  496. // Debug mode enables logging request and response detail
  497. func (a *Agent) Debug(w ...io.Writer) *Agent {
  498. a.debugWriter = os.Stdout
  499. if len(w) > 0 {
  500. a.debugWriter = w[0]
  501. }
  502. return a
  503. }
  504. // Timeout sets request timeout duration.
  505. func (a *Agent) Timeout(timeout time.Duration) *Agent {
  506. a.timeout = timeout
  507. return a
  508. }
  509. // Reuse enables the Agent instance to be used again after one request.
  510. //
  511. // If agent is reusable, then it should be released manually when it is no
  512. // longer used.
  513. func (a *Agent) Reuse() *Agent {
  514. a.reuse = true
  515. return a
  516. }
  517. // InsecureSkipVerify controls whether the Agent verifies the server
  518. // certificate chain and host name.
  519. func (a *Agent) InsecureSkipVerify() *Agent {
  520. if a.HostClient.TLSConfig == nil {
  521. a.HostClient.TLSConfig = &tls.Config{InsecureSkipVerify: true} //nolint:gosec // We explicitly let the user set insecure mode here
  522. } else {
  523. a.HostClient.TLSConfig.InsecureSkipVerify = true
  524. }
  525. return a
  526. }
  527. // TLSConfig sets tls config.
  528. func (a *Agent) TLSConfig(config *tls.Config) *Agent {
  529. a.HostClient.TLSConfig = config
  530. return a
  531. }
  532. // MaxRedirectsCount sets max redirect count for GET and HEAD.
  533. func (a *Agent) MaxRedirectsCount(count int) *Agent {
  534. a.maxRedirectsCount = count
  535. return a
  536. }
  537. // JSONEncoder sets custom json encoder.
  538. func (a *Agent) JSONEncoder(jsonEncoder utils.JSONMarshal) *Agent {
  539. a.jsonEncoder = jsonEncoder
  540. return a
  541. }
  542. // JSONDecoder sets custom json decoder.
  543. func (a *Agent) JSONDecoder(jsonDecoder utils.JSONUnmarshal) *Agent {
  544. a.jsonDecoder = jsonDecoder
  545. return a
  546. }
  547. // Request returns Agent request instance.
  548. func (a *Agent) Request() *Request {
  549. return a.req
  550. }
  551. // SetResponse sets custom response for the Agent instance.
  552. //
  553. // It is recommended obtaining custom response via AcquireResponse and release it
  554. // manually in performance-critical code.
  555. func (a *Agent) SetResponse(customResp *Response) *Agent {
  556. a.resp = customResp
  557. return a
  558. }
  559. // Dest sets custom dest.
  560. //
  561. // The contents of dest will be replaced by the response body, if the dest
  562. // is too small a new slice will be allocated.
  563. func (a *Agent) Dest(dest []byte) *Agent {
  564. a.dest = dest
  565. return a
  566. }
  567. // RetryIf controls whether a retry should be attempted after an error.
  568. //
  569. // By default, will use isIdempotent function from fasthttp
  570. func (a *Agent) RetryIf(retryIf RetryIfFunc) *Agent {
  571. a.HostClient.RetryIf = retryIf
  572. return a
  573. }
  574. /************************** End Agent Setting **************************/
  575. // Bytes returns the status code, bytes body and errors of url.
  576. //
  577. // it's not safe to use Agent after calling [Agent.Bytes]
  578. func (a *Agent) Bytes() (int, []byte, []error) {
  579. defer a.release()
  580. return a.bytes()
  581. }
  582. 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.
  583. if errs = append(errs, a.errs...); len(errs) > 0 {
  584. return code, body, errs
  585. }
  586. var (
  587. req = a.req
  588. resp *Response
  589. nilResp bool
  590. )
  591. if a.resp == nil {
  592. resp = AcquireResponse()
  593. nilResp = true
  594. } else {
  595. resp = a.resp
  596. }
  597. defer func() {
  598. if a.debugWriter != nil {
  599. printDebugInfo(req, resp, a.debugWriter)
  600. }
  601. if len(errs) == 0 {
  602. code = resp.StatusCode()
  603. }
  604. body = append(a.dest, resp.Body()...) //nolint:gocritic // We want to append to the returned slice here
  605. if nilResp {
  606. ReleaseResponse(resp)
  607. }
  608. }()
  609. if a.timeout > 0 {
  610. if err := a.HostClient.DoTimeout(req, resp, a.timeout); err != nil {
  611. errs = append(errs, err)
  612. return code, body, errs
  613. }
  614. } else if a.maxRedirectsCount > 0 && (string(req.Header.Method()) == MethodGet || string(req.Header.Method()) == MethodHead) {
  615. if err := a.HostClient.DoRedirects(req, resp, a.maxRedirectsCount); err != nil {
  616. errs = append(errs, err)
  617. return code, body, errs
  618. }
  619. } else if err := a.HostClient.Do(req, resp); err != nil {
  620. errs = append(errs, err)
  621. }
  622. return code, body, errs
  623. }
  624. func printDebugInfo(req *Request, resp *Response, w io.Writer) {
  625. msg := fmt.Sprintf("Connected to %s(%s)\r\n\r\n", req.URI().Host(), resp.RemoteAddr())
  626. _, _ = w.Write(utils.UnsafeBytes(msg)) //nolint:errcheck // This will never fail
  627. _, _ = req.WriteTo(w) //nolint:errcheck // This will never fail
  628. _, _ = resp.WriteTo(w) //nolint:errcheck // This will never fail
  629. }
  630. // String returns the status code, string body and errors of url.
  631. //
  632. // it's not safe to use Agent after calling [Agent.String]
  633. func (a *Agent) String() (int, string, []error) {
  634. defer a.release()
  635. code, body, errs := a.bytes()
  636. // TODO: There might be a data race here on body. Maybe use utils.CopyBytes on it?
  637. return code, utils.UnsafeString(body), errs
  638. }
  639. // Struct returns the status code, bytes body and errors of URL.
  640. // And bytes body will be unmarshalled to given v.
  641. //
  642. // it's not safe to use Agent after calling [Agent.Struct]
  643. func (a *Agent) Struct(v interface{}) (int, []byte, []error) {
  644. defer a.release()
  645. code, body, errs := a.bytes()
  646. if len(errs) > 0 {
  647. return code, body, errs
  648. }
  649. // TODO: This should only be done once
  650. if a.jsonDecoder == nil {
  651. a.jsonDecoder = json.Unmarshal
  652. }
  653. if err := a.jsonDecoder(body, v); err != nil {
  654. errs = append(errs, err)
  655. }
  656. return code, body, errs
  657. }
  658. func (a *Agent) release() {
  659. if !a.reuse {
  660. ReleaseAgent(a)
  661. } else {
  662. a.errs = a.errs[:0]
  663. }
  664. }
  665. func (a *Agent) reset() {
  666. a.HostClient = nil
  667. a.req.Reset()
  668. a.resp = nil
  669. a.dest = nil
  670. a.timeout = 0
  671. a.args = nil
  672. a.errs = a.errs[:0]
  673. a.debugWriter = nil
  674. a.mw = nil
  675. a.reuse = false
  676. a.parsed = false
  677. a.maxRedirectsCount = 0
  678. a.boundary = ""
  679. a.Name = ""
  680. a.NoDefaultUserAgentHeader = false
  681. for i, ff := range a.formFiles {
  682. if ff.autoRelease {
  683. ReleaseFormFile(ff)
  684. }
  685. a.formFiles[i] = nil
  686. }
  687. a.formFiles = a.formFiles[:0]
  688. }
  689. var (
  690. clientPool sync.Pool
  691. agentPool = sync.Pool{
  692. New: func() interface{} {
  693. return &Agent{req: &Request{}}
  694. },
  695. }
  696. responsePool sync.Pool
  697. argsPool sync.Pool
  698. formFilePool sync.Pool
  699. )
  700. // AcquireClient returns an empty Client instance from client pool.
  701. //
  702. // The returned Client instance may be passed to ReleaseClient when it is
  703. // no longer needed. This allows Client recycling, reduces GC pressure
  704. // and usually improves performance.
  705. func AcquireClient() *Client {
  706. v := clientPool.Get()
  707. if v == nil {
  708. return &Client{}
  709. }
  710. c, ok := v.(*Client)
  711. if !ok {
  712. panic(fmt.Errorf("failed to type-assert to *Client"))
  713. }
  714. return c
  715. }
  716. // ReleaseClient returns c acquired via AcquireClient to client pool.
  717. //
  718. // It is forbidden accessing req and/or it's members after returning
  719. // it to client pool.
  720. func ReleaseClient(c *Client) {
  721. c.UserAgent = ""
  722. c.NoDefaultUserAgentHeader = false
  723. c.JSONEncoder = nil
  724. c.JSONDecoder = nil
  725. clientPool.Put(c)
  726. }
  727. // AcquireAgent returns an empty Agent instance from Agent pool.
  728. //
  729. // The returned Agent instance may be passed to ReleaseAgent when it is
  730. // no longer needed. This allows Agent recycling, reduces GC pressure
  731. // and usually improves performance.
  732. func AcquireAgent() *Agent {
  733. a, ok := agentPool.Get().(*Agent)
  734. if !ok {
  735. panic(fmt.Errorf("failed to type-assert to *Agent"))
  736. }
  737. return a
  738. }
  739. // ReleaseAgent returns an acquired via AcquireAgent to Agent pool.
  740. //
  741. // It is forbidden accessing req and/or it's members after returning
  742. // it to Agent pool.
  743. func ReleaseAgent(a *Agent) {
  744. a.reset()
  745. agentPool.Put(a)
  746. }
  747. // AcquireResponse returns an empty Response instance from response pool.
  748. //
  749. // The returned Response instance may be passed to ReleaseResponse when it is
  750. // no longer needed. This allows Response recycling, reduces GC pressure
  751. // and usually improves performance.
  752. // Copy from fasthttp
  753. func AcquireResponse() *Response {
  754. v := responsePool.Get()
  755. if v == nil {
  756. return &Response{}
  757. }
  758. r, ok := v.(*Response)
  759. if !ok {
  760. panic(fmt.Errorf("failed to type-assert to *Response"))
  761. }
  762. return r
  763. }
  764. // ReleaseResponse return resp acquired via AcquireResponse to response pool.
  765. //
  766. // It is forbidden accessing resp and/or it's members after returning
  767. // it to response pool.
  768. // Copy from fasthttp
  769. func ReleaseResponse(resp *Response) {
  770. resp.Reset()
  771. responsePool.Put(resp)
  772. }
  773. // AcquireArgs returns an empty Args object from the pool.
  774. //
  775. // The returned Args may be returned to the pool with ReleaseArgs
  776. // when no longer needed. This allows reducing GC load.
  777. // Copy from fasthttp
  778. func AcquireArgs() *Args {
  779. v := argsPool.Get()
  780. if v == nil {
  781. return &Args{}
  782. }
  783. a, ok := v.(*Args)
  784. if !ok {
  785. panic(fmt.Errorf("failed to type-assert to *Args"))
  786. }
  787. return a
  788. }
  789. // ReleaseArgs returns the object acquired via AcquireArgs to the pool.
  790. //
  791. // String not access the released Args object, otherwise data races may occur.
  792. // Copy from fasthttp
  793. func ReleaseArgs(a *Args) {
  794. a.Reset()
  795. argsPool.Put(a)
  796. }
  797. // AcquireFormFile returns an empty FormFile object from the pool.
  798. //
  799. // The returned FormFile may be returned to the pool with ReleaseFormFile
  800. // when no longer needed. This allows reducing GC load.
  801. func AcquireFormFile() *FormFile {
  802. v := formFilePool.Get()
  803. if v == nil {
  804. return &FormFile{}
  805. }
  806. ff, ok := v.(*FormFile)
  807. if !ok {
  808. panic(fmt.Errorf("failed to type-assert to *FormFile"))
  809. }
  810. return ff
  811. }
  812. // ReleaseFormFile returns the object acquired via AcquireFormFile to the pool.
  813. //
  814. // String not access the released FormFile object, otherwise data races may occur.
  815. func ReleaseFormFile(ff *FormFile) {
  816. ff.Fieldname = ""
  817. ff.Name = ""
  818. ff.Content = ff.Content[:0]
  819. ff.autoRelease = false
  820. formFilePool.Put(ff)
  821. }
  822. const (
  823. defaultUserAgent = "fiber"
  824. )
  825. type multipartWriter interface {
  826. Boundary() string
  827. SetBoundary(boundary string) error
  828. CreateFormFile(fieldname, filename string) (io.Writer, error)
  829. WriteField(fieldname, value string) error
  830. Close() error
  831. }