forked from justmao945/mallory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
59 lines (53 loc) · 1.35 KB
/
http.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
package mallory
import (
"fmt"
"net"
"net/http"
)
// HostOnly returns host if has port in addr, or addr if missing port
func HostOnly(addr string) string {
host, _, err := net.SplitHostPort(addr)
if err != nil {
return addr
} else {
return host
}
}
// copy and overwrite headers from r to w
func CopyHeader(w http.ResponseWriter, r *http.Response) {
// copy headers
dst, src := w.Header(), r.Header
for k, _ := range dst {
dst.Del(k)
}
for k, vs := range src {
for _, v := range vs {
dst.Add(k, v)
}
}
}
// StatusText returns http status text looks like "200 OK"
func StatusText(c int) string {
return fmt.Sprintf("%d %s", c, http.StatusText(c))
}
// Hop-by-hop headers. These are removed when sent to the backend.
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
var hopHeaders = []string{
// If no Accept-Encoding header exists, Transport will add the headers it can accept
// and would wrap the response body with the relevant reader.
"Accept-Encoding",
"Connection",
"Keep-Alive",
"Proxy-Authenticate",
"Proxy-Authorization",
"Te", // canonicalized version of "TE"
"Trailers",
"Transfer-Encoding",
"Upgrade",
"Proxy-Connection", // added by CURL http://homepage.ntlworld.com/jonathan.deboynepollard/FGA/web-proxy-connection-header.html
}
func RemoveHopHeaders(h http.Header) {
for _, k := range hopHeaders {
h.Del(k)
}
}