Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa HTTP PUT with content-range

Is it possible to use an NSURLConnection/NSURLRequest combination to send a PUT request to a server with a Content-Range header? By that I mean I want to resume an upload to the server which can accept a byte range in the request to resume the upload.

I see you can set an NSInputStream as the request body so I figured that I could subclass that and override the the open/seek functions and set the request header but it seems to call on undocumented selectors and break the implementation.

I'm sure I could do this with CFNetwork but it just seems like there must be a way to do it with the higher level APIs.

Any ideas on where to start?

EDIT:

To answer my own question, this is indeed possible after reading a blog [http://bjhomer.blogspot.com/2011/04/subclassing-nsinputstream.html] which details the undocumented callbacks which relate to CFStream. Once those are implemented I can call the following in the open callback to skip ahead:

CFReadStreamSetProperty((CFReadStreamRef)parentStream, kCFStreamPropertyFileCurrentOffset, (CFNumberRef)[NSNumber numberWithUnsignedLongLong:streamOffset]);

Thanks, J

like image 562
JWood Avatar asked Aug 16 '26 12:08

JWood


2 Answers

I think the server needs to supports put method combines with range but this will be the way to do it with high level Objective-C API

        NSURL *URL = [NSURL URLWithString:strURL];
        NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:URL];
        NSString *range = [NSString stringWithFormat:@"bytes=%lld-%lld",bytesUploaded,uploadSize];
        [urlRequest addValue:range forHTTPHeaderField:@"Range"];
        [urlRequest setHTTPMethod:@"PUT"];
        self.connection = [NSURLConnection connectionWithRequest:urlRequest delegate:self];

Cheers

like image 116
Or Ron Avatar answered Aug 19 '26 04:08

Or Ron


First, if you want to do fancy work with HTTP, I typically recommend ASIHTTPRequest. It's solid stuff that simplifies a lot of more complicated HTTP problems. It's not really needed for setting a simple header, but if you're starting to build something more complex, it can be nice to move over to ASI sooner rather than later.

With an NSMutableURLRequest, you can set any header you want using addValue:forHTTPHeaderField:. You can use that to set your Content-Range.

like image 26
Rob Napier Avatar answered Aug 19 '26 02:08

Rob Napier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!