Skip to content

Commit d9e923c

Browse files
committed
Ripped out support for JSONP.
1 parent c894974 commit d9e923c

File tree

3 files changed

+0
-98
lines changed

3 files changed

+0
-98
lines changed

contracts_structs.go

-15
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,6 @@ type SerializeResult struct {
7979
func (this *SerializeResult) SetContent(value interface{}) { this.Content = value }
8080
func (this *SerializeResult) Result() interface{} { return this }
8181

82-
// JSONPResult provides the ability render a JSON-P result to the response.
83-
type JSONPResult struct {
84-
// StatusCode, if provided, use this value, otherwise HTTP 200.
85-
StatusCode int
86-
87-
// ContentType, if provided, use this value.
88-
ContentType string
89-
90-
// Headers, if provided, are added to the response
91-
Headers map[string][]string
92-
93-
// Content, if provided, use this value, otherwise no content will be written to the response stream.
94-
Content interface{}
95-
}
96-
9782
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
9883

9984
// InputError represents some kind of problem with the calling HTTP request.

writers.go

-59
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,6 @@ func (this *defaultWriter) write(response http.ResponseWriter, request *http.Req
6464
case SerializeResult:
6565
this.responseStatus(this.writeSerializeResult(response, request, &typed))
6666

67-
case *JSONPResult:
68-
this.responseStatus(this.writeJSONPResult(response, request, typed))
69-
case JSONPResult:
70-
this.responseStatus(this.writeJSONPResult(response, request, &typed))
71-
7267
case string:
7368
this.responseStatus(this.writeStringResult(response, typed))
7469
case []byte:
@@ -164,60 +159,6 @@ func (this *defaultWriter) loadSerializer(acceptTypes []string) Serializer {
164159

165160
return this.defaultSerializer
166161
}
167-
func (this *defaultWriter) writeJSONPResult(response http.ResponseWriter, request *http.Request, typed *JSONPResult) (err error) {
168-
callbackFunction := parseJSONPCallbackQueryStringParameter(request.URL.RawQuery)
169-
170-
contentType := typed.ContentType
171-
if len(contentType) == 0 {
172-
contentType = mimeTypeApplicationJavascriptUTF8
173-
}
174-
175-
headers := response.Header()
176-
for key, values := range typed.Headers {
177-
headers[key] = values
178-
}
179-
180-
this.writeHeader(response, typed.StatusCode, contentType, "", true)
181-
182-
_, _ = io.WriteString(response, callbackFunction)
183-
_, _ = io.WriteString(response, "(")
184-
serializer := this.loadSerializer(headerAcceptTypeJavascript)
185-
err = serializer.Serialize(response, typed.Content) // serializes an extra line break
186-
_, _ = io.WriteString(response, ")")
187-
188-
return err
189-
}
190-
func parseJSONPCallbackQueryStringParameter(query string) string {
191-
var key, value string
192-
for len(query) > 0 {
193-
key = query
194-
195-
if i := strings.Index(key, "&"); i >= 0 {
196-
key, query = key[:i], key[i+1:]
197-
} else {
198-
query = ""
199-
}
200-
201-
if i := strings.Index(key, "="); i >= 0 {
202-
key, value = key[:i], key[i+1:]
203-
}
204-
205-
if key == defaultJSONPCallbackParameter {
206-
return sanitizeJSONPCallbackValue(value)
207-
}
208-
}
209-
210-
return defaultJSONPCallbackName
211-
}
212-
func sanitizeJSONPCallbackValue(raw string) string {
213-
for _, item := range raw {
214-
if (item < 'a' || item > 'z') && (item < 'A' || item > 'Z') && (item < '0' || item > '9') && (item != '_') {
215-
return defaultJSONPCallbackName
216-
}
217-
}
218-
219-
return raw
220-
}
221162

222163
func (this *defaultWriter) writeStringResult(response http.ResponseWriter, typed string) (err error) {
223164
this.monitor.NativeResult()

writers_test.go

-24
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,6 @@ func TestWrite(t *testing.T) {
9090
Accept: "application/xml;q=0.8", // simplify and use correct serializer
9191
HTTPResponse: HTTPResponse{StatusCode: 200, ContentType: []string{"application/xml; charset=utf-8"}, Body: string(xmlPrefix) + "{body}"}},
9292

93-
{Input: &JSONPResult{StatusCode: 201, ContentType: "", Content: "body"},
94-
HTTPResponse: HTTPResponse{StatusCode: 201, ContentType: []string{"application/javascript; charset=utf-8"}, Body: "callback({body})"}},
95-
{Input: JSONPResult{StatusCode: 201, ContentType: "", Content: "body"},
96-
HTTPResponse: HTTPResponse{StatusCode: 201, ContentType: []string{"application/javascript; charset=utf-8"}, Body: "callback({body})"}},
97-
9893
{Input: 42, // use serializer for unknown type
9994
HTTPResponse: HTTPResponse{StatusCode: 200, ContentType: []string{"application/json; charset=utf-8"}, Body: "{42}"}},
10095
}
@@ -150,7 +145,6 @@ func TestSerializeResultWriteHTTPHeaders(t *testing.T) {
150145
assertHTTPHeaders(t, TextResult{Headers: headers})
151146
assertHTTPHeaders(t, BinaryResult{Headers: headers})
152147
assertHTTPHeaders(t, StreamResult{Headers: headers})
153-
assertHTTPHeaders(t, JSONPResult{Headers: headers})
154148
}
155149
func assertHTTPHeaders(t *testing.T, result interface{}) {
156150
response := recordResponse(result, "application/json")
@@ -168,24 +162,6 @@ type HTTPResponse struct {
168162

169163
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
170164

171-
func TestJSONPCallbackQueryStringParsing(t *testing.T) {
172-
assertJSONPQueryStringCallback(t, "callback=jsonCallback", "jsonCallback") // simple
173-
assertJSONPQueryStringCallback(t, "key=&callback=jsonCallback", "jsonCallback") // multiple keys
174-
assertJSONPQueryStringCallback(t, "key=value&callback=jsonCallback", "jsonCallback") // multiple keys and values
175-
assertJSONPQueryStringCallback(t, "key=&=value&callback=jsonCallback&other=stuff", "jsonCallback") // blank keys and values
176-
assertJSONPQueryStringCallback(t, "callback=_json_Callback_0123456789", "_json_Callback_0123456789") // complex callback name
177-
assertJSONPQueryStringCallback(t, "key=&=value&other=stuff", "callback") // doesn't exist, use default
178-
assertJSONPQueryStringCallback(t, "callback=malicious!", "callback") // malicious
179-
assertJSONPQueryStringCallback(t, "callback=<malicious>", "callback") // malicious
180-
assertJSONPQueryStringCallback(t, "callback=alert('malicious');", "callback") // malicious
181-
182-
}
183-
func assertJSONPQueryStringCallback(t *testing.T, raw, callback string) {
184-
Assert(t).That(parseJSONPCallbackQueryStringParameter(raw)).Equals(callback)
185-
}
186-
187-
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
188-
189165
type FakeHTTPHandlerResult struct {
190166
response http.ResponseWriter
191167
request *http.Request

0 commit comments

Comments
 (0)