-
Notifications
You must be signed in to change notification settings - Fork 553
/
http.go
94 lines (80 loc) · 3.25 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
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
package proxy
import (
"context"
"errors"
"net/http"
"github.com/devopsfaith/krakend/config"
"github.com/devopsfaith/krakend/encoding"
)
// ErrInvalidStatusCode is the error returned by the http proxy when the received status code
// is not a 200 nor a 201
var ErrInvalidStatusCode = errors.New("Invalid status code")
var httpProxy = CustomHTTPProxyFactory(NewHTTPClient)
// HTTPProxyFactory returns a BackendFactory. The Proxies it creates will use the received net/http.Client
func HTTPProxyFactory(client *http.Client) BackendFactory {
return CustomHTTPProxyFactory(func(_ context.Context) *http.Client { return client })
}
// CustomHTTPProxyFactory returns a BackendFactory. The Proxies it creates will use the received HTTPClientFactory
func CustomHTTPProxyFactory(cf HTTPClientFactory) BackendFactory {
return func(backend *config.Backend) Proxy {
return NewHTTPProxy(backend, cf, backend.Decoder)
}
}
// NewHTTPProxy creates a http proxy with the injected configuration, HTTPClientFactory and Decoder
func NewHTTPProxy(remote *config.Backend, clientFactory HTTPClientFactory, decode encoding.Decoder) Proxy {
return NewHTTPProxyWithHTTPExecutor(remote, DefaultHTTPRequestExecutor(clientFactory), decode)
}
// NewHTTPProxyWithHTTPExecutor creates a http proxy with the injected configuration, HTTPRequestExecutor and Decoder
func NewHTTPProxyWithHTTPExecutor(remote *config.Backend, requestExecutor HTTPRequestExecutor, dec encoding.Decoder) Proxy {
if remote.Encoding == encoding.NOOP {
return NewHTTPProxyDetailed(remote, requestExecutor, NoOpHTTPStatusHandler, NoOpHTTPResponseParser)
}
ef := NewEntityFormatter(remote)
rp := DefaultHTTPResponseParserFactory(HTTPResponseParserConfig{dec, ef})
return NewHTTPProxyDetailed(remote, requestExecutor, DefaultHTTPStatusHandler, rp)
}
// NewHTTPProxyDetailed creates a http proxy with the injected configuration, HTTPRequestExecutor, Decoder and HTTPResponseParser
func NewHTTPProxyDetailed(remote *config.Backend, requestExecutor HTTPRequestExecutor, ch HTTPStatusHandler, rp HTTPResponseParser) Proxy {
return func(ctx context.Context, request *Request) (*Response, error) {
requestToBakend, err := http.NewRequest(request.Method, request.URL.String(), request.Body)
if err != nil {
return nil, err
}
requestToBakend.Header = make(map[string][]string, len(request.Headers))
for k, vs := range request.Headers {
tmp := make([]string, len(vs))
copy(tmp, vs)
requestToBakend.Header[k] = tmp
}
resp, err := requestExecutor(ctx, requestToBakend)
requestToBakend.Body.Close()
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
if err != nil {
return nil, err
}
resp, err = ch(ctx, resp)
if err != nil {
return nil, err
}
return rp(ctx, resp)
}
}
// NewRequestBuilderMiddleware creates a proxy middleware that parses the request params received
// from the outter layer and generates the path to the backend endpoints
func NewRequestBuilderMiddleware(remote *config.Backend) Middleware {
return func(next ...Proxy) Proxy {
if len(next) > 1 {
panic(ErrTooManyProxies)
}
return func(ctx context.Context, request *Request) (*Response, error) {
r := request.Clone()
r.GeneratePath(remote.URLPattern)
r.Method = remote.Method
return next[0](ctx, &r)
}
}
}