Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/fuzz/component/query.go
2070 views
1
package component
2
3
import (
4
"context"
5
6
"github.com/pkg/errors"
7
"github.com/projectdiscovery/nuclei/v3/pkg/fuzz/dataformat"
8
"github.com/projectdiscovery/retryablehttp-go"
9
urlutil "github.com/projectdiscovery/utils/url"
10
)
11
12
// Query is a component for a request query
13
type Query struct {
14
value *Value
15
16
req *retryablehttp.Request
17
}
18
19
var _ Component = &Query{}
20
21
// NewQuery creates a new query component
22
func NewQuery() *Query {
23
return &Query{}
24
}
25
26
// Name returns the name of the component
27
func (q *Query) Name() string {
28
return RequestQueryComponent
29
}
30
31
// Parse parses the component and returns the
32
// parsed component
33
func (q *Query) Parse(req *retryablehttp.Request) (bool, error) {
34
if req.URL.Query().IsEmpty() {
35
return false, nil
36
}
37
q.req = req
38
39
q.value = NewValue(req.URL.Query().Encode())
40
41
parsed, err := dataformat.Get(dataformat.FormDataFormat).Decode(q.value.String())
42
if err != nil {
43
return false, err
44
}
45
q.value.SetParsed(parsed, dataformat.FormDataFormat)
46
return true, nil
47
}
48
49
// Iterate iterates through the component
50
func (q *Query) Iterate(callback func(key string, value interface{}) error) (errx error) {
51
q.value.parsed.Iterate(func(key string, value interface{}) bool {
52
if err := callback(key, value); err != nil {
53
errx = err
54
return false
55
}
56
return true
57
})
58
return
59
}
60
61
// SetValue sets a value in the component
62
// for a key
63
func (q *Query) SetValue(key string, value string) error {
64
// Is this safe?
65
if !q.value.SetParsedValue(key, value) {
66
return ErrSetValue
67
}
68
return nil
69
}
70
71
// Delete deletes a key from the component
72
func (q *Query) Delete(key string) error {
73
if !q.value.Delete(key) {
74
return ErrKeyNotFound
75
}
76
return nil
77
}
78
79
// Rebuild returns a new request with the
80
// component rebuilt
81
func (q *Query) Rebuild() (*retryablehttp.Request, error) {
82
encoded, err := q.value.Encode()
83
if err != nil {
84
return nil, errors.Wrap(err, "could not encode query")
85
}
86
cloned := q.req.Clone(context.Background())
87
cloned.RawQuery = encoded
88
89
// Clear the query parameters and re-add them
90
cloned.Params = nil
91
cloned.Params = urlutil.NewOrderedParams()
92
cloned.Params.Decode(encoded)
93
cloned.Update()
94
return cloned, nil
95
}
96
97
// Clones current state to a new component
98
func (q *Query) Clone() Component {
99
return &Query{
100
value: q.value.Clone(),
101
req: q.req.Clone(context.Background()),
102
}
103
}
104
105