forked from goproxy/goproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
73 lines (62 loc) · 2.05 KB
/
response.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
package goproxy
import (
"fmt"
"net/http"
"strings"
)
// setResponseCacheControlHeader sets the Cache-Control header based on the
// maxAge.
func setResponseCacheControlHeader(rw http.ResponseWriter, maxAge int) {
cacheControl := ""
if maxAge >= 0 {
cacheControl = fmt.Sprintf("public, max-age=%d", maxAge)
} else {
cacheControl = "must-revalidate, no-cache, no-store"
}
rw.Header().Set("Cache-Control", cacheControl)
}
// responseString responses the s as a "text/plain" content to the client with
// the statusCode.
func responseString(rw http.ResponseWriter, statusCode int, s string) {
rw.Header().Set("Content-Type", "text/plain; charset=utf-8")
rw.WriteHeader(statusCode)
rw.Write([]byte(s))
}
// responseJSON responses the b as a "application/json" content to the client
// with the statusCode.
func responseJSON(rw http.ResponseWriter, statusCode int, b []byte) {
rw.Header().Set("Content-Type", "application/json; charset=utf-8")
rw.WriteHeader(statusCode)
rw.Write(b)
}
// responseNotFound responses "not found" to the client with the optional msgs.
func responseNotFound(rw http.ResponseWriter, msgs ...interface{}) {
var msg string
if len(msgs) > 0 {
msg = strings.TrimPrefix(fmt.Sprint(msgs...), "bad request: ")
msg = strings.TrimPrefix(msg, "gone: ")
if msg != "" && !strings.HasPrefix(msg, "not found: ") {
msg = fmt.Sprint("not found: ", msg)
}
}
if msg == "" {
msg = "not found"
}
responseString(rw, http.StatusNotFound, msg)
}
// responseMethodNotAllowed responses "method not allowed" to the client.
func responseMethodNotAllowed(rw http.ResponseWriter) {
responseString(rw, http.StatusMethodNotAllowed, "method not allowed")
}
// responseInternalServerError responses "internal server error" to the client.
func responseInternalServerError(rw http.ResponseWriter) {
responseString(
rw,
http.StatusInternalServerError,
"internal server error",
)
}
// responseBadGateway responses "bad gateway" to the client.
func responseBadGateway(rw http.ResponseWriter) {
responseString(rw, http.StatusBadGateway, "bad gateway")
}