Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,21 +573,25 @@ func (c *Context) writeJSONPHeader(code int, callback string) error {
}

func (c *Context) jsonPBlob(code int, callback string, i any) (err error) {
if err = c.writeJSONPHeader(code, callback); err != nil {
return
}
if _, err = c.response.Write(stringToBytes(callback)); err != nil {
return
}
if _, err = c.response.Write(jsonpOpen); err != nil {
return
if !isValidJSONPCallback(callback) {
return ErrBadRequest.Wrap(ErrInvalidJSONPCallback)
}

// As with json(), hold the status code and headers back until the serializer writes its first
// byte, so a serializer that fails before writing anything sends no response and the error
// handler can send its own.
resp := c.Response()
c.SetResponse(&jsonpWriter{
ResponseWriter: &delayedStatusWriter{ResponseWriter: resp, status: code},
contentType: MIMEApplicationJavaScriptCharsetUTF8,
prefix: []byte(callback + string(jsonpOpen)),
})
defer c.SetResponse(resp)

if err = c.echo.JSONSerializer.Serialize(c, i, ""); err != nil {
return
}
if _, err = c.response.Write(jsonpClose); err != nil {
return
}
_, err = c.response.Write(jsonpClose)
return
}

Expand Down
13 changes: 13 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,19 @@ func TestContextJSONErrorsOut(t *testing.T) {
assert.Empty(t, rec.Body.String()) // body must not be sent to the client
}

func TestContextJSONPErrorsOut(t *testing.T) {
e := New()
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/", nil)
c := e.NewContext(req, rec)

err := c.JSONP(http.StatusAccepted, "callback", make(chan bool))
assert.EqualError(t, err, "json: unsupported type: chan bool")

assert.Equal(t, http.StatusOK, rec.Code) // status code must not be sent to the client
assert.Empty(t, rec.Body.String()) // body must not be sent to the client
}

func TestContextJSONWithNotEchoResponse(t *testing.T) {
e := New()
rec := httptest.NewRecorder()
Expand Down
28 changes: 28 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,34 @@ func (w *delayedStatusWriter) Unwrap() http.ResponseWriter {
return w.ResponseWriter
}

// jsonpWriter writes the JSONP callback prefix ahead of the first serialized byte, so a serializer
// that fails before writing leaves the status code uncommitted for the error handler.
type jsonpWriter struct {
http.ResponseWriter
contentType string
prefix []byte
wrote bool
}

func (w *jsonpWriter) Write(data []byte) (int, error) {
if !w.wrote {
w.wrote = true
header := w.ResponseWriter.Header()
if header.Get(HeaderContentType) == "" {
header.Set(HeaderContentType, w.contentType)
}
header.Set(HeaderXContentTypeOptions, "nosniff")
if _, err := w.ResponseWriter.Write(w.prefix); err != nil {
return 0, err
}
}
return w.ResponseWriter.Write(data)
}

func (w *jsonpWriter) Unwrap() http.ResponseWriter {
return w.ResponseWriter
}

// headResponseWriter captures the response that a GET handler would produce for a
// rewritten HEAD request, suppresses the body, and preserves response metadata.
//
Expand Down