-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxy.go
111 lines (92 loc) · 2.92 KB
/
proxy.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package offloader
import (
"errors"
"io"
"net/http"
"strings"
"time"
)
const (
HeaderPrefix = "Offload-"
HeaderOffloadRequested = HeaderPrefix + "Requested"
HeaderRequestedMethod = HeaderPrefix + "Method"
HeaderRequestedUrl = HeaderPrefix + "Url"
HeaderForwardBody = HeaderPrefix + "Forward-Body"
HeaderCustomHeaderPrefix = HeaderPrefix + "X-"
)
var (
ErrInvalidVerb = errors.New("unsupported verb")
ErrMissingUrl = errors.New("missing url")
)
// Handler offloads requests from an HTTP backend, modifying its behavior based on response headers that the backend
// returns. It's designed to be used with httputil.ReverseProxy as a ModifyResponse function.
func Handler(proxyResponse *http.Response) (err error) {
// If offload hasn't been requested, then simply proxy the backend response to the client
if _, ok := proxyResponse.Header[HeaderOffloadRequested]; !ok {
return nil
}
// The response object is updated in place, so the proxy response is actually the backend response here
proxyRequest, err := newProxyRequestFromBackendResponse(proxyResponse)
if err != nil {
return err
}
// TODO: What's a reasonable timeout? Should this be configurable by the backend too?
c := &http.Client{
Timeout: 30 * time.Second,
}
offloadResponse, err := c.Do(proxyRequest)
if err != nil {
return err
}
proxyResponse.Header = offloadResponse.Header
proxyResponse.StatusCode = offloadResponse.StatusCode
proxyResponse.Body = offloadResponse.Body
return nil
}
func newProxyRequestFromBackendResponse(backendResponse *http.Response) (*http.Request, error) {
method := strings.ToUpper(backendResponse.Header.Get(HeaderRequestedMethod))
if !isSupportedMethod(method) {
return nil, ErrInvalidVerb
}
url := backendResponse.Header.Get(HeaderRequestedUrl)
if url == "" {
return nil, ErrMissingUrl
}
var body io.Reader
if _, ok := backendResponse.Header[HeaderForwardBody]; ok {
body = backendResponse.Body
}
proxyRequest, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
prepareProxyRequestHeaders(proxyRequest, backendResponse)
return proxyRequest, nil
}
func prepareProxyRequestHeaders(proxyRequest *http.Request, backendResponse *http.Response) {
preservedHeaders := map[string]string{}
for key, val := range backendResponse.Header {
if strings.HasPrefix(key, HeaderCustomHeaderPrefix) {
trimmedKey := strings.TrimPrefix(key, HeaderCustomHeaderPrefix)
preservedHeaders[trimmedKey] = val[0]
}
// Clear out all existing response headers from the backend
backendResponse.Header.Del(key)
}
proxyRequest.Header.Set("Content-type", backendResponse.Header.Get("Content-type"))
for key, val := range preservedHeaders {
proxyRequest.Header.Set(key, val)
}
}
func isSupportedMethod(verb string) bool {
switch verb {
case http.MethodGet:
return true
case http.MethodPost:
return true
case http.MethodHead:
return true
default:
return false
}
}