-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
70 lines (57 loc) · 1.39 KB
/
common.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
package restify
import (
"mime"
"net/http"
"github.com/phogolabs/log"
)
// ContentType is an enumeration of common HTTP content types.
type ContentType int
// ContentTypes handled by this package.
const (
ContentTypeUnknown ContentType = iota
ContentTypePlainText
ContentTypeHTML
ContentTypeJSON
ContentTypeXML
ContentTypeForm
ContentTypeEventStream
)
// Content represents the content type
type Content struct {
Type ContentType
Params map[string]string
}
// ParseContent parse the content type
func ParseContent(header string) (*Content, error) {
content := &Content{
Type: ContentTypeUnknown,
Params: make(map[string]string),
}
if header == "" {
return content, nil
}
media, params, err := mime.ParseMediaType(header)
if err != nil {
return nil, err
}
content.Params = params
switch media {
case "text/plain":
content.Type = ContentTypePlainText
case "text/html", "application/xhtml+xml":
content.Type = ContentTypeHTML
case "application/json", "text/javascript":
content.Type = ContentTypeJSON
case "text/xml", "application/xml":
content.Type = ContentTypeXML
case "application/x-www-form-urlencoded":
content.Type = ContentTypeForm
case "text/event-stream":
content.Type = ContentTypeEventStream
}
return content, nil
}
// GetLogger returns the associated request logger
func GetLogger(r *http.Request) log.Logger {
return log.GetContext(r.Context())
}