-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
56 lines (48 loc) · 1.38 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
package oidcauth
import (
"fmt"
"log"
"net/http"
"net/url"
"strings"
)
// Appends a query value to a given URL.
func appendQueryValue(uri, name, value string) (string, error) {
u, err := url.Parse(uri)
if err != nil {
return "", err
}
q := u.Query()
q.Add(name, value)
u.RawQuery = q.Encode()
return u.String(), nil
}
// getURLHost returns the host:port part of an absolute URL or an empty string
// if the host:port part of the URL cannot be determined or if the URL is a relative.
func getURLHost(uri string) string {
// Make sure this is an absolute URL
if !strings.HasPrefix(uri, "http://") && !strings.HasPrefix(uri, "https://") {
log.Printf("URL '%s' is not absolute", uri)
return ""
}
// Get protocol and host part only
parts, err := url.Parse(uri)
if err != nil {
log.Printf("Could not parse URL '%s'", uri)
return ""
}
if parts.Scheme == "" || parts.Host == "" {
return ""
}
return fmt.Sprintf("%s://%s", parts.Scheme, parts.Host)
}
// isGET makes a case insensitive check on the request method,
// and returns true if the method is 'GET', 'get' or "".
func isGET(r *http.Request) bool {
return r.Method == "" || strings.EqualFold(r.Method, "GET")
}
// isGET makes a case insensitive check on the request method,
// and returns true if the method is 'OPTIONS' or 'options'.
func isOPTIONS(r *http.Request) bool {
return strings.EqualFold(r.Method, "OPTIONS")
}