/
jsonp.go
143 lines (124 loc) · 2.91 KB
/
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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Package jsonp impements JSONp
//
// Copyright (C) Philip Schlump, 2015
// License LICENSE.apache.txt or LICENSE.mit.txt
//
//
// Directive for Caddy
//
// jsonp /some/url
// jsonp /some/other-url
//
package jsonp
import (
"fmt"
"log"
"net/http"
"net/url"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
"github.com/pschlump/caddy-jsonp/bufferhtml"
)
const db1 = false // back to debuging this
func init() {
if db1 {
fmt.Printf("jsonp - Calling caddy.RegisterPlugin\n")
}
caddy.RegisterPlugin("jsonp", caddy.Plugin{
ServerType: "http",
Action: Setup,
})
}
// Setup configures a new Jsonp middleware instance.
func Setup(c *caddy.Controller) error {
paths, err := jsonpParse(c)
if err != nil {
return err
}
if db1 {
fmt.Printf("Setup called, paths=%s\n", paths)
}
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
return JsonPHandlerType{
Next: next,
Paths: paths,
}
})
return nil
}
func jsonpParse(c *caddy.Controller) ([]string, error) {
var paths []string
if db1 {
fmt.Printf("jsonpParse called\n")
}
for c.Next() {
if !c.NextArg() {
return paths, c.ArgErr()
}
paths = append(paths, c.Val())
}
return paths, nil
}
type JsonPHandlerType struct {
Paths []string
Next httpserver.Handler
}
func (jph JsonPHandlerType) ServeHTTP(www http.ResponseWriter, req *http.Request) (int, error) {
if db1 {
fmt.Printf("JsonPHandlerType.ServeHTTP called\n")
}
for _, prefix := range jph.Paths {
if db1 {
fmt.Printf("Path Matches\n")
}
if httpserver.Path(req.URL.Path).Matches(prefix) {
if db1 {
fmt.Printf("A\n")
}
ResponseBodyRecorder := bufferhtml.NewBufferHTML()
if db1 {
fmt.Printf("B\n")
}
status, err := jph.Next.ServeHTTP(ResponseBodyRecorder, req)
if db1 {
fmt.Printf("C status=%d err=%s\n", status, err)
}
if status == 200 || status == 0 {
// if there is a "callback" argument then use that to format the JSONp response.
if db1 {
fmt.Printf("D Inside, status=%d\n", status)
}
Prefix := ""
Postfix := ""
u, err := url.ParseRequestURI(req.RequestURI)
if err != nil {
ResponseBodyRecorder.FlushAtEnd(www, "", "")
return status, nil
}
m, err := url.ParseQuery(u.RawQuery)
if err != nil {
ResponseBodyRecorder.FlushAtEnd(www, "", "")
return status, nil
}
callback := m.Get("callback")
if callback != "" {
if db1 {
fmt.Printf("Callback = -[%s]-\n", callback)
}
www.Header().Set("Content-Type", "application/javascript")
Prefix = callback + "("
Postfix = ");"
}
ResponseBodyRecorder.FlushAtEnd(www, Prefix, Postfix)
} else {
if db1 {
fmt.Printf("E - error occured, %s\n", err)
}
log.Printf("Error (%s) status %d - from JSONP directive\n", err, status)
}
return status, err
}
}
return jph.Next.ServeHTTP(www, req)
}
/* vim: set noai ts=4 sw=4: */