-
Notifications
You must be signed in to change notification settings - Fork 3
/
request.go
70 lines (59 loc) · 2.18 KB
/
request.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 request
import (
"fmt"
"net/http"
"strings"
)
type Request struct {
CommandLine string //GET / HTTTP/1.1
Method string
Version string
Headers http.Header
Body []byte
}
//Reconstruct the raw request content from a Request struct
func GetRawHTTPRequest(req Request) (rawRequest []byte) {
// command line
fullCommandLine := append([]byte(req.CommandLine), []byte("\r\n")...)
rawRequest = append(rawRequest, fullCommandLine...)
// headers
for header, value := range req.Headers {
for i := 0; i < len(value); i++ {
headerLine := header + ": " + value[i] + "\r\n"
rawRequest = append(rawRequest, []byte(headerLine)...)
}
}
// body
body := append([]byte("\r\n"), req.Body...)
rawRequest = append(rawRequest, body...)
return rawRequest
}
//SetPath: Change the path uri with the one provided for the HTTP Request (modify first line of the raw request)
func (request *Request) SetPath(path string) {
commandLineSplitted := strings.Split(request.CommandLine, " ") //(0: medthod, 1:path 2: version)
commandLineSplitted[1] = path
request.CommandLine = strings.Join(commandLineSplitted, " ")
}
//SetMethod: Change the method for the HTTP Request (modify first line of the raw request + request obect)
func (request *Request) SetMethod(nMethod string) {
request.Method = nMethod
commandLineSplitted := strings.Split(request.CommandLine, " ") //(0: medthod, 1:path 2: version)
commandLineSplitted[0] = nMethod
request.CommandLine = strings.Join(commandLineSplitted, " ")
}
// From net http package (+ withdraw sanitization)
// AddCookie adds a cookie to the request. Per RFC 6265 section 5.4,
// AddCookie does not attach more than one Cookie header field. That
// means all cookies, if any, are written into the same line,
// separated by semicolon.
// AddCookie only sanitizes c's name and value, and does not sanitize
// a Cookie header already present in the request.
func (r *Request) AddCookie(c *http.Cookie) {
//s := fmt.Sprintf("%s=%s", sanitizeCookieName(c.Name), sanitizeCookieValue(c.Value))
s := fmt.Sprintf("%s=%s", c.Name, c.Value)
if c := r.Headers.Get("Cookie"); c != "" {
r.Headers.Set("Cookie", c+"; "+s)
} else {
r.Headers.Set("Cookie", s)
}
}