This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrequest_functions.go
91 lines (83 loc) · 2.46 KB
/
request_functions.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
package httpx
import (
"net"
"net/http"
"net/url"
"strings"
)
func ReadHeader(request *http.Request, canonicalHeaderName string) string {
if values, contains := request.Header[canonicalHeaderName]; contains && len(values) > 0 {
return values[0]
} else {
return ""
}
}
func WriteHeader(request *http.Request, canonicalHeaderName string, value string) {
request.Header[canonicalHeaderName] = []string{value}
}
func ReadClientIPAddress(request *http.Request, customHeader string) string {
if address, found := readCustomHeader(ReadHeader(request, customHeader)); found {
return address
} else if address, found = readForwardedFor(request, 0); found {
return address
} else {
return remoteAddressFromTCP(request.RemoteAddr)
}
}
func readCustomHeader(value string) (string, bool) {
return value, len(value) > 0
}
func readForwardedFor(request *http.Request, depth int) (string, bool) {
forwardedFor := ReadHeader(request, HeaderXForwardedFor)
address := remoteAddressFromHeader(forwardedFor, depth)
return address, len(address) > 0
}
func remoteAddressFromHeader(value string, depth int) string {
if index := strings.LastIndex(value, ", "); index < 0 {
return value // not found
} else if depth == 0 {
return value[index+2:] // last iteration, return it
} else {
return remoteAddressFromHeader(value[0:index], depth-1) // remove from the end
}
}
func remoteAddressFromTCP(raw string) string {
value, _, _ := net.SplitHostPort(raw)
return value
}
func CalculateRequestSize(request *http.Request) int64 {
return int64(calculatePreambleSize(request)) +
int64(calculateHeaderSize(request)) +
request.ContentLength + 1
}
func calculatePreambleSize(request *http.Request) (size int) {
size += len(request.Method)
size += 1 // space
size += calculateURLSize(request.URL)
size += 1 // space
size += len(request.Proto)
return size + httpLineBreakLength
}
func calculateURLSize(value *url.URL) int {
pathSize := len(value.Path)
if querySize := len(value.RawQuery); querySize > 0 {
return pathSize + querySize
} else {
return pathSize
}
}
func calculateHeaderSize(request *http.Request) (size int) {
for key, values := range request.Header {
for _, value := range values {
size += calculateHeaderLineSize(key, value)
}
}
return size + httpLineBreakLength
}
func calculateHeaderLineSize(key, value string) (size int) {
size += len(key)
size += 2 // colon and space characters
size += len(value)
return size + httpLineBreakLength
}
const httpLineBreakLength = 2