forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_http_helpers.go
92 lines (72 loc) · 1.91 KB
/
util_http_helpers.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
package main
import (
"bytes"
"github.com/gorilla/context"
"io"
"io/ioutil"
"net/http"
"strings"
)
func GetIPFromRequest(r *http.Request) string {
remoteIPString := r.RemoteAddr
forwarded := r.Header.Get("X-Forwarded-For")
if forwarded != "" {
ips := strings.Split(forwarded, ", ")
remoteIPString = ips[0]
log.Debug("X-Forwarded-For set, remote IP: ", remoteIPString)
}
//Split off port
ipPort := strings.Split(remoteIPString, ":")
if len(ipPort) > 1 {
remoteIPString = ipPort[0]
}
return remoteIPString
}
func CopyHttpRequest(r *http.Request) *http.Request {
reqCopy := new(http.Request)
*reqCopy = *r
if r.Body != nil {
defer r.Body.Close()
// Buffer body data
var bodyBuffer bytes.Buffer
bodyBuffer2 := new(bytes.Buffer)
io.Copy(&bodyBuffer, r.Body)
*bodyBuffer2 = bodyBuffer
// Create new ReadClosers so we can split output
r.Body = ioutil.NopCloser(&bodyBuffer)
reqCopy.Body = ioutil.NopCloser(bodyBuffer2)
}
return reqCopy
}
func CopyHttpResponse(r *http.Response) *http.Response {
resCopy := new(http.Response)
*resCopy = *r
if r.Body != nil {
defer r.Body.Close()
// Buffer body data
var bodyBuffer bytes.Buffer
bodyBuffer2 := new(bytes.Buffer)
io.Copy(&bodyBuffer, r.Body)
*bodyBuffer2 = bodyBuffer
// Create new ReadClosers so we can split output
r.Body = ioutil.NopCloser(&bodyBuffer)
resCopy.Body = ioutil.NopCloser(bodyBuffer2)
}
return resCopy
}
func RecordDetail(r *http.Request) bool {
// Are we even checking?
if !config.EnforceOrgDataDeailLogging {
return config.AnalyticsConfig.EnableDetailedRecording
}
// We are, so get session data
ses, found := context.GetOk(r, OrgSessionContext)
var thisSessionState SessionState
if !found {
// no session found, use global config
return config.AnalyticsConfig.EnableDetailedRecording
}
// Session found
thisSessionState = ses.(SessionState)
return thisSessionState.EnableDetailedRecording
}