Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/projectfile/httputil.go
2070 views
1
package projectfile
2
3
import (
4
"bytes"
5
"crypto/sha256"
6
"encoding/gob"
7
"encoding/hex"
8
"io"
9
"maps"
10
"net/http"
11
)
12
13
func hash(v interface{}) (string, error) {
14
data, err := marshal(v)
15
if err != nil {
16
return "", err
17
}
18
19
sh := sha256.New()
20
21
if _, err = sh.Write(data); err != nil {
22
return "", err
23
}
24
return hex.EncodeToString(sh.Sum(nil)), nil
25
}
26
27
func marshal(data interface{}) ([]byte, error) {
28
var b bytes.Buffer
29
enc := gob.NewEncoder(&b)
30
if err := enc.Encode(data); err != nil {
31
return nil, err
32
}
33
34
return b.Bytes(), nil
35
}
36
37
func unmarshal(data []byte, obj interface{}) error {
38
dec := gob.NewDecoder(bytes.NewBuffer(data))
39
if err := dec.Decode(obj); err != nil {
40
return err
41
}
42
43
return nil
44
}
45
46
type HTTPRecord struct {
47
Request []byte
48
Response *InternalResponse
49
}
50
51
type InternalRequest struct {
52
Target string
53
HTTPMajor int
54
HTTPMinor int
55
Method string
56
Headers map[string][]string
57
Body []byte
58
}
59
60
type InternalResponse struct {
61
HTTPMajor int
62
HTTPMinor int
63
StatusCode int
64
StatusReason string
65
Headers map[string][]string
66
Body []byte
67
}
68
69
// Unused
70
// func newInternalRequest() *InternalRequest {
71
// return &InternalRequest{
72
// Headers: make(map[string][]string),
73
// }
74
// }
75
76
func newInternalResponse() *InternalResponse {
77
return &InternalResponse{
78
Headers: make(map[string][]string),
79
}
80
}
81
82
func toInternalResponse(resp *http.Response, body []byte) *InternalResponse {
83
intResp := newInternalResponse()
84
85
intResp.HTTPMajor = resp.ProtoMajor
86
intResp.HTTPMinor = resp.ProtoMinor
87
intResp.StatusCode = resp.StatusCode
88
intResp.StatusReason = resp.Status
89
maps.Copy(intResp.Headers, resp.Header)
90
intResp.Body = body
91
return intResp
92
}
93
94
func fromInternalResponse(intResp *InternalResponse) *http.Response {
95
var contentLength int64
96
if intResp.Body != nil {
97
contentLength = int64(len(intResp.Body))
98
}
99
return &http.Response{
100
ProtoMinor: intResp.HTTPMinor,
101
ProtoMajor: intResp.HTTPMajor,
102
Status: intResp.StatusReason,
103
StatusCode: intResp.StatusCode,
104
Header: intResp.Headers,
105
ContentLength: contentLength,
106
Body: io.NopCloser(bytes.NewReader(intResp.Body)),
107
}
108
}
109
110