@@ -5,8 +5,10 @@ import (
5
5
"context"
6
6
"errors"
7
7
"io"
8
+ "iter"
8
9
"path/filepath"
9
10
"reflect"
11
+ "slices"
10
12
"strconv"
11
13
"sync"
12
14
"time"
@@ -129,6 +131,31 @@ func (r *Request) Header(key string) []string {
129
131
return r .header .PeekMultiple (key )
130
132
}
131
133
134
+ // Headers returns all headers in the request using an iterator.
135
+ // You can use maps.Collect() to collect all headers into a map.
136
+ //
137
+ // The returned value is valid until the request object is released.
138
+ // Any future calls to Headers method will return the modified value. Do not store references to returned value. Make copies instead.
139
+ func (r * Request ) Headers () iter.Seq2 [string , []string ] {
140
+ return func (yield func (string , []string ) bool ) {
141
+ peekKeys := r .header .PeekKeys ()
142
+ keys := make ([][]byte , len (peekKeys ))
143
+ copy (keys , peekKeys ) // It is necessary to have immutable byte slice.
144
+
145
+ for _ , key := range keys {
146
+ vals := r .header .PeekAll (utils .UnsafeString (key ))
147
+ valsStr := make ([]string , len (vals ))
148
+ for i , v := range vals {
149
+ valsStr [i ] = utils .UnsafeString (v )
150
+ }
151
+
152
+ if ! yield (utils .UnsafeString (key ), valsStr ) {
153
+ return
154
+ }
155
+ }
156
+ }
157
+ }
158
+
132
159
// AddHeader method adds a single header field and its value in the request instance.
133
160
func (r * Request ) AddHeader (key , val string ) * Request {
134
161
r .header .Add (key , val )
@@ -168,6 +195,33 @@ func (r *Request) Param(key string) []string {
168
195
return res
169
196
}
170
197
198
+ // Params returns all params in the request using an iterator.
199
+ // You can use maps.Collect() to collect all params into a map.
200
+ //
201
+ // The returned value is valid until the request object is released.
202
+ // Any future calls to Params method will return the modified value. Do not store references to returned value. Make copies instead.
203
+ func (r * Request ) Params () iter.Seq2 [string , []string ] {
204
+ return func (yield func (string , []string ) bool ) {
205
+ keys := r .params .Keys ()
206
+
207
+ for _ , key := range keys {
208
+ if key == "" {
209
+ continue
210
+ }
211
+
212
+ vals := r .params .PeekMulti (key )
213
+ valsStr := make ([]string , len (vals ))
214
+ for i , v := range vals {
215
+ valsStr [i ] = utils .UnsafeString (v )
216
+ }
217
+
218
+ if ! yield (key , valsStr ) {
219
+ return
220
+ }
221
+ }
222
+ }
223
+ }
224
+
171
225
// AddParam method adds a single param field and its value in the request instance.
172
226
func (r * Request ) AddParam (key , val string ) * Request {
173
227
r .params .Add (key , val )
@@ -254,6 +308,18 @@ func (r *Request) Cookie(key string) string {
254
308
return ""
255
309
}
256
310
311
+ // Cookies returns all cookies in the cookies using an iterator.
312
+ // You can use maps.Collect() to collect all cookies into a map.
313
+ func (r * Request ) Cookies () iter.Seq2 [string , string ] {
314
+ return func (yield func (string , string ) bool ) {
315
+ r .cookies .VisitAll (func (key , val string ) {
316
+ if ! yield (key , val ) {
317
+ return
318
+ }
319
+ })
320
+ }
321
+ }
322
+
257
323
// SetCookie method sets a single cookie field and its value in the request instance.
258
324
// It will override cookie which set in client instance.
259
325
func (r * Request ) SetCookie (key , val string ) * Request {
@@ -291,6 +357,18 @@ func (r *Request) PathParam(key string) string {
291
357
return ""
292
358
}
293
359
360
+ // PathParams returns all path params in request instance.
361
+ // You can use maps.Collect() to collect all cookies into a map.
362
+ func (r * Request ) PathParams () iter.Seq2 [string , string ] {
363
+ return func (yield func (string , string ) bool ) {
364
+ r .path .VisitAll (func (key , val string ) {
365
+ if ! yield (key , val ) {
366
+ return
367
+ }
368
+ })
369
+ }
370
+ }
371
+
294
372
// SetPathParam method sets a single path param field and its value in the request instance.
295
373
// It will override path param which set in client instance.
296
374
func (r * Request ) SetPathParam (key , val string ) * Request {
@@ -376,6 +454,33 @@ func (r *Request) FormData(key string) []string {
376
454
return res
377
455
}
378
456
457
+ // AllFormData method returns all form datas in request instance.
458
+ // You can use maps.Collect() to collect all cookies into a map.
459
+ //
460
+ // The returned value is valid until the request object is released.
461
+ // Any future calls to FormDatas method will return the modified value. Do not store references to returned value. Make copies instead.
462
+ func (r * Request ) AllFormData () iter.Seq2 [string , []string ] {
463
+ return func (yield func (string , []string ) bool ) {
464
+ keys := r .formData .Keys ()
465
+
466
+ for _ , key := range keys {
467
+ if key == "" {
468
+ continue
469
+ }
470
+
471
+ vals := r .formData .PeekMulti (key )
472
+ valsStr := make ([]string , len (vals ))
473
+ for i , v := range vals {
474
+ valsStr [i ] = utils .UnsafeString (v )
475
+ }
476
+
477
+ if ! yield (key , valsStr ) {
478
+ return
479
+ }
480
+ }
481
+ }
482
+ }
483
+
379
484
// AddFormData method adds a single form data field and its value in the request instance.
380
485
func (r * Request ) AddFormData (key , val string ) * Request {
381
486
r .formData .AddData (key , val )
@@ -435,6 +540,14 @@ func (r *Request) File(name string) *File {
435
540
return nil
436
541
}
437
542
543
+ // Files method returns all files in request instance.
544
+ //
545
+ // The returned value is valid until the request object is released.
546
+ // Any future calls to Files method will return the modified value. Do not store references to returned value. Make copies instead.
547
+ func (r * Request ) Files () []* File {
548
+ return r .files
549
+ }
550
+
438
551
// FileByPath returns file ptr store in request obj by path.
439
552
func (r * Request ) FileByPath (path string ) * File {
440
553
for _ , v := range r .files {
@@ -617,6 +730,16 @@ type QueryParam struct {
617
730
* fasthttp.Args
618
731
}
619
732
733
+ // Keys method returns all keys in the query params.
734
+ func (p * QueryParam ) Keys () []string {
735
+ keys := make ([]string , 0 , p .Len ())
736
+ p .VisitAll (func (key , _ []byte ) {
737
+ keys = append (keys , utils .UnsafeString (key ))
738
+ })
739
+
740
+ return slices .Compact (keys )
741
+ }
742
+
620
743
// AddParams receive a map and add each value to param.
621
744
func (p * QueryParam ) AddParams (r map [string ][]string ) {
622
745
for k , v := range r {
@@ -747,6 +870,16 @@ type FormData struct {
747
870
* fasthttp.Args
748
871
}
749
872
873
+ // Keys method returns all keys in the form data.
874
+ func (f * FormData ) Keys () []string {
875
+ keys := make ([]string , 0 , f .Len ())
876
+ f .VisitAll (func (key , _ []byte ) {
877
+ keys = append (keys , utils .UnsafeString (key ))
878
+ })
879
+
880
+ return slices .Compact (keys )
881
+ }
882
+
750
883
// AddData method is a wrapper of Args's Add method.
751
884
func (f * FormData ) AddData (key , val string ) {
752
885
f .Add (key , val )
0 commit comments