Get content length from gzipped NSURLResponse - ios

I'm using NSURLSession's dataTaskWithRequest to perform an HTTP GET with Accept-Encoding: gzip. NSURLSession will automatically un-gzip the gzipped stream. How can I get the original response's content length (i.e. the length before un-gzipping)? Note that:
NSURLResponse's expectedContentLength is the length after un-gzipping it.
NSURLSessionTask's countOfBytesReceived property is the same value as expectedContentLength
While the header Content-Length can be obtained from NSHTTPURLResponse, it's not available with HTTP 1.1's chunked transfer encoding.

Related

NSUrlSession dataTask Completion handler parameters

What is the difference between the data and response parameters in the dataTask method's completion handler? Which one is used for what? Which one is the one where the json data will be returned by the web api?
JSON data is returned in the data parameter. The response parameter is a URLResponse object. You can use it to access additional information about the response (also known as metadata), such as its MIME type. If your request was an HTTP request response will be an HTTPURLResponse, which will include the HTTP status code and the HTTP header fields sent back by the server.

Do you need to send "Accept-Encoding: gzip" header for NSURLRequest(s)? [duplicate]

Does anyone knows if NSURLConnection/NSURLRequest have support for gzip requests.
If does, can you provide more information?
although it does not seem to be documented, there is evidence that NSURLConnection does have transparent gzip support. meaning that if the server supports gzip encoding, and your request has an Accept-Encoding header containing gzip*, the server will send a gzipped response, which NSURLConnection will automatically decode.
* NSURLRequest might add that header by default. if not, you have to add it manually like so:
[urlReq setValue:#"gzip" forHTTPHeaderField:#"Accept-Encoding"]
NSURLRequest decodes gzip to NSData; such as the Server response contain "Content-Encoding" = gzip; the NSData will decode.
If you don't want to automatically decode it, add the code below. This is a private API.
//import CFNetwork.framework
extern CFStringRef kCFURLRequestDoNotDecodeData;
typedef const struct _CFURLRequest* CFURLRequestRef;
extern void _CFURLRequestSetProtocolProperty(CFURLRequestRef,CFStringRef,CFTypeRef);
//NSURLRequest init ...
//...
CFURLRequestRef requestRef = (CFURLRequestRef)[request performSelector:#selector(_CFURLRequest)];
_CFURLRequestSetProtocolProperty(requestRef,kCFURLRequestDoNotDecodeData,kCFBooleanTrue);

Check if Ruby request response is gzip encoded

On Net::HTTP.Get request to a URL with accept-encoding header set as gzip, how do I check if the response is gzipped, before calling Zlib unzipping on it?
Found the answer, doing a response['Content-Encoding'] returns gzip if the response is Gzip encoded and nil otherwise. Discovered the param value by inspecting the browser HTTP response headers.

How to find size of object in iOS?

I am working on a app in which I have to find size of web service response and response type is id. Please suggest me a suitable answer to fetch size of id object.
Here in this code I have to find the size of value.
-(void)getOneTimeWebpayCardsHandler:(id) value
{
YPGetOneTimeWebpayCardsResponse* result = (YPGetOneTimeWebpayCardsResponse*)value;
}
If it is just a simple object without any extra internal allocations, this should work:
#import <malloc/malloc.h>
NSLog(#"response size: %zd", malloc_size(response));
If I understand your question correctly:
You might check the HTTP headers of the response:
Content-Type: The value of this header specifies the MIME type, e.g. application/json and possibly additional parameters, like "charset", e.g. application/json; charset=utf-8 of the response data.
Content-Length: The value of this header specifies the length in bytes of the response data.
See also NSHTTPURLResponse and NSURLResponse in the original documentation.

Why does my NSURLConnection report an incorrect expectedContentLength

I have an NSURLConnection and in didReceiveResponse I'm checking [response expectedContentLength] and getting really large values like 18446744073709551615. There's no way this is correct. The download is about 3k bytes, and when I expect the same request in fiddler, I see a (correct) content length header in the response of about 3k bytes.
To avoid this problem, set header field "Accept-Encoding" to #"gzip;q=0". that tells the server that you do not accept gzip, and will if possible send uncompressed.
The answer, related to the comments, is that it's because the result is gzip encoded. Oddly, the value for the expectedContentLength seems to be junk, and cannot be trusted. If the result is gzip encoded, then NSURLConnection cannot properly determine the size of the unencoded result.

Resources