I have a ServiceStack service that compresses the response using RequestContext.ToOptimizedResult() , for example:
[Route("/numbers/search")] public class FindNumbers { } public object Get(FindNumbers query) { var data = new List<string> { "One", "Two", "Three" }; return RequestContext.ToOptimizedResult(data); }
This works fine when issuing a request, for example:
GET http:
And it compresses, as expected, with the Accept-Encoding request header:
Accept-Encoding: gzip,deflate,sdch
I can also issue a JSONP request:
GET http:
which correctly returns the application/javascript callback (uncompressed).
PROBLEM
When I add the Accept-Encoding request header to the JSONP request, the response is compressed JSON data according to the original JSON request, not a compressed application/javascript callback.
Are there obvious reasons why I am missing this behavior, or is it just a bug in ServiceStack? My expectation would be to get a compressed JSONP response in the response, but I'm pretty green with JSONP, and there might be a good reason for backing up.
Notice that I work through the ServiceStack source, but I decided I got it there because more brains are better than one ...
Thanks in advance
EDIT
So, I traced the problem using the following source
https://github.com/ServiceStack/ServiceStack/blob/5d09d439cd1a13712411552e2b3ede5a71af2ee5/src/ServiceStack/Host/Handlers/GenericHandler.cs#L79
and
https://github.com/ServiceStack/ServiceStack/blob/5d09d439cd1a13712411552e2b3ede5a71af2ee5/src/ServiceStack/Host/RestHandler.cs#L107
if (doJsonp && !(response is CompressedResult)) return httpRes.WriteToResponse(httpReq, response, (callback + "(").ToUtf8Bytes(),")".ToUtf8Bytes()); return httpRes.WriteToResponse(httpReq, response);
So, if the answer is a compressed result, then regardless of the requirement for JSONP via ?callback=func answer will simply contain compressed json (in the case of the example above), which corresponds to true with my conclusions above. Thus, it seems that the jsonp callback wrapper should be applied earlier in the stop lot.