forked from pbnjay/harhar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_response.go
130 lines (115 loc) · 3.87 KB
/
convert_response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2023-2024 Princess Beef Heavy Industries, LLC / Dave Shanley
// https://pb33f.io
package harhar
import (
"io"
"net/http"
"strings"
"time"
)
func ConvertResponseIntoHttpResponse(r Response) *http.Response {
resp := &http.Response{
StatusCode: r.StatusCode,
Status: r.StatusText,
Proto: r.HTTPVersion,
}
if r.Cookies != nil {
h := http.Header{}
for _, cookie := range r.Cookies {
exp, _ := time.Parse(time.RFC3339Nano, cookie.Expires)
c := &http.Cookie{
Name: cookie.Name,
Path: cookie.Path,
Value: cookie.Value,
Domain: cookie.Domain,
Expires: exp,
HttpOnly: cookie.HTTPOnly,
Secure: cookie.Secure,
}
h.Add("Set-Cookie", c.String())
}
for _, header := range r.Headers {
h.Add(header.Name, header.Value)
}
if r.RedirectURL != "" {
h.Add("Location", r.RedirectURL)
}
if r.Body.MIMEType != "" {
h.Add("Content-Type", r.Body.MIMEType)
}
if r.Body.Encoding != "" {
h.Add("Content-Encoding", r.Body.Encoding)
}
if r.Body.Compression > 0 {
h.Add("Content-Length", string(rune(r.Body.Compression)))
}
if r.Body.Size > 0 {
h.Add("Content-Length", string(rune(r.Body.Size)))
}
if r.Body.Content != "" {
resp.Body = io.NopCloser(strings.NewReader(r.Body.Content))
}
resp.Header = h
}
return resp
/*
r := Response{
StatusCode: hr.StatusCode,
StatusText: http.StatusText(hr.StatusCode),
HTTPVersion: hr.Proto,
HeadersSize: -1,
BodySize: -1,
}
h2 := hr.Header.Clone()
buf := &bytes.Buffer{}
h2.Write(buf)
r.HeadersSize = buf.Len() + 4 // incl. CRLF CRLF
// parse out headers
r.Headers = make([]NameValuePair, 0, len(hr.Header))
for name, vals := range hr.Header {
for _, val := range vals {
r.Headers = append(r.Headers, NameValuePair{Name: name, Value: val})
}
}
rurl, err := hr.Location()
if err == nil {
r.RedirectURL = rurl.String()
}
// parse out cookies
r.Cookies = make([]Cookie, 0, len(hr.Cookies()))
for _, c := range hr.Cookies() {
nc := Cookie{
Name: c.Name,
Path: c.Path,
Value: c.Value,
Domain: c.Domain,
Expires: c.Expires.Format(time.RFC3339Nano),
HTTPOnly: c.HttpOnly,
Secure: c.Secure,
}
r.Cookies = append(r.Cookies, nc)
}
// FIXME: net/http transparently decompresses content,
// so r.Body.Size and r.Body.Compression are not true to the server's response
// also, if the response is not utf-8, then r.Body.Content and r.Body.Encoding
// are not properly handled (spec says to decode anything into UTF-8)
//
// see hr.Uncompressed for next steps
// read in all the data and replace the ReadCloser
bodyData, err := io.ReadAll(hr.Body)
if err != nil {
return r, err
}
hr.Body.Close()
hr.Body = io.NopCloser(bytes.NewReader(bodyData))
r.Body.Content = string(bodyData)
r.Body.Compression = 0
r.Body.Size = len(bodyData)
r.BodySize = r.Body.Size
r.Body.MIMEType = hr.Header.Get("Content-Type")
if r.Body.MIMEType == "" {
// default per RFC2616
r.Body.MIMEType = "application/octet-stream"
}
*/
}