-
Notifications
You must be signed in to change notification settings - Fork 1
/
httputils.go
69 lines (59 loc) · 1.64 KB
/
httputils.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
package main
import (
"crypto/subtle"
"fmt"
"log"
"mime"
"net/http"
)
type httpError struct {
code int
message string
}
func (e httpError) Error() string {
return fmt.Sprintf("%s (HTTP %d)", e.message, e.code)
}
func newHTTPError(code int, format string, a ...interface{}) error {
message := fmt.Sprintf(format, a...)
return &httpError{code, message}
}
func sendErrorMessage(w http.ResponseWriter, code int, message string) {
if message == "" {
message = http.StatusText(code)
}
log.Printf("ERROR: HTTP %v - %v", code, message)
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(code)
w.Write([]byte(message))
}
func sendErrorFmt(w http.ResponseWriter, code int, format string, a ...interface{}) {
sendErrorMessage(w, code, fmt.Sprintf(format, a...))
}
func sendError(w http.ResponseWriter, err error) {
if httperr, ok := err.(*httpError); ok {
sendErrorMessage(w, httperr.code, httperr.message)
} else {
log.Printf("** Unexpected error: %v", err)
sendErrorMessage(w, http.StatusInternalServerError, "")
}
}
func parseRequestContentType(r *http.Request) (string, error) {
ctype := r.Header.Get("Content-Type")
if ctype == "" {
return "", nil
}
mediatype, _, err := mime.ParseMediaType(ctype)
if err != nil {
return "", newHTTPError(http.StatusBadRequest, "Failed to parse Content-Type: %v", err)
}
return mediatype, nil
}
func verifyToken(token string, correctToken string) error {
if token == "" {
return newHTTPError(http.StatusUnauthorized, "Missing token")
}
if subtle.ConstantTimeCompare([]byte(token), []byte(correctToken)) != 1 {
return newHTTPError(http.StatusUnauthorized, "Invalid token")
}
return nil
}