forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuribuilder.go
114 lines (96 loc) · 3.28 KB
/
uribuilder.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
package uri
import (
"net/url"
"strconv"
"strings"
"time"
"github.com/kataras/iris/cache/cfg"
)
// URIBuilder is the requested url builder
// which keeps all the information the server
// should know to save or retrieve a cached entry's response
// used on client-side only
// both from net/http and valyala/fasthttp
type URIBuilder struct {
serverAddr,
clientMethod,
clientURI string
cacheLifetime time.Duration
cacheStatuscode int
cacheContentType string
}
// ServerAddr sets the server address for the final request url
func (r *URIBuilder) ServerAddr(s string) *URIBuilder {
r.serverAddr = s
return r
}
// ClientMethod sets the method which the client should call the remote server's handler
// used to build the final request url too
func (r *URIBuilder) ClientMethod(s string) *URIBuilder {
r.clientMethod = s
return r
}
// ClientURI sets the client path for the final request url
func (r *URIBuilder) ClientURI(s string) *URIBuilder {
r.clientURI = s
return r
}
// Lifetime sets the cache lifetime for the final request url
func (r *URIBuilder) Lifetime(d time.Duration) *URIBuilder {
r.cacheLifetime = d
return r
}
// StatusCode sets the cache status code for the final request url
func (r *URIBuilder) StatusCode(code int) *URIBuilder {
r.cacheStatuscode = code
return r
}
// ContentType sets the cache content type for the final request url
func (r *URIBuilder) ContentType(s string) *URIBuilder {
r.cacheContentType = s
return r
}
// String returns the full url which should be passed to get a cache entry response back
// (it could be setted by server too but we need some client-freedom on the requested key)
// in order to be sure that the registered cache entries are unique among different clients with the same key
// note1: we do it manually*,
// note2: on fasthttp that is not required because the query args added as expected but we will use it there too to be align with net/http
func (r URIBuilder) String() string {
return r.build()
}
func (r URIBuilder) build() string {
remoteURL := r.serverAddr
// fasthttp appends the "/" in the last uri (with query args also, that's probably a fasthttp bug which I'll fix later)
// for now lets make that check:
if !strings.HasSuffix(remoteURL, "/") {
remoteURL += "/"
}
scheme := "http://"
// validate the remoteURL, should contains a scheme, if not then check if the client has given a scheme and if so
// use that for the server too
if !strings.Contains(remoteURL, "://") {
if strings.Contains(remoteURL, ":443") || strings.Contains(remoteURL, ":https") {
remoteURL = "https://" + remoteURL
} else {
remoteURL = scheme + "://" + remoteURL
}
}
var cacheDurationStr, statusCodeStr string
if r.cacheLifetime.Seconds() > 0 {
cacheDurationStr = strconv.Itoa(int(r.cacheLifetime.Seconds()))
}
if r.cacheStatuscode > 0 {
statusCodeStr = strconv.Itoa(r.cacheStatuscode)
}
s := remoteURL + "?" + cfg.QueryCacheKey + "=" + url.QueryEscape(r.clientMethod+scheme+r.clientURI)
if cacheDurationStr != "" {
s += "&" + cfg.QueryCacheDuration + "=" + url.QueryEscape(cacheDurationStr)
}
if statusCodeStr != "" {
s += "&" + cfg.QueryCacheStatusCode + "=" + url.QueryEscape(statusCodeStr)
}
if r.cacheContentType != "" {
s += "&" + cfg.QueryCacheContentType + "=" + url.QueryEscape(r.cacheContentType)
}
return s
}