forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_http_helpers.go
53 lines (39 loc) · 978 Bytes
/
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
package main
import (
"bytes"
"io"
"io/ioutil"
"net/http"
)
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
}