Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending chunked HTTP 1.1 requests in Objective-C

I have the following issue: I am creating a very big SOAP request (the data is a video encoded as Base64 string) and because of that I cannot send it as a raw SOAP request but rather need to send it in HTTP 1.1 chunks. I cannot seem to figure out how to do it. I used the code in here: What are alternatives to NSURLConnection for chunked transfer encoding but it doesn't seem to be doing what I think it should - I can see that the request arrives on the server as a single request instead of many chunks (I am using WireShark on the server to see the incoming traffic.)

I know that a similar functionality on an Android works using Apache Foundations HTTP libraries for Java - with these, any HTTP request whose length is not specified in advance is transmitted as an HTTP 1.1 Chunked Request - and I can see indeed those requests arriving on the server as individual chunks... I want to emulate that.

(UPDATE: Seems to me AFNetworking might have the functionality, but I fail to find any example as to how to use it.)

Here is my code, more or less:

NSString *soapBody = ....; //some correctly formed SOAP request XML here 


NSURL *url = [NSURL URLWithString:...];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
[request addValue: ... forHTTPHeaderField:@"SOAPAction"];
[request setHTTPMethod:@"POST"];
[request addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[soapBody dataUsingEncoding:NSUTF8StringEncoding]];
ChunkedTransferConnection* connection = [ChunkedTransferConnection alloc];
[connection establishConnectionWithRequest:request];

where ChunkedTransferConnection implementation is the following

@implementation ChunkedTransferConnection

    @synthesize p_connection;
    @synthesize p_responseData;

    - (void)establishConnectionWithRequest:(NSMutableURLRequest *)request
    {
        self.p_responseData = [[NSMutableData alloc] initWithLength:0] ;
        self.p_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    }
...
@end
like image 376
PeterD Avatar asked Feb 28 '13 20:02

PeterD


People also ask

What encoding is the HTTP 1.1 protocol based on?

HTTP/1.1 uses content-coding values in the Accept-Encoding (section 14.3) and Content-Encoding (section 14.11) header fields. Although the value describes the content-coding, what is more important is that it indicates what decoding mechanism will be required to remove the encoding.

How do you send chunked data?

Chunked transfer encoding is a streaming data transfer mechanism available in version 1.1 of the Hypertext Transfer Protocol (HTTP). In chunked transfer encoding, the data stream is divided into a series of non-overlapping "chunks". The chunks are sent out and received independently of one another.


1 Answers

Figured it out:

    NSInputStream *dataStream = [NSInputStream inputStreamWithData:[soapBody dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBodyStream:dataStream];

This causes the request automatically be in HTP 1.1 chunks!

like image 86
PeterD Avatar answered Sep 30 '22 06:09

PeterD