-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
59 lines (48 loc) · 1.25 KB
/
utils.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
package facio
import (
"fmt"
"strings"
)
const (
msgInvalidMethod = "The method '%s' is invalid"
msgInvalidURL = "The URL '%s' is invalid"
)
// Check if defined method exists
func checkMethod(method string) (string, error) {
methodUpper := strings.ToUpper(method)
// Not the prettiest solution
if methodUpper == "GET" ||
methodUpper == "HEAD" ||
methodUpper == "POST" ||
methodUpper == "PUT" ||
methodUpper == "PATCH" ||
methodUpper == "DELETE" ||
methodUpper == "CONNECT" ||
methodUpper == "OPTIONS" ||
methodUpper == "TRACE" {
return methodUpper, nil
}
return "", fmt.Errorf(msgInvalidMethod, method)
}
// parseURL removes the "/" from the end of the string
func parseURL(baseURL string) (string, error) {
baseBytes := []byte(baseURL)
lenBase := len(baseBytes)
if lenBase == 0 {
return "", fmt.Errorf(msgInvalidURL, baseURL)
}
// if base url is http://foo.bar/ it returns http://foo.bar
if baseBytes[lenBase-1] == byte('/') {
baseURL = string(baseBytes[:lenBase-1])
}
return baseURL, nil
}
// parseEndpoint add "/" at the beginning
func parseEndpoint(endpoint string) string {
epBytes := []byte(endpoint)
breaker := []byte("/")
if epBytes[0] == breaker[0] {
return endpoint
}
return string(append(breaker, epBytes...))
}