-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.go
67 lines (59 loc) · 1.48 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
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"net/http"
"strings"
"github.com/gotify/plugin-api"
)
const (
messageTemplate = "Sender: %s\n\n%s"
messageTemplateCodeBlock = "Sender: %s\n\n```\n%s\n```"
)
func makeMarkdownMessage(title, message, remoteIP string, withinCodeBlock bool) plugin.Message {
tmpl := messageTemplate
if withinCodeBlock {
tmpl = messageTemplateCodeBlock
}
return plugin.Message{
Title: title,
Message: fmt.Sprintf(tmpl,
remoteIP,
message,
),
Extras: map[string]interface{}{
"client::display": map[string]interface{}{
"contentType": "text/markdown",
},
},
}
}
// getContentTypeFromRequest tries to read the configured
// content-type from the request in the following order:
// "?content-type=" URL query parameter, "Content-Type" request header,
// "X-Content-Type" request header (for clients that can send custom
// headers, but cannot set the content type)
func getContentTypeFromRequest(req *http.Request) int {
var (
fromQuery = req.URL.Query().Get("content-type")
fromHeader = req.Header.Get("content-type")
fromNonStdHeader = req.Header.Get("x-content-type")
)
var foundType string
if fromNonStdHeader != "" {
foundType = fromNonStdHeader
}
if fromHeader != "" {
foundType = fromHeader
}
if fromQuery != "" {
foundType = fromQuery
}
switch strings.ToLower(foundType) {
case "application/json":
return ContentTypeJSON
case "text/markdown":
return ContentTypeMarkdown
default:
return ContentTypeUnknown
}
}