This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
/
rest.go
180 lines (155 loc) · 5.49 KB
/
rest.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package rest
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strings"
"github.com/fabric8-services/fabric8-auth/errors"
"context"
"github.com/goadesign/goa"
"github.com/goadesign/goa/client"
)
// Doer is a wrapper interface for goa client Doer
type HttpDoer interface {
client.Doer
}
// HttpClient defines the Do method of the http client.
type HttpClient interface {
Do(req *http.Request) (*http.Response, error)
}
type configuration interface {
IsPostgresDeveloperModeEnabled() bool
}
// HttpClientDoer implements HttpDoer
type HttpClientDoer struct {
HttpClient HttpClient
}
// DefaultHttpDoer creates a new HttpDoer with default http client
func DefaultHttpDoer() HttpDoer {
return &HttpClientDoer{HttpClient: http.DefaultClient}
}
// Do overrides Do method of the default goa client Doer. It's needed for mocking http clients in tests.
func (d *HttpClientDoer) Do(ctx context.Context, req *http.Request) (*http.Response, error) {
return d.HttpClient.Do(req)
}
// Host returns the host from the given request if run in prod mode or if config is nil
// and "auth.openshift.io" if run in dev mode
func Host(req *goa.RequestData, config configuration) string {
if config != nil && config.IsPostgresDeveloperModeEnabled() {
return "auth.openshift.io"
}
return req.Host
}
// AbsoluteURL prefixes a relative URL with absolute address
// If config is not nil and run in dev mode then host is replaced by "auth.openshift.io"
func AbsoluteURL(req *goa.RequestData, relative string, config configuration) string {
host := Host(req, config)
return absoluteURLForHost(req, host, relative)
}
// ReplaceDomainPrefixInAbsoluteURL replaces the last name in the host of the URL by a new name.
// Example: https://api.service.domain.org -> https://sso.service.domain.org
// If replaceBy == "" then return trim the last name.
// Example: https://api.service.domain.org -> https://service.domain.org
// Also prefixes a relative URL with absolute address
// If config is not nil and run in dev mode then "auth.openshift.io" is used as a host
func ReplaceDomainPrefixInAbsoluteURL(req *goa.RequestData, replaceBy, relative string, config configuration) (string, error) {
host := Host(req, config)
if host == "" { // this happens for tests. See https://github.com/goadesign/goa/issues/1861
return "", nil
}
newHost, err := ReplaceDomainPrefix(host, replaceBy)
if err != nil {
return "", err
}
return absoluteURLForHost(req, newHost, relative), nil
}
// ReplaceDomainPrefixInAbsoluteURLStr check ReplaceDomainPrefixInAbsoluteURL. This works on url string.
func ReplaceDomainPrefixInAbsoluteURLStr(urlStr, replaceBy, relative string) (string, error) {
if urlStr == "" { // this happens for tests. See https://github.com/goadesign/goa/issues/1861
return "", nil
}
url, err := url.Parse(urlStr)
if err != nil {
return "", err
}
newHost, err := ReplaceDomainPrefix(url.Host, replaceBy)
if err != nil {
return "", err
}
return toAbsoluteURL(url.Scheme, newHost, relative), nil
}
func absoluteURLForHost(req *goa.RequestData, host, relative string) string {
scheme := "http"
if req.URL != nil && req.URL.Scheme == "https" { // isHTTPS
scheme = "https"
}
xForwardProto := req.Header.Get("X-Forwarded-Proto")
if xForwardProto != "" {
scheme = xForwardProto
}
return fmt.Sprintf("%s://%s%s", scheme, host, relative)
}
func toAbsoluteURL(scheme, host, relative string) string {
if scheme == "" {
scheme = "http"
}
return fmt.Sprintf("%s://%s%s", scheme, host, relative)
}
// ReplaceDomainPrefix replaces the last name in the host by a new name. Example: api.service.domain.org -> sso.service.domain.org
// If replaceBy == "" then return trim the last name. Example: api.service.domain.org -> service.domain.org
func ReplaceDomainPrefix(host string, replaceBy string) (string, error) {
split := strings.SplitN(host, ".", 2)
if len(split) < 2 {
return host, errors.NewBadParameterError("host", host).Expected("must contain more at least one subdomain")
}
if replaceBy == "" {
return split[1], nil
}
return replaceBy + "." + split[1], nil
}
// ReadBody reads body from a ReadCloser and returns it as a string
func ReadBody(body io.ReadCloser) string {
buf := new(bytes.Buffer)
buf.ReadFrom(body)
return buf.String()
}
// CloseResponse reads the body and close the response. To be used to prevent file descriptor leaks.
func CloseResponse(response *http.Response) {
ioutil.ReadAll(response.Body)
response.Body.Close()
}
// ValidateEmail return true if the string is a valid email address
// This is a very simple validation. It just checks if there is @ and dot in the address
func ValidateEmail(email string) (bool, error) {
// .+@.+\..+
return regexp.MatchString(".+@.+\\..+", email)
}
// AddParam adds a parameter to URL
func AddParam(urlString string, paramName string, paramValue string) (string, error) {
return AddParams(urlString, map[string]string{paramName: paramValue})
}
// AddParams adds parameters to URL
func AddParams(urlString string, params map[string]string) (string, error) {
parsedURL, err := url.Parse(urlString)
if err != nil {
return "", err
}
parameters := parsedURL.Query()
for k, v := range params {
parameters.Add(k, v)
}
parsedURL.RawQuery = parameters.Encode()
return parsedURL.String(), nil
}
// AddTrailingSlashToURL adds a trailing slash to the URL if it doesn't have it already
// If URL is an empty string the function returns an empty string too
func AddTrailingSlashToURL(url string) string {
if url != "" && !strings.HasSuffix(url, "/") {
return url + "/"
}
return url
}