forked from tomnomnom/meg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rawhttp.go
70 lines (54 loc) · 1.31 KB
/
rawhttp.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
package main
import (
"fmt"
"strconv"
"strings"
"github.com/tomnomnom/rawhttp"
)
func rawRequest(r request) response {
req, err := rawhttp.FromURL(r.method, r.host)
if err != nil {
return response{request: r, err: err}
}
req.Timeout = r.timeout
req.Path = r.path
r.headers = append(r.headers, "Connection: close")
if !r.HasHeader("Host") {
// add the host header to the request manually so it shows up in the output
r.headers = append(r.headers, fmt.Sprintf("Host: %s", r.Hostname()))
}
if !r.HasHeader("User-Agent") {
r.headers = append(r.headers, fmt.Sprintf("User-Agent: %s", userAgent))
}
for _, h := range r.headers {
req.AddHeader(h)
}
if r.body != "" {
req.Body = r.body
}
if !r.HasHeader("Content-Length") {
req.AutoSetContentLength()
}
resp, err := rawhttp.Do(req)
if err != nil {
return response{request: r, err: err}
}
// Silly me. I should have done this in rawhttp
code, err := strconv.Atoi(resp.StatusCode())
if err != nil {
return response{request: r, err: err}
}
// This should be done in rawhttp too. Whoops.
status := resp.StatusLine()
p := strings.SplitN(resp.StatusLine(), " ", 2)
if len(p) == 2 {
status = p[1]
}
return response{
request: r,
status: status,
statusCode: code,
headers: resp.Headers(),
body: resp.Body(),
}
}