args.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. package fasthttp
  2. import (
  3. "bytes"
  4. "errors"
  5. "io"
  6. "sort"
  7. "sync"
  8. )
  9. const (
  10. argsNoValue = true
  11. argsHasValue = false
  12. )
  13. // AcquireArgs returns an empty Args object from the pool.
  14. //
  15. // The returned Args may be returned to the pool with ReleaseArgs
  16. // when no longer needed. This allows reducing GC load.
  17. func AcquireArgs() *Args {
  18. return argsPool.Get().(*Args)
  19. }
  20. // ReleaseArgs returns the object acquired via AcquireArgs to the pool.
  21. //
  22. // Do not access the released Args object, otherwise data races may occur.
  23. func ReleaseArgs(a *Args) {
  24. a.Reset()
  25. argsPool.Put(a)
  26. }
  27. var argsPool = &sync.Pool{
  28. New: func() any {
  29. return &Args{}
  30. },
  31. }
  32. // Args represents query arguments.
  33. //
  34. // It is forbidden copying Args instances. Create new instances instead
  35. // and use CopyTo().
  36. //
  37. // Args instance MUST NOT be used from concurrently running goroutines.
  38. type Args struct {
  39. noCopy noCopy
  40. args []argsKV
  41. buf []byte
  42. }
  43. type argsKV struct {
  44. key []byte
  45. value []byte
  46. noValue bool
  47. }
  48. // Reset clears query args.
  49. func (a *Args) Reset() {
  50. a.args = a.args[:0]
  51. }
  52. // CopyTo copies all args to dst.
  53. func (a *Args) CopyTo(dst *Args) {
  54. dst.args = copyArgs(dst.args, a.args)
  55. }
  56. // VisitAll calls f for each existing arg.
  57. //
  58. // f must not retain references to key and value after returning.
  59. // Make key and/or value copies if you need storing them after returning.
  60. func (a *Args) VisitAll(f func(key, value []byte)) {
  61. visitArgs(a.args, f)
  62. }
  63. // Len returns the number of query args.
  64. func (a *Args) Len() int {
  65. return len(a.args)
  66. }
  67. // Parse parses the given string containing query args.
  68. func (a *Args) Parse(s string) {
  69. a.buf = append(a.buf[:0], s...)
  70. a.ParseBytes(a.buf)
  71. }
  72. // ParseBytes parses the given b containing query args.
  73. func (a *Args) ParseBytes(b []byte) {
  74. a.Reset()
  75. var s argsScanner
  76. s.b = b
  77. var kv *argsKV
  78. a.args, kv = allocArg(a.args)
  79. for s.next(kv) {
  80. if len(kv.key) > 0 || len(kv.value) > 0 {
  81. a.args, kv = allocArg(a.args)
  82. }
  83. }
  84. a.args = releaseArg(a.args)
  85. }
  86. // String returns string representation of query args.
  87. func (a *Args) String() string {
  88. return string(a.QueryString())
  89. }
  90. // QueryString returns query string for the args.
  91. //
  92. // The returned value is valid until the Args is reused or released (ReleaseArgs).
  93. // Do not store references to the returned value. Make copies instead.
  94. func (a *Args) QueryString() []byte {
  95. a.buf = a.AppendBytes(a.buf[:0])
  96. return a.buf
  97. }
  98. // Sort sorts Args by key and then value using 'f' as comparison function.
  99. //
  100. // For example args.Sort(bytes.Compare).
  101. func (a *Args) Sort(f func(x, y []byte) int) {
  102. sort.SliceStable(a.args, func(i, j int) bool {
  103. n := f(a.args[i].key, a.args[j].key)
  104. if n == 0 {
  105. return f(a.args[i].value, a.args[j].value) == -1
  106. }
  107. return n == -1
  108. })
  109. }
  110. // AppendBytes appends query string to dst and returns the extended dst.
  111. func (a *Args) AppendBytes(dst []byte) []byte {
  112. for i, n := 0, len(a.args); i < n; i++ {
  113. kv := &a.args[i]
  114. dst = AppendQuotedArg(dst, kv.key)
  115. if !kv.noValue {
  116. dst = append(dst, '=')
  117. if len(kv.value) > 0 {
  118. dst = AppendQuotedArg(dst, kv.value)
  119. }
  120. }
  121. if i+1 < n {
  122. dst = append(dst, '&')
  123. }
  124. }
  125. return dst
  126. }
  127. // WriteTo writes query string to w.
  128. //
  129. // WriteTo implements io.WriterTo interface.
  130. func (a *Args) WriteTo(w io.Writer) (int64, error) {
  131. n, err := w.Write(a.QueryString())
  132. return int64(n), err
  133. }
  134. // Del deletes argument with the given key from query args.
  135. func (a *Args) Del(key string) {
  136. a.args = delAllArgs(a.args, key)
  137. }
  138. // DelBytes deletes argument with the given key from query args.
  139. func (a *Args) DelBytes(key []byte) {
  140. a.args = delAllArgs(a.args, b2s(key))
  141. }
  142. // Add adds 'key=value' argument.
  143. //
  144. // Multiple values for the same key may be added.
  145. func (a *Args) Add(key, value string) {
  146. a.args = appendArg(a.args, key, value, argsHasValue)
  147. }
  148. // AddBytesK adds 'key=value' argument.
  149. //
  150. // Multiple values for the same key may be added.
  151. func (a *Args) AddBytesK(key []byte, value string) {
  152. a.args = appendArg(a.args, b2s(key), value, argsHasValue)
  153. }
  154. // AddBytesV adds 'key=value' argument.
  155. //
  156. // Multiple values for the same key may be added.
  157. func (a *Args) AddBytesV(key string, value []byte) {
  158. a.args = appendArg(a.args, key, b2s(value), argsHasValue)
  159. }
  160. // AddBytesKV adds 'key=value' argument.
  161. //
  162. // Multiple values for the same key may be added.
  163. func (a *Args) AddBytesKV(key, value []byte) {
  164. a.args = appendArg(a.args, b2s(key), b2s(value), argsHasValue)
  165. }
  166. // AddNoValue adds only 'key' as argument without the '='.
  167. //
  168. // Multiple values for the same key may be added.
  169. func (a *Args) AddNoValue(key string) {
  170. a.args = appendArg(a.args, key, "", argsNoValue)
  171. }
  172. // AddBytesKNoValue adds only 'key' as argument without the '='.
  173. //
  174. // Multiple values for the same key may be added.
  175. func (a *Args) AddBytesKNoValue(key []byte) {
  176. a.args = appendArg(a.args, b2s(key), "", argsNoValue)
  177. }
  178. // Set sets 'key=value' argument.
  179. func (a *Args) Set(key, value string) {
  180. a.args = setArg(a.args, key, value, argsHasValue)
  181. }
  182. // SetBytesK sets 'key=value' argument.
  183. func (a *Args) SetBytesK(key []byte, value string) {
  184. a.args = setArg(a.args, b2s(key), value, argsHasValue)
  185. }
  186. // SetBytesV sets 'key=value' argument.
  187. func (a *Args) SetBytesV(key string, value []byte) {
  188. a.args = setArg(a.args, key, b2s(value), argsHasValue)
  189. }
  190. // SetBytesKV sets 'key=value' argument.
  191. func (a *Args) SetBytesKV(key, value []byte) {
  192. a.args = setArgBytes(a.args, key, value, argsHasValue)
  193. }
  194. // SetNoValue sets only 'key' as argument without the '='.
  195. //
  196. // Only key in argument, like key1&key2.
  197. func (a *Args) SetNoValue(key string) {
  198. a.args = setArg(a.args, key, "", argsNoValue)
  199. }
  200. // SetBytesKNoValue sets 'key' argument.
  201. func (a *Args) SetBytesKNoValue(key []byte) {
  202. a.args = setArg(a.args, b2s(key), "", argsNoValue)
  203. }
  204. // Peek returns query arg value for the given key.
  205. //
  206. // The returned value is valid until the Args is reused or released (ReleaseArgs).
  207. // Do not store references to the returned value. Make copies instead.
  208. func (a *Args) Peek(key string) []byte {
  209. return peekArgStr(a.args, key)
  210. }
  211. // PeekBytes returns query arg value for the given key.
  212. //
  213. // The returned value is valid until the Args is reused or released (ReleaseArgs).
  214. // Do not store references to the returned value. Make copies instead.
  215. func (a *Args) PeekBytes(key []byte) []byte {
  216. return peekArgBytes(a.args, key)
  217. }
  218. // PeekMulti returns all the arg values for the given key.
  219. func (a *Args) PeekMulti(key string) [][]byte {
  220. var values [][]byte
  221. a.VisitAll(func(k, v []byte) {
  222. if string(k) == key {
  223. values = append(values, v)
  224. }
  225. })
  226. return values
  227. }
  228. // PeekMultiBytes returns all the arg values for the given key.
  229. func (a *Args) PeekMultiBytes(key []byte) [][]byte {
  230. return a.PeekMulti(b2s(key))
  231. }
  232. // Has returns true if the given key exists in Args.
  233. func (a *Args) Has(key string) bool {
  234. return hasArg(a.args, key)
  235. }
  236. // HasBytes returns true if the given key exists in Args.
  237. func (a *Args) HasBytes(key []byte) bool {
  238. return hasArg(a.args, b2s(key))
  239. }
  240. // ErrNoArgValue is returned when Args value with the given key is missing.
  241. var ErrNoArgValue = errors.New("no Args value for the given key")
  242. // GetUint returns uint value for the given key.
  243. func (a *Args) GetUint(key string) (int, error) {
  244. value := a.Peek(key)
  245. if len(value) == 0 {
  246. return -1, ErrNoArgValue
  247. }
  248. return ParseUint(value)
  249. }
  250. // SetUint sets uint value for the given key.
  251. func (a *Args) SetUint(key string, value int) {
  252. a.buf = AppendUint(a.buf[:0], value)
  253. a.SetBytesV(key, a.buf)
  254. }
  255. // SetUintBytes sets uint value for the given key.
  256. func (a *Args) SetUintBytes(key []byte, value int) {
  257. a.SetUint(b2s(key), value)
  258. }
  259. // GetUintOrZero returns uint value for the given key.
  260. //
  261. // Zero (0) is returned on error.
  262. func (a *Args) GetUintOrZero(key string) int {
  263. n, err := a.GetUint(key)
  264. if err != nil {
  265. n = 0
  266. }
  267. return n
  268. }
  269. // GetUfloat returns ufloat value for the given key.
  270. func (a *Args) GetUfloat(key string) (float64, error) {
  271. value := a.Peek(key)
  272. if len(value) == 0 {
  273. return -1, ErrNoArgValue
  274. }
  275. return ParseUfloat(value)
  276. }
  277. // GetUfloatOrZero returns ufloat value for the given key.
  278. //
  279. // Zero (0) is returned on error.
  280. func (a *Args) GetUfloatOrZero(key string) float64 {
  281. f, err := a.GetUfloat(key)
  282. if err != nil {
  283. f = 0
  284. }
  285. return f
  286. }
  287. // GetBool returns boolean value for the given key.
  288. //
  289. // true is returned for "1", "t", "T", "true", "TRUE", "True", "y", "yes", "Y", "YES", "Yes",
  290. // otherwise false is returned.
  291. func (a *Args) GetBool(key string) bool {
  292. switch string(a.Peek(key)) {
  293. // Support the same true cases as strconv.ParseBool
  294. // See: https://github.com/golang/go/blob/4e1b11e2c9bdb0ddea1141eed487be1a626ff5be/src/strconv/atob.go#L12
  295. // and Y and Yes versions.
  296. case "1", "t", "T", "true", "TRUE", "True", "y", "yes", "Y", "YES", "Yes":
  297. return true
  298. default:
  299. return false
  300. }
  301. }
  302. func visitArgs(args []argsKV, f func(k, v []byte)) {
  303. for i, n := 0, len(args); i < n; i++ {
  304. kv := &args[i]
  305. f(kv.key, kv.value)
  306. }
  307. }
  308. func copyArgs(dst, src []argsKV) []argsKV {
  309. if cap(dst) < len(src) {
  310. tmp := make([]argsKV, len(src))
  311. dstLen := len(dst)
  312. dst = dst[:cap(dst)] // copy all of dst.
  313. copy(tmp, dst)
  314. for i := dstLen; i < len(tmp); i++ {
  315. // Make sure nothing is nil.
  316. tmp[i].key = []byte{}
  317. tmp[i].value = []byte{}
  318. }
  319. dst = tmp
  320. }
  321. n := len(src)
  322. dst = dst[:n]
  323. for i := 0; i < n; i++ {
  324. dstKV := &dst[i]
  325. srcKV := &src[i]
  326. dstKV.key = append(dstKV.key[:0], srcKV.key...)
  327. if srcKV.noValue {
  328. dstKV.value = dstKV.value[:0]
  329. } else {
  330. dstKV.value = append(dstKV.value[:0], srcKV.value...)
  331. }
  332. dstKV.noValue = srcKV.noValue
  333. }
  334. return dst
  335. }
  336. func delAllArgsBytes(args []argsKV, key []byte) []argsKV {
  337. return delAllArgs(args, b2s(key))
  338. }
  339. func delAllArgs(args []argsKV, key string) []argsKV {
  340. for i, n := 0, len(args); i < n; i++ {
  341. kv := &args[i]
  342. if key == string(kv.key) {
  343. tmp := *kv
  344. copy(args[i:], args[i+1:])
  345. n--
  346. i--
  347. args[n] = tmp
  348. args = args[:n]
  349. }
  350. }
  351. return args
  352. }
  353. func setArgBytes(h []argsKV, key, value []byte, noValue bool) []argsKV {
  354. return setArg(h, b2s(key), b2s(value), noValue)
  355. }
  356. func setArg(h []argsKV, key, value string, noValue bool) []argsKV {
  357. n := len(h)
  358. for i := 0; i < n; i++ {
  359. kv := &h[i]
  360. if key == string(kv.key) {
  361. if noValue {
  362. kv.value = kv.value[:0]
  363. } else {
  364. kv.value = append(kv.value[:0], value...)
  365. }
  366. kv.noValue = noValue
  367. return h
  368. }
  369. }
  370. return appendArg(h, key, value, noValue)
  371. }
  372. func appendArgBytes(h []argsKV, key, value []byte, noValue bool) []argsKV {
  373. return appendArg(h, b2s(key), b2s(value), noValue)
  374. }
  375. func appendArg(args []argsKV, key, value string, noValue bool) []argsKV {
  376. var kv *argsKV
  377. args, kv = allocArg(args)
  378. kv.key = append(kv.key[:0], key...)
  379. if noValue {
  380. kv.value = kv.value[:0]
  381. } else {
  382. kv.value = append(kv.value[:0], value...)
  383. }
  384. kv.noValue = noValue
  385. return args
  386. }
  387. func allocArg(h []argsKV) ([]argsKV, *argsKV) {
  388. n := len(h)
  389. if cap(h) > n {
  390. h = h[:n+1]
  391. } else {
  392. h = append(h, argsKV{
  393. value: []byte{},
  394. })
  395. }
  396. return h, &h[n]
  397. }
  398. func releaseArg(h []argsKV) []argsKV {
  399. return h[:len(h)-1]
  400. }
  401. func hasArg(h []argsKV, key string) bool {
  402. for i, n := 0, len(h); i < n; i++ {
  403. kv := &h[i]
  404. if key == string(kv.key) {
  405. return true
  406. }
  407. }
  408. return false
  409. }
  410. func peekArgBytes(h []argsKV, k []byte) []byte {
  411. for i, n := 0, len(h); i < n; i++ {
  412. kv := &h[i]
  413. if bytes.Equal(kv.key, k) {
  414. return kv.value
  415. }
  416. }
  417. return nil
  418. }
  419. func peekArgStr(h []argsKV, k string) []byte {
  420. for i, n := 0, len(h); i < n; i++ {
  421. kv := &h[i]
  422. if string(kv.key) == k {
  423. return kv.value
  424. }
  425. }
  426. return nil
  427. }
  428. type argsScanner struct {
  429. b []byte
  430. }
  431. func (s *argsScanner) next(kv *argsKV) bool {
  432. if len(s.b) == 0 {
  433. return false
  434. }
  435. kv.noValue = argsHasValue
  436. isKey := true
  437. k := 0
  438. for i, c := range s.b {
  439. switch c {
  440. case '=':
  441. if isKey {
  442. isKey = false
  443. kv.key = decodeArgAppend(kv.key[:0], s.b[:i])
  444. k = i + 1
  445. }
  446. case '&':
  447. if isKey {
  448. kv.key = decodeArgAppend(kv.key[:0], s.b[:i])
  449. kv.value = kv.value[:0]
  450. kv.noValue = argsNoValue
  451. } else {
  452. kv.value = decodeArgAppend(kv.value[:0], s.b[k:i])
  453. }
  454. s.b = s.b[i+1:]
  455. return true
  456. }
  457. }
  458. if isKey {
  459. kv.key = decodeArgAppend(kv.key[:0], s.b)
  460. kv.value = kv.value[:0]
  461. kv.noValue = argsNoValue
  462. } else {
  463. kv.value = decodeArgAppend(kv.value[:0], s.b[k:])
  464. }
  465. s.b = s.b[len(s.b):]
  466. return true
  467. }
  468. func decodeArgAppend(dst, src []byte) []byte {
  469. idxPercent := bytes.IndexByte(src, '%')
  470. idxPlus := bytes.IndexByte(src, '+')
  471. if idxPercent == -1 && idxPlus == -1 {
  472. // fast path: src doesn't contain encoded chars
  473. return append(dst, src...)
  474. }
  475. var idx int
  476. switch {
  477. case idxPercent == -1:
  478. idx = idxPlus
  479. case idxPlus == -1:
  480. idx = idxPercent
  481. case idxPercent > idxPlus:
  482. idx = idxPlus
  483. default:
  484. idx = idxPercent
  485. }
  486. dst = append(dst, src[:idx]...)
  487. // slow path
  488. for i := idx; i < len(src); i++ {
  489. c := src[i]
  490. switch c {
  491. case '%':
  492. if i+2 >= len(src) {
  493. return append(dst, src[i:]...)
  494. }
  495. x2 := hex2intTable[src[i+2]]
  496. x1 := hex2intTable[src[i+1]]
  497. if x1 == 16 || x2 == 16 {
  498. dst = append(dst, '%')
  499. } else {
  500. dst = append(dst, x1<<4|x2)
  501. i += 2
  502. }
  503. case '+':
  504. dst = append(dst, ' ')
  505. default:
  506. dst = append(dst, c)
  507. }
  508. }
  509. return dst
  510. }
  511. // decodeArgAppendNoPlus is almost identical to decodeArgAppend, but it doesn't
  512. // substitute '+' with ' '.
  513. //
  514. // The function is copy-pasted from decodeArgAppend due to the performance
  515. // reasons only.
  516. func decodeArgAppendNoPlus(dst, src []byte) []byte {
  517. idx := bytes.IndexByte(src, '%')
  518. if idx < 0 {
  519. // fast path: src doesn't contain encoded chars
  520. return append(dst, src...)
  521. }
  522. dst = append(dst, src[:idx]...)
  523. // slow path
  524. for i := idx; i < len(src); i++ {
  525. c := src[i]
  526. if c == '%' {
  527. if i+2 >= len(src) {
  528. return append(dst, src[i:]...)
  529. }
  530. x2 := hex2intTable[src[i+2]]
  531. x1 := hex2intTable[src[i+1]]
  532. if x1 == 16 || x2 == 16 {
  533. dst = append(dst, '%')
  534. } else {
  535. dst = append(dst, x1<<4|x2)
  536. i += 2
  537. }
  538. } else {
  539. dst = append(dst, c)
  540. }
  541. }
  542. return dst
  543. }
  544. func peekAllArgBytesToDst(dst [][]byte, h []argsKV, k []byte) [][]byte {
  545. for i, n := 0, len(h); i < n; i++ {
  546. kv := &h[i]
  547. if bytes.Equal(kv.key, k) {
  548. dst = append(dst, kv.value)
  549. }
  550. }
  551. return dst
  552. }
  553. func peekArgsKeys(dst [][]byte, h []argsKV) [][]byte {
  554. for i, n := 0, len(h); i < n; i++ {
  555. kv := &h[i]
  556. dst = append(dst, kv.key)
  557. }
  558. return dst
  559. }