-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathjsonp.go
31 lines (24 loc) · 918 Bytes
/
jsonp.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
package mango
import (
"fmt"
"regexp"
"strings"
)
var jsonp_valid_callback_matcher *regexp.Regexp = regexp.MustCompile("^[a-zA-Z_$][a-zA-Z_0-9$]*([.]?[a-zA-Z_$][a-zA-Z_0-9$]*)*$")
func JSONP(env Env, app App) (Status, Headers, Body) {
callback := env.Request().FormValue("callback")
if callback != "" {
if !jsonp_valid_callback_matcher.MatchString(callback) {
return 400, Headers{"Content-Type": []string{"text/plain"}, "Content-Length": []string{"11"}}, "Bad Request"
}
}
status, headers, body := app(env)
if callback != "" && strings.Contains(headers.Get("Content-Type"), "application/json") {
headers.Set("Content-Type", strings.Replace(headers.Get("Content-Type"), "json", "javascript", -1))
body = Body(fmt.Sprintf("%s(%s)", callback, body))
if headers.Get("Content-Length") != "" {
headers.Set("Content-Length", fmt.Sprintf("%d", len(body)))
}
}
return status, headers, body
}