Skip to content

Commit

Permalink
Merge branch 'gzip-compression-experimental' into experimental-gcd-pr…
Browse files Browse the repository at this point in the history
…ocessing

Conflicts:
	AFNetworking/AFHTTPRequestOperation.m
	AFNetworking/AFImageRequestOperation.m
	AFNetworking/AFJSONRequestOperation.m
  • Loading branch information
mattt committed Aug 15, 2011
2 parents a2138c6 + 48be808 commit 390c51a
Show file tree
Hide file tree
Showing 17 changed files with 466 additions and 17,734 deletions.
6 changes: 5 additions & 1 deletion AFNetworking/AFHTTPRequestOperation.h
Expand Up @@ -35,6 +35,7 @@ extern NSString * const AFHTTPOperationDidFinishNotification;

NSData *_responseBody;
NSMutableData *_dataAccumulator;
NSOutputStream *_outputStream;
}

@property (nonatomic, retain) NSURLConnection *connection;
Expand All @@ -50,7 +51,10 @@ extern NSString * const AFHTTPOperationDidFinishNotification;
+ (id)operationWithRequest:(NSURLRequest *)urlRequest
completion:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error))completion;

- (id)initWithRequest:(NSURLRequest *)urlRequest;
+ (id)operationWithRequest:(NSURLRequest *)urlRequest
inputStream:(NSInputStream *)inputStream
outputStream:(NSOutputStream *)outputStream
completion:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))completion;

- (void)setProgressBlock:(void (^)(NSUInteger totalBytesWritten, NSUInteger totalBytesExpectedToWrite))block;

Expand Down
69 changes: 59 additions & 10 deletions AFNetworking/AFHTTPRequestOperation.m
Expand Up @@ -80,9 +80,11 @@ @interface AFHTTPRequestOperation ()
@property (nonatomic, assign) AFHTTPOperationState state;
@property (nonatomic, assign) BOOL isCancelled;
@property (readwrite, nonatomic, retain) NSMutableData *dataAccumulator;
@property (readwrite, nonatomic, retain) NSOutputStream *outputStream;
@property (readwrite, nonatomic, copy) AFHTTPRequestOperationProgressBlock progress;
@property (readwrite, nonatomic, copy) AFHTTPRequestOperationCompletionBlock completion;

- (id)initWithRequest:(NSURLRequest *)urlRequest;
- (void)cleanup;
@end

Expand All @@ -96,6 +98,7 @@ @implementation AFHTTPRequestOperation
@synthesize error = _error;
@synthesize responseBody = _responseBody;
@synthesize dataAccumulator = _dataAccumulator;
@synthesize outputStream = _outputStream;
@synthesize progress = _progress;
@synthesize completion = _completion;

Expand All @@ -108,6 +111,26 @@ + (id)operationWithRequest:(NSURLRequest *)urlRequest
return operation;
}

+ (id)operationWithRequest:(NSURLRequest *)urlRequest
inputStream:(NSInputStream *)inputStream
outputStream:(NSOutputStream *)outputStream
completion:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))completion
{
NSMutableURLRequest *mutableURLRequest = [[urlRequest mutableCopy] autorelease];
[mutableURLRequest setHTTPBodyStream:inputStream];
if ([[mutableURLRequest HTTPMethod] isEqualToString:@"GET"]) {
[mutableURLRequest setHTTPMethod:@"POST"];
}

AFHTTPRequestOperation *operation = [self operationWithRequest:mutableURLRequest completion:^(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error) {
if (completion) {
completion(request, response, error);
}
}];

return operation;
}

- (id)initWithRequest:(NSURLRequest *)urlRequest {
self = [super init];
if (!self) {
Expand All @@ -130,17 +153,21 @@ - (void)dealloc {
[_response release];
[_responseBody release];
[_dataAccumulator release];
[_outputStream release]; _outputStream = nil;

[_connection release];
[_connection release]; _connection = nil;

[_progress release];
[_completion release];
[_progress release];
[super dealloc];
}

- (void)cleanup {
[self.outputStream close];
for (NSString *runLoopMode in self.runLoopModes) {
[self.connection unscheduleFromRunLoop:[NSRunLoop currentRunLoop] forMode:runLoopMode];
[self.outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:runLoopMode];
}
CFRunLoopStop([[NSRunLoop currentRunLoop] getCFRunLoop]);
}
Expand Down Expand Up @@ -212,6 +239,7 @@ - (void)start {
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
for (NSString *runLoopMode in self.runLoopModes) {
[self.connection scheduleInRunLoop:runLoop forMode:runLoopMode];
[self.outputStream scheduleInRunLoop:runLoop forMode:runLoopMode];
}

[self.connection start];
Expand Down Expand Up @@ -241,27 +269,48 @@ - (void)finish {

#pragma mark - NSURLConnection

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response
{
self.response = (NSHTTPURLResponse *)response;
NSUInteger contentLength = MIN(MAX(abs(response.expectedContentLength), 1024), 1024 * 1024 * 8);

self.dataAccumulator = [NSMutableData dataWithCapacity:contentLength];

if (self.outputStream) {
[self.outputStream open];
} else {
NSUInteger contentLength = MIN(MAX(abs(response.expectedContentLength), 1024), 1024 * 1024 * 8);
self.dataAccumulator = [NSMutableData dataWithCapacity:contentLength];
}
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.dataAccumulator appendData:data];
- (void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data
{
if (self.outputStream) {
if ([self.outputStream hasSpaceAvailable]) {
const uint8_t *dataBuffer = [data bytes];
[self.outputStream write:&dataBuffer[0] maxLength:[data length]];
}
} else {
[self.dataAccumulator appendData:data];
}
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
self.state = AFHTTPOperationFinishedState;

self.responseBody = [NSData dataWithData:self.dataAccumulator];
self.dataAccumulator = nil;
if (self.outputStream) {
[self.outputStream close];
} else {
self.responseBody = [NSData dataWithData:self.dataAccumulator];
[_dataAccumulator release]; _dataAccumulator = nil;
}

[self performSelectorOnMainThread:@selector(finish) withObject:nil waitUntilDone:NO];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
self.state = AFHTTPOperationFinishedState;

self.error = error;
Expand Down
6 changes: 5 additions & 1 deletion AFNetworking/AFImageRequestOperation.m
Expand Up @@ -55,7 +55,7 @@ + (id)operationWithRequest:(NSURLRequest *)urlRequest
options:(AFImageRequestOptions)options
success:(void (^)(UIImage *image))success
{
return [self operationWithRequest:urlRequest completion:^(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error) {
AFImageRequestOperation *operation = [self operationWithRequest:urlRequest completion:^(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error) {
dispatch_async(image_request_operation_processing_queue(), ^(void) {
UIImage *image = nil;
if ([[UIScreen mainScreen] scale] == 2.0) {
Expand All @@ -81,6 +81,10 @@ + (id)operationWithRequest:(NSURLRequest *)urlRequest
[[AFImageCache sharedImageCache] cacheImage:image forRequest:request imageSize:imageSize options:options];
});
}];

operation.runLoopModes = [NSSet setWithObject:NSRunLoopCommonModes];

return operation;
}

@end
4 changes: 2 additions & 2 deletions AFNetworking/AFJSONRequestOperation.h
Expand Up @@ -34,8 +34,8 @@
+ (id)operationWithRequest:(NSURLRequest *)urlRequest
acceptableStatusCodes:(NSIndexSet *)acceptableStatusCodes
acceptableContentTypes:(NSSet *)acceptableContentTypes
success:(void (^)(id JSON))success
failure:(void (^)(NSError *error))failure;
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON))success
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure;

+ (NSIndexSet *)defaultAcceptableStatusCodes;
+ (NSSet *)defaultAcceptableContentTypes;
Expand Down
20 changes: 14 additions & 6 deletions AFNetworking/AFJSONRequestOperation.m
Expand Up @@ -46,14 +46,22 @@ + (id)operationWithRequest:(NSURLRequest *)urlRequest
success:(void (^)(id JSON))success
failure:(void (^)(NSError *error))failure
{
return [self operationWithRequest:urlRequest acceptableStatusCodes:[self defaultAcceptableStatusCodes] acceptableContentTypes:[self defaultAcceptableContentTypes] success:success failure:failure];
return [self operationWithRequest:urlRequest acceptableStatusCodes:[self defaultAcceptableStatusCodes] acceptableContentTypes:[self defaultAcceptableContentTypes] success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
if (success) {
success(JSON);
}
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
if (failure) {
failure(error);
}
}];
}

+ (id)operationWithRequest:(NSURLRequest *)urlRequest
acceptableStatusCodes:(NSIndexSet *)acceptableStatusCodes
acceptableContentTypes:(NSSet *)acceptableContentTypes
success:(void (^)(id JSON))success
failure:(void (^)(NSError *error))failure
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON))success
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure
{
return [self operationWithRequest:urlRequest completion:^(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error) {
BOOL statusCodeAcceptable = [acceptableStatusCodes containsIndex:[response statusCode]];
Expand All @@ -68,7 +76,7 @@ + (id)operationWithRequest:(NSURLRequest *)urlRequest

if (error) {
if (failure) {
failure(error);
failure(request, response, error);
}
} else {
dispatch_async(json_request_operation_processing_queue(), ^(void) {
Expand All @@ -87,11 +95,11 @@ + (id)operationWithRequest:(NSURLRequest *)urlRequest
dispatch_sync(dispatch_get_main_queue(), ^(void) {
if (JSONError) {
if (failure) {
failure(JSONError);
failure(request, response, JSONError);
}
} else {
if (success) {
success(JSON);
success(request, response, JSON);
}
}
});
Expand Down
54 changes: 39 additions & 15 deletions AFNetworking/AFRestClient.h
Expand Up @@ -23,6 +23,9 @@
#import <Foundation/Foundation.h>
#import "AFHTTPRequestOperation.h"

#import "NSMutableURLRequest+AFNetworking.h"
#import "NSString+AFNetworking.h"

@protocol AFRestClient <NSObject>
+ (NSURL *)baseURL;
@end
Expand All @@ -38,25 +41,46 @@
- (void)setAuthorizationHeaderWithToken:(NSString *)token;
- (void)clearAuthorizationHeader;

- (NSMutableURLRequest *)requestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters;
- (void)enqueueHTTPOperationWithRequest:(NSURLRequest *)request success:(void (^)(id response))success failure:(void (^)(NSError *error))failure;
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
path:(NSString *)path parameters:(NSDictionary *)parameters;

- (void)getPath:(NSString *)path parameters:(NSDictionary *)parameters success:(void (^)(id response))success;
- (void)getPath:(NSString *)path parameters:(NSDictionary *)parameters success:(void (^)(id response))success failure:(void (^)(NSError *error))failure;
- (void)enqueueHTTPOperationWithRequest:(NSURLRequest *)request
success:(void (^)(id response))success
failure:(void (^)(NSError *error))failure;

- (void)postPath:(NSString *)path parameters:(NSDictionary *)parameters success:(void (^)(id response))success;
- (void)postPath:(NSString *)path parameters:(NSDictionary *)parameters success:(void (^)(id response))success failure:(void (^)(NSError *error))failure;
- (void)getPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(id response))success;

- (void)putPath:(NSString *)path parameters:(NSDictionary *)parameters success:(void (^)(id response))success;
- (void)putPath:(NSString *)path parameters:(NSDictionary *)parameters success:(void (^)(id response))success failure:(void (^)(NSError *error))failure;
- (void)getPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(id response))success
failure:(void (^)(NSError *error))failure;

- (void)deletePath:(NSString *)path parameters:(NSDictionary *)parameters success:(void (^)(id response))success;
- (void)deletePath:(NSString *)path parameters:(NSDictionary *)parameters success:(void (^)(id response))success failure:(void (^)(NSError *error))failure;
@end
- (void)postPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(id response))success;

- (void)postPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(id response))success
failure:(void (^)(NSError *error))failure;

- (void)putPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(id response))success;

- (void)putPath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(id response))success
failure:(void (^)(NSError *error))failure;

#pragma mark - NSString + AFRestClient
- (void)deletePath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(id response))success;

@interface NSString (AFRestClient)
- (NSString *)urlEncodedString;
- (NSString *)urlEncodedStringWithEncoding:(NSStringEncoding)encoding;
- (void)deletePath:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(id response))success
failure:(void (^)(NSError *error))failure;
@end
23 changes: 0 additions & 23 deletions AFNetworking/AFRestClient.m
Expand Up @@ -28,8 +28,6 @@
@interface AFRestClient ()
@property (readwrite, nonatomic, retain) NSMutableDictionary *defaultHeaders;
@property (readwrite, nonatomic, retain) NSOperationQueue *operationQueue;

- (void)enqueueHTTPOperationWithRequest:(NSURLRequest *)request success:(void (^)(id response))success failure:(void (^)(NSError *error))failure;
@end

@implementation AFRestClient
Expand Down Expand Up @@ -174,24 +172,3 @@ - (void)deletePath:(NSString *)path parameters:(NSDictionary *)parameters succes
}

@end

#pragma mark - NSString + AFRestClient

@implementation NSString (AFRestClient)

// See http://github.com/pokeb/asi-http-request/raw/master/Classes/ASIFormDataRequest.m
- (NSString*)urlEncodedString {
return [self urlEncodedStringWithEncoding:NSUTF8StringEncoding];
}

- (NSString *)urlEncodedStringWithEncoding:(NSStringEncoding)encoding {
NSString *newString = [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding(encoding)) autorelease];

if (newString) {
return newString;
}

return @"";
}

@end
32 changes: 32 additions & 0 deletions AFNetworking/NSData+AFNetworking.h
@@ -0,0 +1,32 @@
// NSData+AFNetworking.h
//
// Copyright (c) 2011 Gowalla (http://gowalla.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import <Foundation/Foundation.h>

extern NSString * const kAFZlibErrorDomain;

@interface NSData (AFNetworking)

- (NSData *)dataByGZipCompressingWithError:(NSError **)error;
- (NSData *)dataByGZipDecompressingDataWithError:(NSError **)error;

@end

0 comments on commit 390c51a

Please sign in to comment.