-
Notifications
You must be signed in to change notification settings - Fork 551
/
endpoint.go
152 lines (122 loc) · 3.95 KB
/
endpoint.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// SPDX-License-Identifier: Apache-2.0
package gin
import (
"context"
"fmt"
"net/textproto"
"strings"
"github.com/gin-gonic/gin"
"github.com/luraproject/lura/config"
"github.com/luraproject/lura/core"
"github.com/luraproject/lura/proxy"
"github.com/luraproject/lura/router"
"github.com/luraproject/lura/router/mux"
)
const requestParamsAsterisk string = "*"
// HandlerFactory creates a handler function that adapts the gin router with the injected proxy
type HandlerFactory func(*config.EndpointConfig, proxy.Proxy) gin.HandlerFunc
// EndpointHandler implements the HandleFactory interface using the default ToHTTPError function
func EndpointHandler(configuration *config.EndpointConfig, proxy proxy.Proxy) gin.HandlerFunc {
return CustomErrorEndpointHandler(configuration, proxy, router.DefaultToHTTPError)
}
// CustomErrorEndpointHandler implements the HandleFactory interface
func CustomErrorEndpointHandler(configuration *config.EndpointConfig, prxy proxy.Proxy, errF router.ToHTTPError) gin.HandlerFunc {
cacheControlHeaderValue := fmt.Sprintf("public, max-age=%d", int(configuration.CacheTTL.Seconds()))
isCacheEnabled := configuration.CacheTTL.Seconds() != 0
requestGenerator := NewRequest(configuration.HeadersToPass)
render := getRender(configuration)
return func(c *gin.Context) {
requestCtx, cancel := context.WithTimeout(c, configuration.Timeout)
c.Header(core.KrakendHeaderName, core.KrakendHeaderValue)
response, err := prxy(requestCtx, requestGenerator(c, configuration.QueryString))
select {
case <-requestCtx.Done():
if err == nil {
err = router.ErrInternalError
}
default:
}
complete := router.HeaderIncompleteResponseValue
if response != nil && len(response.Data) > 0 {
if response.IsComplete {
complete = router.HeaderCompleteResponseValue
if isCacheEnabled {
c.Header("Cache-Control", cacheControlHeaderValue)
}
}
for k, vs := range response.Metadata.Headers {
for _, v := range vs {
c.Writer.Header().Add(k, v)
}
}
}
c.Header(router.CompleteResponseHeaderName, complete)
if err != nil {
c.Error(err)
if response == nil {
if t, ok := err.(responseError); ok {
c.Status(t.StatusCode())
} else {
c.Status(errF(err))
}
cancel()
return
}
}
render(c, response)
cancel()
}
}
// NewRequest gets a request from the current gin context and the received query string
func NewRequest(headersToSend []string) func(*gin.Context, []string) *proxy.Request {
if len(headersToSend) == 0 {
headersToSend = router.HeadersToSend
}
return func(c *gin.Context, queryString []string) *proxy.Request {
params := make(map[string]string, len(c.Params))
for _, param := range c.Params {
params[strings.Title(param.Key[:1])+param.Key[1:]] = param.Value
}
headers := make(map[string][]string, 3+len(headersToSend))
for _, k := range headersToSend {
if k == requestParamsAsterisk {
headers = c.Request.Header
break
}
if h, ok := c.Request.Header[textproto.CanonicalMIMEHeaderKey(k)]; ok {
headers[k] = h
}
}
headers["X-Forwarded-For"] = []string{mux.ClientIP(c.Request)}
headers["X-Forwarded-Host"] = []string{c.Request.Host}
// if User-Agent is not forwarded using headersToSend, we set
// the KrakenD router User Agent value
if _, ok := headers["User-Agent"]; !ok {
headers["User-Agent"] = router.UserAgentHeaderValue
} else {
headers["X-Forwarded-Via"] = router.UserAgentHeaderValue
}
query := make(map[string][]string, len(queryString))
queryValues := c.Request.URL.Query()
for i := range queryString {
if queryString[i] == requestParamsAsterisk {
query = c.Request.URL.Query()
break
}
if v, ok := queryValues[queryString[i]]; ok && len(v) > 0 {
query[queryString[i]] = v
}
}
return &proxy.Request{
Method: c.Request.Method,
Query: query,
Body: c.Request.Body,
Params: params,
Headers: headers,
}
}
}
type responseError interface {
error
StatusCode() int
}