diff --git a/AFHTTPClient.h b/AFHTTPClient.h new file mode 100644 index 0000000..aae96f3 --- /dev/null +++ b/AFHTTPClient.h @@ -0,0 +1,641 @@ +// AFHTTPClient.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 +#import "AFURLConnectionOperation.h" + +#import + +/** + `AFHTTPClient` captures the common patterns of communicating with an web application over HTTP. It encapsulates information like base URL, authorization credentials, and HTTP headers, and uses them to construct and manage the execution of HTTP request operations. + + ## Automatic Content Parsing + + Instances of `AFHTTPClient` may specify which types of requests it expects and should handle by registering HTTP operation classes for automatic parsing. Registered classes will determine whether they can handle a particular request, and then construct a request operation accordingly in `enqueueHTTPRequestOperationWithRequest:success:failure`. + + ## Subclassing Notes + + In most cases, one should create an `AFHTTPClient` subclass for each website or web application that your application communicates with. It is often useful, also, to define a class method that returns a singleton shared HTTP client in each subclass, that persists authentication credentials and other configuration across the entire application. + + ## Methods to Override + + To change the behavior of all url request construction for an `AFHTTPClient` subclass, override `requestWithMethod:path:parameters`. + + To change the behavior of all request operation construction for an `AFHTTPClient` subclass, override `HTTPRequestOperationWithRequest:success:failure`. + + ## Default Headers + + By default, `AFHTTPClient` sets the following HTTP headers: + + - `Accept-Language: (comma-delimited preferred languages), en-us;q=0.8` + - `User-Agent: (generated user agent)` + + You can override these HTTP headers or define new ones using `setDefaultHeader:value:`. + + ## URL Construction Using Relative Paths + + Both `-requestWithMethod:path:parameters:` and `-multipartFormRequestWithMethod:path:parameters:constructingBodyWithBlock:` construct URLs from the path relative to the `-baseURL`, using `NSURL +URLWithString:relativeToURL:`. Below are a few examples of how `baseURL` and relative paths interact: + + NSURL *baseURL = [NSURL URLWithString:@"http://example.com/v1/"]; + [NSURL URLWithString:@"foo" relativeToURL:baseURL]; // http://example.com/v1/foo + [NSURL URLWithString:@"foo?bar=baz" relativeToURL:baseURL]; // http://example.com/v1/foo?bar=baz + [NSURL URLWithString:@"/foo" relativeToURL:baseURL]; // http://example.com/foo + [NSURL URLWithString:@"foo/" relativeToURL:baseURL]; // http://example.com/v1/foo + [NSURL URLWithString:@"/foo/" relativeToURL:baseURL]; // http://example.com/foo/ + [NSURL URLWithString:@"http://example2.com/" relativeToURL:baseURL]; // http://example2.com/ + + Also important to note is that a trailing slash will be added to any `baseURL` without one, which would otherwise cause unexpected behavior when constructing URLs using paths without a leading slash. + + ## NSCoding / NSCopying Conformance + + `AFHTTPClient` conforms to the `NSCoding` and `NSCopying` protocols, allowing operations to be archived to disk, and copied in memory, respectively. There are a few minor caveats to keep in mind, however: + + - Archives and copies of HTTP clients will be initialized with an empty operation queue. + - NSCoding cannot serialize / deserialize block properties, so an archive of an HTTP client will not include any reachability callback block that may be set. + */ + +#ifdef _SYSTEMCONFIGURATION_H +typedef enum { + AFNetworkReachabilityStatusUnknown = -1, + AFNetworkReachabilityStatusNotReachable = 0, + AFNetworkReachabilityStatusReachableViaWWAN = 1, + AFNetworkReachabilityStatusReachableViaWiFi = 2, +} AFNetworkReachabilityStatus; +#else +//#pragma message("SystemConfiguration framework not found in project, or not included in precompiled header. Network reachability functionality will not be available.") +#endif + +//#ifndef __UTTYPE__ +//#if __IPHONE_OS_VERSION_MIN_REQUIRED +//#pragma message("MobileCoreServices framework not found in project, or not included in precompiled header. Automatic MIME type detection when uploading files in multipart requests will not be available.") +//#else +//#pragma message("CoreServices framework not found in project, or not included in precompiled header. Automatic MIME type detection when uploading files in multipart requests will not be available.") +//#endif +//#endif + +typedef enum { + AFFormURLParameterEncoding, + AFJSONParameterEncoding, + AFPropertyListParameterEncoding, +} AFHTTPClientParameterEncoding; + +@class AFHTTPRequestOperation; +@protocol AFMultipartFormData; + +@interface AFHTTPClient : NSObject + +///--------------------------------------- +/// @name Accessing HTTP Client Properties +///--------------------------------------- + +/** + The url used as the base for paths specified in methods such as `getPath:parameters:success:failure` + */ +@property (readonly, nonatomic, strong) NSURL *baseURL; + +/** + The string encoding used in constructing url requests. This is `NSUTF8StringEncoding` by default. + */ +@property (nonatomic, assign) NSStringEncoding stringEncoding; + +/** + The `AFHTTPClientParameterEncoding` value corresponding to how parameters are encoded into a request body. This is `AFFormURLParameterEncoding` by default. + + @warning Some nested parameter structures, such as a keyed array of hashes containing inconsistent keys (i.e. `@{@"": @[@{@"a" : @(1)}, @{@"b" : @(2)}]}`), cannot be unambiguously represented in query strings. It is strongly recommended that an unambiguous encoding, such as `AFJSONParameterEncoding`, is used when posting complicated or nondeterministic parameter structures. + */ +@property (nonatomic, assign) AFHTTPClientParameterEncoding parameterEncoding; + +/** + The operation queue which manages operations enqueued by the HTTP client. + */ +@property (readonly, nonatomic, strong) NSOperationQueue *operationQueue; + +/** + The reachability status from the device to the current `baseURL` of the `AFHTTPClient`. + + @warning This property requires the `SystemConfiguration` framework. Add it in the active target's "Link Binary With Library" build phase, and add `#import ` to the header prefix of the project (`Prefix.pch`). + */ +#ifdef _SYSTEMCONFIGURATION_H +@property (readonly, nonatomic, assign) AFNetworkReachabilityStatus networkReachabilityStatus; +#endif + +/** + Default SSL pinning mode for each `AFHTTPRequestOperation` created by `HTTPRequestOperationWithRequest:success:failure:`. + */ +@property (nonatomic, assign) AFURLConnectionOperationSSLPinningMode defaultSSLPinningMode; + +/** + Whether each `AFHTTPRequestOperation` created by `HTTPRequestOperationWithRequest:success:failure:` should accept an invalid SSL certificate. + + If `_AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_` is set, this property defaults to `YES` for backwards compatibility. Otherwise, this property defaults to `NO`. + */ +@property (nonatomic, assign) BOOL allowsInvalidSSLCertificate; + +///--------------------------------------------- +/// @name Creating and Initializing HTTP Clients +///--------------------------------------------- + +/** + Creates and initializes an `AFHTTPClient` object with the specified base URL. + + @param url The base URL for the HTTP client. This argument must not be `nil`. + + @return The newly-initialized HTTP client + */ ++ (instancetype)clientWithBaseURL:(NSURL *)url; + +/** + Initializes an `AFHTTPClient` object with the specified base URL. + + This is the designated initializer. + + @param url The base URL for the HTTP client. This argument must not be `nil`. + + @return The newly-initialized HTTP client + */ +- (id)initWithBaseURL:(NSURL *)url; + +///----------------------------------- +/// @name Managing Reachability Status +///----------------------------------- + +/** + Sets a callback to be executed when the network availability of the `baseURL` host changes. + + @param block A block object to be executed when the network availability of the `baseURL` host changes.. This block has no return value and takes a single argument which represents the various reachability states from the device to the `baseURL`. + + @warning This method requires the `SystemConfiguration` framework. Add it in the active target's "Link Binary With Library" build phase, and add `#import ` to the header prefix of the project (`Prefix.pch`). + */ +#ifdef _SYSTEMCONFIGURATION_H +- (void)setReachabilityStatusChangeBlock:(void (^)(AFNetworkReachabilityStatus status))block; +#endif + +///------------------------------- +/// @name Managing HTTP Operations +///------------------------------- + +/** + Attempts to register a subclass of `AFHTTPRequestOperation`, adding it to a chain to automatically generate request operations from a URL request. + + When `enqueueHTTPRequestOperationWithRequest:success:failure` is invoked, each registered class is consulted in turn to see if it can handle the specific request. The first class to return `YES` when sent a `canProcessRequest:` message is used to create an operation using `initWithURLRequest:` and do `setCompletionBlockWithSuccess:failure:`. There is no guarantee that all registered classes will be consulted. Classes are consulted in the reverse order of their registration. Attempting to register an already-registered class will move it to the top of the list. + + @param operationClass The subclass of `AFHTTPRequestOperation` to register + + @return `YES` if the registration is successful, `NO` otherwise. The only failure condition is if `operationClass` is not a subclass of `AFHTTPRequestOperation`. + */ +- (BOOL)registerHTTPOperationClass:(Class)operationClass; + +/** + Unregisters the specified subclass of `AFHTTPRequestOperation` from the chain of classes consulted when `-requestWithMethod:path:parameters` is called. + + @param operationClass The subclass of `AFHTTPRequestOperation` to register + */ +- (void)unregisterHTTPOperationClass:(Class)operationClass; + +///---------------------------------- +/// @name Managing HTTP Header Values +///---------------------------------- + +/** + Returns the value for the HTTP headers set in request objects created by the HTTP client. + + @param header The HTTP header to return the default value for + + @return The default value for the HTTP header, or `nil` if unspecified + */ +- (NSString *)defaultValueForHeader:(NSString *)header; + +/** + Sets the value for the HTTP headers set in request objects made by the HTTP client. If `nil`, removes the existing value for that header. + + @param header The HTTP header to set a default value for + @param value The value set as default for the specified header, or `nil + */ +- (void)setDefaultHeader:(NSString *)header + value:(NSString *)value; + +/** + Sets the "Authorization" HTTP header set in request objects made by the HTTP client to a basic authentication value with Base64-encoded username and password. This overwrites any existing value for this header. + + @param username The HTTP basic auth username + @param password The HTTP basic auth password + */ +- (void)setAuthorizationHeaderWithUsername:(NSString *)username + password:(NSString *)password; + +/** + Sets the "Authorization" HTTP header set in request objects made by the HTTP client to a token-based authentication value, such as an OAuth access token. This overwrites any existing value for this header. + + @param token The authentication token + */ +- (void)setAuthorizationHeaderWithToken:(NSString *)token; + + +/** + Clears any existing value for the "Authorization" HTTP header. + */ +- (void)clearAuthorizationHeader; + +///------------------------------- +/// @name Managing URL Credentials +///------------------------------- + +/** + Set the default URL credential to be set for request operations. + + @param credential The URL credential + */ +- (void)setDefaultCredential:(NSURLCredential *)credential; + +///------------------------------- +/// @name Creating Request Objects +///------------------------------- + +/** + Creates an `NSMutableURLRequest` object with the specified HTTP method and path. + + If the HTTP method is `GET`, `HEAD`, or `DELETE`, the parameters will be used to construct a url-encoded query string that is appended to the request's URL. Otherwise, the parameters will be encoded according to the value of the `parameterEncoding` property, and set as the request body. + + @param method The HTTP method for the request, such as `GET`, `POST`, `PUT`, or `DELETE`. This parameter must not be `nil`. + @param path The path to be appended to the HTTP client's base URL and used as the request URL. If `nil`, no path will be appended to the base URL. + @param parameters The parameters to be either set as a query string for `GET` requests, or the request HTTP body. + + @return An `NSMutableURLRequest` object + */ +- (NSMutableURLRequest *)requestWithMethod:(NSString *)method + path:(NSString *)path + parameters:(NSDictionary *)parameters; + +/** + Creates an `NSMutableURLRequest` object with the specified HTTP method and path, and constructs a `multipart/form-data` HTTP body, using the specified parameters and multipart form data block. See http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.2 + + Multipart form requests are automatically streamed, reading files directly from disk along with in-memory data in a single HTTP body. The resulting `NSMutableURLRequest` object has an `HTTPBodyStream` property, so refrain from setting `HTTPBodyStream` or `HTTPBody` on this request object, as it will clear out the multipart form body stream. + + @param method The HTTP method for the request. This parameter must not be `GET` or `HEAD`, or `nil`. + @param path The path to be appended to the HTTP client's base URL and used as the request URL. + @param parameters The parameters to be encoded and set in the request HTTP body. + @param block A block that takes a single argument and appends data to the HTTP body. The block argument is an object adopting the `AFMultipartFormData` protocol. This can be used to upload files, encode HTTP body as JSON or XML, or specify multiple values for the same parameter, as one might for array values. + + @return An `NSMutableURLRequest` object + */ +- (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method + path:(NSString *)path + parameters:(NSDictionary *)parameters + constructingBodyWithBlock:(void (^)(id formData))block; + +///------------------------------- +/// @name Creating HTTP Operations +///------------------------------- + +/** + Creates an `AFHTTPRequestOperation`. + + In order to determine what kind of operation is created, each registered subclass conforming to the `AFHTTPClient` protocol is consulted (in reverse order of when they were specified) to see if it can handle the specific request. The first class to return `YES` when sent a `canProcessRequest:` message is used to generate an operation using `HTTPRequestOperationWithRequest:success:failure:`. + + @param urlRequest The request object to be loaded asynchronously during execution of the operation. + @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the created request operation and the object created from the response data of request. + @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes two arguments:, the created request operation and the `NSError` object describing the network or parsing error that occurred. + */ +- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest + success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success + failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure; + +///---------------------------------------- +/// @name Managing Enqueued HTTP Operations +///---------------------------------------- + +/** + Enqueues an `AFHTTPRequestOperation` to the HTTP client's operation queue. + + @param operation The HTTP request operation to be enqueued. + */ +- (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation; + +/** + Cancels all operations in the HTTP client's operation queue whose URLs match the specified HTTP method and path. + + This method only cancels `AFHTTPRequestOperations` whose request URL matches the HTTP client base URL with the path appended. For complete control over the lifecycle of enqueued operations, you can access the `operationQueue` property directly, which allows you to, for instance, cancel operations filtered by a predicate, or simply use `-cancelAllRequests`. Note that the operation queue may include non-HTTP operations, so be sure to check the type before attempting to directly introspect an operation's `request` property. + + @param method The HTTP method to match for the cancelled requests, such as `GET`, `POST`, `PUT`, or `DELETE`. If `nil`, all request operations with URLs matching the path will be cancelled. + @param path The path appended to the HTTP client base URL to match against the cancelled requests. If `nil`, no path will be appended to the base URL. + */ +- (void)cancelAllHTTPOperationsWithMethod:(NSString *)method path:(NSString *)path; + +///--------------------------------------- +/// @name Batching HTTP Request Operations +///--------------------------------------- + +/** + Creates and enqueues an `AFHTTPRequestOperation` to the HTTP client's operation queue for each specified request object into a batch. When each request operation finishes, the specified progress block is executed, until all of the request operations have finished, at which point the completion block also executes. + + Operations are created by passing the specified `NSURLRequest` objects in `requests`, using `-HTTPRequestOperationWithRequest:success:failure:`, with `nil` for both the `success` and `failure` parameters. + + @param urlRequests The `NSURLRequest` objects used to create and enqueue operations. + @param progressBlock A block object to be executed upon the completion of each request operation in the batch. This block has no return value and takes two arguments: the number of operations that have already finished execution, and the total number of operations. + @param completionBlock A block object to be executed upon the completion of all of the request operations in the batch. This block has no return value and takes a single argument: the batched request operations. + */ +- (void)enqueueBatchOfHTTPRequestOperationsWithRequests:(NSArray *)urlRequests + progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock + completionBlock:(void (^)(NSArray *operations))completionBlock; + +/** + Enqueues the specified request operations into a batch. When each request operation finishes, the specified progress block is executed, until all of the request operations have finished, at which point the completion block also executes. + + @param operations The request operations used to be batched and enqueued. + @param progressBlock A block object to be executed upon the completion of each request operation in the batch. This block has no return value and takes two arguments: the number of operations that have already finished execution, and the total number of operations. + @param completionBlock A block object to be executed upon the completion of all of the request operations in the batch. This block has no return value and takes a single argument: the batched request operations. + */ +- (void)enqueueBatchOfHTTPRequestOperations:(NSArray *)operations + progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock + completionBlock:(void (^)(NSArray *operations))completionBlock; + +///--------------------------- +/// @name Making HTTP Requests +///--------------------------- + +/** + Creates an `AFHTTPRequestOperation` with a `GET` request, and enqueues it to the HTTP client's operation queue. + + @param path The path to be appended to the HTTP client's base URL and used as the request URL. + @param parameters The parameters to be encoded and appended as the query string for the request URL. + @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the created request operation and the object created from the response data of request. + @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes two arguments: the created request operation and the `NSError` object describing the network or parsing error that occurred. + + @see -HTTPRequestOperationWithRequest:success:failure: + */ +- (void)getPath:(NSString *)path + parameters:(NSDictionary *)parameters + success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success + failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure; + +/** + Creates an `AFHTTPRequestOperation` with a `POST` request, and enqueues it to the HTTP client's operation queue. + + @param path The path to be appended to the HTTP client's base URL and used as the request URL. + @param parameters The parameters to be encoded and set in the request HTTP body. + @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the created request operation and the object created from the response data of request. + @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes two arguments: the created request operation and the `NSError` object describing the network or parsing error that occurred. + + @see -HTTPRequestOperationWithRequest:success:failure: + */ +- (void)postPath:(NSString *)path + parameters:(NSDictionary *)parameters + success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success + failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure; + +/** + Creates an `AFHTTPRequestOperation` with a `PUT` request, and enqueues it to the HTTP client's operation queue. + + @param path The path to be appended to the HTTP client's base URL and used as the request URL. + @param parameters The parameters to be encoded and set in the request HTTP body. + @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the created request operation and the object created from the response data of request. + @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes two arguments: the created request operation and the `NSError` object describing the network or parsing error that occurred. + + @see -HTTPRequestOperationWithRequest:success:failure: + */ +- (void)putPath:(NSString *)path + parameters:(NSDictionary *)parameters + success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success + failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure; + +/** + Creates an `AFHTTPRequestOperation` with a `DELETE` request, and enqueues it to the HTTP client's operation queue. + + @param path The path to be appended to the HTTP client's base URL and used as the request URL. + @param parameters The parameters to be encoded and appended as the query string for the request URL. + @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the created request operation and the object created from the response data of request. + @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes two arguments: the created request operation and the `NSError` object describing the network or parsing error that occurred. + + @see -HTTPRequestOperationWithRequest:success:failure: + */ +- (void)deletePath:(NSString *)path + parameters:(NSDictionary *)parameters + success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success + failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure; + +/** + Creates an `AFHTTPRequestOperation` with a `PATCH` request, and enqueues it to the HTTP client's operation queue. + + @param path The path to be appended to the HTTP client's base URL and used as the request URL. + @param parameters The parameters to be encoded and set in the request HTTP body. + @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the created request operation and the object created from the response data of request. + @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes two arguments: the created request operation and the `NSError` object describing the network or parsing error that occurred. + + @see -HTTPRequestOperationWithRequest:success:failure: + */ +- (void)patchPath:(NSString *)path + parameters:(NSDictionary *)parameters + success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success + failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure; +@end + +///---------------- +/// @name Constants +///---------------- + +/** + ## Network Reachability + + The following constants are provided by `AFHTTPClient` as possible network reachability statuses. + + enum { + AFNetworkReachabilityStatusUnknown, + AFNetworkReachabilityStatusNotReachable, + AFNetworkReachabilityStatusReachableViaWWAN, + AFNetworkReachabilityStatusReachableViaWiFi, + } + + `AFNetworkReachabilityStatusUnknown` + The `baseURL` host reachability is not known. + + `AFNetworkReachabilityStatusNotReachable` + The `baseURL` host cannot be reached. + + `AFNetworkReachabilityStatusReachableViaWWAN` + The `baseURL` host can be reached via a cellular connection, such as EDGE or GPRS. + + `AFNetworkReachabilityStatusReachableViaWiFi` + The `baseURL` host can be reached via a Wi-Fi connection. + + ### Keys for Notification UserInfo Dictionary + + Strings that are used as keys in a `userInfo` dictionary in a network reachability status change notification. + + `AFNetworkingReachabilityNotificationStatusItem` + A key in the userInfo dictionary in a `AFNetworkingReachabilityDidChangeNotification` notification. + The corresponding value is an `NSNumber` object representing the `AFNetworkReachabilityStatus` value for the current reachability status. + + ## Parameter Encoding + + The following constants are provided by `AFHTTPClient` as possible methods for serializing parameters into query string or message body values. + + enum { + AFFormURLParameterEncoding, + AFJSONParameterEncoding, + AFPropertyListParameterEncoding, + } + + `AFFormURLParameterEncoding` + Parameters are encoded into field/key pairs in the URL query string for `GET` `HEAD` and `DELETE` requests, and in the message body otherwise. Dictionary keys are sorted with the `caseInsensitiveCompare:` selector of their description, in order to mitigate the possibility of ambiguous query strings being generated non-deterministically. See the warning for the `parameterEncoding` property for additional information. + + `AFJSONParameterEncoding` + Parameters are encoded into JSON in the message body. + + `AFPropertyListParameterEncoding` + Parameters are encoded into a property list in the message body. + */ + +///---------------- +/// @name Functions +///---------------- + +/** + Returns a query string constructed by a set of parameters, using the specified encoding. + + Query strings are constructed by collecting each key-value pair, percent escaping a string representation of the key-value pair, and then joining the pairs with "&". + + If a query string pair has a an `NSArray` for its value, each member of the array will be represented in the format `field[]=value1&field[]value2`. Otherwise, the pair will be formatted as "field=value". String representations of both keys and values are derived using the `-description` method. The constructed query string does not include the ? character used to delimit the query component. + + @param parameters The parameters used to construct the query string + @param encoding The encoding to use in constructing the query string. If you are uncertain of the correct encoding, you should use UTF-8 (`NSUTF8StringEncoding`), which is the encoding designated by RFC 3986 as the correct encoding for use in URLs. + + @return A percent-escaped query string + */ +extern NSString * AFQueryStringFromParametersWithEncoding(NSDictionary *parameters, NSStringEncoding encoding); + +///-------------------- +/// @name Notifications +///-------------------- + +/** + Posted when network reachability changes. + This notification assigns no notification object. The `userInfo` dictionary contains an `NSNumber` object under the `AFNetworkingReachabilityNotificationStatusItem` key, representing the `AFNetworkReachabilityStatus` value for the current network reachability. + + @warning In order for network reachability to be monitored, include the `SystemConfiguration` framework in the active target's "Link Binary With Library" build phase, and add `#import ` to the header prefix of the project (`Prefix.pch`). + */ +#ifdef _SYSTEMCONFIGURATION_H +extern NSString * const AFNetworkingReachabilityDidChangeNotification; +extern NSString * const AFNetworkingReachabilityNotificationStatusItem; +#endif + +#pragma mark - + +extern NSUInteger const kAFUploadStream3GSuggestedPacketSize; +extern NSTimeInterval const kAFUploadStream3GSuggestedDelay; + +/** + The `AFMultipartFormData` protocol defines the methods supported by the parameter in the block argument of `AFHTTPClient -multipartFormRequestWithMethod:path:parameters:constructingBodyWithBlock:`. + */ +@protocol AFMultipartFormData + +/** + Appends the HTTP header `Content-Disposition: file; filename=#{generated filename}; name=#{name}"` and `Content-Type: #{generated mimeType}`, followed by the encoded file data and the multipart form boundary. + + The filename and MIME type for this data in the form will be automatically generated, using the last path component of the `fileURL` and system associated MIME type for the `fileURL` extension, respectively. + + @param fileURL The URL corresponding to the file whose content will be appended to the form. This parameter must not be `nil`. + @param name The name to be associated with the specified data. This parameter must not be `nil`. + @param error If an error occurs, upon return contains an `NSError` object that describes the problem. + + @return `YES` if the file data was successfully appended, otherwise `NO`. + */ +- (BOOL)appendPartWithFileURL:(NSURL *)fileURL + name:(NSString *)name + error:(NSError * __autoreleasing *)error; + +/** + Appends the HTTP header `Content-Disposition: file; filename=#{filename}; name=#{name}"` and `Content-Type: #{mimeType}`, followed by the encoded file data and the multipart form boundary. + + @param fileURL The URL corresponding to the file whose content will be appended to the form. This parameter must not be `nil`. + @param name The name to be associated with the specified data. This parameter must not be `nil`. + @param fileName The file name to be used in the `Content-Disposition` header. This parameter must not be `nil`. + @param mimeType The declared MIME type of the file data. This parameter must not be `nil`. + @param error If an error occurs, upon return contains an `NSError` object that describes the problem. + + @return `YES` if the file data was successfully appended otherwise `NO`. + */ +- (BOOL)appendPartWithFileURL:(NSURL *)fileURL + name:(NSString *)name + fileName:(NSString *)fileName + mimeType:(NSString *)mimeType + error:(NSError * __autoreleasing *)error; + +/** + Appends the HTTP header `Content-Disposition: file; filename=#{filename}; name=#{name}"` and `Content-Type: #{mimeType}`, followed by the data from the input stream and the multipart form boundary. + + @param inputStream The input stream to be appended to the form data + @param name The name to be associated with the specified input stream. This parameter must not be `nil`. + @param fileName The filename to be associated with the specified input stream. This parameter must not be `nil`. + @param length The length of the specified input stream in bytes. + @param mimeType The MIME type of the specified data. (For example, the MIME type for a JPEG image is image/jpeg.) For a list of valid MIME types, see http://www.iana.org/assignments/media-types/. This parameter must not be `nil`. + */ +- (void)appendPartWithInputStream:(NSInputStream *)inputStream + name:(NSString *)name + fileName:(NSString *)fileName + length:(unsigned long long)length + mimeType:(NSString *)mimeType; + +/** + Appends the HTTP header `Content-Disposition: file; filename=#{filename}; name=#{name}"` and `Content-Type: #{mimeType}`, followed by the encoded file data and the multipart form boundary. + + @param data The data to be encoded and appended to the form data. + @param name The name to be associated with the specified data. This parameter must not be `nil`. + @param fileName The filename to be associated with the specified data. This parameter must not be `nil`. + @param mimeType The MIME type of the specified data. (For example, the MIME type for a JPEG image is image/jpeg.) For a list of valid MIME types, see http://www.iana.org/assignments/media-types/. This parameter must not be `nil`. + */ +- (void)appendPartWithFileData:(NSData *)data + name:(NSString *)name + fileName:(NSString *)fileName + mimeType:(NSString *)mimeType; + +/** + Appends the HTTP headers `Content-Disposition: form-data; name=#{name}"`, followed by the encoded data and the multipart form boundary. + + @param data The data to be encoded and appended to the form data. + @param name The name to be associated with the specified data. This parameter must not be `nil`. + */ + +- (void)appendPartWithFormData:(NSData *)data + name:(NSString *)name; + + +/** + Appends HTTP headers, followed by the encoded data and the multipart form boundary. + + @param headers The HTTP headers to be appended to the form data. + @param body The data to be encoded and appended to the form data. + */ +- (void)appendPartWithHeaders:(NSDictionary *)headers + body:(NSData *)body; + +/** + Throttles request bandwidth by limiting the packet size and adding a delay for each chunk read from the upload stream. + + When uploading over a 3G or EDGE connection, requests may fail with "request body stream exhausted". Setting a maximum packet size and delay according to the recommended values (`kAFUploadStream3GSuggestedPacketSize` and `kAFUploadStream3GSuggestedDelay`) lowers the risk of the input stream exceeding its allocated bandwidth. Unfortunately, as of iOS 6, there is no definite way to distinguish between a 3G, EDGE, or LTE connection. As such, it is not recommended that you throttle bandwidth based solely on network reachability. Instead, you should consider checking for the "request body stream exhausted" in a failure block, and then retrying the request with throttled bandwidth. + + @param numberOfBytes Maximum packet size, in number of bytes. The default packet size for an input stream is 32kb. + @param delay Duration of delay each time a packet is read. By default, no delay is set. + */ +- (void)throttleBandwidthWithPacketSize:(NSUInteger)numberOfBytes + delay:(NSTimeInterval)delay; + +@end diff --git a/AFHTTPClient.m b/AFHTTPClient.m new file mode 100644 index 0000000..e985eac --- /dev/null +++ b/AFHTTPClient.m @@ -0,0 +1,1370 @@ +// AFHTTPClient.m +// +// 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 + +#import "AFHTTPClient.h" +#import "AFHTTPRequestOperation.h" + +#import + +#ifdef _SYSTEMCONFIGURATION_H +#import +#import +#import +#import +#import +#endif + +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +#import +#endif + +#ifdef _SYSTEMCONFIGURATION_H +NSString * const AFNetworkingReachabilityDidChangeNotification = @"com.alamofire.networking.reachability.change"; +NSString * const AFNetworkingReachabilityNotificationStatusItem = @"AFNetworkingReachabilityNotificationStatusItem"; + +typedef SCNetworkReachabilityRef AFNetworkReachabilityRef; +typedef void (^AFNetworkReachabilityStatusBlock)(AFNetworkReachabilityStatus status); +#else +typedef id AFNetworkReachabilityRef; +#endif + +typedef void (^AFCompletionBlock)(void); + +static NSString * AFBase64EncodedStringFromString(NSString *string) { + NSData *data = [NSData dataWithBytes:[string UTF8String] length:[string lengthOfBytesUsingEncoding:NSUTF8StringEncoding]]; + NSUInteger length = [data length]; + NSMutableData *mutableData = [NSMutableData dataWithLength:((length + 2) / 3) * 4]; + + uint8_t *input = (uint8_t *)[data bytes]; + uint8_t *output = (uint8_t *)[mutableData mutableBytes]; + + for (NSUInteger i = 0; i < length; i += 3) { + NSUInteger value = 0; + for (NSUInteger j = i; j < (i + 3); j++) { + value <<= 8; + if (j < length) { + value |= (0xFF & input[j]); + } + } + + static uint8_t const kAFBase64EncodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + + NSUInteger idx = (i / 3) * 4; + output[idx + 0] = kAFBase64EncodingTable[(value >> 18) & 0x3F]; + output[idx + 1] = kAFBase64EncodingTable[(value >> 12) & 0x3F]; + output[idx + 2] = (i + 1) < length ? kAFBase64EncodingTable[(value >> 6) & 0x3F] : '='; + output[idx + 3] = (i + 2) < length ? kAFBase64EncodingTable[(value >> 0) & 0x3F] : '='; + } + + return [[NSString alloc] initWithData:mutableData encoding:NSASCIIStringEncoding]; +} + +static NSString * AFPercentEscapedQueryStringPairMemberFromStringWithEncoding(NSString *string, NSStringEncoding encoding) { + static NSString * const kAFCharactersToBeEscaped = @":/?&=;+!@#$()',*"; + static NSString * const kAFCharactersToLeaveUnescaped = @"[]."; + + return (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, (__bridge CFStringRef)kAFCharactersToLeaveUnescaped, (__bridge CFStringRef)kAFCharactersToBeEscaped, CFStringConvertNSStringEncodingToEncoding(encoding)); +} + +#pragma mark - + +@interface AFQueryStringPair : NSObject +@property (readwrite, nonatomic, strong) id field; +@property (readwrite, nonatomic, strong) id value; + +- (id)initWithField:(id)field value:(id)value; + +- (NSString *)URLEncodedStringValueWithEncoding:(NSStringEncoding)stringEncoding; +@end + +@implementation AFQueryStringPair +@synthesize field = _field; +@synthesize value = _value; + +- (id)initWithField:(id)field value:(id)value { + self = [super init]; + if (!self) { + return nil; + } + + self.field = field; + self.value = value; + + return self; +} + +- (NSString *)URLEncodedStringValueWithEncoding:(NSStringEncoding)stringEncoding { + if (!self.value || [self.value isEqual:[NSNull null]]) { + return AFPercentEscapedQueryStringPairMemberFromStringWithEncoding([self.field description], stringEncoding); + } else { + return [NSString stringWithFormat:@"%@=%@", AFPercentEscapedQueryStringPairMemberFromStringWithEncoding([self.field description], stringEncoding), AFPercentEscapedQueryStringPairMemberFromStringWithEncoding([self.value description], stringEncoding)]; + } +} + +@end + +#pragma mark - + +extern NSArray * AFQueryStringPairsFromDictionary(NSDictionary *dictionary); +extern NSArray * AFQueryStringPairsFromKeyAndValue(NSString *key, id value); + +NSString * AFQueryStringFromParametersWithEncoding(NSDictionary *parameters, NSStringEncoding stringEncoding) { + NSMutableArray *mutablePairs = [NSMutableArray array]; + for (AFQueryStringPair *pair in AFQueryStringPairsFromDictionary(parameters)) { + [mutablePairs addObject:[pair URLEncodedStringValueWithEncoding:stringEncoding]]; + } + + return [mutablePairs componentsJoinedByString:@"&"]; +} + +NSArray * AFQueryStringPairsFromDictionary(NSDictionary *dictionary) { + return AFQueryStringPairsFromKeyAndValue(nil, dictionary); +} + +NSArray * AFQueryStringPairsFromKeyAndValue(NSString *key, id value) { + NSMutableArray *mutableQueryStringComponents = [NSMutableArray array]; + + if ([value isKindOfClass:[NSDictionary class]]) { + NSDictionary *dictionary = value; + // Sort dictionary keys to ensure consistent ordering in query string, which is important when deserializing potentially ambiguous sequences, such as an array of dictionaries + NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"description" ascending:YES selector:@selector(caseInsensitiveCompare:)]; + for (id nestedKey in [dictionary.allKeys sortedArrayUsingDescriptors:@[ sortDescriptor ]]) { + id nestedValue = [dictionary objectForKey:nestedKey]; + if (nestedValue) { + [mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue((key ? [NSString stringWithFormat:@"%@[%@]", key, nestedKey] : nestedKey), nestedValue)]; + } + } + } else if ([value isKindOfClass:[NSArray class]]) { + NSArray *array = value; + for (id nestedValue in array) { + [mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue([NSString stringWithFormat:@"%@[]", key], nestedValue)]; + } + } else if ([value isKindOfClass:[NSSet class]]) { + NSSet *set = value; + for (id obj in set) { + [mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue(key, obj)]; + } + } else { + [mutableQueryStringComponents addObject:[[AFQueryStringPair alloc] initWithField:key value:value]]; + } + + return mutableQueryStringComponents; +} + +@interface AFStreamingMultipartFormData : NSObject +- (id)initWithURLRequest:(NSMutableURLRequest *)urlRequest + stringEncoding:(NSStringEncoding)encoding; + +- (NSMutableURLRequest *)requestByFinalizingMultipartFormData; +@end + +#pragma mark - + +@interface AFHTTPClient () +@property (readwrite, nonatomic, strong) NSURL *baseURL; +@property (readwrite, nonatomic, strong) NSMutableArray *registeredHTTPOperationClassNames; +@property (readwrite, nonatomic, strong) NSMutableDictionary *defaultHeaders; +@property (readwrite, nonatomic, strong) NSURLCredential *defaultCredential; +@property (readwrite, nonatomic, strong) NSOperationQueue *operationQueue; +#ifdef _SYSTEMCONFIGURATION_H +@property (readwrite, nonatomic, assign) AFNetworkReachabilityRef networkReachability; +@property (readwrite, nonatomic, assign) AFNetworkReachabilityStatus networkReachabilityStatus; +@property (readwrite, nonatomic, copy) AFNetworkReachabilityStatusBlock networkReachabilityStatusBlock; +#endif + +#ifdef _SYSTEMCONFIGURATION_H +- (void)startMonitoringNetworkReachability; +- (void)stopMonitoringNetworkReachability; +#endif +@end + +@implementation AFHTTPClient +@synthesize baseURL = _baseURL; +@synthesize stringEncoding = _stringEncoding; +@synthesize parameterEncoding = _parameterEncoding; +@synthesize registeredHTTPOperationClassNames = _registeredHTTPOperationClassNames; +@synthesize defaultHeaders = _defaultHeaders; +@synthesize defaultCredential = _defaultCredential; +@synthesize operationQueue = _operationQueue; +#ifdef _SYSTEMCONFIGURATION_H +@synthesize networkReachability = _networkReachability; +@synthesize networkReachabilityStatus = _networkReachabilityStatus; +@synthesize networkReachabilityStatusBlock = _networkReachabilityStatusBlock; +#endif +@synthesize defaultSSLPinningMode = _defaultSSLPinningMode; +@synthesize allowsInvalidSSLCertificate = _allowsInvalidSSLCertificate; + ++ (instancetype)clientWithBaseURL:(NSURL *)url { + return [[self alloc] initWithBaseURL:url]; +} + +- (id)init { + @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"%@ Failed to call designated initializer. Invoke `initWithBaseURL:` instead.", NSStringFromClass([self class])] userInfo:nil]; +} + +- (id)initWithBaseURL:(NSURL *)url { + NSParameterAssert(url); + + self = [super init]; + if (!self) { + return nil; + } + + // Ensure terminal slash for baseURL path, so that NSURL +URLWithString:relativeToURL: works as expected + if ([[url path] length] > 0 && ![[url absoluteString] hasSuffix:@"/"]) { + url = [url URLByAppendingPathComponent:@""]; + } + + self.baseURL = url; + + self.stringEncoding = NSUTF8StringEncoding; + self.parameterEncoding = AFFormURLParameterEncoding; + + self.registeredHTTPOperationClassNames = [NSMutableArray array]; + + self.defaultHeaders = [NSMutableDictionary dictionary]; + + // Accept-Language HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4 + NSMutableArray *acceptLanguagesComponents = [NSMutableArray array]; + [[NSLocale preferredLanguages] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { + float q = 1.0f - (idx * 0.1f); + [acceptLanguagesComponents addObject:[NSString stringWithFormat:@"%@;q=%0.1g", obj, q]]; + *stop = q <= 0.5f; + }]; + [self setDefaultHeader:@"Accept-Language" value:[acceptLanguagesComponents componentsJoinedByString:@", "]]; + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wgnu" +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) + // User-Agent Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43 + [self setDefaultHeader:@"User-Agent" value:[NSString stringWithFormat:@"%@/%@ (%@; iOS %@; Scale/%0.2f)", [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleExecutableKey] ?: [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleIdentifierKey], (__bridge id)CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), kCFBundleVersionKey) ?: [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleVersionKey], [[UIDevice currentDevice] model], [[UIDevice currentDevice] systemVersion], ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] ? [[UIScreen mainScreen] scale] : 1.0f)]]; +#elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED) + [self setDefaultHeader:@"User-Agent" value:[NSString stringWithFormat:@"%@/%@ (Mac OS X %@)", [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleExecutableKey] ?: [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleIdentifierKey], [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] ?: [[[NSBundle mainBundle] infoDictionary] objectForKey:(__bridge NSString *)kCFBundleVersionKey], [[NSProcessInfo processInfo] operatingSystemVersionString]]]; +#endif +#pragma clang diagnostic pop + +#ifdef _SYSTEMCONFIGURATION_H + self.networkReachabilityStatus = AFNetworkReachabilityStatusUnknown; + [self startMonitoringNetworkReachability]; +#endif + + self.operationQueue = [[NSOperationQueue alloc] init]; + [self.operationQueue setMaxConcurrentOperationCount:NSOperationQueueDefaultMaxConcurrentOperationCount]; + + // #ifdef included for backwards-compatibility +#ifdef _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_ + self.allowsInvalidSSLCertificate = YES; +#endif + + return self; +} + +- (void)dealloc { +#ifdef _SYSTEMCONFIGURATION_H + [self stopMonitoringNetworkReachability]; +#endif +} + +- (NSString *)description { + return [NSString stringWithFormat:@"<%@: %p, baseURL: %@, defaultHeaders: %@, registeredOperationClasses: %@, operationQueue: %@>", NSStringFromClass([self class]), self, [self.baseURL absoluteString], self.defaultHeaders, self.registeredHTTPOperationClassNames, self.operationQueue]; +} + +#pragma mark - + +#ifdef _SYSTEMCONFIGURATION_H +static BOOL AFURLHostIsIPAddress(NSURL *url) { + struct sockaddr_in sa_in; + struct sockaddr_in6 sa_in6; + + return [url host] && (inet_pton(AF_INET, [[url host] UTF8String], &sa_in) == 1 || inet_pton(AF_INET6, [[url host] UTF8String], &sa_in6) == 1); +} + +static AFNetworkReachabilityStatus AFNetworkReachabilityStatusForFlags(SCNetworkReachabilityFlags flags) { + BOOL isReachable = ((flags & kSCNetworkReachabilityFlagsReachable) != 0); + BOOL needsConnection = ((flags & kSCNetworkReachabilityFlagsConnectionRequired) != 0); + BOOL canConnectionAutomatically = (((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) || ((flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0)); + BOOL canConnectWithoutUserInteraction = (canConnectionAutomatically && (flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0); + BOOL isNetworkReachable = (isReachable && (!needsConnection || canConnectWithoutUserInteraction)); + + AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusUnknown; + if (isNetworkReachable == NO) { + status = AFNetworkReachabilityStatusNotReachable; + } +#if TARGET_OS_IPHONE + else if ((flags & kSCNetworkReachabilityFlagsIsWWAN) != 0) { + status = AFNetworkReachabilityStatusReachableViaWWAN; + } +#endif + else { + status = AFNetworkReachabilityStatusReachableViaWiFi; + } + + return status; +} + +static void AFNetworkReachabilityCallback(SCNetworkReachabilityRef __unused target, SCNetworkReachabilityFlags flags, void *info) { + AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusForFlags(flags); + AFNetworkReachabilityStatusBlock block = (__bridge AFNetworkReachabilityStatusBlock)info; + if (block) { + block(status); + } + + dispatch_async(dispatch_get_main_queue(), ^{ + NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; + [notificationCenter postNotificationName:AFNetworkingReachabilityDidChangeNotification object:nil userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:status] forKey:AFNetworkingReachabilityNotificationStatusItem]]; + }); +} + +static const void * AFNetworkReachabilityRetainCallback(const void *info) { + return Block_copy(info); +} + +static void AFNetworkReachabilityReleaseCallback(const void *info) { + if (info) { + Block_release(info); + } +} + +- (void)startMonitoringNetworkReachability { + [self stopMonitoringNetworkReachability]; + + if (!self.baseURL) { + return; + } + + self.networkReachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [[self.baseURL host] UTF8String]); + + if (!self.networkReachability) { + return; + } + + __weak __typeof(&*self)weakSelf = self; + AFNetworkReachabilityStatusBlock callback = ^(AFNetworkReachabilityStatus status) { + __strong __typeof(&*weakSelf)strongSelf = weakSelf; + if (!strongSelf) { + return; + } + + strongSelf.networkReachabilityStatus = status; + if (strongSelf.networkReachabilityStatusBlock) { + strongSelf.networkReachabilityStatusBlock(status); + } + }; + + SCNetworkReachabilityContext context = {0, (__bridge void *)callback, AFNetworkReachabilityRetainCallback, AFNetworkReachabilityReleaseCallback, NULL}; + SCNetworkReachabilitySetCallback(self.networkReachability, AFNetworkReachabilityCallback, &context); + + /* Network reachability monitoring does not establish a baseline for IP addresses as it does for hostnames, so if the base URL host is an IP address, the initial reachability callback is manually triggered. + */ + if (AFURLHostIsIPAddress(self.baseURL)) { + SCNetworkReachabilityFlags flags; + SCNetworkReachabilityGetFlags(self.networkReachability, &flags); + dispatch_async(dispatch_get_main_queue(), ^{ + AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusForFlags(flags); + callback(status); + }); + } + + SCNetworkReachabilityScheduleWithRunLoop(self.networkReachability, CFRunLoopGetMain(), kCFRunLoopCommonModes); +} + +- (void)stopMonitoringNetworkReachability { + if (self.networkReachability) { + SCNetworkReachabilityUnscheduleFromRunLoop(self.networkReachability, CFRunLoopGetMain(), kCFRunLoopCommonModes); + + CFRelease(_networkReachability); + _networkReachability = NULL; + } +} + +- (void)setReachabilityStatusChangeBlock:(void (^)(AFNetworkReachabilityStatus status))block { + self.networkReachabilityStatusBlock = block; +} +#endif + +#pragma mark - + +- (BOOL)registerHTTPOperationClass:(Class)operationClass { + if (![operationClass isSubclassOfClass:[AFHTTPRequestOperation class]]) { + return NO; + } + + NSString *className = NSStringFromClass(operationClass); + [self.registeredHTTPOperationClassNames removeObject:className]; + [self.registeredHTTPOperationClassNames insertObject:className atIndex:0]; + + return YES; +} + +- (void)unregisterHTTPOperationClass:(Class)operationClass { + NSString *className = NSStringFromClass(operationClass); + [self.registeredHTTPOperationClassNames removeObject:className]; +} + +#pragma mark - + +- (NSString *)defaultValueForHeader:(NSString *)header { + return [self.defaultHeaders valueForKey:header]; +} + +- (void)setDefaultHeader:(NSString *)header value:(NSString *)value { + [self.defaultHeaders setValue:value forKey:header]; +} + +- (void)setAuthorizationHeaderWithUsername:(NSString *)username password:(NSString *)password { + NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", username, password]; + [self setDefaultHeader:@"Authorization" value:[NSString stringWithFormat:@"Basic %@", AFBase64EncodedStringFromString(basicAuthCredentials)]]; +} + +- (void)setAuthorizationHeaderWithToken:(NSString *)token { + [self setDefaultHeader:@"Authorization" value:[NSString stringWithFormat:@"Token token=\"%@\"", token]]; +} + +- (void)clearAuthorizationHeader { + [self.defaultHeaders removeObjectForKey:@"Authorization"]; +} + +#pragma mark - + +- (NSMutableURLRequest *)requestWithMethod:(NSString *)method + path:(NSString *)path + parameters:(NSDictionary *)parameters +{ + NSParameterAssert(method); + + if (!path) { + path = @""; + } + + NSURL *url = [NSURL URLWithString:path relativeToURL:self.baseURL]; + NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; + [request setHTTPMethod:method]; + [request setAllHTTPHeaderFields:self.defaultHeaders]; + + if (parameters) { + if ([method isEqualToString:@"GET"] || [method isEqualToString:@"HEAD"] || [method isEqualToString:@"DELETE"]) { + url = [NSURL URLWithString:[[url absoluteString] stringByAppendingFormat:[path rangeOfString:@"?"].location == NSNotFound ? @"?%@" : @"&%@", AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding)]]; + [request setURL:url]; + } else { + NSString *charset = (__bridge NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(self.stringEncoding)); + NSError *error = nil; + + switch (self.parameterEncoding) { + case AFFormURLParameterEncoding:; + [request setValue:[NSString stringWithFormat:@"application/x-www-form-urlencoded; charset=%@", charset] forHTTPHeaderField:@"Content-Type"]; + [request setHTTPBody:[AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding) dataUsingEncoding:self.stringEncoding]]; + break; + case AFJSONParameterEncoding:; + [request setValue:[NSString stringWithFormat:@"application/json; charset=%@", charset] forHTTPHeaderField:@"Content-Type"]; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wassign-enum" + [request setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error]]; +#pragma clang diagnostic pop + break; + case AFPropertyListParameterEncoding:; + [request setValue:[NSString stringWithFormat:@"application/x-plist; charset=%@", charset] forHTTPHeaderField:@"Content-Type"]; + [request setHTTPBody:[NSPropertyListSerialization dataWithPropertyList:parameters format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]]; + break; + } + + if (error) { + + } + } + } + + return request; +} + +- (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method + path:(NSString *)path + parameters:(NSDictionary *)parameters + constructingBodyWithBlock:(void (^)(id formData))block +{ + NSParameterAssert(method); + NSParameterAssert(![method isEqualToString:@"GET"] && ![method isEqualToString:@"HEAD"]); + + NSMutableURLRequest *request = [self requestWithMethod:method path:path parameters:nil]; + + __block AFStreamingMultipartFormData *formData = [[AFStreamingMultipartFormData alloc] initWithURLRequest:request stringEncoding:self.stringEncoding]; + + if (parameters) { + for (AFQueryStringPair *pair in AFQueryStringPairsFromDictionary(parameters)) { + NSData *data = nil; + if ([pair.value isKindOfClass:[NSData class]]) { + data = pair.value; + } else if ([pair.value isEqual:[NSNull null]]) { + data = [NSData data]; + } else { + data = [[pair.value description] dataUsingEncoding:self.stringEncoding]; + } + + if (data) { + [formData appendPartWithFormData:data name:[pair.field description]]; + } + } + } + + if (block) { + block(formData); + } + + return [formData requestByFinalizingMultipartFormData]; +} + +- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest + success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success + failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure +{ + AFHTTPRequestOperation *operation = nil; + + for (NSString *className in self.registeredHTTPOperationClassNames) { + Class operationClass = NSClassFromString(className); + if (operationClass && [operationClass canProcessRequest:urlRequest]) { + operation = [(AFHTTPRequestOperation *)[operationClass alloc] initWithRequest:urlRequest]; + break; + } + } + + if (!operation) { + operation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest]; + } + + [operation setCompletionBlockWithSuccess:success failure:failure]; + + operation.credential = self.defaultCredential; + operation.SSLPinningMode = self.defaultSSLPinningMode; + operation.allowsInvalidSSLCertificate = self.allowsInvalidSSLCertificate; + + return operation; +} + +#pragma mark - + +- (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation { + [self.operationQueue addOperation:operation]; +} + +- (void)cancelAllHTTPOperationsWithMethod:(NSString *)method + path:(NSString *)path +{ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wgnu" + NSString *pathToBeMatched = [[[self requestWithMethod:(method ?: @"GET") path:path parameters:nil] URL] path]; +#pragma clang diagnostic pop + + for (NSOperation *operation in [self.operationQueue operations]) { + if (![operation isKindOfClass:[AFHTTPRequestOperation class]]) { + continue; + } + + BOOL hasMatchingMethod = !method || [method isEqualToString:[[(AFHTTPRequestOperation *)operation request] HTTPMethod]]; + BOOL hasMatchingPath = [[[[(AFHTTPRequestOperation *)operation request] URL] path] isEqual:pathToBeMatched]; + + if (hasMatchingMethod && hasMatchingPath) { + [operation cancel]; + } + } +} + +- (void)enqueueBatchOfHTTPRequestOperationsWithRequests:(NSArray *)urlRequests + progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock + completionBlock:(void (^)(NSArray *operations))completionBlock +{ + NSMutableArray *mutableOperations = [NSMutableArray array]; + for (NSURLRequest *request in urlRequests) { + AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:nil failure:nil]; + [mutableOperations addObject:operation]; + } + + [self enqueueBatchOfHTTPRequestOperations:mutableOperations progressBlock:progressBlock completionBlock:completionBlock]; +} + +- (void)enqueueBatchOfHTTPRequestOperations:(NSArray *)operations + progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock + completionBlock:(void (^)(NSArray *operations))completionBlock +{ + __block dispatch_group_t dispatchGroup = dispatch_group_create(); + NSBlockOperation *batchedOperation = [NSBlockOperation blockOperationWithBlock:^{ + dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^{ + if (completionBlock) { + completionBlock(operations); + } + }); +#if !OS_OBJECT_USE_OBJC + dispatch_release(dispatchGroup); +#endif + }]; + + for (AFHTTPRequestOperation *operation in operations) { + AFCompletionBlock originalCompletionBlock = [operation.completionBlock copy]; + __weak __typeof(&*operation)weakOperation = operation; + operation.completionBlock = ^{ + __strong __typeof(&*weakOperation)strongOperation = weakOperation; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wgnu" + dispatch_queue_t queue = strongOperation.successCallbackQueue ?: dispatch_get_main_queue(); +#pragma clang diagnostic pop + dispatch_group_async(dispatchGroup, queue, ^{ + if (originalCompletionBlock) { + originalCompletionBlock(); + } + + NSUInteger numberOfFinishedOperations = [[operations indexesOfObjectsPassingTest:^BOOL(id op, NSUInteger __unused idx, BOOL __unused *stop) { + return [op isFinished]; + }] count]; + + if (progressBlock) { + progressBlock(numberOfFinishedOperations, [operations count]); + } + + dispatch_group_leave(dispatchGroup); + }); + }; + + dispatch_group_enter(dispatchGroup); + [batchedOperation addDependency:operation]; + } + [self.operationQueue addOperations:operations waitUntilFinished:NO]; + [self.operationQueue addOperation:batchedOperation]; +} + +#pragma mark - + +- (void)getPath:(NSString *)path + parameters:(NSDictionary *)parameters + success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success + failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure +{ + NSURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:parameters]; + AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure]; + [self enqueueHTTPRequestOperation:operation]; +} + +- (void)postPath:(NSString *)path + parameters:(NSDictionary *)parameters + success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success + failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure +{ + NSURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters]; + AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure]; + [self enqueueHTTPRequestOperation:operation]; +} + +- (void)putPath:(NSString *)path + parameters:(NSDictionary *)parameters + success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success + failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure +{ + NSURLRequest *request = [self requestWithMethod:@"PUT" path:path parameters:parameters]; + AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure]; + [self enqueueHTTPRequestOperation:operation]; +} + +- (void)deletePath:(NSString *)path + parameters:(NSDictionary *)parameters + success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success + failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure +{ + NSURLRequest *request = [self requestWithMethod:@"DELETE" path:path parameters:parameters]; + AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure]; + [self enqueueHTTPRequestOperation:operation]; +} + +- (void)patchPath:(NSString *)path + parameters:(NSDictionary *)parameters + success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success + failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure +{ + NSURLRequest *request = [self requestWithMethod:@"PATCH" path:path parameters:parameters]; + AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure]; + [self enqueueHTTPRequestOperation:operation]; +} + +#pragma mark - NSCoding + +- (id)initWithCoder:(NSCoder *)aDecoder { + NSURL *baseURL = [aDecoder decodeObjectForKey:@"baseURL"]; + + self = [self initWithBaseURL:baseURL]; + if (!self) { + return nil; + } + + self.stringEncoding = [aDecoder decodeIntegerForKey:@"stringEncoding"]; + self.parameterEncoding = (AFHTTPClientParameterEncoding) [aDecoder decodeIntegerForKey:@"parameterEncoding"]; + self.registeredHTTPOperationClassNames = [aDecoder decodeObjectForKey:@"registeredHTTPOperationClassNames"]; + self.defaultHeaders = [aDecoder decodeObjectForKey:@"defaultHeaders"]; + + return self; +} + +- (void)encodeWithCoder:(NSCoder *)aCoder { + [aCoder encodeObject:self.baseURL forKey:@"baseURL"]; + [aCoder encodeInteger:(NSInteger)self.stringEncoding forKey:@"stringEncoding"]; + [aCoder encodeInteger:self.parameterEncoding forKey:@"parameterEncoding"]; + [aCoder encodeObject:self.registeredHTTPOperationClassNames forKey:@"registeredHTTPOperationClassNames"]; + [aCoder encodeObject:self.defaultHeaders forKey:@"defaultHeaders"]; +} + +#pragma mark - NSCopying + +- (id)copyWithZone:(NSZone *)zone { + AFHTTPClient *HTTPClient = [[[self class] allocWithZone:zone] initWithBaseURL:self.baseURL]; + + HTTPClient.stringEncoding = self.stringEncoding; + HTTPClient.parameterEncoding = self.parameterEncoding; + HTTPClient.registeredHTTPOperationClassNames = [self.registeredHTTPOperationClassNames mutableCopyWithZone:zone]; + HTTPClient.defaultHeaders = [self.defaultHeaders mutableCopyWithZone:zone]; +#ifdef _SYSTEMCONFIGURATION_H + HTTPClient.networkReachabilityStatusBlock = self.networkReachabilityStatusBlock; +#endif + return HTTPClient; +} + +@end + +#pragma mark - + +static NSString * const kAFMultipartFormBoundary = @"Boundary+0xAbCdEfGbOuNdArY"; + +static NSString * const kAFMultipartFormCRLF = @"\r\n"; + +static NSInteger const kAFStreamToStreamBufferSize = 1024 * 1024; //1 meg default + +static inline NSString * AFMultipartFormInitialBoundary() { + return [NSString stringWithFormat:@"--%@%@", kAFMultipartFormBoundary, kAFMultipartFormCRLF]; +} + +static inline NSString * AFMultipartFormEncapsulationBoundary() { + return [NSString stringWithFormat:@"%@--%@%@", kAFMultipartFormCRLF, kAFMultipartFormBoundary, kAFMultipartFormCRLF]; +} + +static inline NSString * AFMultipartFormFinalBoundary() { + return [NSString stringWithFormat:@"%@--%@--%@", kAFMultipartFormCRLF, kAFMultipartFormBoundary, kAFMultipartFormCRLF]; +} + +static inline NSString * AFContentTypeForPathExtension(NSString *extension) { +#ifdef __UTTYPE__ + NSString *UTI = (__bridge_transfer NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)extension, NULL); + NSString *contentType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)UTI, kUTTagClassMIMEType); + if (!contentType) { + return @"application/octet-stream"; + } else { + return contentType; + } +#else + return @"application/octet-stream"; +#endif +} + +NSUInteger const kAFUploadStream3GSuggestedPacketSize = 1024 * 16; +NSTimeInterval const kAFUploadStream3GSuggestedDelay = 0.2; + +@interface AFHTTPBodyPart : NSObject +@property (nonatomic, assign) NSStringEncoding stringEncoding; +@property (nonatomic, strong) NSDictionary *headers; +@property (nonatomic, strong) id body; +@property (nonatomic, assign) unsigned long long bodyContentLength; +@property (nonatomic, strong) NSInputStream *inputStream; + +@property (nonatomic, assign) BOOL hasInitialBoundary; +@property (nonatomic, assign) BOOL hasFinalBoundary; + +@property (nonatomic, readonly, getter = hasBytesAvailable) BOOL bytesAvailable; +@property (nonatomic, readonly) unsigned long long contentLength; + +- (NSInteger)read:(uint8_t *)buffer + maxLength:(NSUInteger)length; +@end + +@interface AFMultipartBodyStream : NSInputStream +@property (nonatomic, assign) NSUInteger numberOfBytesInPacket; +@property (nonatomic, assign) NSTimeInterval delay; +@property (nonatomic, strong) NSInputStream *inputStream; +@property (nonatomic, readonly) unsigned long long contentLength; +@property (nonatomic, readonly, getter = isEmpty) BOOL empty; + +- (id)initWithStringEncoding:(NSStringEncoding)encoding; +- (void)setInitialAndFinalBoundaries; +- (void)appendHTTPBodyPart:(AFHTTPBodyPart *)bodyPart; +@end + +#pragma mark - + +@interface AFStreamingMultipartFormData () +@property (readwrite, nonatomic, copy) NSMutableURLRequest *request; +@property (readwrite, nonatomic, strong) AFMultipartBodyStream *bodyStream; +@property (readwrite, nonatomic, assign) NSStringEncoding stringEncoding; +@end + +@implementation AFStreamingMultipartFormData +@synthesize request = _request; +@synthesize bodyStream = _bodyStream; +@synthesize stringEncoding = _stringEncoding; + +- (id)initWithURLRequest:(NSMutableURLRequest *)urlRequest + stringEncoding:(NSStringEncoding)encoding +{ + self = [super init]; + if (!self) { + return nil; + } + + self.request = urlRequest; + self.stringEncoding = encoding; + self.bodyStream = [[AFMultipartBodyStream alloc] initWithStringEncoding:encoding]; + + return self; +} + +- (BOOL)appendPartWithFileURL:(NSURL *)fileURL + name:(NSString *)name + error:(NSError * __autoreleasing *)error +{ + NSParameterAssert(fileURL); + NSParameterAssert(name); + + NSString *fileName = [fileURL lastPathComponent]; + NSString *mimeType = AFContentTypeForPathExtension([fileURL pathExtension]); + + return [self appendPartWithFileURL:fileURL name:name fileName:fileName mimeType:mimeType error:error]; +} + +- (BOOL)appendPartWithFileURL:(NSURL *)fileURL + name:(NSString *)name + fileName:(NSString *)fileName + mimeType:(NSString *)mimeType + error:(NSError * __autoreleasing *)error +{ + NSParameterAssert(fileURL); + NSParameterAssert(name); + NSParameterAssert(fileName); + NSParameterAssert(mimeType); + + if (![fileURL isFileURL]) { + NSDictionary *userInfo = [NSDictionary dictionaryWithObject:NSLocalizedStringFromTable(@"Expected URL to be a file URL", @"AFNetworking", nil) forKey:NSLocalizedFailureReasonErrorKey]; + if (error != NULL) { + *error = [[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorBadURL userInfo:userInfo]; + } + + return NO; + } else if ([fileURL checkResourceIsReachableAndReturnError:error] == NO) { + NSDictionary *userInfo = [NSDictionary dictionaryWithObject:NSLocalizedStringFromTable(@"File URL not reachable.", @"AFNetworking", nil) forKey:NSLocalizedFailureReasonErrorKey]; + if (error != NULL) { + *error = [[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorBadURL userInfo:userInfo]; + } + + return NO; + } + + NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary]; + [mutableHeaders setValue:[NSString stringWithFormat:@"form-data; name=\"%@\"; filename=\"%@\"", name, fileName] forKey:@"Content-Disposition"]; + [mutableHeaders setValue:mimeType forKey:@"Content-Type"]; + + AFHTTPBodyPart *bodyPart = [[AFHTTPBodyPart alloc] init]; + bodyPart.stringEncoding = self.stringEncoding; + bodyPart.headers = mutableHeaders; + bodyPart.body = fileURL; + + NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileURL path] error:nil]; + bodyPart.bodyContentLength = [[fileAttributes objectForKey:NSFileSize] unsignedLongLongValue]; + + [self.bodyStream appendHTTPBodyPart:bodyPart]; + + return YES; +} + + +- (void)appendPartWithInputStream:(NSInputStream *)inputStream + name:(NSString *)name + fileName:(NSString *)fileName + length:(unsigned long long)length + mimeType:(NSString *)mimeType +{ + NSParameterAssert(name); + NSParameterAssert(fileName); + NSParameterAssert(mimeType); + + NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary]; + [mutableHeaders setValue:[NSString stringWithFormat:@"form-data; name=\"%@\"; filename=\"%@\"", name, fileName] forKey:@"Content-Disposition"]; + [mutableHeaders setValue:mimeType forKey:@"Content-Type"]; + + + AFHTTPBodyPart *bodyPart = [[AFHTTPBodyPart alloc] init]; + bodyPart.stringEncoding = self.stringEncoding; + bodyPart.headers = mutableHeaders; + bodyPart.body = inputStream; + + bodyPart.bodyContentLength = length; + + [self.bodyStream appendHTTPBodyPart:bodyPart]; +} + +- (void)appendPartWithFileData:(NSData *)data + name:(NSString *)name + fileName:(NSString *)fileName + mimeType:(NSString *)mimeType +{ + NSParameterAssert(name); + NSParameterAssert(fileName); + NSParameterAssert(mimeType); + + NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary]; + [mutableHeaders setValue:[NSString stringWithFormat:@"form-data; name=\"%@\"; filename=\"%@\"", name, fileName] forKey:@"Content-Disposition"]; + [mutableHeaders setValue:mimeType forKey:@"Content-Type"]; + + [self appendPartWithHeaders:mutableHeaders body:data]; +} + +- (void)appendPartWithFormData:(NSData *)data + name:(NSString *)name +{ + NSParameterAssert(name); + + NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary]; + [mutableHeaders setValue:[NSString stringWithFormat:@"form-data; name=\"%@\"", name] forKey:@"Content-Disposition"]; + + [self appendPartWithHeaders:mutableHeaders body:data]; +} + +- (void)appendPartWithHeaders:(NSDictionary *)headers + body:(NSData *)body +{ + NSParameterAssert(body); + + AFHTTPBodyPart *bodyPart = [[AFHTTPBodyPart alloc] init]; + bodyPart.stringEncoding = self.stringEncoding; + bodyPart.headers = headers; + bodyPart.bodyContentLength = [body length]; + bodyPart.body = body; + + [self.bodyStream appendHTTPBodyPart:bodyPart]; +} + +- (void)throttleBandwidthWithPacketSize:(NSUInteger)numberOfBytes + delay:(NSTimeInterval)delay +{ + self.bodyStream.numberOfBytesInPacket = numberOfBytes; + self.bodyStream.delay = delay; +} + +- (NSMutableURLRequest *)requestByFinalizingMultipartFormData { + if ([self.bodyStream isEmpty]) { + return self.request; + } + + // Reset the initial and final boundaries to ensure correct Content-Length + [self.bodyStream setInitialAndFinalBoundaries]; + + [self.request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", kAFMultipartFormBoundary] forHTTPHeaderField:@"Content-Type"]; + [self.request setValue:[NSString stringWithFormat:@"%llu", [self.bodyStream contentLength]] forHTTPHeaderField:@"Content-Length"]; + [self.request setHTTPBodyStream:self.bodyStream]; + + return self.request; +} + +@end + +#pragma mark - + +@interface AFMultipartBodyStream () +@property (nonatomic, assign) NSStreamStatus streamStatus; +@property (nonatomic, strong) NSError *streamError; +@property (nonatomic, assign) NSStringEncoding stringEncoding; +@property (nonatomic, strong) NSMutableArray *HTTPBodyParts; +@property (nonatomic, strong) NSEnumerator *HTTPBodyPartEnumerator; +@property (nonatomic, strong) AFHTTPBodyPart *currentHTTPBodyPart; +@property (nonatomic, strong) NSOutputStream *outputStream; +@property (nonatomic, strong) NSMutableData *buffer; +@end + +@implementation AFMultipartBodyStream +@synthesize streamStatus = _streamStatus; +@synthesize streamError = _streamError; +@synthesize stringEncoding = _stringEncoding; +@synthesize HTTPBodyParts = _HTTPBodyParts; +@synthesize HTTPBodyPartEnumerator = _HTTPBodyPartEnumerator; +@synthesize currentHTTPBodyPart = _currentHTTPBodyPart; +@synthesize inputStream = _inputStream; +@synthesize outputStream = _outputStream; +@synthesize buffer = _buffer; +@synthesize numberOfBytesInPacket = _numberOfBytesInPacket; +@synthesize delay = _delay; + +- (id)initWithStringEncoding:(NSStringEncoding)encoding { + self = [super init]; + if (!self) { + return nil; + } + + self.stringEncoding = encoding; + self.HTTPBodyParts = [NSMutableArray array]; + self.numberOfBytesInPacket = NSIntegerMax; + + return self; +} + +- (void)setInitialAndFinalBoundaries { + if ([self.HTTPBodyParts count] > 0) { + for (AFHTTPBodyPart *bodyPart in self.HTTPBodyParts) { + bodyPart.hasInitialBoundary = NO; + bodyPart.hasFinalBoundary = NO; + } + + [[self.HTTPBodyParts objectAtIndex:0] setHasInitialBoundary:YES]; + [[self.HTTPBodyParts lastObject] setHasFinalBoundary:YES]; + } +} + +- (void)appendHTTPBodyPart:(AFHTTPBodyPart *)bodyPart { + [self.HTTPBodyParts addObject:bodyPart]; +} + +- (BOOL)isEmpty { + return [self.HTTPBodyParts count] == 0; +} + +#pragma mark - NSInputStream + +- (NSInteger)read:(uint8_t *)buffer + maxLength:(NSUInteger)length +{ + if ([self streamStatus] == NSStreamStatusClosed) { + return 0; + } + NSInteger bytesRead = 0; + + while ((NSUInteger)bytesRead < MIN(length, self.numberOfBytesInPacket)) { + if (!self.currentHTTPBodyPart || ![self.currentHTTPBodyPart hasBytesAvailable]) { + if (!(self.currentHTTPBodyPart = [self.HTTPBodyPartEnumerator nextObject])) { + break; + } + } else { + bytesRead += [self.currentHTTPBodyPart read:&buffer[bytesRead] maxLength:(length - (NSUInteger)bytesRead)]; + if (self.delay > 0.0f) { + [NSThread sleepForTimeInterval:self.delay]; + } + } + } + return bytesRead; +} + +- (BOOL)getBuffer:(__unused uint8_t **)buffer + length:(__unused NSUInteger *)len +{ + return NO; +} + +- (BOOL)hasBytesAvailable { + return [self streamStatus] == NSStreamStatusOpen; +} + +#pragma mark - NSStream + +- (void)open { + if (self.streamStatus == NSStreamStatusOpen) { + return; + } + + self.streamStatus = NSStreamStatusOpen; + + [self setInitialAndFinalBoundaries]; + self.HTTPBodyPartEnumerator = [self.HTTPBodyParts objectEnumerator]; +} + +- (void)close { + self.streamStatus = NSStreamStatusClosed; +} + +- (id)propertyForKey:(__unused NSString *)key { + return nil; +} + +- (BOOL)setProperty:(__unused id)property + forKey:(__unused NSString *)key +{ + return NO; +} + +- (void)scheduleInRunLoop:(__unused NSRunLoop *)aRunLoop + forMode:(__unused NSString *)mode +{} + +- (void)removeFromRunLoop:(__unused NSRunLoop *)aRunLoop + forMode:(__unused NSString *)mode +{} + +- (unsigned long long)contentLength { + unsigned long long length = 0; + for (AFHTTPBodyPart *bodyPart in self.HTTPBodyParts) { + length += [bodyPart contentLength]; + } + + return length; +} + +#pragma mark - Undocumented CFReadStream Bridged Methods + +- (void)_scheduleInCFRunLoop:(__unused CFRunLoopRef)aRunLoop + forMode:(__unused CFStringRef)aMode +{} + +- (void)_unscheduleFromCFRunLoop:(__unused CFRunLoopRef)aRunLoop + forMode:(__unused CFStringRef)aMode +{} + +- (BOOL)_setCFClientFlags:(__unused CFOptionFlags)inFlags + callback:(__unused CFReadStreamClientCallBack)inCallback + context:(__unused CFStreamClientContext *)inContext { + return NO; +} + +#pragma mark - NSCopying + +-(id)copyWithZone:(NSZone *)zone { + AFMultipartBodyStream *bodyStreamCopy = [[[self class] allocWithZone:zone] initWithStringEncoding:self.stringEncoding]; + + for (AFHTTPBodyPart *bodyPart in self.HTTPBodyParts) { + [bodyStreamCopy appendHTTPBodyPart:[bodyPart copy]]; + } + + [bodyStreamCopy setInitialAndFinalBoundaries]; + + return bodyStreamCopy; +} + +@end + +#pragma mark - + +typedef enum { + AFEncapsulationBoundaryPhase = 1, + AFHeaderPhase = 2, + AFBodyPhase = 3, + AFFinalBoundaryPhase = 4, +} AFHTTPBodyPartReadPhase; + +@interface AFHTTPBodyPart () { + AFHTTPBodyPartReadPhase _phase; + NSInputStream *_inputStream; + unsigned long long _phaseReadOffset; +} + +- (BOOL)transitionToNextPhase; +- (NSInteger)readData:(NSData *)data + intoBuffer:(uint8_t *)buffer + maxLength:(NSUInteger)length; +@end + +@implementation AFHTTPBodyPart +@synthesize stringEncoding = _stringEncoding; +@synthesize headers = _headers; +@synthesize body = _body; +@synthesize bodyContentLength = _bodyContentLength; +@synthesize inputStream = _inputStream; +@synthesize hasInitialBoundary = _hasInitialBoundary; +@synthesize hasFinalBoundary = _hasFinalBoundary; + +- (id)init { + self = [super init]; + if (!self) { + return nil; + } + + [self transitionToNextPhase]; + + return self; +} + +- (void)dealloc { + if (_inputStream) { + [_inputStream close]; + _inputStream = nil; + } +} + +- (NSInputStream *)inputStream { + if (!_inputStream) { + if ([self.body isKindOfClass:[NSData class]]) { + _inputStream = [NSInputStream inputStreamWithData:self.body]; + } else if ([self.body isKindOfClass:[NSURL class]]) { + _inputStream = [NSInputStream inputStreamWithURL:self.body]; + } else if ([self.body isKindOfClass:[NSInputStream class]]) { + _inputStream = self.body; + } + } + + return _inputStream; +} + +- (NSString *)stringForHeaders { + NSMutableString *headerString = [NSMutableString string]; + for (NSString *field in [self.headers allKeys]) { + [headerString appendString:[NSString stringWithFormat:@"%@: %@%@", field, [self.headers valueForKey:field], kAFMultipartFormCRLF]]; + } + [headerString appendString:kAFMultipartFormCRLF]; + + return [NSString stringWithString:headerString]; +} + +- (unsigned long long)contentLength { + unsigned long long length = 0; + + NSData *encapsulationBoundaryData = [([self hasInitialBoundary] ? AFMultipartFormInitialBoundary() : AFMultipartFormEncapsulationBoundary()) dataUsingEncoding:self.stringEncoding]; + length += [encapsulationBoundaryData length]; + + NSData *headersData = [[self stringForHeaders] dataUsingEncoding:self.stringEncoding]; + length += [headersData length]; + + length += _bodyContentLength; + + NSData *closingBoundaryData = ([self hasFinalBoundary] ? [AFMultipartFormFinalBoundary() dataUsingEncoding:self.stringEncoding] : [NSData data]); + length += [closingBoundaryData length]; + + return length; +} + +- (BOOL)hasBytesAvailable { + // Allows `read:maxLength:` to be called again if `AFMultipartFormFinalBoundary` doesn't fit into the available buffer + if (_phase == AFFinalBoundaryPhase) { + return YES; + } + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wcovered-switch-default" + switch (self.inputStream.streamStatus) { + case NSStreamStatusNotOpen: + case NSStreamStatusOpening: + case NSStreamStatusOpen: + case NSStreamStatusReading: + case NSStreamStatusWriting: + return YES; + case NSStreamStatusAtEnd: + case NSStreamStatusClosed: + case NSStreamStatusError: + default: + return NO; + } +#pragma clang diagnostic pop +} + +- (NSInteger)read:(uint8_t *)buffer + maxLength:(NSUInteger)length +{ + NSInteger bytesRead = 0; + + if (_phase == AFEncapsulationBoundaryPhase) { + NSData *encapsulationBoundaryData = [([self hasInitialBoundary] ? AFMultipartFormInitialBoundary() : AFMultipartFormEncapsulationBoundary()) dataUsingEncoding:self.stringEncoding]; + bytesRead += [self readData:encapsulationBoundaryData intoBuffer:&buffer[bytesRead] maxLength:(length - (NSUInteger)bytesRead)]; + } + + if (_phase == AFHeaderPhase) { + NSData *headersData = [[self stringForHeaders] dataUsingEncoding:self.stringEncoding]; + bytesRead += [self readData:headersData intoBuffer:&buffer[bytesRead] maxLength:(length - (NSUInteger)bytesRead)]; + } + + if (_phase == AFBodyPhase) { + if ([self.inputStream hasBytesAvailable]) { + bytesRead += [self.inputStream read:&buffer[bytesRead] maxLength:(length - (NSUInteger)bytesRead)]; + } + + if (![self.inputStream hasBytesAvailable]) { + [self transitionToNextPhase]; + } + } + + if (_phase == AFFinalBoundaryPhase) { + NSData *closingBoundaryData = ([self hasFinalBoundary] ? [AFMultipartFormFinalBoundary() dataUsingEncoding:self.stringEncoding] : [NSData data]); + bytesRead += [self readData:closingBoundaryData intoBuffer:&buffer[bytesRead] maxLength:(length - (NSUInteger)bytesRead)]; + } + + return bytesRead; +} + +- (NSInteger)readData:(NSData *)data + intoBuffer:(uint8_t *)buffer + maxLength:(NSUInteger)length +{ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wgnu" + NSRange range = NSMakeRange((NSUInteger)_phaseReadOffset, MIN([data length] - ((NSUInteger)_phaseReadOffset), length)); + [data getBytes:buffer range:range]; +#pragma clang diagnostic pop + + _phaseReadOffset += range.length; + + if (((NSUInteger)_phaseReadOffset) >= [data length]) { + [self transitionToNextPhase]; + } + + return (NSInteger)range.length; +} + +- (BOOL)transitionToNextPhase { + if (![[NSThread currentThread] isMainThread]) { + [self performSelectorOnMainThread:@selector(transitionToNextPhase) withObject:nil waitUntilDone:YES]; + return YES; + } + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wcovered-switch-default" + switch (_phase) { + case AFEncapsulationBoundaryPhase: + _phase = AFHeaderPhase; + break; + case AFHeaderPhase: + [self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; + [self.inputStream open]; + _phase = AFBodyPhase; + break; + case AFBodyPhase: + [self.inputStream close]; + _phase = AFFinalBoundaryPhase; + break; + case AFFinalBoundaryPhase: + default: + _phase = AFEncapsulationBoundaryPhase; + break; + } + _phaseReadOffset = 0; +#pragma clang diagnostic pop + + return YES; +} + +#pragma mark - NSCopying + +- (id)copyWithZone:(NSZone *)zone { + AFHTTPBodyPart *bodyPart = [[[self class] allocWithZone:zone] init]; + + bodyPart.stringEncoding = self.stringEncoding; + bodyPart.headers = self.headers; + bodyPart.bodyContentLength = self.bodyContentLength; + bodyPart.body = self.body; + + return bodyPart; +} + +@end diff --git a/AFHTTPRequestOperation.h b/AFHTTPRequestOperation.h new file mode 100644 index 0000000..b40e3d5 --- /dev/null +++ b/AFHTTPRequestOperation.h @@ -0,0 +1,133 @@ +// AFHTTPRequestOperation.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 +#import "AFURLConnectionOperation.h" + +/** + `AFHTTPRequestOperation` is a subclass of `AFURLConnectionOperation` for requests using the HTTP or HTTPS protocols. It encapsulates the concept of acceptable status codes and content types, which determine the success or failure of a request. + */ +@interface AFHTTPRequestOperation : AFURLConnectionOperation + +///---------------------------------------------- +/// @name Getting HTTP URL Connection Information +///---------------------------------------------- + +/** + The last HTTP response received by the operation's connection. + */ +@property (readonly, nonatomic, strong) NSHTTPURLResponse *response; + +///---------------------------------------------------------- +/// @name Managing And Checking For Acceptable HTTP Responses +///---------------------------------------------------------- + +/** + A Boolean value that corresponds to whether the status code of the response is within the specified set of acceptable status codes. Returns `YES` if `acceptableStatusCodes` is `nil`. + */ +@property (nonatomic, readonly) BOOL hasAcceptableStatusCode; + +/** + A Boolean value that corresponds to whether the MIME type of the response is among the specified set of acceptable content types. Returns `YES` if `acceptableContentTypes` is `nil`. + */ +@property (nonatomic, readonly) BOOL hasAcceptableContentType; + +/** + The callback dispatch queue on success. If `NULL` (default), the main queue is used. + */ +@property (nonatomic, assign) dispatch_queue_t successCallbackQueue; + +/** + The callback dispatch queue on failure. If `NULL` (default), the main queue is used. + */ +@property (nonatomic, assign) dispatch_queue_t failureCallbackQueue; + +///------------------------------------------------------------ +/// @name Managing Acceptable HTTP Status Codes & Content Types +///------------------------------------------------------------ + +/** + Returns an `NSIndexSet` object containing the ranges of acceptable HTTP status codes. When non-`nil`, the operation will set the `error` property to an error in `AFErrorDomain`. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html + + By default, this is the range 200 to 299, inclusive. + */ ++ (NSIndexSet *)acceptableStatusCodes; + +/** + Adds status codes to the set of acceptable HTTP status codes returned by `+acceptableStatusCodes` in subsequent calls by this class and its descendants. + + @param statusCodes The status codes to be added to the set of acceptable HTTP status codes + */ ++ (void)addAcceptableStatusCodes:(NSIndexSet *)statusCodes; + +/** + Returns an `NSSet` object containing the acceptable MIME types. When non-`nil`, the operation will set the `error` property to an error in `AFErrorDomain`. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17 + + By default, this is `nil`. + */ ++ (NSSet *)acceptableContentTypes; + +/** + Adds content types to the set of acceptable MIME types returned by `+acceptableContentTypes` in subsequent calls by this class and its descendants. + + @param contentTypes The content types to be added to the set of acceptable MIME types + */ ++ (void)addAcceptableContentTypes:(NSSet *)contentTypes; + + +///----------------------------------------------------- +/// @name Determining Whether A Request Can Be Processed +///----------------------------------------------------- + +/** + A Boolean value determining whether or not the class can process the specified request. For example, `AFJSONRequestOperation` may check to make sure the content type was `application/json` or the URL path extension was `.json`. + + @param urlRequest The request that is determined to be supported or not supported for this class. + */ ++ (BOOL)canProcessRequest:(NSURLRequest *)urlRequest; + +///----------------------------------------------------------- +/// @name Setting Completion Block Success / Failure Callbacks +///----------------------------------------------------------- + +/** + Sets the `completionBlock` property with a block that executes either the specified success or failure block, depending on the state of the request on completion. If `error` returns a value, which can be caused by an unacceptable status code or content type, then `failure` is executed. Otherwise, `success` is executed. + + This method should be overridden in subclasses in order to specify the response object passed into the success block. + + @param success The block to be executed on the completion of a successful request. This block has no return value and takes two arguments: the receiver operation and the object constructed from the response data of the request. + @param failure The block to be executed on the completion of an unsuccessful request. This block has no return value and takes two arguments: the receiver operation and the error that occurred during the request. + */ +- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success + failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure; + +@end + +///---------------- +/// @name Functions +///---------------- + +/** + Returns a set of MIME types detected in an HTTP `Accept` or `Content-Type` header. + */ +extern NSSet * AFContentTypesFromHTTPHeader(NSString *string); + diff --git a/AFHTTPRequestOperation.m b/AFHTTPRequestOperation.m new file mode 100644 index 0000000..be49c21 --- /dev/null +++ b/AFHTTPRequestOperation.m @@ -0,0 +1,323 @@ +// AFHTTPRequestOperation.m +// +// 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 "AFHTTPRequestOperation.h" +#import + +// Workaround for change in imp_implementationWithBlock() with Xcode 4.5 +#if defined(__IPHONE_6_0) || defined(__MAC_10_8) +#define AF_CAST_TO_BLOCK id +#else +#define AF_CAST_TO_BLOCK __bridge void * +#endif + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wstrict-selector-match" + +NSSet * AFContentTypesFromHTTPHeader(NSString *string) { + if (!string) { + return nil; + } + + NSArray *mediaRanges = [string componentsSeparatedByString:@","]; + NSMutableSet *mutableContentTypes = [NSMutableSet setWithCapacity:mediaRanges.count]; + + [mediaRanges enumerateObjectsUsingBlock:^(NSString *mediaRange, __unused NSUInteger idx, __unused BOOL *stop) { + NSRange parametersRange = [mediaRange rangeOfString:@";"]; + if (parametersRange.location != NSNotFound) { + mediaRange = [mediaRange substringToIndex:parametersRange.location]; + } + + mediaRange = [mediaRange stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; + + if (mediaRange.length > 0) { + [mutableContentTypes addObject:mediaRange]; + } + }]; + + return [NSSet setWithSet:mutableContentTypes]; +} + +static void AFGetMediaTypeAndSubtypeWithString(NSString *string, NSString **type, NSString **subtype) { + if (!string) { + return; + } + + NSScanner *scanner = [NSScanner scannerWithString:string]; + [scanner setCharactersToBeSkipped:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; + [scanner scanUpToString:@"/" intoString:type]; + [scanner scanString:@"/" intoString:nil]; + [scanner scanUpToString:@";" intoString:subtype]; +} + +static NSString * AFStringFromIndexSet(NSIndexSet *indexSet) { + NSMutableString *string = [NSMutableString string]; + + NSRange range = NSMakeRange([indexSet firstIndex], 1); + while (range.location != NSNotFound) { + NSUInteger nextIndex = [indexSet indexGreaterThanIndex:range.location]; + while (nextIndex == range.location + range.length) { + range.length++; + nextIndex = [indexSet indexGreaterThanIndex:nextIndex]; + } + + if (string.length) { + [string appendString:@","]; + } + + if (range.length == 1) { + [string appendFormat:@"%lu", (long)range.location]; + } else { + NSUInteger firstIndex = range.location; + NSUInteger lastIndex = firstIndex + range.length - 1; + [string appendFormat:@"%lu-%lu", (long)firstIndex, (long)lastIndex]; + } + + range.location = nextIndex; + range.length = 1; + } + + return string; +} + +static void AFSwizzleClassMethodWithClassAndSelectorUsingBlock(Class klass, SEL selector, id block) { + Method originalMethod = class_getClassMethod(klass, selector); + IMP implementation = imp_implementationWithBlock((AF_CAST_TO_BLOCK)block); + class_replaceMethod(objc_getMetaClass([NSStringFromClass(klass) UTF8String]), selector, implementation, method_getTypeEncoding(originalMethod)); +} + +#pragma mark - + +@interface AFHTTPRequestOperation () +@property (readwrite, nonatomic, strong) NSURLRequest *request; +@property (readwrite, nonatomic, strong) NSHTTPURLResponse *response; +@property (readwrite, nonatomic, strong) NSError *HTTPError; +@end + +@implementation AFHTTPRequestOperation +@synthesize HTTPError = _HTTPError; +@synthesize successCallbackQueue = _successCallbackQueue; +@synthesize failureCallbackQueue = _failureCallbackQueue; +@dynamic request; +@dynamic response; + +- (void)dealloc { + if (_successCallbackQueue) { +#if !OS_OBJECT_USE_OBJC + dispatch_release(_successCallbackQueue); +#endif + _successCallbackQueue = NULL; + } + + if (_failureCallbackQueue) { +#if !OS_OBJECT_USE_OBJC + dispatch_release(_failureCallbackQueue); +#endif + _failureCallbackQueue = NULL; + } +} + +- (NSError *)error { + if (!self.HTTPError && self.response) { + if (![self hasAcceptableStatusCode] || ![self hasAcceptableContentType]) { + NSMutableDictionary *userInfo = [NSMutableDictionary dictionary]; + [userInfo setValue:self.responseString forKey:NSLocalizedRecoverySuggestionErrorKey]; + [userInfo setValue:[self.request URL] forKey:NSURLErrorFailingURLErrorKey]; + [userInfo setValue:self.request forKey:AFNetworkingOperationFailingURLRequestErrorKey]; + [userInfo setValue:self.response forKey:AFNetworkingOperationFailingURLResponseErrorKey]; + + if (![self hasAcceptableStatusCode]) { + NSUInteger statusCode = ([self.response isKindOfClass:[NSHTTPURLResponse class]]) ? (NSUInteger)[self.response statusCode] : 200; + [userInfo setValue:[NSString stringWithFormat:NSLocalizedStringFromTable(@"Expected status code in (%@), got %d", @"AFNetworking", nil), AFStringFromIndexSet([[self class] acceptableStatusCodes]), statusCode] forKey:NSLocalizedDescriptionKey]; + self.HTTPError = [[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorBadServerResponse userInfo:userInfo]; + } else if (![self hasAcceptableContentType]) { + // Don't invalidate content type if there is no content + if ([self.responseData length] > 0) { + [userInfo setValue:[NSString stringWithFormat:NSLocalizedStringFromTable(@"Expected content type %@, got %@", @"AFNetworking", nil), [[self class] acceptableContentTypes], [self.response MIMEType]] forKey:NSLocalizedDescriptionKey]; + self.HTTPError = [[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorCannotDecodeContentData userInfo:userInfo]; + } + } + } + } + + if (self.HTTPError) { + return self.HTTPError; + } else { + return [super error]; + } +} + +- (NSStringEncoding)responseStringEncoding { + // When no explicit charset parameter is provided by the sender, media subtypes of the "text" type are defined to have a default charset value of "ISO-8859-1" when received via HTTP. Data in character sets other than "ISO-8859-1" or its subsets MUST be labeled with an appropriate charset value. + // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.4.1 + if (self.response && !self.response.textEncodingName && self.responseData && [self.response respondsToSelector:@selector(allHeaderFields)]) { + NSString *type = nil; + AFGetMediaTypeAndSubtypeWithString([[self.response allHeaderFields] valueForKey:@"Content-Type"], &type, nil); + + if ([type isEqualToString:@"text"]) { + return NSISOLatin1StringEncoding; + } + } + + return [super responseStringEncoding]; +} + +- (void)pause { + unsigned long long offset = 0; + if ([self.outputStream propertyForKey:NSStreamFileCurrentOffsetKey]) { + offset = [[self.outputStream propertyForKey:NSStreamFileCurrentOffsetKey] unsignedLongLongValue]; + } else { + offset = [[self.outputStream propertyForKey:NSStreamDataWrittenToMemoryStreamKey] length]; + } + + NSMutableURLRequest *mutableURLRequest = [self.request mutableCopy]; + if ([self.response respondsToSelector:@selector(allHeaderFields)] && [[self.response allHeaderFields] valueForKey:@"ETag"]) { + [mutableURLRequest setValue:[[self.response allHeaderFields] valueForKey:@"ETag"] forHTTPHeaderField:@"If-Range"]; + } + [mutableURLRequest setValue:[NSString stringWithFormat:@"bytes=%llu-", offset] forHTTPHeaderField:@"Range"]; + self.request = mutableURLRequest; + + [super pause]; +} + +- (BOOL)hasAcceptableStatusCode { + if (!self.response) { + return NO; + } + + NSUInteger statusCode = ([self.response isKindOfClass:[NSHTTPURLResponse class]]) ? (NSUInteger)[self.response statusCode] : 200; + return ![[self class] acceptableStatusCodes] || [[[self class] acceptableStatusCodes] containsIndex:statusCode]; +} + +- (BOOL)hasAcceptableContentType { + if (!self.response) { + return NO; + } + + // Any HTTP/1.1 message containing an entity-body SHOULD include a Content-Type header field defining the media type of that body. If and only if the media type is not given by a Content-Type field, the recipient MAY attempt to guess the media type via inspection of its content and/or the name extension(s) of the URI used to identify the resource. If the media type remains unknown, the recipient SHOULD treat it as type "application/octet-stream". + // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec7.html + NSString *contentType = [self.response MIMEType]; + if (!contentType) { + contentType = @"application/octet-stream"; + } + + return ![[self class] acceptableContentTypes] || [[[self class] acceptableContentTypes] containsObject:contentType]; +} + +- (void)setSuccessCallbackQueue:(dispatch_queue_t)successCallbackQueue { + if (successCallbackQueue != _successCallbackQueue) { + if (_successCallbackQueue) { +#if !OS_OBJECT_USE_OBJC + dispatch_release(_successCallbackQueue); +#endif + _successCallbackQueue = NULL; + } + + if (successCallbackQueue) { +#if !OS_OBJECT_USE_OBJC + dispatch_retain(successCallbackQueue); +#endif + _successCallbackQueue = successCallbackQueue; + } + } +} + +- (void)setFailureCallbackQueue:(dispatch_queue_t)failureCallbackQueue { + if (failureCallbackQueue != _failureCallbackQueue) { + if (_failureCallbackQueue) { +#if !OS_OBJECT_USE_OBJC + dispatch_release(_failureCallbackQueue); +#endif + _failureCallbackQueue = NULL; + } + + if (failureCallbackQueue) { +#if !OS_OBJECT_USE_OBJC + dispatch_retain(failureCallbackQueue); +#endif + _failureCallbackQueue = failureCallbackQueue; + } + } +} + +- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success + failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure +{ + // completionBlock is manually nilled out in AFURLConnectionOperation to break the retain cycle. +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Warc-retain-cycles" +#pragma clang diagnostic ignored "-Wgnu" + self.completionBlock = ^{ + if (self.error) { + if (failure) { + dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{ + failure(self, self.error); + }); + } + } else { + if (success) { + dispatch_async(self.successCallbackQueue ?: dispatch_get_main_queue(), ^{ + success(self, self.responseData); + }); + } + } + }; +#pragma clang diagnostic pop +} + +#pragma mark - AFHTTPRequestOperation + ++ (NSIndexSet *)acceptableStatusCodes { + return [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 100)]; +} + ++ (void)addAcceptableStatusCodes:(NSIndexSet *)statusCodes { + NSMutableIndexSet *mutableStatusCodes = [[NSMutableIndexSet alloc] initWithIndexSet:[self acceptableStatusCodes]]; + [mutableStatusCodes addIndexes:statusCodes]; + AFSwizzleClassMethodWithClassAndSelectorUsingBlock([self class], @selector(acceptableStatusCodes), ^(__unused id _self) { + return mutableStatusCodes; + }); +} + ++ (NSSet *)acceptableContentTypes { + return nil; +} + ++ (void)addAcceptableContentTypes:(NSSet *)contentTypes { + NSMutableSet *mutableContentTypes = [[NSMutableSet alloc] initWithSet:[self acceptableContentTypes] copyItems:YES]; + [mutableContentTypes unionSet:contentTypes]; + AFSwizzleClassMethodWithClassAndSelectorUsingBlock([self class], @selector(acceptableContentTypes), ^(__unused id _self) { + return mutableContentTypes; + }); +} + ++ (BOOL)canProcessRequest:(NSURLRequest *)request { + if ([[self class] isEqual:[AFHTTPRequestOperation class]]) { + return YES; + } + + return [[self acceptableContentTypes] intersectsSet:AFContentTypesFromHTTPHeader([request valueForHTTPHeaderField:@"Accept"])]; +} + +@end + +#pragma clang diagnostic pop diff --git a/AFImageRequestOperation.h b/AFImageRequestOperation.h new file mode 100644 index 0000000..d5e6596 --- /dev/null +++ b/AFImageRequestOperation.h @@ -0,0 +1,113 @@ +// AFImageRequestOperation.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 +#import "AFHTTPRequestOperation.h" + +#import + +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +#import +#elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED) +#import +#endif + +/** + `AFImageRequestOperation` is a subclass of `AFHTTPRequestOperation` for downloading and processing images. + + ## Acceptable Content Types + + By default, `AFImageRequestOperation` accepts the following MIME types, which correspond to the image formats supported by UIImage or NSImage: + + - `image/tiff` + - `image/jpeg` + - `image/gif` + - `image/png` + - `image/ico` + - `image/x-icon` + - `image/bmp` + - `image/x-bmp` + - `image/x-xbitmap` + - `image/x-win-bitmap` + */ +@interface AFImageRequestOperation : AFHTTPRequestOperation + +/** + An image constructed from the response data. If an error occurs during the request, `nil` will be returned, and the `error` property will be set to the error. + */ +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +@property (readonly, nonatomic, strong) UIImage *responseImage; +#elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED) +@property (readonly, nonatomic, strong) NSImage *responseImage; +#endif + +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +/** + The scale factor used when interpreting the image data to construct `responseImage`. Specifying a scale factor of 1.0 results in an image whose size matches the pixel-based dimensions of the image. Applying a different scale factor changes the size of the image as reported by the size property. This is set to the value of scale of the main screen by default, which automatically scales images for retina displays, for instance. + */ +@property (nonatomic, assign) CGFloat imageScale; + +/** + Whether to automatically inflate response image data for compressed formats (such as PNG or JPEG). Enabling this can significantly improve drawing performance on iOS when used with `setCompletionBlockWithSuccess:failure:`, as it allows a bitmap representation to be constructed in the background rather than on the main thread. `YES` by default. + */ +@property (nonatomic, assign) BOOL automaticallyInflatesResponseImage; +#endif + +/** + Creates and returns an `AFImageRequestOperation` object and sets the specified success callback. + + @param urlRequest The request object to be loaded asynchronously during execution of the operation. + @param success A block object to be executed when the request finishes successfully. This block has no return value and takes a single argument, the image created from the response data of the request. + + @return A new image request operation + */ +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) ++ (instancetype)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest + success:(void (^)(UIImage *image))success; +#elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED) ++ (instancetype)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest + success:(void (^)(NSImage *image))success; +#endif + +/** + Creates and returns an `AFImageRequestOperation` object and sets the specified success callback. + + @param urlRequest The request object to be loaded asynchronously during execution of the operation. + @param imageProcessingBlock A block object to be executed after the image request finishes successfully, but before the image is returned in the `success` block. This block takes a single argument, the image loaded from the response body, and returns the processed image. + @param success A block object to be executed when the request finishes successfully, with a status code in the 2xx range, and with an acceptable content type (e.g. `image/png`). This block has no return value and takes three arguments: the request object of the operation, the response for the request, and the image created from the response data. + @param failure A block object to be executed when the request finishes unsuccessfully. This block has no return value and takes three arguments: the request object of the operation, the response for the request, and the error associated with the cause for the unsuccessful operation. + + @return A new image request operation + */ +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) ++ (instancetype)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest + imageProcessingBlock:(UIImage *(^)(UIImage *image))imageProcessingBlock + success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success + failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure; +#elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED) ++ (instancetype)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest + imageProcessingBlock:(NSImage *(^)(NSImage *image))imageProcessingBlock + success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSImage *image))success + failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure; +#endif + +@end diff --git a/AFImageRequestOperation.m b/AFImageRequestOperation.m new file mode 100644 index 0000000..9fedbfa --- /dev/null +++ b/AFImageRequestOperation.m @@ -0,0 +1,325 @@ +// AFImageRequestOperation.m +// +// 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 "AFImageRequestOperation.h" + +static dispatch_queue_t image_request_operation_processing_queue() { + static dispatch_queue_t af_image_request_operation_processing_queue; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + af_image_request_operation_processing_queue = dispatch_queue_create("com.alamofire.networking.image-request.processing", DISPATCH_QUEUE_CONCURRENT); + }); + + return af_image_request_operation_processing_queue; +} + +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +#import + +static UIImage * AFImageWithDataAtScale(NSData *data, CGFloat scale) { + if ([UIImage instancesRespondToSelector:@selector(initWithData:scale:)]) { + return [[UIImage alloc] initWithData:data scale:scale]; + } else { + UIImage *image = [[UIImage alloc] initWithData:data]; + return [[UIImage alloc] initWithCGImage:[image CGImage] scale:scale orientation:image.imageOrientation]; + } +} + +static UIImage * AFInflatedImageFromResponseWithDataAtScale(NSHTTPURLResponse *response, NSData *data, CGFloat scale) { + if (!data || [data length] == 0) { + return nil; + } + + CGImageRef imageRef = nil; + CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data); + + if ([response.MIMEType isEqualToString:@"image/png"]) { + imageRef = CGImageCreateWithPNGDataProvider(dataProvider, NULL, true, kCGRenderingIntentDefault); + } else if ([response.MIMEType isEqualToString:@"image/jpeg"]) { + imageRef = CGImageCreateWithJPEGDataProvider(dataProvider, NULL, true, kCGRenderingIntentDefault); + } + + if (!imageRef) { + UIImage *image = AFImageWithDataAtScale(data, scale); + if (image.images) { + CGDataProviderRelease(dataProvider); + + return image; + } + + imageRef = CGImageCreateCopy([image CGImage]); + } + + CGDataProviderRelease(dataProvider); + + if (!imageRef) { + return nil; + } + + size_t width = CGImageGetWidth(imageRef); + size_t height = CGImageGetHeight(imageRef); + size_t bitsPerComponent = CGImageGetBitsPerComponent(imageRef); + size_t bytesPerRow = 0; // CGImageGetBytesPerRow() calculates incorrectly in iOS 5.0, so defer to CGBitmapContextCreate() + CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); + CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); + + if (CGColorSpaceGetNumberOfComponents(colorSpace) == 3) { + int alpha = (bitmapInfo & kCGBitmapAlphaInfoMask); + if (alpha == kCGImageAlphaNone) { + bitmapInfo &= ~kCGBitmapAlphaInfoMask; + bitmapInfo |= kCGImageAlphaNoneSkipFirst; + } else if (!(alpha == kCGImageAlphaNoneSkipFirst || alpha == kCGImageAlphaNoneSkipLast)) { + bitmapInfo &= ~kCGBitmapAlphaInfoMask; + bitmapInfo |= kCGImageAlphaPremultipliedFirst; + } + } + + CGContextRef context = CGBitmapContextCreate(NULL, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo); + + CGColorSpaceRelease(colorSpace); + + if (!context) { + CGImageRelease(imageRef); + + return [[UIImage alloc] initWithData:data]; + } + + CGRect rect = CGRectMake(0.0f, 0.0f, width, height); + CGContextDrawImage(context, rect, imageRef); + CGImageRef inflatedImageRef = CGBitmapContextCreateImage(context); + CGContextRelease(context); + + UIImage *inflatedImage = [[UIImage alloc] initWithCGImage:inflatedImageRef scale:scale orientation:UIImageOrientationUp]; + CGImageRelease(inflatedImageRef); + CGImageRelease(imageRef); + + return inflatedImage; +} +#endif + +@interface AFImageRequestOperation () +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +@property (readwrite, nonatomic, strong) UIImage *responseImage; +#elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED) +@property (readwrite, nonatomic, strong) NSImage *responseImage; +#endif +@end + +@implementation AFImageRequestOperation +@synthesize responseImage = _responseImage; +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +@synthesize imageScale = _imageScale; +#endif + +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) ++ (instancetype)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest + success:(void (^)(UIImage *image))success +{ + return [self imageRequestOperationWithRequest:urlRequest imageProcessingBlock:nil success:^(NSURLRequest __unused *request, NSHTTPURLResponse __unused *response, UIImage *image) { + if (success) { + success(image); + } + } failure:nil]; +} +#elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED) ++ (instancetype)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest + success:(void (^)(NSImage *image))success +{ + return [self imageRequestOperationWithRequest:urlRequest imageProcessingBlock:nil success:^(NSURLRequest __unused *request, NSHTTPURLResponse __unused *response, NSImage *image) { + if (success) { + success(image); + } + } failure:nil]; +} +#endif + + +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) ++ (instancetype)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest + imageProcessingBlock:(UIImage *(^)(UIImage *))imageProcessingBlock + success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success + failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure +{ + AFImageRequestOperation *requestOperation = [(AFImageRequestOperation *)[self alloc] initWithRequest:urlRequest]; + [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { + if (success) { + UIImage *image = responseObject; + if (imageProcessingBlock) { + dispatch_async(image_request_operation_processing_queue(), ^(void) { + UIImage *processedImage = imageProcessingBlock(image); +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wgnu" + dispatch_async(operation.successCallbackQueue ?: dispatch_get_main_queue(), ^(void) { + success(operation.request, operation.response, processedImage); + }); +#pragma clang diagnostic pop + }); + } else { + success(operation.request, operation.response, image); + } + } + } failure:^(AFHTTPRequestOperation *operation, NSError *error) { + if (failure) { + failure(operation.request, operation.response, error); + } + }]; + + + return requestOperation; +} +#elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED) ++ (instancetype)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest + imageProcessingBlock:(NSImage *(^)(NSImage *))imageProcessingBlock + success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSImage *image))success + failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure +{ + AFImageRequestOperation *requestOperation = [(AFImageRequestOperation *)[self alloc] initWithRequest:urlRequest]; + [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { + if (success) { + NSImage *image = responseObject; + if (imageProcessingBlock) { + dispatch_async(image_request_operation_processing_queue(), ^(void) { + NSImage *processedImage = imageProcessingBlock(image); + + dispatch_async(operation.successCallbackQueue ?: dispatch_get_main_queue(), ^(void) { + success(operation.request, operation.response, processedImage); + }); + }); + } else { + success(operation.request, operation.response, image); + } + } + } failure:^(AFHTTPRequestOperation *operation, NSError *error) { + if (failure) { + failure(operation.request, operation.response, error); + } + }]; + + return requestOperation; +} +#endif + +- (id)initWithRequest:(NSURLRequest *)urlRequest { + self = [super initWithRequest:urlRequest]; + if (!self) { + return nil; + } + +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) + self.imageScale = [[UIScreen mainScreen] scale]; + self.automaticallyInflatesResponseImage = YES; +#endif + + return self; +} + + +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +- (UIImage *)responseImage { + if (!_responseImage && [self.responseData length] > 0 && [self isFinished]) { + if (self.automaticallyInflatesResponseImage) { + self.responseImage = AFInflatedImageFromResponseWithDataAtScale(self.response, self.responseData, self.imageScale); + } else { + self.responseImage = AFImageWithDataAtScale(self.responseData, self.imageScale); + } + } + + return _responseImage; +} + +- (void)setImageScale:(CGFloat)imageScale { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wfloat-equal" + if (imageScale == _imageScale) { + return; + } +#pragma clang diagnostic pop + + _imageScale = imageScale; + + self.responseImage = nil; +} +#elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED) +- (NSImage *)responseImage { + if (!_responseImage && [self.responseData length] > 0 && [self isFinished]) { + // Ensure that the image is set to it's correct pixel width and height + NSBitmapImageRep *bitimage = [[NSBitmapImageRep alloc] initWithData:self.responseData]; + self.responseImage = [[NSImage alloc] initWithSize:NSMakeSize([bitimage pixelsWide], [bitimage pixelsHigh])]; + [self.responseImage addRepresentation:bitimage]; + } + + return _responseImage; +} +#endif + +#pragma mark - AFHTTPRequestOperation + ++ (NSSet *)acceptableContentTypes { + return [NSSet setWithObjects:@"image/tiff", @"image/jpeg", @"image/gif", @"image/png", @"image/ico", @"image/x-icon", @"image/bmp", @"image/x-bmp", @"image/x-xbitmap", @"image/x-win-bitmap", nil]; +} + ++ (BOOL)canProcessRequest:(NSURLRequest *)request { + static NSSet * _acceptablePathExtension = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + _acceptablePathExtension = [[NSSet alloc] initWithObjects:@"tif", @"tiff", @"jpg", @"jpeg", @"gif", @"png", @"ico", @"bmp", @"cur", nil]; + }); + + return [_acceptablePathExtension containsObject:[[request URL] pathExtension]] || [super canProcessRequest:request]; +} + +- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success + failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure +{ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Warc-retain-cycles" +#pragma clang diagnostic ignored "-Wgnu" + + self.completionBlock = ^ { + dispatch_async(image_request_operation_processing_queue(), ^(void) { + if (self.error) { + if (failure) { + dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{ + failure(self, self.error); + }); + } + } else { + if (success) { +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) + UIImage *image = nil; +#elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED) + NSImage *image = nil; +#endif + + image = self.responseImage; + + dispatch_async(self.successCallbackQueue ?: dispatch_get_main_queue(), ^{ + success(self, image); + }); + } + } + }); + }; +#pragma clang diagnostic pop +} + +@end diff --git a/AFJSONRequestOperation.h b/AFJSONRequestOperation.h new file mode 100644 index 0000000..5493a40 --- /dev/null +++ b/AFJSONRequestOperation.h @@ -0,0 +1,71 @@ +// AFJSONRequestOperation.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 +#import "AFHTTPRequestOperation.h" + +/** + `AFJSONRequestOperation` is a subclass of `AFHTTPRequestOperation` for downloading and working with JSON response data. + + ## Acceptable Content Types + + By default, `AFJSONRequestOperation` accepts the following MIME types, which includes the official standard, `application/json`, as well as other commonly-used types: + + - `application/json` + - `text/json` + + @warning JSON parsing will use the built-in `NSJSONSerialization` class. + */ +@interface AFJSONRequestOperation : AFHTTPRequestOperation + +///---------------------------- +/// @name Getting Response Data +///---------------------------- + +/** + A JSON object constructed from the response data. If an error occurs while parsing, `nil` will be returned, and the `error` property will be set to the error. + */ +@property (readonly, nonatomic, strong) id responseJSON; + +/** + Options for reading the response JSON data and creating the Foundation objects. For possible values, see the `NSJSONSerialization` documentation section "NSJSONReadingOptions". + */ +@property (nonatomic, assign) NSJSONReadingOptions JSONReadingOptions; + +///---------------------------------- +/// @name Creating Request Operations +///---------------------------------- + +/** + Creates and returns an `AFJSONRequestOperation` object and sets the specified success and failure callbacks. + + @param urlRequest The request object to be loaded asynchronously during execution of the operation + @param success A block object to be executed when the operation finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the JSON object created from the response data of request. + @param failure A block object to be executed when the operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data as JSON. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the error describing the network or parsing error that occurred. + + @return A new JSON request operation + */ ++ (instancetype)JSONRequestOperationWithRequest:(NSURLRequest *)urlRequest + success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON))success + failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON))failure; + +@end diff --git a/AFJSONRequestOperation.m b/AFJSONRequestOperation.m new file mode 100644 index 0000000..fffc60c --- /dev/null +++ b/AFJSONRequestOperation.m @@ -0,0 +1,150 @@ +// AFJSONRequestOperation.m +// +// 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 "AFJSONRequestOperation.h" + +static dispatch_queue_t json_request_operation_processing_queue() { + static dispatch_queue_t af_json_request_operation_processing_queue; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + af_json_request_operation_processing_queue = dispatch_queue_create("com.alamofire.networking.json-request.processing", DISPATCH_QUEUE_CONCURRENT); + }); + + return af_json_request_operation_processing_queue; +} + +@interface AFJSONRequestOperation () +@property (readwrite, nonatomic, strong) id responseJSON; +@property (readwrite, nonatomic, strong) NSError *JSONError; +@property (readwrite, nonatomic, strong) NSRecursiveLock *lock; +@end + +@implementation AFJSONRequestOperation +@synthesize responseJSON = _responseJSON; +@synthesize JSONReadingOptions = _JSONReadingOptions; +@synthesize JSONError = _JSONError; +@dynamic lock; + ++ (instancetype)JSONRequestOperationWithRequest:(NSURLRequest *)urlRequest + success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON))success + failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON))failure +{ + AFJSONRequestOperation *requestOperation = [(AFJSONRequestOperation *)[self alloc] initWithRequest:urlRequest]; + [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { + if (success) { + success(operation.request, operation.response, responseObject); + } + } failure:^(AFHTTPRequestOperation *operation, NSError *error) { + if (failure) { + failure(operation.request, operation.response, error, [(AFJSONRequestOperation *)operation responseJSON]); + } + }]; + + return requestOperation; +} + + +- (id)responseJSON { + [self.lock lock]; + if (!_responseJSON && [self.responseData length] > 0 && [self isFinished] && !self.JSONError) { + NSError *error = nil; + + // Workaround for behavior of Rails to return a single space for `head :ok` (a workaround for a bug in Safari), which is not interpreted as valid input by NSJSONSerialization. + // See https://github.com/rails/rails/issues/1742 + if (self.responseString && ![self.responseString isEqualToString:@" "]) { + // Workaround for a bug in NSJSONSerialization when Unicode character escape codes are used instead of the actual character + // See http://stackoverflow.com/a/12843465/157142 + NSData *data = [self.responseString dataUsingEncoding:NSUTF8StringEncoding]; + + if (data) { + self.responseJSON = [NSJSONSerialization JSONObjectWithData:data options:self.JSONReadingOptions error:&error]; + } else { + NSMutableDictionary *userInfo = [NSMutableDictionary dictionary]; + [userInfo setValue:@"Operation responseData failed decoding as a UTF-8 string" forKey:NSLocalizedDescriptionKey]; + [userInfo setValue:[NSString stringWithFormat:@"Could not decode string: %@", self.responseString] forKey:NSLocalizedFailureReasonErrorKey]; + error = [[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorCannotDecodeContentData userInfo:userInfo]; + } + } + + self.JSONError = error; + } + [self.lock unlock]; + + return _responseJSON; +} + +- (NSError *)error { + if (_JSONError) { + return _JSONError; + } else { + return [super error]; + } +} + +#pragma mark - AFHTTPRequestOperation + ++ (NSSet *)acceptableContentTypes { + return [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil]; +} + ++ (BOOL)canProcessRequest:(NSURLRequest *)request { + return [[[request URL] pathExtension] isEqualToString:@"json"] || [super canProcessRequest:request]; +} + +- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success + failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure +{ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Warc-retain-cycles" +#pragma clang diagnostic ignored "-Wgnu" + + self.completionBlock = ^ { + if (self.error) { + if (failure) { + dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{ + failure(self, self.error); + }); + } + } else { + dispatch_async(json_request_operation_processing_queue(), ^{ + id JSON = self.responseJSON; + + if (self.error) { + if (failure) { + dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{ + failure(self, self.error); + }); + } + } else { + if (success) { + dispatch_async(self.successCallbackQueue ?: dispatch_get_main_queue(), ^{ + success(self, JSON); + }); + } + } + }); + } + }; +#pragma clang diagnostic pop +} + +@end diff --git a/AFNetworkActivityIndicatorManager.h b/AFNetworkActivityIndicatorManager.h new file mode 100644 index 0000000..714193b --- /dev/null +++ b/AFNetworkActivityIndicatorManager.h @@ -0,0 +1,75 @@ +// AFNetworkActivityIndicatorManager.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 + +#import + +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +#import + +/** + `AFNetworkActivityIndicatorManager` manages the state of the network activity indicator in the status bar. When enabled, it will listen for notifications indicating that a network request operation has started or finished, and start or stop animating the indicator accordingly. The number of active requests is incremented and decremented much like a stack or a semaphore, and the activity indicator will animate so long as that number is greater than zero. + + You should enable the shared instance of `AFNetworkActivityIndicatorManager` when your application finishes launching. In `AppDelegate application:didFinishLaunchingWithOptions:` you can do so with the following code: + + [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]; + + By setting `isNetworkActivityIndicatorVisible` to `YES` for `sharedManager`, the network activity indicator will show and hide automatically as requests start and finish. You should not ever need to call `incrementActivityCount` or `decrementActivityCount` yourself. + + See the Apple Human Interface Guidelines section about the Network Activity Indicator for more information: + http://developer.apple.com/library/iOS/#documentation/UserExperience/Conceptual/MobileHIG/UIElementGuidelines/UIElementGuidelines.html#//apple_ref/doc/uid/TP40006556-CH13-SW44 + */ +@interface AFNetworkActivityIndicatorManager : NSObject + +/** + A Boolean value indicating whether the manager is enabled. + + If YES, the manager will change status bar network activity indicator according to network operation notifications it receives. The default value is NO. + */ +@property (nonatomic, assign, getter = isEnabled) BOOL enabled; + +/** + A Boolean value indicating whether the network activity indicator is currently displayed in the status bar. + */ +@property (readonly, nonatomic, assign) BOOL isNetworkActivityIndicatorVisible; + +/** + Returns the shared network activity indicator manager object for the system. + + @return The systemwide network activity indicator manager. + */ ++ (instancetype)sharedManager; + +/** + Increments the number of active network requests. If this number was zero before incrementing, this will start animating the status bar network activity indicator. + */ +- (void)incrementActivityCount; + +/** + Decrements the number of active network requests. If this number becomes zero before decrementing, this will stop animating the status bar network activity indicator. + */ +- (void)decrementActivityCount; + +@end + +#endif diff --git a/AFNetworkActivityIndicatorManager.m b/AFNetworkActivityIndicatorManager.m new file mode 100644 index 0000000..68cbd33 --- /dev/null +++ b/AFNetworkActivityIndicatorManager.m @@ -0,0 +1,157 @@ +// AFNetworkActivityIndicatorManager.m +// +// 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 "AFNetworkActivityIndicatorManager.h" + +#import "AFHTTPRequestOperation.h" + +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +static NSTimeInterval const kAFNetworkActivityIndicatorInvisibilityDelay = 0.17; + +@interface AFNetworkActivityIndicatorManager () +@property (readwrite, nonatomic, assign) NSInteger activityCount; +@property (readwrite, nonatomic, strong) NSTimer *activityIndicatorVisibilityTimer; +@property (readonly, nonatomic, getter = isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible; + +- (void)updateNetworkActivityIndicatorVisibility; +- (void)updateNetworkActivityIndicatorVisibilityDelayed; +@end + +@implementation AFNetworkActivityIndicatorManager +@synthesize activityCount = _activityCount; +@synthesize activityIndicatorVisibilityTimer = _activityIndicatorVisibilityTimer; +@synthesize enabled = _enabled; +@dynamic networkActivityIndicatorVisible; + ++ (instancetype)sharedManager { + static AFNetworkActivityIndicatorManager *_sharedManager = nil; + static dispatch_once_t oncePredicate; + dispatch_once(&oncePredicate, ^{ + _sharedManager = [[self alloc] init]; + }); + + return _sharedManager; +} + ++ (NSSet *)keyPathsForValuesAffectingIsNetworkActivityIndicatorVisible { + return [NSSet setWithObject:@"activityCount"]; +} + +- (id)init { + self = [super init]; + if (!self) { + return nil; + } + + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkingOperationDidStart:) name:AFNetworkingOperationDidStartNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkingOperationDidFinish:) name:AFNetworkingOperationDidFinishNotification object:nil]; + + return self; +} + +- (void)dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:self]; + + [_activityIndicatorVisibilityTimer invalidate]; + +} + +- (void)updateNetworkActivityIndicatorVisibilityDelayed { + if (self.enabled) { + // Delay hiding of activity indicator for a short interval, to avoid flickering + if (![self isNetworkActivityIndicatorVisible]) { + [self.activityIndicatorVisibilityTimer invalidate]; + self.activityIndicatorVisibilityTimer = [NSTimer timerWithTimeInterval:kAFNetworkActivityIndicatorInvisibilityDelay target:self selector:@selector(updateNetworkActivityIndicatorVisibility) userInfo:nil repeats:NO]; + [[NSRunLoop mainRunLoop] addTimer:self.activityIndicatorVisibilityTimer forMode:NSRunLoopCommonModes]; + } else { + [self performSelectorOnMainThread:@selector(updateNetworkActivityIndicatorVisibility) withObject:nil waitUntilDone:NO modes:[NSArray arrayWithObject:NSRunLoopCommonModes]]; + } + } +} + +- (BOOL)isNetworkActivityIndicatorVisible { + return _activityCount > 0; +} + +- (void)updateNetworkActivityIndicatorVisibility { + [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:[self isNetworkActivityIndicatorVisible]]; +} + +// Not exposed, but used if activityCount is set via KVC. +- (NSInteger)activityCount { + return _activityCount; +} + +- (void)setActivityCount:(NSInteger)activityCount { + @synchronized(self) { + _activityCount = activityCount; + } + + dispatch_async(dispatch_get_main_queue(), ^{ + [self updateNetworkActivityIndicatorVisibilityDelayed]; + }); +} + +- (void)incrementActivityCount { + [self willChangeValueForKey:@"activityCount"]; + @synchronized(self) { + _activityCount++; + } + [self didChangeValueForKey:@"activityCount"]; + + dispatch_async(dispatch_get_main_queue(), ^{ + [self updateNetworkActivityIndicatorVisibilityDelayed]; + }); +} + +- (void)decrementActivityCount { + [self willChangeValueForKey:@"activityCount"]; + @synchronized(self) { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wgnu" + _activityCount = MAX(_activityCount - 1, 0); +#pragma clang diagnostic pop + } + [self didChangeValueForKey:@"activityCount"]; + + dispatch_async(dispatch_get_main_queue(), ^{ + [self updateNetworkActivityIndicatorVisibilityDelayed]; + }); +} + +- (void)networkingOperationDidStart:(NSNotification *)notification { + AFURLConnectionOperation *connectionOperation = [notification object]; + if (connectionOperation.request.URL) { + [self incrementActivityCount]; + } +} + +- (void)networkingOperationDidFinish:(NSNotification *)notification { + AFURLConnectionOperation *connectionOperation = [notification object]; + if (connectionOperation.request.URL) { + [self decrementActivityCount]; + } +} + +@end + +#endif diff --git a/AFNetworking.h b/AFNetworking.h new file mode 100644 index 0000000..b8f840b --- /dev/null +++ b/AFNetworking.h @@ -0,0 +1,43 @@ +// 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 +#import + +#ifndef _AFNETWORKING_ + #define _AFNETWORKING_ + + #import "AFURLConnectionOperation.h" + + #import "AFHTTPRequestOperation.h" + #import "AFJSONRequestOperation.h" + #import "AFXMLRequestOperation.h" + #import "AFPropertyListRequestOperation.h" + #import "AFHTTPClient.h" + + #import "AFImageRequestOperation.h" + + #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) + #import "AFNetworkActivityIndicatorManager.h" + #import "UIImageView+AFNetworking.h" + #endif +#endif /* _AFNETWORKING_ */ diff --git a/AFPropertyListRequestOperation.h b/AFPropertyListRequestOperation.h new file mode 100644 index 0000000..9ebb605 --- /dev/null +++ b/AFPropertyListRequestOperation.h @@ -0,0 +1,68 @@ +// AFPropertyListRequestOperation.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 +#import "AFHTTPRequestOperation.h" + +/** + `AFPropertyListRequestOperation` is a subclass of `AFHTTPRequestOperation` for downloading and deserializing objects with property list (plist) response data. + + ## Acceptable Content Types + + By default, `AFPropertyListRequestOperation` accepts the following MIME types: + + - `application/x-plist` + */ +@interface AFPropertyListRequestOperation : AFHTTPRequestOperation + +///---------------------------- +/// @name Getting Response Data +///---------------------------- + +/** + An object deserialized from a plist constructed using the response data. + */ +@property (readonly, nonatomic) id responsePropertyList; + +///-------------------------------------- +/// @name Managing Property List Behavior +///-------------------------------------- + +/** + One of the `NSPropertyListMutabilityOptions` options, specifying the mutability of objects deserialized from the property list. By default, this is `NSPropertyListImmutable`. + */ +@property (nonatomic, assign) NSPropertyListReadOptions propertyListReadOptions; + +/** + Creates and returns an `AFPropertyListRequestOperation` object and sets the specified success and failure callbacks. + + @param urlRequest The request object to be loaded asynchronously during execution of the operation + @param success A block object to be executed when the operation finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the object deserialized from a plist constructed using the response data. + @param failure A block object to be executed when the operation finishes unsuccessfully, or that finishes successfully, but encountered an error while deserializing the object from a property list. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the error describing the network or parsing error that occurred. + + @return A new property list request operation + */ ++ (instancetype)propertyListRequestOperationWithRequest:(NSURLRequest *)urlRequest + success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id propertyList))success + failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id propertyList))failure; + +@end diff --git a/AFPropertyListRequestOperation.m b/AFPropertyListRequestOperation.m new file mode 100644 index 0000000..370e12b --- /dev/null +++ b/AFPropertyListRequestOperation.m @@ -0,0 +1,143 @@ +// AFPropertyListRequestOperation.m +// +// 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 "AFPropertyListRequestOperation.h" + +static dispatch_queue_t property_list_request_operation_processing_queue() { + static dispatch_queue_t af_property_list_request_operation_processing_queue; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + af_property_list_request_operation_processing_queue = dispatch_queue_create("com.alamofire.networking.property-list-request.processing", DISPATCH_QUEUE_CONCURRENT); + }); + + return af_property_list_request_operation_processing_queue; +} + +@interface AFPropertyListRequestOperation () +@property (readwrite, nonatomic) id responsePropertyList; +@property (readwrite, nonatomic, assign) NSPropertyListFormat propertyListFormat; +@property (readwrite, nonatomic) NSError *propertyListError; +@end + +@implementation AFPropertyListRequestOperation +@synthesize responsePropertyList = _responsePropertyList; +@synthesize propertyListReadOptions = _propertyListReadOptions; +@synthesize propertyListFormat = _propertyListFormat; +@synthesize propertyListError = _propertyListError; + ++ (instancetype)propertyListRequestOperationWithRequest:(NSURLRequest *)request + success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id propertyList))success + failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id propertyList))failure +{ + AFPropertyListRequestOperation *requestOperation = [(AFPropertyListRequestOperation *)[self alloc] initWithRequest:request]; + [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { + if (success) { + success(operation.request, operation.response, responseObject); + } + } failure:^(AFHTTPRequestOperation *operation, NSError *error) { + if (failure) { + failure(operation.request, operation.response, error, [(AFPropertyListRequestOperation *)operation responsePropertyList]); + } + }]; + + return requestOperation; +} + +- (id)initWithRequest:(NSURLRequest *)urlRequest { + self = [super initWithRequest:urlRequest]; + if (!self) { + return nil; + } + + self.propertyListReadOptions = NSPropertyListImmutable; + + return self; +} + + +- (id)responsePropertyList { + if (!_responsePropertyList && [self.responseData length] > 0 && [self isFinished]) { + NSPropertyListFormat format; + NSError *error = nil; + self.responsePropertyList = [NSPropertyListSerialization propertyListWithData:self.responseData options:self.propertyListReadOptions format:&format error:&error]; + self.propertyListFormat = format; + self.propertyListError = error; + } + + return _responsePropertyList; +} + +- (NSError *)error { + if (_propertyListError) { + return _propertyListError; + } else { + return [super error]; + } +} + +#pragma mark - AFHTTPRequestOperation + ++ (NSSet *)acceptableContentTypes { + return [NSSet setWithObjects:@"application/x-plist", nil]; +} + ++ (BOOL)canProcessRequest:(NSURLRequest *)request { + return [[[request URL] pathExtension] isEqualToString:@"plist"] || [super canProcessRequest:request]; +} + +- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success + failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure +{ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Warc-retain-cycles" +#pragma clang diagnostic ignored "-Wgnu" + self.completionBlock = ^ { + if (self.error) { + if (failure) { + dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{ + failure(self, self.error); + }); + } + } else { + dispatch_async(property_list_request_operation_processing_queue(), ^(void) { + id propertyList = self.responsePropertyList; + + if (self.propertyListError) { + if (failure) { + dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{ + failure(self, self.error); + }); + } + } else { + if (success) { + dispatch_async(self.successCallbackQueue ?: dispatch_get_main_queue(), ^{ + success(self, propertyList); + }); + } + } + }); + } + }; +#pragma clang diagnostic pop +} + +@end diff --git a/AFURLConnectionOperation.h b/AFURLConnectionOperation.h new file mode 100644 index 0000000..2ce5609 --- /dev/null +++ b/AFURLConnectionOperation.h @@ -0,0 +1,372 @@ +// AFURLConnectionOperation.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 + +#import + +/** + `AFURLConnectionOperation` is a subclass of `NSOperation` that implements `NSURLConnection` delegate methods. + + ## Subclassing Notes + + This is the base class of all network request operations. You may wish to create your own subclass in order to implement additional `NSURLConnection` delegate methods (see "`NSURLConnection` Delegate Methods" below), or to provide additional properties and/or class constructors. + + If you are creating a subclass that communicates over the HTTP or HTTPS protocols, you may want to consider subclassing `AFHTTPRequestOperation` instead, as it supports specifying acceptable content types or status codes. + + ## NSURLConnection Delegate Methods + + `AFURLConnectionOperation` implements the following `NSURLConnection` delegate methods: + + - `connection:didReceiveResponse:` + - `connection:didReceiveData:` + - `connectionDidFinishLoading:` + - `connection:didFailWithError:` + - `connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:` + - `connection:willCacheResponse:` + - `connectionShouldUseCredentialStorage:` + - `connection:needNewBodyStream:` + - `connection:willSendRequestForAuthenticationChallenge:` + + If any of these methods are overridden in a subclass, they _must_ call the `super` implementation first. + + ## Class Constructors + + Class constructors, or methods that return an unowned instance, are the preferred way for subclasses to encapsulate any particular logic for handling the setup or parsing of response data. For instance, `AFJSONRequestOperation` provides `JSONRequestOperationWithRequest:success:failure:`, which takes block arguments, whose parameter on for a successful request is the JSON object initialized from the `response data`. + + ## Callbacks and Completion Blocks + + The built-in `completionBlock` provided by `NSOperation` allows for custom behavior to be executed after the request finishes. It is a common pattern for class constructors in subclasses to take callback block parameters, and execute them conditionally in the body of its `completionBlock`. Make sure to handle cancelled operations appropriately when setting a `completionBlock` (i.e. returning early before parsing response data). See the implementation of any of the `AFHTTPRequestOperation` subclasses for an example of this. + + Subclasses are strongly discouraged from overriding `setCompletionBlock:`, as `AFURLConnectionOperation`'s implementation includes a workaround to mitigate retain cycles, and what Apple rather ominously refers to as ["The Deallocation Problem"](http://developer.apple.com/library/ios/#technotes/tn2109/). + + ## SSL Pinning + + Relying on the CA trust model to validate SSL certificates exposes your app to security vulnerabilities, such as man-in-the-middle attacks. For applications that connect to known servers, SSL certificate pinning provides an increased level of security, by checking server certificate validity against those specified in the app bundle. + + SSL with certificate pinning is strongly recommended for any application that transmits sensitive information to an external webservice. + + When `defaultSSLPinningMode` is defined on `AFHTTPClient` and the Security framework is linked, connections will be validated on all matching certificates with a `.cer` extension in the bundle root. + + ## NSCoding & NSCopying Conformance + + `AFURLConnectionOperation` conforms to the `NSCoding` and `NSCopying` protocols, allowing operations to be archived to disk, and copied in memory, respectively. However, because of the intrinsic limitations of capturing the exact state of an operation at a particular moment, there are some important caveats to keep in mind: + + ### NSCoding Caveats + + - Encoded operations do not include any block or stream properties. Be sure to set `completionBlock`, `outputStream`, and any callback blocks as necessary when using `-initWithCoder:` or `NSKeyedUnarchiver`. + - Operations are paused on `encodeWithCoder:`. If the operation was encoded while paused or still executing, its archived state will return `YES` for `isReady`. Otherwise, the state of an operation when encoding will remain unchanged. + + ### NSCopying Caveats + + - `-copy` and `-copyWithZone:` return a new operation with the `NSURLRequest` of the original. So rather than an exact copy of the operation at that particular instant, the copying mechanism returns a completely new instance, which can be useful for retrying operations. + - A copy of an operation will not include the `outputStream` of the original. + - Operation copies do not include `completionBlock`. `completionBlock` often strongly captures a reference to `self`, which would otherwise have the unintuitive side-effect of pointing to the _original_ operation when copied. + */ + +typedef enum { + AFSSLPinningModeNone, + AFSSLPinningModePublicKey, + AFSSLPinningModeCertificate, +} AFURLConnectionOperationSSLPinningMode; + +@interface AFURLConnectionOperation : NSOperation = 50000) || \ + (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080) +NSURLConnectionDataDelegate, +#endif +NSCoding, NSCopying> + +///------------------------------- +/// @name Accessing Run Loop Modes +///------------------------------- + +/** + The run loop modes in which the operation will run on the network thread. By default, this is a single-member set containing `NSRunLoopCommonModes`. + */ +@property (nonatomic, strong) NSSet *runLoopModes; + +///----------------------------------------- +/// @name Getting URL Connection Information +///----------------------------------------- + +/** + The request used by the operation's connection. + */ +@property (readonly, nonatomic, strong) NSURLRequest *request; + +/** + The last response received by the operation's connection. + */ +@property (readonly, nonatomic, strong) NSURLResponse *response; + +/** + The error, if any, that occurred in the lifecycle of the request. + */ +@property (readonly, nonatomic, strong) NSError *error; + +/** + Whether the connection should accept an invalid SSL certificate. + + If `_AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_` is set, this property defaults to `YES` for backwards compatibility. Otherwise, this property defaults to `NO`. + */ +@property (nonatomic, assign) BOOL allowsInvalidSSLCertificate; + +///---------------------------- +/// @name Getting Response Data +///---------------------------- + +/** + The data received during the request. + */ +@property (readonly, nonatomic, strong) NSData *responseData; + +/** + The string representation of the response data. + */ +@property (readonly, nonatomic, copy) NSString *responseString; + +/** + The string encoding of the response. + + If the response does not specify a valid string encoding, `responseStringEncoding` will return `NSUTF8StringEncoding`. + */ +@property (readonly, nonatomic, assign) NSStringEncoding responseStringEncoding; + +///------------------------------- +/// @name Managing URL Credentials +///------------------------------- + +/** + Whether the URL connection should consult the credential storage for authenticating the connection. `YES` by default. + + This is the value that is returned in the `NSURLConnectionDelegate` method `-connectionShouldUseCredentialStorage:`. + */ +@property (nonatomic, assign) BOOL shouldUseCredentialStorage; + +/** + The credential used for authentication challenges in `-connection:didReceiveAuthenticationChallenge:`. + + This will be overridden by any shared credentials that exist for the username or password of the request URL, if present. + */ +@property (nonatomic, strong) NSURLCredential *credential; + +/** + The pinning mode which will be used for SSL connections. `AFSSLPinningModePublicKey` by default. + + SSL Pinning requires that the Security framework is linked with the binary. See the "SSL Pinning" section in the `AFURLConnectionOperation`" header for more information. + */ +@property (nonatomic, assign) AFURLConnectionOperationSSLPinningMode SSLPinningMode; + +///------------------------ +/// @name Accessing Streams +///------------------------ + +/** + The input stream used to read data to be sent during the request. + + This property acts as a proxy to the `HTTPBodyStream` property of `request`. + */ +@property (nonatomic, strong) NSInputStream *inputStream; + +/** + The output stream that is used to write data received until the request is finished. + + By default, data is accumulated into a buffer that is stored into `responseData` upon completion of the request. When `outputStream` is set, the data will not be accumulated into an internal buffer, and as a result, the `responseData` property of the completed request will be `nil`. The output stream will be scheduled in the network thread runloop upon being set. + */ +@property (nonatomic, strong) NSOutputStream *outputStream; + +///--------------------------------------------- +/// @name Managing Request Operation Information +///--------------------------------------------- + +/** + The user info dictionary for the receiver. + */ +@property (nonatomic, strong) NSDictionary *userInfo; + +///------------------------------------------------------ +/// @name Initializing an AFURLConnectionOperation Object +///------------------------------------------------------ + +/** + Initializes and returns a newly allocated operation object with a url connection configured with the specified url request. + + This is the designated initializer. + + @param urlRequest The request object to be used by the operation connection. + */ +- (id)initWithRequest:(NSURLRequest *)urlRequest; + +///---------------------------------- +/// @name Pausing / Resuming Requests +///---------------------------------- + +/** + Pauses the execution of the request operation. + + A paused operation returns `NO` for `-isReady`, `-isExecuting`, and `-isFinished`. As such, it will remain in an `NSOperationQueue` until it is either cancelled or resumed. Pausing a finished, cancelled, or paused operation has no effect. + */ +- (void)pause; + +/** + Whether the request operation is currently paused. + + @return `YES` if the operation is currently paused, otherwise `NO`. + */ +- (BOOL)isPaused; + +/** + Resumes the execution of the paused request operation. + + Pause/Resume behavior varies depending on the underlying implementation for the operation class. In its base implementation, resuming a paused requests restarts the original request. However, since HTTP defines a specification for how to request a specific content range, `AFHTTPRequestOperation` will resume downloading the request from where it left off, instead of restarting the original request. + */ +- (void)resume; + +///---------------------------------------------- +/// @name Configuring Backgrounding Task Behavior +///---------------------------------------------- + +/** + Specifies that the operation should continue execution after the app has entered the background, and the expiration handler for that background task. + + @param handler A handler to be called shortly before the application’s remaining background time reaches 0. The handler is wrapped in a block that cancels the operation, and cleans up and marks the end of execution, unlike the `handler` parameter in `UIApplication -beginBackgroundTaskWithExpirationHandler:`, which expects this to be done in the handler itself. The handler is called synchronously on the main thread, thus blocking the application’s suspension momentarily while the application is notified. + */ +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +- (void)setShouldExecuteAsBackgroundTaskWithExpirationHandler:(void (^)(void))handler; +#endif + +///--------------------------------- +/// @name Setting Progress Callbacks +///--------------------------------- + +/** + Sets a callback to be called when an undetermined number of bytes have been uploaded to the server. + + @param block A block object to be called when an undetermined number of bytes have been uploaded to the server. This block has no return value and takes three arguments: the number of bytes written since the last time the upload progress block was called, the total bytes written, and the total bytes expected to be written during the request, as initially determined by the length of the HTTP body. This block may be called multiple times, and will execute on the main thread. + */ +- (void)setUploadProgressBlock:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))block; + +/** + Sets a callback to be called when an undetermined number of bytes have been downloaded from the server. + + @param block A block object to be called when an undetermined number of bytes have been downloaded from the server. This block has no return value and takes three arguments: the number of bytes read since the last time the download progress block was called, the total bytes read, and the total bytes expected to be read during the request, as initially determined by the expected content size of the `NSHTTPURLResponse` object. This block may be called multiple times, and will execute on the main thread. + */ +- (void)setDownloadProgressBlock:(void (^)(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead))block; + +///------------------------------------------------- +/// @name Setting NSURLConnection Delegate Callbacks +///------------------------------------------------- + +/** + Sets a block to be executed when the connection will authenticate a challenge in order to download its request, as handled by the `NSURLConnectionDelegate` method `connection:willSendRequestForAuthenticationChallenge:`. + + @param block A block object to be executed when the connection will authenticate a challenge in order to download its request. The block has no return type and takes two arguments: the URL connection object, and the challenge that must be authenticated. This block must invoke one of the challenge-responder methods (NSURLAuthenticationChallengeSender protocol). + + If `allowsInvalidSSLCertificate` is set to YES, `connection:willSendRequestForAuthenticationChallenge:` will attempt to have the challenge sender use credentials with invalid SSL certificates. + */ +- (void)setWillSendRequestForAuthenticationChallengeBlock:(void (^)(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge))block; + +/** + Sets a block to be executed when the server redirects the request from one URL to another URL, or when the request URL changed by the `NSURLProtocol` subclass handling the request in order to standardize its format, as handled by the `NSURLConnectionDelegate` method `connection:willSendRequest:redirectResponse:`. + + @param block A block object to be executed when the request URL was changed. The block returns an `NSURLRequest` object, the URL request to redirect, and takes three arguments: the URL connection object, the the proposed redirected request, and the URL response that caused the redirect. + */ +- (void)setRedirectResponseBlock:(NSURLRequest * (^)(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse))block; + + +/** + Sets a block to be executed to modify the response a connection will cache, if any, as handled by the `NSURLConnectionDelegate` method `connection:willCacheResponse:`. + + @param block A block object to be executed to determine what response a connection will cache, if any. The block returns an `NSCachedURLResponse` object, the cached response to store in memory or `nil` to prevent the response from being cached, and takes two arguments: the URL connection object, and the cached response provided for the request. + */ +- (void)setCacheResponseBlock:(NSCachedURLResponse * (^)(NSURLConnection *connection, NSCachedURLResponse *cachedResponse))block; + +@end + +///---------------- +/// @name Constants +///---------------- + +/** + ## SSL Pinning Options + + The following constants are provided by `AFURLConnectionOperation` as possible SSL Pinning options. + + enum { + AFSSLPinningModeNone, + AFSSLPinningModePublicKey, + AFSSLPinningModeCertificate, + } + + `AFSSLPinningModeNone` + Do not pin SSL connections + + `AFSSLPinningModePublicKey` + Pin SSL connections to certificate public key (SPKI). + + `AFSSLPinningModeCertificate` + Pin SSL connections to exact certificate. This may cause problems when your certificate expires and needs re-issuance. + + ## User info dictionary keys + + These keys may exist in the user info dictionary, in addition to those defined for NSError. + + - `NSString * const AFNetworkingOperationFailingURLRequestErrorKey` + - `NSString * const AFNetworkingOperationFailingURLResponseErrorKey` + + ### Constants + + `AFNetworkingOperationFailingURLRequestErrorKey` + The corresponding value is an `NSURLRequest` containing the request of the operation associated with an error. This key is only present in the `AFNetworkingErrorDomain`. + + `AFNetworkingOperationFailingURLResponseErrorKey` + The corresponding value is an `NSURLResponse` containing the response of the operation associated with an error. This key is only present in the `AFNetworkingErrorDomain`. + + ## Error Domains + + The following error domain is predefined. + + - `NSString * const AFNetworkingErrorDomain` + + ### Constants + + `AFNetworkingErrorDomain` + AFNetworking errors. Error codes for `AFNetworkingErrorDomain` correspond to codes in `NSURLErrorDomain`. + */ +extern NSString * const AFNetworkingErrorDomain; +extern NSString * const AFNetworkingOperationFailingURLRequestErrorKey; +extern NSString * const AFNetworkingOperationFailingURLResponseErrorKey; + +///-------------------- +/// @name Notifications +///-------------------- + +/** + Posted when an operation begins executing. + */ +extern NSString * const AFNetworkingOperationDidStartNotification; + +/** + Posted when an operation finishes. + */ +extern NSString * const AFNetworkingOperationDidFinishNotification; diff --git a/AFURLConnectionOperation.m b/AFURLConnectionOperation.m new file mode 100644 index 0000000..58c305e --- /dev/null +++ b/AFURLConnectionOperation.m @@ -0,0 +1,840 @@ +// AFURLConnectionOperation.m +// +// 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 "AFURLConnectionOperation.h" + +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +#import +#endif + +#if !__has_feature(objc_arc) +#error AFNetworking must be built with ARC. +// You can turn on ARC for only AFNetworking files by adding -fobjc-arc to the build phase for each of its files. +#endif + +typedef enum { + AFOperationPausedState = -1, + AFOperationReadyState = 1, + AFOperationExecutingState = 2, + AFOperationFinishedState = 3, +} _AFOperationState; + +typedef signed short AFOperationState; + +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +typedef UIBackgroundTaskIdentifier AFBackgroundTaskIdentifier; +#else +typedef id AFBackgroundTaskIdentifier; +#endif + +static NSString * const kAFNetworkingLockName = @"com.alamofire.networking.operation.lock"; + +NSString * const AFNetworkingErrorDomain = @"AFNetworkingErrorDomain"; +NSString * const AFNetworkingOperationFailingURLRequestErrorKey = @"AFNetworkingOperationFailingURLRequestErrorKey"; +NSString * const AFNetworkingOperationFailingURLResponseErrorKey = @"AFNetworkingOperationFailingURLResponseErrorKey"; + +NSString * const AFNetworkingOperationDidStartNotification = @"com.alamofire.networking.operation.start"; +NSString * const AFNetworkingOperationDidFinishNotification = @"com.alamofire.networking.operation.finish"; + +typedef void (^AFURLConnectionOperationProgressBlock)(NSUInteger bytes, long long totalBytes, long long totalBytesExpected); +typedef void (^AFURLConnectionOperationAuthenticationChallengeBlock)(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge); +typedef NSCachedURLResponse * (^AFURLConnectionOperationCacheResponseBlock)(NSURLConnection *connection, NSCachedURLResponse *cachedResponse); +typedef NSURLRequest * (^AFURLConnectionOperationRedirectResponseBlock)(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse); + +static inline NSString * AFKeyPathFromOperationState(AFOperationState state) { + switch (state) { + case AFOperationReadyState: + return @"isReady"; + case AFOperationExecutingState: + return @"isExecuting"; + case AFOperationFinishedState: + return @"isFinished"; + case AFOperationPausedState: + return @"isPaused"; + default: + return @"state"; + } +} + +static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperationState toState, BOOL isCancelled) { + switch (fromState) { + case AFOperationReadyState: + switch (toState) { + case AFOperationPausedState: + case AFOperationExecutingState: + return YES; + case AFOperationFinishedState: + return isCancelled; + default: + return NO; + } + case AFOperationExecutingState: + switch (toState) { + case AFOperationPausedState: + case AFOperationFinishedState: + return YES; + default: + return NO; + } + case AFOperationFinishedState: + return NO; + case AFOperationPausedState: + return toState == AFOperationReadyState; + default: + return YES; + } +} + +#if !defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +static NSData *AFSecKeyGetData(SecKeyRef key) { + CFDataRef data = NULL; + + OSStatus status = SecItemExport(key, kSecFormatUnknown, kSecItemPemArmour, NULL, &data); + NSCAssert(status == errSecSuccess, @"SecItemExport error: %ld", (long int)status); + NSCParameterAssert(data); + + return (__bridge_transfer NSData *)data; +} +#endif + +static BOOL AFSecKeyIsEqualToKey(SecKeyRef key1, SecKeyRef key2) { +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) + return [(__bridge id)key1 isEqual:(__bridge id)key2]; +#else + return [AFSecKeyGetData(key1) isEqual:AFSecKeyGetData(key2)]; +#endif +} + +@interface AFURLConnectionOperation () +@property (readwrite, nonatomic, assign) AFOperationState state; +@property (readwrite, nonatomic, assign, getter = isCancelled) BOOL cancelled; +@property (readwrite, nonatomic, strong) NSRecursiveLock *lock; +@property (readwrite, nonatomic, strong) NSURLConnection *connection; +@property (readwrite, nonatomic, strong) NSURLRequest *request; +@property (readwrite, nonatomic, strong) NSURLResponse *response; +@property (readwrite, nonatomic, strong) NSError *error; +@property (readwrite, nonatomic, strong) NSData *responseData; +@property (readwrite, nonatomic, copy) NSString *responseString; +@property (readwrite, nonatomic, assign) NSStringEncoding responseStringEncoding; +@property (readwrite, nonatomic, assign) long long totalBytesRead; +@property (readwrite, nonatomic, assign) AFBackgroundTaskIdentifier backgroundTaskIdentifier; +@property (readwrite, nonatomic, copy) AFURLConnectionOperationProgressBlock uploadProgress; +@property (readwrite, nonatomic, copy) AFURLConnectionOperationProgressBlock downloadProgress; +@property (readwrite, nonatomic, copy) AFURLConnectionOperationAuthenticationChallengeBlock authenticationChallenge; +@property (readwrite, nonatomic, copy) AFURLConnectionOperationCacheResponseBlock cacheResponse; +@property (readwrite, nonatomic, copy) AFURLConnectionOperationRedirectResponseBlock redirectResponse; + +- (void)operationDidStart; +- (void)finish; +- (void)cancelConnection; +@end + +@implementation AFURLConnectionOperation +@synthesize state = _state; +@synthesize cancelled = _cancelled; +@synthesize connection = _connection; +@synthesize runLoopModes = _runLoopModes; +@synthesize request = _request; +@synthesize response = _response; +@synthesize error = _error; +@synthesize allowsInvalidSSLCertificate = _allowsInvalidSSLCertificate; +@synthesize responseData = _responseData; +@synthesize responseString = _responseString; +@synthesize responseStringEncoding = _responseStringEncoding; +@synthesize totalBytesRead = _totalBytesRead; +@dynamic inputStream; +@synthesize outputStream = _outputStream; +@synthesize credential = _credential; +@synthesize SSLPinningMode = _SSLPinningMode; +@synthesize shouldUseCredentialStorage = _shouldUseCredentialStorage; +@synthesize userInfo = _userInfo; +@synthesize backgroundTaskIdentifier = _backgroundTaskIdentifier; +@synthesize uploadProgress = _uploadProgress; +@synthesize downloadProgress = _downloadProgress; +@synthesize authenticationChallenge = _authenticationChallenge; +@synthesize cacheResponse = _cacheResponse; +@synthesize redirectResponse = _redirectResponse; +@synthesize lock = _lock; + ++ (void)networkRequestThreadEntryPoint:(id __unused)object { + @autoreleasepool { + [[NSThread currentThread] setName:@"AFNetworking"]; + + NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; + [runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode]; + [runLoop run]; + } +} + ++ (NSThread *)networkRequestThread { + static NSThread *_networkRequestThread = nil; + static dispatch_once_t oncePredicate; + dispatch_once(&oncePredicate, ^{ + _networkRequestThread = [[NSThread alloc] initWithTarget:self selector:@selector(networkRequestThreadEntryPoint:) object:nil]; + [_networkRequestThread start]; + }); + + return _networkRequestThread; +} + ++ (NSArray *)pinnedCertificates { + static NSArray *_pinnedCertificates = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + NSBundle *bundle = [NSBundle bundleForClass:[self class]]; + NSArray *paths = [bundle pathsForResourcesOfType:@"cer" inDirectory:@"."]; + + NSMutableArray *certificates = [NSMutableArray arrayWithCapacity:[paths count]]; + for (NSString *path in paths) { + NSData *certificateData = [NSData dataWithContentsOfFile:path]; + [certificates addObject:certificateData]; + } + + _pinnedCertificates = [[NSArray alloc] initWithArray:certificates]; + }); + + return _pinnedCertificates; +} + ++ (NSArray *)pinnedPublicKeys { + static NSArray *_pinnedPublicKeys = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + NSArray *pinnedCertificates = [self pinnedCertificates]; + NSMutableArray *publicKeys = [NSMutableArray arrayWithCapacity:[pinnedCertificates count]]; + + for (NSData *data in pinnedCertificates) { + SecCertificateRef allowedCertificate = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)data); + NSParameterAssert(allowedCertificate); + + SecCertificateRef allowedCertificates[] = {allowedCertificate}; + CFArrayRef certificates = CFArrayCreate(NULL, (const void **)allowedCertificates, 1, NULL); + + SecPolicyRef policy = SecPolicyCreateBasicX509(); + SecTrustRef allowedTrust = NULL; + OSStatus status = SecTrustCreateWithCertificates(certificates, policy, &allowedTrust); + NSAssert(status == errSecSuccess, @"SecTrustCreateWithCertificates error: %ld", (long int)status); + if (status == errSecSuccess && allowedTrust) { + SecTrustResultType result = 0; + status = SecTrustEvaluate(allowedTrust, &result); + NSAssert(status == errSecSuccess, @"SecTrustEvaluate error: %ld", (long int)status); + if (status == errSecSuccess) { + SecKeyRef allowedPublicKey = SecTrustCopyPublicKey(allowedTrust); + NSParameterAssert(allowedPublicKey); + if (allowedPublicKey) { + [publicKeys addObject:(__bridge_transfer id)allowedPublicKey]; + } + } + + CFRelease(allowedTrust); + } + + CFRelease(policy); + CFRelease(certificates); + CFRelease(allowedCertificate); + } + + _pinnedPublicKeys = [[NSArray alloc] initWithArray:publicKeys]; + }); + + return _pinnedPublicKeys; +} + +- (id)initWithRequest:(NSURLRequest *)urlRequest { + NSParameterAssert(urlRequest); + + self = [super init]; + if (!self) { + return nil; + } + + self.lock = [[NSRecursiveLock alloc] init]; + self.lock.name = kAFNetworkingLockName; + + self.runLoopModes = [NSSet setWithObject:NSRunLoopCommonModes]; + + self.request = urlRequest; + + self.shouldUseCredentialStorage = YES; + + // #ifdef included for backwards-compatibility +#ifdef _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_ + self.allowsInvalidSSLCertificate = YES; +#endif + + self.state = AFOperationReadyState; + + return self; +} + +- (void)dealloc { + if (_outputStream) { + [_outputStream close]; + _outputStream = nil; + } + +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) + if (_backgroundTaskIdentifier) { + [[UIApplication sharedApplication] endBackgroundTask:_backgroundTaskIdentifier]; + _backgroundTaskIdentifier = UIBackgroundTaskInvalid; + } +#endif +} + +- (NSString *)description { + return [NSString stringWithFormat:@"<%@: %p, state: %@, cancelled: %@ request: %@, response: %@>", NSStringFromClass([self class]), self, AFKeyPathFromOperationState(self.state), ([self isCancelled] ? @"YES" : @"NO"), self.request, self.response]; +} + +- (void)setCompletionBlock:(void (^)(void))block { + [self.lock lock]; + if (!block) { + [super setCompletionBlock:nil]; + } else { + __weak __typeof(&*self)weakSelf = self; + [super setCompletionBlock:^ { + __strong __typeof(&*weakSelf)strongSelf = weakSelf; + + block(); + [strongSelf setCompletionBlock:nil]; + }]; + } + [self.lock unlock]; +} + +- (NSInputStream *)inputStream { + return self.request.HTTPBodyStream; +} + +- (void)setInputStream:(NSInputStream *)inputStream { + [self willChangeValueForKey:@"inputStream"]; + NSMutableURLRequest *mutableRequest = [self.request mutableCopy]; + mutableRequest.HTTPBodyStream = inputStream; + self.request = mutableRequest; + [self didChangeValueForKey:@"inputStream"]; +} + +- (NSOutputStream *)outputStream { + if (!_outputStream) { + self.outputStream = [NSOutputStream outputStreamToMemory]; + } + + return _outputStream; +} + +- (void)setOutputStream:(NSOutputStream *)outputStream { + [self.lock lock]; + if (outputStream != _outputStream) { + [self willChangeValueForKey:@"outputStream"]; + if (_outputStream) { + [_outputStream close]; + } + _outputStream = outputStream; + [self didChangeValueForKey:@"outputStream"]; + } + [self.lock unlock]; +} + +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +- (void)setShouldExecuteAsBackgroundTaskWithExpirationHandler:(void (^)(void))handler { + [self.lock lock]; + if (!self.backgroundTaskIdentifier) { + UIApplication *application = [UIApplication sharedApplication]; + __weak __typeof(&*self)weakSelf = self; + self.backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{ + __strong __typeof(&*weakSelf)strongSelf = weakSelf; + + if (handler) { + handler(); + } + + if (strongSelf) { + [strongSelf cancel]; + + [application endBackgroundTask:strongSelf.backgroundTaskIdentifier]; + strongSelf.backgroundTaskIdentifier = UIBackgroundTaskInvalid; + } + }]; + } + [self.lock unlock]; +} +#endif + +- (void)setUploadProgressBlock:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))block { + self.uploadProgress = block; +} + +- (void)setDownloadProgressBlock:(void (^)(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead))block { + self.downloadProgress = block; +} + +- (void)setWillSendRequestForAuthenticationChallengeBlock:(void (^)(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge))block { + self.authenticationChallenge = block; +} + +- (void)setCacheResponseBlock:(NSCachedURLResponse * (^)(NSURLConnection *connection, NSCachedURLResponse *cachedResponse))block { + self.cacheResponse = block; +} + +- (void)setRedirectResponseBlock:(NSURLRequest * (^)(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse))block { + self.redirectResponse = block; +} + +- (void)setState:(AFOperationState)state { + if (!AFStateTransitionIsValid(self.state, state, [self isCancelled])) { + return; + } + + [self.lock lock]; + NSString *oldStateKey = AFKeyPathFromOperationState(self.state); + NSString *newStateKey = AFKeyPathFromOperationState(state); + + [self willChangeValueForKey:newStateKey]; + [self willChangeValueForKey:oldStateKey]; + _state = state; + [self didChangeValueForKey:oldStateKey]; + [self didChangeValueForKey:newStateKey]; + [self.lock unlock]; +} + +- (NSString *)responseString { + [self.lock lock]; + if (!_responseString && self.response && self.responseData) { + self.responseString = [[NSString alloc] initWithData:self.responseData encoding:self.responseStringEncoding]; + } + [self.lock unlock]; + + return _responseString; +} + +- (NSStringEncoding)responseStringEncoding { + [self.lock lock]; + if (!_responseStringEncoding && self.response) { + NSStringEncoding stringEncoding = NSUTF8StringEncoding; + if (self.response.textEncodingName) { + CFStringEncoding IANAEncoding = CFStringConvertIANACharSetNameToEncoding((__bridge CFStringRef)self.response.textEncodingName); + if (IANAEncoding != kCFStringEncodingInvalidId) { + stringEncoding = CFStringConvertEncodingToNSStringEncoding(IANAEncoding); + } + } + + self.responseStringEncoding = stringEncoding; + } + [self.lock unlock]; + + return _responseStringEncoding; +} + +- (void)pause { + if ([self isPaused] || [self isFinished] || [self isCancelled]) { + return; + } + + [self.lock lock]; + + if ([self isExecuting]) { + [self.connection performSelector:@selector(cancel) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:NO modes:[self.runLoopModes allObjects]]; + + dispatch_async(dispatch_get_main_queue(), ^{ + NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; + [notificationCenter postNotificationName:AFNetworkingOperationDidFinishNotification object:self]; + }); + } + + self.state = AFOperationPausedState; + + [self.lock unlock]; +} + +- (BOOL)isPaused { + return self.state == AFOperationPausedState; +} + +- (void)resume { + if (![self isPaused]) { + return; + } + + [self.lock lock]; + self.state = AFOperationReadyState; + + [self start]; + [self.lock unlock]; +} + +#pragma mark - NSOperation + +- (BOOL)isReady { + return self.state == AFOperationReadyState && [super isReady]; +} + +- (BOOL)isExecuting { + return self.state == AFOperationExecutingState; +} + +- (BOOL)isFinished { + return self.state == AFOperationFinishedState; +} + +- (BOOL)isConcurrent { + return YES; +} + +- (void)start { + [self.lock lock]; + if ([self isReady]) { + self.state = AFOperationExecutingState; + + [self performSelector:@selector(operationDidStart) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:NO modes:[self.runLoopModes allObjects]]; + } + [self.lock unlock]; +} + +- (void)operationDidStart { + [self.lock lock]; + if (! [self isCancelled]) { + self.connection = [[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:NO]; + + 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]; + } + [self.lock unlock]; + + dispatch_async(dispatch_get_main_queue(), ^{ + [[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingOperationDidStartNotification object:self]; + }); + + if ([self isCancelled]) { + [self finish]; + } +} + +- (void)finish { + self.state = AFOperationFinishedState; + + dispatch_async(dispatch_get_main_queue(), ^{ + [[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingOperationDidFinishNotification object:self]; + }); +} + +- (void)cancel { + [self.lock lock]; + if (![self isFinished] && ![self isCancelled]) { + [self willChangeValueForKey:@"isCancelled"]; + _cancelled = YES; + [super cancel]; + [self didChangeValueForKey:@"isCancelled"]; + + // Cancel the connection on the thread it runs on to prevent race conditions + [self performSelector:@selector(cancelConnection) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:NO modes:[self.runLoopModes allObjects]]; + } + [self.lock unlock]; +} + +- (void)cancelConnection { + NSDictionary *userInfo = nil; + if ([self.request URL]) { + userInfo = [NSDictionary dictionaryWithObject:[self.request URL] forKey:NSURLErrorFailingURLErrorKey]; + } + self.error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:userInfo]; + + if (self.connection) { + [self.connection cancel]; + + // Manually send this delegate message since `[self.connection cancel]` causes the connection to never send another message to its delegate + [self performSelector:@selector(connection:didFailWithError:) withObject:self.connection withObject:self.error]; + } +} + +#pragma mark - NSURLConnectionDelegate + +- (void)connection:(NSURLConnection *)connection +willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge +{ + if (self.authenticationChallenge) { + self.authenticationChallenge(connection, challenge); + return; + } + + if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { + SecTrustRef serverTrust = challenge.protectionSpace.serverTrust; + + SecPolicyRef policy = SecPolicyCreateBasicX509(); + CFIndex certificateCount = SecTrustGetCertificateCount(serverTrust); + NSMutableArray *trustChain = [NSMutableArray arrayWithCapacity:certificateCount]; + + for (CFIndex i = 0; i < certificateCount; i++) { + SecCertificateRef certificate = SecTrustGetCertificateAtIndex(serverTrust, i); + + if (self.SSLPinningMode == AFSSLPinningModeCertificate) { + [trustChain addObject:(__bridge_transfer NSData *)SecCertificateCopyData(certificate)]; + } else if (self.SSLPinningMode == AFSSLPinningModePublicKey) { + SecCertificateRef someCertificates[] = {certificate}; + CFArrayRef certificates = CFArrayCreate(NULL, (const void **)someCertificates, 1, NULL); + + SecTrustRef trust = NULL; + + OSStatus status = SecTrustCreateWithCertificates(certificates, policy, &trust); + NSAssert(status == errSecSuccess, @"SecTrustCreateWithCertificates error: %ld", (long int)status); + if (status == errSecSuccess && trust) { + SecTrustResultType result; + status = SecTrustEvaluate(trust, &result); + NSAssert(status == errSecSuccess, @"SecTrustEvaluate error: %ld", (long int)status); + if (status == errSecSuccess) { + [trustChain addObject:(__bridge_transfer id)SecTrustCopyPublicKey(trust)]; + } + + CFRelease(trust); + } + + CFRelease(certificates); + } + } + + CFRelease(policy); + + switch (self.SSLPinningMode) { + case AFSSLPinningModePublicKey: { + NSArray *pinnedPublicKeys = [self.class pinnedPublicKeys]; + + for (id publicKey in trustChain) { + for (id pinnedPublicKey in pinnedPublicKeys) { + if (AFSecKeyIsEqualToKey((__bridge SecKeyRef)publicKey, (__bridge SecKeyRef)pinnedPublicKey)) { + NSURLCredential *credential = [NSURLCredential credentialForTrust:serverTrust]; + [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; + return; + } + } + } + + [[challenge sender] cancelAuthenticationChallenge:challenge]; + break; + } + case AFSSLPinningModeCertificate: { + for (id serverCertificateData in trustChain) { + if ([[self.class pinnedCertificates] containsObject:serverCertificateData]) { + NSURLCredential *credential = [NSURLCredential credentialForTrust:serverTrust]; + [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; + return; + } + } + + [[challenge sender] cancelAuthenticationChallenge:challenge]; + break; + } + case AFSSLPinningModeNone: { + if (self.allowsInvalidSSLCertificate){ + NSURLCredential *credential = [NSURLCredential credentialForTrust:serverTrust]; + [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; + } else { + SecTrustResultType result = 0; + OSStatus status = SecTrustEvaluate(serverTrust, &result); + NSAssert(status == errSecSuccess, @"SecTrustEvaluate error: %ld", (long int)status); + + if (status == errSecSuccess && (result == kSecTrustResultUnspecified || result == kSecTrustResultProceed)) { + NSURLCredential *credential = [NSURLCredential credentialForTrust:serverTrust]; + [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; + } else { + [[challenge sender] cancelAuthenticationChallenge:challenge]; + } + } + break; + } + } + } else { + if ([challenge previousFailureCount] == 0) { + if (self.credential) { + [[challenge sender] useCredential:self.credential forAuthenticationChallenge:challenge]; + } else { + [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge]; + } + } else { + [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge]; + } + } +} + +- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection __unused *)connection { + return self.shouldUseCredentialStorage; +} + +- (NSInputStream *)connection:(NSURLConnection __unused *)connection + needNewBodyStream:(NSURLRequest *)request +{ + if ([request.HTTPBodyStream conformsToProtocol:@protocol(NSCopying)]) { + return [request.HTTPBodyStream copy]; + } else { + [self cancelConnection]; + + return nil; + } +} + +- (NSURLRequest *)connection:(NSURLConnection *)connection + willSendRequest:(NSURLRequest *)request + redirectResponse:(NSURLResponse *)redirectResponse +{ + if (self.redirectResponse) { + return self.redirectResponse(connection, request, redirectResponse); + } else { + return request; + } +} + +- (void)connection:(NSURLConnection __unused *)connection + didSendBodyData:(NSInteger)bytesWritten + totalBytesWritten:(NSInteger)totalBytesWritten +totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite +{ + if (self.uploadProgress) { + dispatch_async(dispatch_get_main_queue(), ^{ + self.uploadProgress((NSUInteger)bytesWritten, totalBytesWritten, totalBytesExpectedToWrite); + }); + } +} + +- (void)connection:(NSURLConnection __unused *)connection +didReceiveResponse:(NSURLResponse *)response +{ + self.response = response; + + [self.outputStream open]; +} + +- (void)connection:(NSURLConnection __unused *)connection + didReceiveData:(NSData *)data +{ + NSUInteger length = [data length]; + if ([self.outputStream hasSpaceAvailable]) { + const uint8_t *dataBuffer = (uint8_t *) [data bytes]; + [self.outputStream write:&dataBuffer[0] maxLength:length]; + } + + dispatch_async(dispatch_get_main_queue(), ^{ + self.totalBytesRead += length; + + if (self.downloadProgress) { + self.downloadProgress(length, self.totalBytesRead, self.response.expectedContentLength); + } + }); +} + +- (void)connectionDidFinishLoading:(NSURLConnection __unused *)connection { + self.responseData = [self.outputStream propertyForKey:NSStreamDataWrittenToMemoryStreamKey]; + + [self.outputStream close]; + + [self finish]; + + self.connection = nil; +} + +- (void)connection:(NSURLConnection __unused *)connection + didFailWithError:(NSError *)error +{ + self.error = error; + + [self.outputStream close]; + + [self finish]; + + self.connection = nil; +} + +- (NSCachedURLResponse *)connection:(NSURLConnection *)connection + willCacheResponse:(NSCachedURLResponse *)cachedResponse +{ + if (self.cacheResponse) { + return self.cacheResponse(connection, cachedResponse); + } else { + if ([self isCancelled]) { + return nil; + } + + return cachedResponse; + } +} + +#pragma mark - NSCoding + +- (id)initWithCoder:(NSCoder *)aDecoder { + NSURLRequest *request = [aDecoder decodeObjectForKey:@"request"]; + + self = [self initWithRequest:request]; + if (!self) { + return nil; + } + + self.state = (AFOperationState)[aDecoder decodeIntegerForKey:@"state"]; + self.cancelled = [aDecoder decodeBoolForKey:@"isCancelled"]; + self.response = [aDecoder decodeObjectForKey:@"response"]; + self.error = [aDecoder decodeObjectForKey:@"error"]; + self.responseData = [aDecoder decodeObjectForKey:@"responseData"]; + self.totalBytesRead = [[aDecoder decodeObjectForKey:@"totalBytesRead"] longLongValue]; + self.allowsInvalidSSLCertificate = [[aDecoder decodeObjectForKey:@"allowsInvalidSSLCertificate"] boolValue]; + + return self; +} + +- (void)encodeWithCoder:(NSCoder *)aCoder { + [self pause]; + + [aCoder encodeObject:self.request forKey:@"request"]; + + switch (self.state) { + case AFOperationExecutingState: + case AFOperationPausedState: + [aCoder encodeInteger:AFOperationReadyState forKey:@"state"]; + break; + default: + [aCoder encodeInteger:self.state forKey:@"state"]; + break; + } + + [aCoder encodeBool:[self isCancelled] forKey:@"isCancelled"]; + [aCoder encodeObject:self.response forKey:@"response"]; + [aCoder encodeObject:self.error forKey:@"error"]; + [aCoder encodeObject:self.responseData forKey:@"responseData"]; + [aCoder encodeObject:[NSNumber numberWithLongLong:self.totalBytesRead] forKey:@"totalBytesRead"]; + [aCoder encodeObject:[NSNumber numberWithBool:self.allowsInvalidSSLCertificate] forKey:@"allowsInvalidSSLCertificate"]; +} + +#pragma mark - NSCopying + +- (id)copyWithZone:(NSZone *)zone { + AFURLConnectionOperation *operation = [(AFURLConnectionOperation *)[[self class] allocWithZone:zone] initWithRequest:self.request]; + + operation.uploadProgress = self.uploadProgress; + operation.downloadProgress = self.downloadProgress; + operation.authenticationChallenge = self.authenticationChallenge; + operation.cacheResponse = self.cacheResponse; + operation.redirectResponse = self.redirectResponse; + operation.allowsInvalidSSLCertificate = self.allowsInvalidSSLCertificate; + + return operation; +} + +@end diff --git a/AFXMLRequestOperation.h b/AFXMLRequestOperation.h new file mode 100644 index 0000000..4130932 --- /dev/null +++ b/AFXMLRequestOperation.h @@ -0,0 +1,89 @@ +// AFXMLRequestOperation.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 +#import "AFHTTPRequestOperation.h" + +#import + +/** + `AFXMLRequestOperation` is a subclass of `AFHTTPRequestOperation` for downloading and working with XML response data. + + ## Acceptable Content Types + + By default, `AFXMLRequestOperation` accepts the following MIME types, which includes the official standard, `application/xml`, as well as other commonly-used types: + + - `application/xml` + - `text/xml` + + ## Use With AFHTTPClient + + When `AFXMLRequestOperation` is registered with `AFHTTPClient`, the response object in the success callback of `HTTPRequestOperationWithRequest:success:failure:` will be an instance of `NSXMLParser`. On platforms that support `NSXMLDocument`, you have the option to ignore the response object, and simply use the `responseXMLDocument` property of the operation argument of the callback. + */ +@interface AFXMLRequestOperation : AFHTTPRequestOperation + +///---------------------------- +/// @name Getting Response Data +///---------------------------- + +/** + An `NSXMLParser` object constructed from the response data. + */ +@property (readonly, nonatomic, strong) NSXMLParser *responseXMLParser; + +#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED +/** + An `NSXMLDocument` object constructed from the response data. If an error occurs while parsing, `nil` will be returned, and the `error` property will be set to the error. + */ +@property (readonly, nonatomic, strong) NSXMLDocument *responseXMLDocument; +#endif + +/** + Creates and returns an `AFXMLRequestOperation` object and sets the specified success and failure callbacks. + + @param urlRequest The request object to be loaded asynchronously during execution of the operation + @param success A block object to be executed when the operation finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the XML parser constructed with the response data of request. + @param failure A block object to be executed when the operation finishes unsuccessfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the error describing the network error that occurred. + + @return A new XML request operation + */ ++ (instancetype)XMLParserRequestOperationWithRequest:(NSURLRequest *)urlRequest + success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser))success + failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParser))failure; + + +#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED +/** + Creates and returns an `AFXMLRequestOperation` object and sets the specified success and failure callbacks. + + @param urlRequest The request object to be loaded asynchronously during execution of the operation + @param success A block object to be executed when the operation finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the XML document created from the response data of request. + @param failure A block object to be executed when the operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data as XML. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the error describing the network or parsing error that occurred. + + @return A new XML request operation + */ ++ (instancetype)XMLDocumentRequestOperationWithRequest:(NSURLRequest *)urlRequest + success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLDocument *document))success + failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLDocument *document))failure; +#endif + +@end diff --git a/AFXMLRequestOperation.m b/AFXMLRequestOperation.m new file mode 100644 index 0000000..a97cd88 --- /dev/null +++ b/AFXMLRequestOperation.m @@ -0,0 +1,167 @@ +// AFXMLRequestOperation.m +// +// 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 "AFXMLRequestOperation.h" + +#include + +static dispatch_queue_t xml_request_operation_processing_queue() { + static dispatch_queue_t af_xml_request_operation_processing_queue; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + af_xml_request_operation_processing_queue = dispatch_queue_create("com.alamofire.networking.xml-request.processing", DISPATCH_QUEUE_CONCURRENT); + }); + + return af_xml_request_operation_processing_queue; +} + +@interface AFXMLRequestOperation () +@property (readwrite, nonatomic, strong) NSXMLParser *responseXMLParser; +#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED +@property (readwrite, nonatomic, strong) NSXMLDocument *responseXMLDocument; +#endif +@property (readwrite, nonatomic, strong) NSError *XMLError; +@end + +@implementation AFXMLRequestOperation +@synthesize responseXMLParser = _responseXMLParser; +#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED +@synthesize responseXMLDocument = _responseXMLDocument; +#endif +@synthesize XMLError = _XMLError; + ++ (instancetype)XMLParserRequestOperationWithRequest:(NSURLRequest *)urlRequest + success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser))success + failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParser))failure +{ + AFXMLRequestOperation *requestOperation = [(AFXMLRequestOperation *)[self alloc] initWithRequest:urlRequest]; + [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { + if (success) { + success(operation.request, operation.response, responseObject); + } + } failure:^(AFHTTPRequestOperation *operation, NSError *error) { + if (failure) { + failure(operation.request, operation.response, error, [(AFXMLRequestOperation *)operation responseXMLParser]); + } + }]; + + return requestOperation; +} + +#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED ++ (instancetype)XMLDocumentRequestOperationWithRequest:(NSURLRequest *)urlRequest + success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLDocument *document))success + failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLDocument *document))failure +{ + AFXMLRequestOperation *requestOperation = [[self alloc] initWithRequest:urlRequest]; + [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, __unused id responseObject) { + if (success) { + NSXMLDocument *XMLDocument = [(AFXMLRequestOperation *)operation responseXMLDocument]; + success(operation.request, operation.response, XMLDocument); + } + } failure:^(AFHTTPRequestOperation *operation, NSError *error) { + if (failure) { + NSXMLDocument *XMLDocument = [(AFXMLRequestOperation *)operation responseXMLDocument]; + failure(operation.request, operation.response, error, XMLDocument); + } + }]; + + return requestOperation; +} +#endif + + +- (NSXMLParser *)responseXMLParser { + if (!_responseXMLParser && [self.responseData length] > 0 && [self isFinished]) { + self.responseXMLParser = [[NSXMLParser alloc] initWithData:self.responseData]; + } + + return _responseXMLParser; +} + +#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED +- (NSXMLDocument *)responseXMLDocument { + if (!_responseXMLDocument && [self.responseData length] > 0 && [self isFinished]) { + NSError *error = nil; + self.responseXMLDocument = [[NSXMLDocument alloc] initWithData:self.responseData options:0 error:&error]; + self.XMLError = error; + } + + return _responseXMLDocument; +} +#endif + +- (NSError *)error { + if (_XMLError) { + return _XMLError; + } else { + return [super error]; + } +} + +#pragma mark - NSOperation + +- (void)cancel { + [super cancel]; + + self.responseXMLParser.delegate = nil; +} + +#pragma mark - AFHTTPRequestOperation + ++ (NSSet *)acceptableContentTypes { + return [NSSet setWithObjects:@"application/xml", @"text/xml", nil]; +} + ++ (BOOL)canProcessRequest:(NSURLRequest *)request { + return [[[request URL] pathExtension] isEqualToString:@"xml"] || [super canProcessRequest:request]; +} + +- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success + failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure +{ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Warc-retain-cycles" +#pragma clang diagnostic ignored "-Wgnu" + self.completionBlock = ^ { + dispatch_async(xml_request_operation_processing_queue(), ^(void) { + NSXMLParser *XMLParser = self.responseXMLParser; + + if (self.error) { + if (failure) { + dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{ + failure(self, self.error); + }); + } + } else { + if (success) { + dispatch_async(self.successCallbackQueue ?: dispatch_get_main_queue(), ^{ + success(self, XMLParser); + }); + } + } + }); + }; +#pragma clang diagnostic pop +} + +@end diff --git a/ASIAuthenticationDialog.h b/ASIAuthenticationDialog.h new file mode 100644 index 0000000..6bbb282 --- /dev/null +++ b/ASIAuthenticationDialog.h @@ -0,0 +1,35 @@ +// +// ASIAuthenticationDialog.h +// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest +// +// Created by Ben Copsey on 21/08/2009. +// Copyright 2009 All-Seeing Interactive. All rights reserved. +// + +#import +#import +@class ASIHTTPRequest; + +typedef enum _ASIAuthenticationType { + ASIStandardAuthenticationType = 0, + ASIProxyAuthenticationType = 1 +} ASIAuthenticationType; + +@interface ASIAutorotatingViewController : UIViewController +@end + +@interface ASIAuthenticationDialog : ASIAutorotatingViewController { + ASIHTTPRequest *request; + ASIAuthenticationType type; + UITableView *tableView; + UIViewController *presentingController; + BOOL didEnableRotationNotifications; +} ++ (void)presentAuthenticationDialogForRequest:(ASIHTTPRequest *)request; ++ (void)dismiss; + +@property (retain) ASIHTTPRequest *request; +@property (assign) ASIAuthenticationType type; +@property (assign) BOOL didEnableRotationNotifications; +@property (retain, nonatomic) UIViewController *presentingController; +@end diff --git a/ASIAuthenticationDialog.m b/ASIAuthenticationDialog.m new file mode 100644 index 0000000..9aa25c2 --- /dev/null +++ b/ASIAuthenticationDialog.m @@ -0,0 +1,489 @@ +// +// ASIAuthenticationDialog.m +// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest +// +// Created by Ben Copsey on 21/08/2009. +// Copyright 2009 All-Seeing Interactive. All rights reserved. +// + +#import "ASIAuthenticationDialog.h" +#import "ASIHTTPRequest.h" +#import + +static ASIAuthenticationDialog *sharedDialog = nil; +BOOL isDismissing = NO; +static NSMutableArray *requestsNeedingAuthentication = nil; + +static const NSUInteger kUsernameRow = 0; +static const NSUInteger kUsernameSection = 0; +static const NSUInteger kPasswordRow = 1; +static const NSUInteger kPasswordSection = 0; +static const NSUInteger kDomainRow = 0; +static const NSUInteger kDomainSection = 1; + + +@implementation ASIAutorotatingViewController + +- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation +{ + return YES; +} + +@end + + +@interface ASIAuthenticationDialog () +- (void)showTitle; +- (void)show; +- (NSArray *)requestsRequiringTheseCredentials; +- (void)presentNextDialog; +@property (retain) UITableView *tableView; +@end + +@implementation ASIAuthenticationDialog + +#pragma mark init / dealloc + ++ (void)initialize +{ + if (self == [ASIAuthenticationDialog class]) { + requestsNeedingAuthentication = [[NSMutableArray array] retain]; + } +} + ++ (void)presentAuthenticationDialogForRequest:(ASIHTTPRequest *)request +{ + // No need for a lock here, this will always be called on the main thread + if (!sharedDialog) { + sharedDialog = [[self alloc] init]; + [sharedDialog setRequest:request]; + if ([request authenticationNeeded] == ASIProxyAuthenticationNeeded) { + [sharedDialog setType:ASIProxyAuthenticationType]; + } else { + [sharedDialog setType:ASIStandardAuthenticationType]; + } + [sharedDialog show]; + } else { + [requestsNeedingAuthentication addObject:request]; + } +} + +- (id)init +{ + if ((self = [self initWithNibName:nil bundle:nil])) { + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; + +#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2 + if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { +#endif + if (![UIDevice currentDevice].generatesDeviceOrientationNotifications) { + [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; + [self setDidEnableRotationNotifications:YES]; + } + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil]; +#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2 + } +#endif + } + return self; +} + +- (void)dealloc +{ + if ([self didEnableRotationNotifications]) { + [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil]; + } + [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; + + [request release]; + [tableView release]; + [presentingController.view removeFromSuperview]; + [presentingController release]; + [super dealloc]; +} + +#pragma mark keyboard notifications + +- (void)keyboardWillShow:(NSNotification *)notification +{ +#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2 + if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { +#endif + NSValue *keyboardBoundsValue; +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_2 + keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]; +#else + keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey]; +#endif + CGRect keyboardBounds; + [keyboardBoundsValue getValue:&keyboardBounds]; + UIEdgeInsets e = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0); + [[self tableView] setScrollIndicatorInsets:e]; + [[self tableView] setContentInset:e]; +#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2 + } +#endif +} + +// Manually handles orientation changes on iPhone +- (void)orientationChanged:(NSNotification *)notification +{ + [self showTitle]; + + UIDeviceOrientation o = [[UIApplication sharedApplication] statusBarOrientation]; + CGFloat angle = 0; + switch (o) { + case UIDeviceOrientationLandscapeLeft: angle = 90; break; + case UIDeviceOrientationLandscapeRight: angle = -90; break; + case UIDeviceOrientationPortraitUpsideDown: angle = 180; break; + default: break; + } + + CGRect f = [[UIScreen mainScreen] applicationFrame]; + + // Swap the frame height and width if necessary + if (UIDeviceOrientationIsLandscape(o)) { + CGFloat t; + t = f.size.width; + f.size.width = f.size.height; + f.size.height = t; + } + + CGAffineTransform previousTransform = self.view.layer.affineTransform; + CGAffineTransform newTransform = CGAffineTransformMakeRotation(angle * M_PI / 180.0); + + // Reset the transform so we can set the size + self.view.layer.affineTransform = CGAffineTransformIdentity; + self.view.frame = (CGRect){0,0,f.size}; + + // Revert to the previous transform for correct animation + self.view.layer.affineTransform = previousTransform; + + [UIView beginAnimations:nil context:NULL]; + [UIView setAnimationDuration:0.3]; + + // Set the new transform + self.view.layer.affineTransform = newTransform; + + // Fix the view origin + self.view.frame = (CGRect){f.origin.x,f.origin.y,self.view.frame.size}; + [UIView commitAnimations]; +} + +#pragma mark utilities + +- (UIViewController *)presentingController +{ + if (!presentingController) { + presentingController = [[ASIAutorotatingViewController alloc] initWithNibName:nil bundle:nil]; + + // Attach to the window, but don't interfere. + UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0]; + [window addSubview:[presentingController view]]; + [[presentingController view] setFrame:CGRectZero]; + [[presentingController view] setUserInteractionEnabled:NO]; + } + + return presentingController; +} + +- (UITextField *)textFieldInRow:(NSUInteger)row section:(NSUInteger)section +{ + return [[[[[self tableView] cellForRowAtIndexPath: + [NSIndexPath indexPathForRow:row inSection:section]] + contentView] subviews] objectAtIndex:0]; +} + +- (UITextField *)usernameField +{ + return [self textFieldInRow:kUsernameRow section:kUsernameSection]; +} + +- (UITextField *)passwordField +{ + return [self textFieldInRow:kPasswordRow section:kPasswordSection]; +} + +- (UITextField *)domainField +{ + return [self textFieldInRow:kDomainRow section:kDomainSection]; +} + +#pragma mark show / dismiss + ++ (void)dismiss +{ + //[[sharedDialog parentViewController] dismissModalViewControllerAnimated:YES]; + [[sharedDialog parentViewController] dismissViewControllerAnimated:YES completion:nil]; +} + +- (void)viewDidDisappear:(BOOL)animated +{ + [self retain]; + [sharedDialog release]; + sharedDialog = nil; + [self presentNextDialog]; + [self release]; +} + +- (void)dismiss +{ + if (self == sharedDialog) { + [[self class] dismiss]; + } else + { + [[self parentViewController] dismissViewControllerAnimated:YES completion:nil]; + //[[self parentViewController] dismissModalViewControllerAnimated:YES]; + } +} + +- (void)showTitle +{ + UINavigationBar *navigationBar = [[[self view] subviews] objectAtIndex:0]; + UINavigationItem *navItem = [[navigationBar items] objectAtIndex:0]; + if (UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] orientation])) { + // Setup the title + if ([self type] == ASIProxyAuthenticationType) { + [navItem setPrompt:@"Login to this secure proxy server."]; + } else { + [navItem setPrompt:@"Login to this secure server."]; + } + } else { + [navItem setPrompt:nil]; + } + [navigationBar sizeToFit]; + CGRect f = [[self view] bounds]; + f.origin.y = [navigationBar frame].size.height; + f.size.height -= f.origin.y; + [[self tableView] setFrame:f]; +} + +- (void)show +{ + // Remove all subviews + UIView *v; + while ((v = [[[self view] subviews] lastObject])) { + [v removeFromSuperview]; + } + + // Setup toolbar + UINavigationBar *bar = [[[UINavigationBar alloc] init] autorelease]; + [bar setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; + + UINavigationItem *navItem = [[[UINavigationItem alloc] init] autorelease]; + bar.items = [NSArray arrayWithObject:navItem]; + + [[self view] addSubview:bar]; + + [self showTitle]; + + // Setup toolbar buttons + if ([self type] == ASIProxyAuthenticationType) { + [navItem setTitle:[[self request] proxyHost]]; + } else { + [navItem setTitle:[[[self request] url] host]]; + } + + [navItem setLeftBarButtonItem:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelAuthenticationFromDialog:)] autorelease]]; + [navItem setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithTitle:@"Login" style:UIBarButtonItemStyleDone target:self action:@selector(loginWithCredentialsFromDialog:)] autorelease]]; + + // We show the login form in a table view, similar to Safari's authentication dialog + [bar sizeToFit]; + CGRect f = [[self view] bounds]; + f.origin.y = [bar frame].size.height; + f.size.height -= f.origin.y; + + [self setTableView:[[[UITableView alloc] initWithFrame:f style:UITableViewStyleGrouped] autorelease]]; + [[self tableView] setDelegate:self]; + [[self tableView] setDataSource:self]; + [[self tableView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; + [[self view] addSubview:[self tableView]]; + + // Force reload the table content, and focus the first field to show the keyboard + [[self tableView] reloadData]; + [[[[[self tableView] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]].contentView subviews] objectAtIndex:0] becomeFirstResponder]; + +#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2 + if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { + [self setModalPresentationStyle:UIModalPresentationFormSheet]; + } +#endif + + //[[self presentingController] presentModalViewController:self animated:YES]; + [[self presentingController] presentViewController:self animated:YES completion:nil]; + +} + +#pragma mark button callbacks + +- (void)cancelAuthenticationFromDialog:(id)sender +{ + for (ASIHTTPRequest *theRequest in [self requestsRequiringTheseCredentials]) { + [theRequest cancelAuthentication]; + [requestsNeedingAuthentication removeObject:theRequest]; + } + [self dismiss]; +} + +- (NSArray *)requestsRequiringTheseCredentials +{ + NSMutableArray *requestsRequiringTheseCredentials = [NSMutableArray array]; + NSURL *requestURL = [[self request] url]; + for (ASIHTTPRequest *otherRequest in requestsNeedingAuthentication) { + NSURL *theURL = [otherRequest url]; + if (([otherRequest authenticationNeeded] == [[self request] authenticationNeeded]) && [[theURL host] isEqualToString:[requestURL host]] && ([theURL port] == [requestURL port] || ([requestURL port] && [[theURL port] isEqualToNumber:[requestURL port]])) && [[theURL scheme] isEqualToString:[requestURL scheme]] && ((![otherRequest authenticationRealm] && ![[self request] authenticationRealm]) || ([otherRequest authenticationRealm] && [[self request] authenticationRealm] && [[[self request] authenticationRealm] isEqualToString:[otherRequest authenticationRealm]]))) { + [requestsRequiringTheseCredentials addObject:otherRequest]; + } + } + [requestsRequiringTheseCredentials addObject:[self request]]; + return requestsRequiringTheseCredentials; +} + +- (void)presentNextDialog +{ + if ([requestsNeedingAuthentication count]) { + ASIHTTPRequest *nextRequest = [requestsNeedingAuthentication objectAtIndex:0]; + [requestsNeedingAuthentication removeObjectAtIndex:0]; + [[self class] presentAuthenticationDialogForRequest:nextRequest]; + } +} + + +- (void)loginWithCredentialsFromDialog:(id)sender +{ + for (ASIHTTPRequest *theRequest in [self requestsRequiringTheseCredentials]) { + + NSString *username = [[self usernameField] text]; + NSString *password = [[self passwordField] text]; + + if (username == nil) { username = @""; } + if (password == nil) { password = @""; } + + if ([self type] == ASIProxyAuthenticationType) { + [theRequest setProxyUsername:username]; + [theRequest setProxyPassword:password]; + } else { + [theRequest setUsername:username]; + [theRequest setPassword:password]; + } + + // Handle NTLM domains + NSString *scheme = ([self type] == ASIStandardAuthenticationType) ? [[self request] authenticationScheme] : [[self request] proxyAuthenticationScheme]; + if ([scheme isEqualToString:(NSString *)kCFHTTPAuthenticationSchemeNTLM]) { + NSString *domain = [[self domainField] text]; + if ([self type] == ASIProxyAuthenticationType) { + [theRequest setProxyDomain:domain]; + } else { + [theRequest setDomain:domain]; + } + } + + [theRequest retryUsingSuppliedCredentials]; + [requestsNeedingAuthentication removeObject:theRequest]; + } + [self dismiss]; +} + +#pragma mark table view data source + +- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView +{ + NSString *scheme = ([self type] == ASIStandardAuthenticationType) ? [[self request] authenticationScheme] : [[self request] proxyAuthenticationScheme]; + if ([scheme isEqualToString:(NSString *)kCFHTTPAuthenticationSchemeNTLM]) { + return 2; + } + return 1; +} + +- (CGFloat)tableView:(UITableView *)aTableView heightForFooterInSection:(NSInteger)section +{ + if (section == [self numberOfSectionsInTableView:aTableView]-1) { + return 30; + } + return 0; +} + +- (CGFloat)tableView:(UITableView *)aTableView heightForHeaderInSection:(NSInteger)section +{ + if (section == 0) { +#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2 + if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { + return 54; + } +#endif + return 30; + } + return 0; +} + +- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section +{ + if (section == 0) { + return [[self request] authenticationRealm]; + } + return nil; +} + +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath +{ +#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_0 + UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease]; +#else + UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,0,0) reuseIdentifier:nil] autorelease]; +#endif + + [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; + + CGRect f = CGRectInset([cell bounds], 10, 10); + UITextField *textField = [[[UITextField alloc] initWithFrame:f] autorelease]; + [textField setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; + [textField setAutocapitalizationType:UITextAutocapitalizationTypeNone]; + [textField setAutocorrectionType:UITextAutocorrectionTypeNo]; + + NSUInteger s = [indexPath section]; + NSUInteger r = [indexPath row]; + + if (s == kUsernameSection && r == kUsernameRow) { + [textField setPlaceholder:@"User"]; + } else if (s == kPasswordSection && r == kPasswordRow) { + [textField setPlaceholder:@"Password"]; + [textField setSecureTextEntry:YES]; + } else if (s == kDomainSection && r == kDomainRow) { + [textField setPlaceholder:@"Domain"]; + } + [cell.contentView addSubview:textField]; + + return cell; +} + +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section +{ + if (section == 0) { + return 2; + } else { + return 1; + } +} + +- (NSString *)tableView:(UITableView *)aTableView titleForFooterInSection:(NSInteger)section +{ + if (section == [self numberOfSectionsInTableView:aTableView]-1) { + // If we're using Basic authentication and the connection is not using SSL, we'll show the plain text message + if ([[[self request] authenticationScheme] isEqualToString:(NSString *)kCFHTTPAuthenticationSchemeBasic] && ![[[[self request] url] scheme] isEqualToString:@"https"]) { + return @"Password will be sent in the clear."; + // We are using Digest, NTLM, or any scheme over SSL + } else { + return @"Password will be sent securely."; + } + } + return nil; +} + +#pragma mark - + +@synthesize request; +@synthesize type; +@synthesize tableView; +@synthesize didEnableRotationNotifications; +@synthesize presentingController; +@end diff --git a/ASICacheDelegate.h b/ASICacheDelegate.h new file mode 100644 index 0000000..e94c4fd --- /dev/null +++ b/ASICacheDelegate.h @@ -0,0 +1,55 @@ +// +// ASICacheDelegate.h +// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest +// +// Created by Ben Copsey on 01/05/2010. +// Copyright 2010 All-Seeing Interactive. All rights reserved. +// + +#import +@class ASIHTTPRequest; + +typedef enum _ASICachePolicy { + ASIDefaultCachePolicy = 0, + ASIIgnoreCachePolicy = 1, + ASIReloadIfDifferentCachePolicy = 2, + ASIOnlyLoadIfNotCachedCachePolicy = 3, + ASIUseCacheIfLoadFailsCachePolicy = 4 +} ASICachePolicy; + +typedef enum _ASICacheStoragePolicy { + ASICacheForSessionDurationCacheStoragePolicy = 0, + ASICachePermanentlyCacheStoragePolicy = 1 +} ASICacheStoragePolicy; + + +@protocol ASICacheDelegate + +@required + +// Should return the cache policy that will be used when requests have their cache policy set to ASIDefaultCachePolicy +- (ASICachePolicy)defaultCachePolicy; + +// Should Remove cached data for a particular request +- (void)removeCachedDataForRequest:(ASIHTTPRequest *)request; + +// Should return YES if the cache considers its cached response current for the request +// Should return NO is the data is not cached, or (for example) if the cached headers state the request should have expired +- (BOOL)isCachedDataCurrentForRequest:(ASIHTTPRequest *)request; + +// Should store the response for the passed request in the cache +// When a non-zero maxAge is passed, it should be used as the expiry time for the cached response +- (void)storeResponseForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge; + +// Should return an NSDictionary of cached headers for the passed request, if it is stored in the cache +- (NSDictionary *)cachedHeadersForRequest:(ASIHTTPRequest *)request; + +// Should return the cached body of a response for the passed request, if it is stored in the cache +- (NSData *)cachedResponseDataForRequest:(ASIHTTPRequest *)request; + +// Same as the above, but returns a path to the cached response body instead +- (NSString *)pathToCachedResponseDataForRequest:(ASIHTTPRequest *)request; + +// Clear cached data stored for the passed storage policy +- (void)clearCachedResponsesForStoragePolicy:(ASICacheStoragePolicy)cachePolicy; +@end diff --git a/ASIDownloadCache.h b/ASIDownloadCache.h new file mode 100644 index 0000000..b751060 --- /dev/null +++ b/ASIDownloadCache.h @@ -0,0 +1,47 @@ +// +// ASIDownloadCache.h +// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest +// +// Created by Ben Copsey on 01/05/2010. +// Copyright 2010 All-Seeing Interactive. All rights reserved. +// + +#import +#import "ASICacheDelegate.h" + +@interface ASIDownloadCache : NSObject { + + // The default cache policy for this cache + // Requests that store data in the cache will use this cache policy if their cache policy is set to ASIDefaultCachePolicy + // Defaults to ASIReloadIfDifferentCachePolicy + ASICachePolicy defaultCachePolicy; + + // The directory in which cached data will be stored + // Defaults to a directory called 'ASIHTTPRequestCache' in the temporary directory + NSString *storagePath; + + // Mediates access to the cache + NSRecursiveLock *accessLock; + + // When YES, the cache will look for cache-control / pragma: no-cache headers, and won't reuse store responses if it finds them + BOOL shouldRespectCacheControlHeaders; +} + +// Returns a static instance of an ASIDownloadCache +// In most circumstances, it will make sense to use this as a global cache, rather than creating your own cache +// To make ASIHTTPRequests use it automatically, use [ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]]; ++ (id)sharedCache; + +// A helper function that determines if the server has requested data should not be cached by looking at the request's response headers ++ (BOOL)serverAllowsResponseCachingForRequest:(ASIHTTPRequest *)request; + +// A date formatter that can be used to construct an RFC 1123 date +// The returned formatter is safe to use on the calling thread +// Do not use this formatter for parsing dates because the format can vary slightly - use ASIHTTPRequest's dateFromRFC1123String: class method instead ++ (NSDateFormatter *)rfc1123DateFormatter; + +@property (assign, nonatomic) ASICachePolicy defaultCachePolicy; +@property (retain, nonatomic) NSString *storagePath; +@property (retain) NSRecursiveLock *accessLock; +@property (assign) BOOL shouldRespectCacheControlHeaders; +@end diff --git a/ASIDownloadCache.m b/ASIDownloadCache.m new file mode 100644 index 0000000..05ffdce --- /dev/null +++ b/ASIDownloadCache.m @@ -0,0 +1,387 @@ +// +// ASIDownloadCache.m +// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest +// +// Created by Ben Copsey on 01/05/2010. +// Copyright 2010 All-Seeing Interactive. All rights reserved. +// + +#import "ASIDownloadCache.h" +#import "ASIHTTPRequest.h" +#import + +static ASIDownloadCache *sharedCache = nil; + +static NSString *sessionCacheFolder = @"SessionStore"; +static NSString *permanentCacheFolder = @"PermanentStore"; + +@interface ASIDownloadCache () ++ (NSString *)keyForRequest:(ASIHTTPRequest *)request; +@end + +@implementation ASIDownloadCache + +- (id)init +{ + self = [super init]; + [self setShouldRespectCacheControlHeaders:YES]; + [self setDefaultCachePolicy:ASIReloadIfDifferentCachePolicy]; + [self setAccessLock:[[[NSRecursiveLock alloc] init] autorelease]]; + return self; +} + ++ (id)sharedCache +{ + if (!sharedCache) { + sharedCache = [[self alloc] init]; + [sharedCache setStoragePath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"ASIHTTPRequestCache"]]; + + } + return sharedCache; +} + +- (void)dealloc +{ + [storagePath release]; + [accessLock release]; + [super dealloc]; +} + +- (NSString *)storagePath +{ + [[self accessLock] lock]; + NSString *p = [[storagePath retain] autorelease]; + [[self accessLock] unlock]; + return p; +} + + +- (void)setStoragePath:(NSString *)path +{ + [[self accessLock] lock]; + [self clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; + [storagePath release]; + storagePath = [path retain]; + BOOL isDirectory = NO; + NSArray *directories = [NSArray arrayWithObjects:path,[path stringByAppendingPathComponent:sessionCacheFolder],[path stringByAppendingPathComponent:permanentCacheFolder],nil]; + for (NSString *directory in directories) { + BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:directory isDirectory:&isDirectory]; + if (exists && !isDirectory) { + [[self accessLock] unlock]; + [NSException raise:@"FileExistsAtCachePath" format:@"Cannot create a directory for the cache at '%@', because a file already exists",directory]; + } else if (!exists) { + [[NSFileManager defaultManager] createDirectoryAtPath:directory withIntermediateDirectories:NO attributes:nil error:nil]; + if (![[NSFileManager defaultManager] fileExistsAtPath:directory]) { + [[self accessLock] unlock]; + [NSException raise:@"FailedToCreateCacheDirectory" format:@"Failed to create a directory for the cache at '%@'",directory]; + } + } + } + [self clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; + [[self accessLock] unlock]; +} + +- (void)storeResponseForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge +{ + [[self accessLock] lock]; + + if ([request error] || ![request responseHeaders] || ([request responseStatusCode] != 200)) { + [[self accessLock] unlock]; + return; + } + + if ([self shouldRespectCacheControlHeaders] && ![[self class] serverAllowsResponseCachingForRequest:request]) { + [[self accessLock] unlock]; + return; + } + + // If the request is set to use the default policy, use this cache's default policy + ASICachePolicy policy = [request cachePolicy]; + if (policy == ASIDefaultCachePolicy) { + policy = [self defaultCachePolicy]; + } + + if (policy == ASIIgnoreCachePolicy) { + [[self accessLock] unlock]; + return; + } + NSString *path = nil; + if ([request cacheStoragePolicy] == ASICacheForSessionDurationCacheStoragePolicy) { + path = [[self storagePath] stringByAppendingPathComponent:sessionCacheFolder]; + } else { + path = [[self storagePath] stringByAppendingPathComponent:permanentCacheFolder]; + } + path = [path stringByAppendingPathComponent:[[self class] keyForRequest:request]]; + NSString *metadataPath = [path stringByAppendingPathExtension:@"cachedheaders"]; + NSString *dataPath = [path stringByAppendingPathExtension:@"cacheddata"]; + + NSMutableDictionary *responseHeaders = [NSMutableDictionary dictionaryWithDictionary:[request responseHeaders]]; + if ([request isResponseCompressed]) { + [responseHeaders removeObjectForKey:@"Content-Encoding"]; + } + if (maxAge != 0) { + [responseHeaders removeObjectForKey:@"Expires"]; + [responseHeaders setObject:[NSString stringWithFormat:@"max-age=%i",(int)maxAge] forKey:@"Cache-Control"]; + } + // We use this special key to help expire the request when we get a max-age header + [responseHeaders setObject:[[[self class] rfc1123DateFormatter] stringFromDate:[NSDate date]] forKey:@"X-ASIHTTPRequest-Fetch-date"]; + [responseHeaders writeToFile:metadataPath atomically:NO]; + + if ([request responseData]) { + [[request responseData] writeToFile:dataPath atomically:NO]; + } else if ([request downloadDestinationPath]) { + NSError *error = nil; + [[NSFileManager defaultManager] copyItemAtPath:[request downloadDestinationPath] toPath:dataPath error:&error]; + } + [[self accessLock] unlock]; +} + +- (NSDictionary *)cachedHeadersForRequest:(ASIHTTPRequest *)request +{ + [[self accessLock] lock]; + if (![self storagePath]) { + [[self accessLock] unlock]; + return nil; + } + // Look in the session store + NSString *path = [[self storagePath] stringByAppendingPathComponent:sessionCacheFolder]; + NSString *dataPath = [path stringByAppendingPathComponent:[[[self class] keyForRequest:request] stringByAppendingPathExtension:@"cachedheaders"]]; + if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]) { + [[self accessLock] unlock]; + return [NSDictionary dictionaryWithContentsOfFile:dataPath]; + } + // Look in the permanent store + path = [[self storagePath] stringByAppendingPathComponent:permanentCacheFolder]; + dataPath = [path stringByAppendingPathComponent:[[[self class] keyForRequest:request] stringByAppendingPathExtension:@"cachedheaders"]]; + if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]) { + [[self accessLock] unlock]; + return [NSDictionary dictionaryWithContentsOfFile:dataPath]; + } + [[self accessLock] unlock]; + return nil; +} + +- (NSData *)cachedResponseDataForRequest:(ASIHTTPRequest *)request +{ + NSString *path = [self pathToCachedResponseDataForRequest:request]; + if (path) { + return [NSData dataWithContentsOfFile:path]; + } + return nil; +} + +- (NSString *)pathToCachedResponseDataForRequest:(ASIHTTPRequest *)request +{ + [[self accessLock] lock]; + if (![self storagePath]) { + [[self accessLock] unlock]; + return nil; + } + // Look in the session store + NSString *path = [[self storagePath] stringByAppendingPathComponent:sessionCacheFolder]; + NSString *dataPath = [path stringByAppendingPathComponent:[[[self class] keyForRequest:request] stringByAppendingPathExtension:@"cacheddata"]]; + if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]) { + [[self accessLock] unlock]; + return dataPath; + } + // Look in the permanent store + path = [[self storagePath] stringByAppendingPathComponent:permanentCacheFolder]; + dataPath = [path stringByAppendingPathComponent:[[[self class] keyForRequest:request] stringByAppendingPathExtension:@"cacheddata"]]; + if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]) { + [[self accessLock] unlock]; + return dataPath; + } + [[self accessLock] unlock]; + return nil; +} + +- (void)removeCachedDataForRequest:(ASIHTTPRequest *)request +{ + [[self accessLock] lock]; + if (![self storagePath]) { + [[self accessLock] unlock]; + return; + } + NSString *cachedHeadersPath = [[self storagePath] stringByAppendingPathComponent:[[[self class] keyForRequest:request] stringByAppendingPathExtension:@"cachedheaders"]]; + if (!cachedHeadersPath) { + [[self accessLock] unlock]; + return; + } + NSString *dataPath = [self pathToCachedResponseDataForRequest:request]; + if (!dataPath) { + [[self accessLock] unlock]; + return; + } + [[NSFileManager defaultManager] removeItemAtPath:cachedHeadersPath error:NULL]; + [[NSFileManager defaultManager] removeItemAtPath:dataPath error:NULL]; + [[self accessLock] unlock]; +} + +- (BOOL)isCachedDataCurrentForRequest:(ASIHTTPRequest *)request +{ + [[self accessLock] lock]; + if (![self storagePath]) { + [[self accessLock] unlock]; + return NO; + } + NSDictionary *cachedHeaders = [self cachedHeadersForRequest:request]; + if (!cachedHeaders) { + [[self accessLock] unlock]; + return NO; + } + NSString *dataPath = [self pathToCachedResponseDataForRequest:request]; + if (!dataPath) { + [[self accessLock] unlock]; + return NO; + } + + if ([self shouldRespectCacheControlHeaders]) { + + // Look for an Expires header to see if the content is out of data + NSString *expires = [cachedHeaders objectForKey:@"Expires"]; + if (expires) { + if ([[ASIHTTPRequest dateFromRFC1123String:expires] timeIntervalSinceNow] < 0) { + [[self accessLock] unlock]; + return NO; + } + } + // Look for a max-age header + NSString *cacheControl = [[cachedHeaders objectForKey:@"Cache-Control"] lowercaseString]; + if (cacheControl) { + NSScanner *scanner = [NSScanner scannerWithString:cacheControl]; + if ([scanner scanString:@"max-age" intoString:NULL]) { + [scanner scanString:@"=" intoString:NULL]; + NSTimeInterval maxAge = 0; + [scanner scanDouble:&maxAge]; + NSDate *fetchDate = [ASIHTTPRequest dateFromRFC1123String:[cachedHeaders objectForKey:@"X-ASIHTTPRequest-Fetch-date"]]; + + NSDate *expiryDate = [[[NSDate alloc] initWithTimeInterval:maxAge sinceDate:fetchDate] autorelease]; + + if ([expiryDate timeIntervalSinceNow] < 0) { + [[self accessLock] unlock]; + return NO; + } + } + } + + } + + // If we already have response headers for this request, check to see if the new content is different + if ([request responseHeaders] && [request responseStatusCode] != 304) { + // If the Etag or Last-Modified date are different from the one we have, fetch the document again + NSArray *headersToCompare = [NSArray arrayWithObjects:@"Etag",@"Last-Modified",nil]; + for (NSString *header in headersToCompare) { + if (![[[request responseHeaders] objectForKey:header] isEqualToString:[cachedHeaders objectForKey:header]]) { + [[self accessLock] unlock]; + return NO; + } + } + } + [[self accessLock] unlock]; + return YES; +} + +- (ASICachePolicy)defaultCachePolicy +{ + [[self accessLock] lock]; + ASICachePolicy cp = defaultCachePolicy; + [[self accessLock] unlock]; + return cp; +} + + +- (void)setDefaultCachePolicy:(ASICachePolicy)cachePolicy +{ + [[self accessLock] lock]; + if (cachePolicy == ASIDefaultCachePolicy) { + defaultCachePolicy = ASIReloadIfDifferentCachePolicy; + } else { + defaultCachePolicy = cachePolicy; + } + [[self accessLock] unlock]; +} + +- (void)clearCachedResponsesForStoragePolicy:(ASICacheStoragePolicy)storagePolicy +{ + [[self accessLock] lock]; + if (![self storagePath]) { + [[self accessLock] unlock]; + return; + } + NSString *path; + if (storagePolicy == ASICachePermanentlyCacheStoragePolicy) { + path = [[self storagePath] stringByAppendingPathComponent:permanentCacheFolder]; + } else { + path = [[self storagePath] stringByAppendingPathComponent:sessionCacheFolder]; + } + BOOL isDirectory = NO; + BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]; + if ((exists && !isDirectory) || !exists) { + [[self accessLock] unlock]; + return; + } + NSError *error = nil; + NSArray *cacheFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error]; + if (error) { + [[self accessLock] unlock]; + [NSException raise:@"FailedToTraverseCacheDirectory" format:@"Listing cache directory failed at path '%@'",path]; + } + for (NSString *file in cacheFiles) { + NSString *extension = [file pathExtension]; + if ([extension isEqualToString:@"cacheddata"] || [extension isEqualToString:@"cachedheaders"]) { + [[NSFileManager defaultManager] removeItemAtPath:[path stringByAppendingPathComponent:file] error:&error]; + if (error) { + [[self accessLock] unlock]; + [NSException raise:@"FailedToRemoveCacheFile" format:@"Failed to remove cached data at path '%@'",path]; + } + } + } + [[self accessLock] unlock]; +} + ++ (BOOL)serverAllowsResponseCachingForRequest:(ASIHTTPRequest *)request +{ + NSString *cacheControl = [[[request responseHeaders] objectForKey:@"Cache-Control"] lowercaseString]; + if (cacheControl) { + if ([cacheControl isEqualToString:@"no-cache"] || [cacheControl isEqualToString:@"no-store"]) { + return NO; + } + } + NSString *pragma = [[[request responseHeaders] objectForKey:@"Pragma"] lowercaseString]; + if (pragma) { + if ([pragma isEqualToString:@"no-cache"]) { + return NO; + } + } + return YES; +} + +// Borrowed from: http://stackoverflow.com/questions/652300/using-md5-hash-on-a-string-in-cocoa ++ (NSString *)keyForRequest:(ASIHTTPRequest *)request +{ + const char *cStr = [[[request url] absoluteString] UTF8String]; + unsigned char result[16]; + CC_MD5(cStr, (CC_LONG)strlen(cStr), result); + return [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],result[8], result[9], result[10], result[11],result[12], result[13], result[14], result[15]]; +} + ++ (NSDateFormatter *)rfc1123DateFormatter +{ + NSMutableDictionary *threadDict = [[NSThread currentThread] threadDictionary]; + NSDateFormatter *dateFormatter = [threadDict objectForKey:@"ASIDownloadCacheDateFormatter"]; + if (dateFormatter == nil) { + dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; + [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]]; + [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; + [dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss 'GMT'"]; + [threadDict setObject:dateFormatter forKey:@"ASIDownloadCacheDateFormatter"]; + } + return dateFormatter; +} + + +@synthesize storagePath; +@synthesize defaultCachePolicy; +@synthesize accessLock; +@synthesize shouldRespectCacheControlHeaders; +@end diff --git a/ASIFormDataRequest.h b/ASIFormDataRequest.h new file mode 100644 index 0000000..e206fd2 --- /dev/null +++ b/ASIFormDataRequest.h @@ -0,0 +1,76 @@ +// +// ASIFormDataRequest.h +// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest +// +// Created by Ben Copsey on 07/11/2008. +// Copyright 2008-2009 All-Seeing Interactive. All rights reserved. +// + +#import +#import "ASIHTTPRequest.h" +#import "ASIHTTPRequestConfig.h" + +typedef enum _ASIPostFormat { + ASIMultipartFormDataPostFormat = 0, + ASIURLEncodedPostFormat = 1 + +} ASIPostFormat; + +@interface ASIFormDataRequest : ASIHTTPRequest { + + // Parameters that will be POSTed to the url + NSMutableArray *postData; + + // Files that will be POSTed to the url + NSMutableArray *fileData; + + ASIPostFormat postFormat; + + NSStringEncoding stringEncoding; + +#if DEBUG_FORM_DATA_REQUEST + // Will store a string version of the request body that will be printed to the console when ASIHTTPREQUEST_DEBUG is set in GCC_PREPROCESSOR_DEFINITIONS + NSString *debugBodyString; +#endif + +} + +#pragma mark utilities +- (NSString*)encodeURL:(NSString *)string; + +#pragma mark setup request + +// Add a POST variable to the request +- (void)addPostValue:(id )value forKey:(NSString *)key; + +// Set a POST variable for this request, clearing any others with the same key +- (void)setPostValue:(id )value forKey:(NSString *)key; + +// Add the contents of a local file to the request +- (void)addFile:(NSString *)filePath forKey:(NSString *)key; + +// Same as above, but you can specify the content-type and file name +- (void)addFile:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key; + +// Add the contents of a local file to the request, clearing any others with the same key +- (void)setFile:(NSString *)filePath forKey:(NSString *)key; + +// Same as above, but you can specify the content-type and file name +- (void)setFile:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key; + +// Add the contents of an NSData object to the request +- (void)addData:(NSData *)data forKey:(NSString *)key; + +// Same as above, but you can specify the content-type and file name +- (void)addData:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key; + +// Add the contents of an NSData object to the request, clearing any others with the same key +- (void)setData:(NSData *)data forKey:(NSString *)key; + +// Same as above, but you can specify the content-type and file name +- (void)setData:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key; + + +@property (assign) ASIPostFormat postFormat; +@property (assign) NSStringEncoding stringEncoding; +@end diff --git a/ASIFormDataRequest.m b/ASIFormDataRequest.m new file mode 100644 index 0000000..3149c14 --- /dev/null +++ b/ASIFormDataRequest.m @@ -0,0 +1,355 @@ +// +// ASIFormDataRequest.m +// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest +// +// Created by Ben Copsey on 07/11/2008. +// Copyright 2008-2009 All-Seeing Interactive. All rights reserved. +// + +#import "ASIFormDataRequest.h" + + +// Private stuff +@interface ASIFormDataRequest () +- (void)buildMultipartFormDataPostBody; +- (void)buildURLEncodedPostBody; +- (void)appendPostString:(NSString *)string; + +@property (retain) NSMutableArray *postData; +@property (retain) NSMutableArray *fileData; + +#if DEBUG_FORM_DATA_REQUEST +- (void)addToDebugBody:(NSString *)string; +@property (retain, nonatomic) NSString *debugBodyString; +#endif + +@end + +@implementation ASIFormDataRequest + +#pragma mark utilities +- (NSString*)encodeURL:(NSString *)string +{ + NSString *newString = NSMakeCollectable([(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding([self stringEncoding])) autorelease]); + if (newString) { + return newString; + } + return @""; +} + +#pragma mark init / dealloc + ++ (id)requestWithURL:(NSURL *)newURL +{ + return [[[self alloc] initWithURL:newURL] autorelease]; +} + +- (id)initWithURL:(NSURL *)newURL +{ + self = [super initWithURL:newURL]; + [self setPostFormat:ASIURLEncodedPostFormat]; + [self setStringEncoding:NSUTF8StringEncoding]; + return self; +} + +- (void)dealloc +{ +#if DEBUG_FORM_DATA_REQUEST + [debugBodyString release]; +#endif + + [postData release]; + [fileData release]; + [super dealloc]; +} + +#pragma mark setup request + +- (void)addPostValue:(id )value forKey:(NSString *)key +{ + if (![self postData]) { + [self setPostData:[NSMutableArray array]]; + } + [[self postData] addObject:[NSDictionary dictionaryWithObjectsAndKeys:[value description],@"value",key,@"key",nil]]; +} + +- (void)setPostValue:(id )value forKey:(NSString *)key +{ + // Remove any existing value + NSUInteger i; + for (i=0; i<[[self postData] count]; i++) { + NSDictionary *val = [[self postData] objectAtIndex:i]; + if ([[val objectForKey:@"key"] isEqualToString:key]) { + [[self postData] removeObjectAtIndex:i]; + i--; + } + } + [self addPostValue:value forKey:key]; +} + + +- (void)addFile:(NSString *)filePath forKey:(NSString *)key +{ + [self addFile:filePath withFileName:nil andContentType:nil forKey:key]; +} + +- (void)addFile:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key +{ + if (![self fileData]) { + [self setFileData:[NSMutableArray array]]; + } + + // If data is a path to a local file + if ([data isKindOfClass:[NSString class]]) { + BOOL isDirectory = NO; + BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:(NSString *)data isDirectory:&isDirectory]; + if (!fileExists || isDirectory) { + [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"No file exists at %@",data],NSLocalizedDescriptionKey,nil]]]; + } + + // If the caller didn't specify a custom file name, we'll use the file name of the file we were passed + if (!fileName) { + fileName = [(NSString *)data lastPathComponent]; + } + + // If we were given the path to a file, and the user didn't specify a mime type, we can detect it from the file extension + if (!contentType) { + contentType = [ASIHTTPRequest mimeTypeForFileAtPath:data]; + } + } + + NSDictionary *fileInfo = [NSDictionary dictionaryWithObjectsAndKeys:data, @"data", contentType, @"contentType", fileName, @"fileName", key, @"key", nil]; + [[self fileData] addObject:fileInfo]; +} + + +- (void)setFile:(NSString *)filePath forKey:(NSString *)key +{ + [self setFile:filePath withFileName:nil andContentType:nil forKey:key]; +} + +- (void)setFile:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key +{ + // Remove any existing value + NSUInteger i; + for (i=0; i<[[self fileData] count]; i++) { + NSDictionary *val = [[self fileData] objectAtIndex:i]; + if ([[val objectForKey:@"key"] isEqualToString:key]) { + [[self fileData] removeObjectAtIndex:i]; + i--; + } + } + [self addFile:data withFileName:fileName andContentType:contentType forKey:key]; +} + +- (void)addData:(NSData *)data forKey:(NSString *)key +{ + [self addData:data withFileName:@"file" andContentType:nil forKey:key]; +} + +- (void)addData:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key +{ + if (![self fileData]) { + [self setFileData:[NSMutableArray array]]; + } + if (!contentType) { + contentType = @"application/octet-stream"; + } + + NSDictionary *fileInfo = [NSDictionary dictionaryWithObjectsAndKeys:data, @"data", contentType, @"contentType", fileName, @"fileName", key, @"key", nil]; + [[self fileData] addObject:fileInfo]; +} + +- (void)setData:(NSData *)data forKey:(NSString *)key +{ + [self setData:data withFileName:@"file" andContentType:nil forKey:key]; +} + +- (void)setData:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key +{ + // Remove any existing value + NSUInteger i; + for (i=0; i<[[self fileData] count]; i++) { + NSDictionary *val = [[self fileData] objectAtIndex:i]; + if ([[val objectForKey:@"key"] isEqualToString:key]) { + [[self fileData] removeObjectAtIndex:i]; + i--; + } + } + [self addData:data withFileName:fileName andContentType:contentType forKey:key]; +} + +- (void)buildPostBody +{ + if ([self haveBuiltPostBody]) { + return; + } + +#if DEBUG_FORM_DATA_REQUEST + [self setDebugBodyString:@""]; +#endif + + if (![self postData] && ![self fileData]) { + [super buildPostBody]; + return; + } + if ([[self fileData] count] > 0) { + [self setShouldStreamPostDataFromDisk:YES]; + } + + if ([self postFormat] == ASIURLEncodedPostFormat) { + [self buildURLEncodedPostBody]; + } else { + [self buildMultipartFormDataPostBody]; + } + + [super buildPostBody]; + +#if DEBUG_FORM_DATA_REQUEST + ////nslog(@"%@",[self debugBodyString]); + [self setDebugBodyString:nil]; +#endif +} + + +- (void)buildMultipartFormDataPostBody +{ +#if DEBUG_FORM_DATA_REQUEST + [self addToDebugBody:@"\r\n==== Building a multipart/form-data body ====\r\n"]; +#endif + + NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding([self stringEncoding])); + + // Set your own boundary string only if really obsessive. We don't bother to check if post data contains the boundary, since it's pretty unlikely that it does. + NSString *stringBoundary = @"0xKhTmLbOuNdArY"; + + [self addRequestHeader:@"Content-Type" value:[NSString stringWithFormat:@"multipart/form-data; charset=%@; boundary=%@", charset, stringBoundary]]; + + [self appendPostString:[NSString stringWithFormat:@"--%@\r\n",stringBoundary]]; + + // Adds post data + NSString *endItemBoundary = [NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary]; + NSUInteger i=0; + for (NSDictionary *val in [self postData]) { + [self appendPostString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",[val objectForKey:@"key"]]]; + [self appendPostString:[val objectForKey:@"value"]]; + i++; + if (i != [[self postData] count] || [[self fileData] count] > 0) { //Only add the boundary if this is not the last item in the post body + [self appendPostString:endItemBoundary]; + } + } + + // Adds files to upload + i=0; + for (NSDictionary *val in [self fileData]) { + + [self appendPostString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", [val objectForKey:@"key"], [val objectForKey:@"fileName"]]]; + [self appendPostString:[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", [val objectForKey:@"contentType"]]]; + + id data = [val objectForKey:@"data"]; + if ([data isKindOfClass:[NSString class]]) { + [self appendPostDataFromFile:data]; + } else { + [self appendPostData:data]; + } + i++; + // Only add the boundary if this is not the last item in the post body + if (i != [[self fileData] count]) { + [self appendPostString:endItemBoundary]; + } + } + + [self appendPostString:[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary]]; + +#if DEBUG_FORM_DATA_REQUEST + [self addToDebugBody:@"==== End of multipart/form-data body ====\r\n"]; +#endif +} + +- (void)buildURLEncodedPostBody +{ + + // We can't post binary data using application/x-www-form-urlencoded + if ([[self fileData] count] > 0) { + [self setPostFormat:ASIMultipartFormDataPostFormat]; + [self buildMultipartFormDataPostBody]; + return; + } + +#if DEBUG_FORM_DATA_REQUEST + [self addToDebugBody:@"\r\n==== Building an application/x-www-form-urlencoded body ====\r\n"]; +#endif + + + NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding([self stringEncoding])); + + [self addRequestHeader:@"Content-Type" value:[NSString stringWithFormat:@"application/x-www-form-urlencoded; charset=%@",charset]]; + + + NSUInteger i=0; + NSUInteger count = [[self postData] count]-1; + for (NSDictionary *val in [self postData]) { + NSString *data = [NSString stringWithFormat:@"%@=%@%@", [self encodeURL:[val objectForKey:@"key"]], [self encodeURL:[val objectForKey:@"value"]],(i +#if TARGET_OS_IPHONE + #import +#endif +#import +#import "ASIHTTPRequestConfig.h" +#import "ASIHTTPRequestDelegate.h" +#import "ASIProgressDelegate.h" +#import "ASICacheDelegate.h" + +extern NSString *ASIHTTPRequestVersion; + +// Make targeting different platforms more reliable +// See: http://www.blumtnwerx.com/blog/2009/06/cross-sdk-code-hygiene-in-xcode/ +#ifndef __IPHONE_3_2 + #define __IPHONE_3_2 30200 +#endif +#ifndef __IPHONE_4_0 + #define __IPHONE_4_0 40000 +#endif +#ifndef __MAC_10_5 + #define __MAC_10_5 1050 +#endif +#ifndef __MAC_10_6 + #define __MAC_10_6 1060 +#endif + +typedef enum _ASIAuthenticationState { + ASINoAuthenticationNeededYet = 0, + ASIHTTPAuthenticationNeeded = 1, + ASIProxyAuthenticationNeeded = 2 +} ASIAuthenticationState; + +typedef enum _ASINetworkErrorType { + ASIConnectionFailureErrorType = 1, + ASIRequestTimedOutErrorType = 2, + ASIAuthenticationErrorType = 3, + ASIRequestCancelledErrorType = 4, + ASIUnableToCreateRequestErrorType = 5, + ASIInternalErrorWhileBuildingRequestType = 6, + ASIInternalErrorWhileApplyingCredentialsType = 7, + ASIFileManagementError = 8, + ASITooMuchRedirectionErrorType = 9, + ASIUnhandledExceptionError = 10 + +} ASINetworkErrorType; + +// The error domain that all errors generated by ASIHTTPRequest use +extern NSString* const NetworkRequestErrorDomain; + +// You can use this number to throttle upload and download bandwidth in iPhone OS apps send or receive a large amount of data +// This may help apps that might otherwise be rejected for inclusion into the app store for using excessive bandwidth +// This number is not official, as far as I know there is no officially documented bandwidth limit +extern unsigned long const ASIWWANBandwidthThrottleAmount; + +@interface ASIHTTPRequest : NSOperation { + + // The url for this operation, should include GET params in the query string where appropriate + NSURL *url; + + // Will always contain the original url used for making the request (the value of url can change when a request is redirected) + NSURL *originalURL; + + // The delegate, you need to manage setting and talking to your delegate in your subclasses + id delegate; + + // Another delegate that is also notified of request status changes and progress updates + // Generally, you won't use this directly, but ASINetworkQueue sets itself as the queue so it can proxy updates to its own delegates + id queue; + + // HTTP method to use (GET / POST / PUT / DELETE / HEAD). Defaults to GET + NSString *requestMethod; + + // Request body - only used when the whole body is stored in memory (shouldStreamPostDataFromDisk is false) + NSMutableData *postBody; + + // gzipped request body used when shouldCompressRequestBody is YES + NSData *compressedPostBody; + + // When true, post body will be streamed from a file on disk, rather than loaded into memory at once (useful for large uploads) + // Automatically set to true in ASIFormDataRequests when using setFile:forKey: + BOOL shouldStreamPostDataFromDisk; + + // Path to file used to store post body (when shouldStreamPostDataFromDisk is true) + // You can set this yourself - useful if you want to PUT a file from local disk + NSString *postBodyFilePath; + + // Path to a temporary file used to store a deflated post body (when shouldCompressPostBody is YES) + NSString *compressedPostBodyFilePath; + + // Set to true when ASIHTTPRequest automatically created a temporary file containing the request body (when true, the file at postBodyFilePath will be deleted at the end of the request) + BOOL didCreateTemporaryPostDataFile; + + // Used when writing to the post body when shouldStreamPostDataFromDisk is true (via appendPostData: or appendPostDataFromFile:) + NSOutputStream *postBodyWriteStream; + + // Used for reading from the post body when sending the request + NSInputStream *postBodyReadStream; + + // Dictionary for custom HTTP request headers + NSMutableDictionary *requestHeaders; + + // Set to YES when the request header dictionary has been populated, used to prevent this happening more than once + BOOL haveBuiltRequestHeaders; + + // Will be populated with HTTP response headers from the server + NSDictionary *responseHeaders; + + // Can be used to manually insert cookie headers to a request, but it's more likely that sessionCookies will do this for you + NSMutableArray *requestCookies; + + // Will be populated with cookies + NSArray *responseCookies; + + // If use useCookiePersistence is true, network requests will present valid cookies from previous requests + BOOL useCookiePersistence; + + // If useKeychainPersistence is true, network requests will attempt to read credentials from the keychain, and will save them in the keychain when they are successfully presented + BOOL useKeychainPersistence; + + // If useSessionPersistence is true, network requests will save credentials and reuse for the duration of the session (until clearSession is called) + BOOL useSessionPersistence; + + // If allowCompressedResponse is true, requests will inform the server they can accept compressed data, and will automatically decompress gzipped responses. Default is true. + BOOL allowCompressedResponse; + + // If shouldCompressRequestBody is true, the request body will be gzipped. Default is false. + // You will probably need to enable this feature on your webserver to make this work. Tested with apache only. + BOOL shouldCompressRequestBody; + + // When downloadDestinationPath is set, the result of this request will be downloaded to the file at this location + // If downloadDestinationPath is not set, download data will be stored in memory + NSString *downloadDestinationPath; + + //The location that files will be downloaded to. Once a download is complete, files will be decompressed (if necessary) and moved to downloadDestinationPath + NSString *temporaryFileDownloadPath; + + // Used for writing data to a file when downloadDestinationPath is set + NSOutputStream *fileDownloadOutputStream; + + // When the request fails or completes successfully, complete will be true + BOOL complete; + + // If an error occurs, error will contain an NSError + // If error code is = ASIConnectionFailureErrorType (1, Connection failure occurred) - inspect [[error userInfo] objectForKey:NSUnderlyingErrorKey] for more information + NSError *error; + + // Username and password used for authentication + NSString *username; + NSString *password; + + // Domain used for NTLM authentication + NSString *domain; + + // Username and password used for proxy authentication + NSString *proxyUsername; + NSString *proxyPassword; + + // Domain used for NTLM proxy authentication + NSString *proxyDomain; + + // Delegate for displaying upload progress (usually an NSProgressIndicator, but you can supply a different object and handle this yourself) + id uploadProgressDelegate; + + // Delegate for displaying download progress (usually an NSProgressIndicator, but you can supply a different object and handle this yourself) + id downloadProgressDelegate; + + // Whether we've seen the headers of the response yet + BOOL haveExaminedHeaders; + + // Data we receive will be stored here. Data may be compressed unless allowCompressedResponse is false - you should use [request responseData] instead in most cases + NSMutableData *rawResponseData; + + // Used for sending and receiving data + CFHTTPMessageRef request; + NSInputStream *readStream; + + // Used for authentication + CFHTTPAuthenticationRef requestAuthentication; + NSDictionary *requestCredentials; + + // Used during NTLM authentication + int authenticationRetryCount; + + // Authentication scheme (Basic, Digest, NTLM) + NSString *authenticationScheme; + + // Realm for authentication when credentials are required + NSString *authenticationRealm; + + // When YES, ASIHTTPRequest will present a dialog allowing users to enter credentials when no-matching credentials were found for a server that requires authentication + // The dialog will not be shown if your delegate responds to authenticationNeededForRequest: + // Default is NO. + BOOL shouldPresentAuthenticationDialog; + + // When YES, ASIHTTPRequest will present a dialog allowing users to enter credentials when no-matching credentials were found for a proxy server that requires authentication + // The dialog will not be shown if your delegate responds to proxyAuthenticationNeededForRequest: + // Default is YES (basically, because most people won't want the hassle of adding support for authenticating proxies to their apps) + BOOL shouldPresentProxyAuthenticationDialog; + + // Used for proxy authentication + CFHTTPAuthenticationRef proxyAuthentication; + NSDictionary *proxyCredentials; + + // Used during authentication with an NTLM proxy + int proxyAuthenticationRetryCount; + + // Authentication scheme for the proxy (Basic, Digest, NTLM) + NSString *proxyAuthenticationScheme; + + // Realm for proxy authentication when credentials are required + NSString *proxyAuthenticationRealm; + + // HTTP status code, eg: 200 = OK, 404 = Not found etc + int responseStatusCode; + + // Description of the HTTP status code + NSString *responseStatusMessage; + + // Size of the response + unsigned long long contentLength; + + // Size of the partially downloaded content + unsigned long long partialDownloadSize; + + // Size of the POST payload + unsigned long long postLength; + + // The total amount of downloaded data + unsigned long long totalBytesRead; + + // The total amount of uploaded data + unsigned long long totalBytesSent; + + // Last amount of data read (used for incrementing progress) + unsigned long long lastBytesRead; + + // Last amount of data sent (used for incrementing progress) + unsigned long long lastBytesSent; + + // This lock prevents the operation from being cancelled at an inopportune moment + NSRecursiveLock *cancelledLock; + + // Called on the delegate (if implemented) when the request starts. Default is requestStarted: + SEL didStartSelector; + + // Called on the delegate (if implemented) when the request receives response headers. Default is requestDidReceiveResponseHeaders: + SEL didReceiveResponseHeadersSelector; + + // Called on the delegate (if implemented) when the request completes successfully. Default is requestFinished: + SEL didFinishSelector; + + // Called on the delegate (if implemented) when the request fails. Default is requestFailed: + SEL didFailSelector; + + // Called on the delegate (if implemented) when the request receives data. Default is request:didReceiveData: + // If you set this and implement the method in your delegate, you must handle the data yourself - ASIHTTPRequest will not populate responseData or write the data to downloadDestinationPath + SEL didReceiveDataSelector; + + // Used for recording when something last happened during the request, we will compare this value with the current date to time out requests when appropriate + NSDate *lastActivityTime; + + // Number of seconds to wait before timing out - default is 10 + NSTimeInterval timeOutSeconds; + + // Will be YES when a HEAD request will handle the content-length before this request starts + BOOL shouldResetUploadProgress; + BOOL shouldResetDownloadProgress; + + // Used by HEAD requests when showAccurateProgress is YES to preset the content-length for this request + ASIHTTPRequest *mainRequest; + + // When NO, this request will only update the progress indicator when it completes + // When YES, this request will update the progress indicator according to how much data it has received so far + // The default for requests is YES + // Also see the comments in ASINetworkQueue.h + BOOL showAccurateProgress; + + // Used to ensure the progress indicator is only incremented once when showAccurateProgress = NO + BOOL updatedProgress; + + // Prevents the body of the post being built more than once (largely for subclasses) + BOOL haveBuiltPostBody; + + // Used internally, may reflect the size of the internal buffer used by CFNetwork + // POST / PUT operations with body sizes greater than uploadBufferSize will not timeout unless more than uploadBufferSize bytes have been sent + // Likely to be 32KB on iPhone 3.0, 128KB on Mac OS X Leopard and iPhone 2.2.x + unsigned long long uploadBufferSize; + + // Text encoding for responses that do not send a Content-Type with a charset value. Defaults to NSISOLatin1StringEncoding + NSStringEncoding defaultResponseEncoding; + + // The text encoding of the response, will be defaultResponseEncoding if the server didn't specify. Can't be set. + NSStringEncoding responseEncoding; + + // Tells ASIHTTPRequest not to delete partial downloads, and allows it to use an existing file to resume a download. Defaults to NO. + BOOL allowResumeForFileDownloads; + + // Custom user information associated with the request + NSDictionary *userInfo; + + // Use HTTP 1.0 rather than 1.1 (defaults to false) + BOOL useHTTPVersionOne; + + // When YES, requests will automatically redirect when they get a HTTP 30x header (defaults to YES) + BOOL shouldRedirect; + + // Used internally to tell the main loop we need to stop and retry with a new url + BOOL needsRedirect; + + // Incremented every time this request redirects. When it reaches 5, we give up + int redirectCount; + + // When NO, requests will not check the secure certificate is valid (use for self-signed certificates during development, DO NOT USE IN PRODUCTION) Default is YES + BOOL validatesSecureCertificate; + + // Details on the proxy to use - you could set these yourself, but it's probably best to let ASIHTTPRequest detect the system proxy settings + NSString *proxyHost; + int proxyPort; + + // ASIHTTPRequest will assume kCFProxyTypeHTTP if the proxy type could not be automatically determined + // Set to kCFProxyTypeSOCKS if you are manually configuring a SOCKS proxy + NSString *proxyType; + + // URL for a PAC (Proxy Auto Configuration) file. If you want to set this yourself, it's probably best if you use a local file + NSURL *PACurl; + + // See ASIAuthenticationState values above. 0 == default == No authentication needed yet + ASIAuthenticationState authenticationNeeded; + + // When YES, ASIHTTPRequests will present credentials from the session store for requests to the same server before being asked for them + // This avoids an extra round trip for requests after authentication has succeeded, which is much for efficient for authenticated requests with large bodies, or on slower connections + // Set to NO to only present credentials when explicitly asked for them + // This only affects credentials stored in the session cache when useSessionPersistence is YES. Credentials from the keychain are never presented unless the server asks for them + // Default is YES + BOOL shouldPresentCredentialsBeforeChallenge; + + // YES when the request hasn't finished yet. Will still be YES even if the request isn't doing anything (eg it's waiting for delegate authentication). READ-ONLY + BOOL inProgress; + + // Used internally to track whether the stream is scheduled on the run loop or not + // Bandwidth throttling can unschedule the stream to slow things down while a request is in progress + BOOL readStreamIsScheduled; + + // Set to allow a request to automatically retry itself on timeout + // Default is zero - timeout will stop the request + int numberOfTimesToRetryOnTimeout; + + // The number of times this request has retried (when numberOfTimesToRetryOnTimeout > 0) + int retryCount; + + // When YES, requests will keep the connection to the server alive for a while to allow subsequent requests to re-use it for a substantial speed-boost + // Persistent connections will not be used if the server explicitly closes the connection + // Default is YES + BOOL shouldAttemptPersistentConnection; + + // Number of seconds to keep an inactive persistent connection open on the client side + // Default is 60 + // If we get a keep-alive header, this is this value is replaced with how long the server told us to keep the connection around + // A future date is created from this and used for expiring the connection, this is stored in connectionInfo's expires value + NSTimeInterval persistentConnectionTimeoutSeconds; + + // Set to yes when an appropriate keep-alive header is found + BOOL connectionCanBeReused; + + // Stores information about the persistent connection that is currently in use. + // It may contain: + // * The id we set for a particular connection, incremented every time we want to specify that we need a new connection + // * The date that connection should expire + // * A host, port and scheme for the connection. These are used to determine whether that connection can be reused by a subsequent request (all must match the new request) + // * An id for the request that is currently using the connection. This is used for determining if a connection is available or not (we store a number rather than a reference to the request so we don't need to hang onto a request until the connection expires) + // * A reference to the stream that is currently using the connection. This is necessary because we need to keep the old stream open until we've opened a new one. + // The stream will be closed + released either when another request comes to use the connection, or when the timer fires to tell the connection to expire + NSMutableDictionary *connectionInfo; + + // When set to YES, 301 and 302 automatic redirects will use the original method and and body, according to the HTTP 1.1 standard + // Default is NO (to follow the behaviour of most browsers) + BOOL shouldUseRFC2616RedirectBehaviour; + + // Used internally to record when a request has finished downloading data + BOOL downloadComplete; + + // An ID that uniquely identifies this request - primarily used for debugging persistent connections + NSNumber *requestID; + + // Will be ASIHTTPRequestRunLoopMode for synchronous requests, NSDefaultRunLoopMode for all other requests + NSString *runLoopMode; + + // This timer checks up on the request every 0.25 seconds, and updates progress + NSTimer *statusTimer; + + + // The download cache that will be used for this request (use [ASIHTTPRequest setDefaultCache:cache] to configure a default cache + id downloadCache; + + // The cache policy that will be used for this request - See ASICacheDelegate.h for possible values + ASICachePolicy cachePolicy; + + // The cache storage policy that will be used for this request - See ASICacheDelegate.h for possible values + ASICacheStoragePolicy cacheStoragePolicy; + + // Will be true when the response was pulled from the cache rather than downloaded + BOOL didUseCachedResponse; + + // Set secondsToCache to use a custom time interval for expiring the response when it is stored in a cache + NSTimeInterval secondsToCache; +} + +#pragma mark init / dealloc + +// Should be an HTTP or HTTPS url, may include username and password if appropriate +- (id)initWithURL:(NSURL *)newURL; + +// Convenience constructor ++ (id)requestWithURL:(NSURL *)newURL; + ++ (id)requestWithURL:(NSURL *)newURL usingCache:(id )cache; ++ (id)requestWithURL:(NSURL *)newURL usingCache:(id )cache andCachePolicy:(ASICachePolicy)policy; + +#pragma mark setup request + +// Add a custom header to the request +- (void)addRequestHeader:(NSString *)header value:(NSString *)value; + +// Called during buildRequestHeaders and after a redirect to create a cookie header from request cookies and the global store +- (void)applyCookieHeader; + +// Populate the request headers dictionary. Called before a request is started, or by a HEAD request that needs to borrow them +- (void)buildRequestHeaders; + +// Used to apply authorization header to a request before it is sent (when shouldPresentCredentialsBeforeChallenge is YES) +- (void)applyAuthorizationHeader; + + +// Create the post body +- (void)buildPostBody; + +// Called to add data to the post body. Will append to postBody when shouldStreamPostDataFromDisk is false, or write to postBodyWriteStream when true +- (void)appendPostData:(NSData *)data; +- (void)appendPostDataFromFile:(NSString *)file; + +#pragma mark get information about this request + +// Returns the contents of the result as an NSString (not appropriate for binary data - used responseData instead) +- (NSString *)responseString; + +// Response data, automatically uncompressed where appropriate +- (NSData *)responseData; + +// Returns true if the response was gzip compressed +- (BOOL)isResponseCompressed; + +#pragma mark running a request + + +// Run a request synchronously, and return control when the request completes or fails +- (void)startSynchronous; + +// Run request in the background +- (void)startAsynchronous; + +#pragma mark request logic + +// Call to delete the temporary file used during a file download (if it exists) +// No need to call this if the request succeeds - it is removed automatically +- (void)removeTemporaryDownloadFile; + +// Call to remove the file used as the request body +// No need to call this if the request succeeds and you didn't specify postBodyFilePath manually - it is removed automatically +- (void)removePostDataFile; + +#pragma mark HEAD request + +// Used by ASINetworkQueue to create a HEAD request appropriate for this request with the same headers (though you can use it yourself) +- (ASIHTTPRequest *)HEADRequest; + +#pragma mark upload/download progress + +// Called approximately every 0.25 seconds to update the progress delegates +- (void)updateProgressIndicators; + +// Updates upload progress (notifies the queue and/or uploadProgressDelegate of this request) +- (void)updateUploadProgress; + +// Updates download progress (notifies the queue and/or uploadProgressDelegate of this request) +- (void)updateDownloadProgress; + +// Called when authorisation is needed, as we only find out we don't have permission to something when the upload is complete +- (void)removeUploadProgressSoFar; + +// Called when we get a content-length header and shouldResetDownloadProgress is true +- (void)incrementDownloadSizeBy:(long long)length; + +// Called when a request starts and shouldResetUploadProgress is true +// Also called (with a negative length) to remove the size of the underlying buffer used for uploading +- (void)incrementUploadSizeBy:(long long)length; + +// Helper method for interacting with progress indicators to abstract the details of different APIS (NSProgressIndicator and UIProgressView) ++ (void)updateProgressIndicator:(id)indicator withProgress:(unsigned long long)progress ofTotal:(unsigned long long)total; + +// Helper method used for performing invocations on the main thread (used for progress) ++ (void)performSelector:(SEL)selector onTarget:(id)target withObject:(id)object amount:(void *)amount; + +#pragma mark handling request complete / failure + +// Called when a request starts, lets the delegate know via didStartSelector +- (void)requestStarted; + +// Called when a request receives response headers, lets the delegate know via didReceiveResponseHeadersSelector +- (void)requestReceivedResponseHeaders; + +// Called when a request completes successfully, lets the delegate know via didFinishSelector +- (void)requestFinished; + +// Called when a request fails, and lets the delegate know via didFailSelector +- (void)failWithError:(NSError *)theError; + +// Called to retry our request when our persistent connection is closed +// Returns YES if we haven't already retried, and connection will be restarted +// Otherwise, returns NO, and nothing will happen +- (BOOL)retryUsingNewConnection; + +#pragma mark parsing HTTP response headers + +// Reads the response headers to find the content length, encoding, cookies for the session +// Also initiates request redirection when shouldRedirect is true +// And works out if HTTP auth is required +- (void)readResponseHeaders; + +// Attempts to set the correct encoding by looking at the Content-Type header, if this is one +- (void)parseStringEncodingFromHeaders; + +#pragma mark http authentication stuff + +// Apply credentials to this request +- (BOOL)applyCredentials:(NSDictionary *)newCredentials; +- (BOOL)applyProxyCredentials:(NSDictionary *)newCredentials; + +// Attempt to obtain credentials for this request from the URL, username and password or keychain +- (NSMutableDictionary *)findCredentials; +- (NSMutableDictionary *)findProxyCredentials; + +// Unlock (unpause) the request thread so it can resume the request +// Should be called by delegates when they have populated the authentication information after an authentication challenge +- (void)retryUsingSuppliedCredentials; + +// Should be called by delegates when they wish to cancel authentication and stop +- (void)cancelAuthentication; + +// Apply authentication information and resume the request after an authentication challenge +- (void)attemptToApplyCredentialsAndResume; +- (void)attemptToApplyProxyCredentialsAndResume; + +// Attempt to show the built-in authentication dialog, returns YES if credentials were supplied, NO if user cancelled dialog / dialog is disabled / running on main thread +// Currently only used on iPhone OS +- (BOOL)showProxyAuthenticationDialog; +- (BOOL)showAuthenticationDialog; + +// Construct a basic authentication header from the username and password supplied, and add it to the request headers +// Used when shouldPresentCredentialsBeforeChallenge is YES +- (void)addBasicAuthenticationHeaderWithUsername:(NSString *)theUsername andPassword:(NSString *)thePassword; + +#pragma mark stream status handlers + +// CFnetwork event handlers +- (void)handleNetworkEvent:(CFStreamEventType)type; +- (void)handleBytesAvailable; +- (void)handleStreamComplete; +- (void)handleStreamError; + +#pragma mark persistent connections + +// Get the ID of the connection this request used (only really useful in tests and debugging) +- (NSNumber *)connectionID; + +// Called automatically when a request is started to clean up any persistent connections that have expired ++ (void)expirePersistentConnections; + +#pragma mark default time out + ++ (NSTimeInterval)defaultTimeOutSeconds; ++ (void)setDefaultTimeOutSeconds:(NSTimeInterval)newTimeOutSeconds; + +#pragma mark session credentials + ++ (NSMutableArray *)sessionProxyCredentialsStore; ++ (NSMutableArray *)sessionCredentialsStore; + ++ (void)storeProxyAuthenticationCredentialsInSessionStore:(NSDictionary *)credentials; ++ (void)storeAuthenticationCredentialsInSessionStore:(NSDictionary *)credentials; + ++ (void)removeProxyAuthenticationCredentialsFromSessionStore:(NSDictionary *)credentials; ++ (void)removeAuthenticationCredentialsFromSessionStore:(NSDictionary *)credentials; + +- (NSDictionary *)findSessionProxyAuthenticationCredentials; +- (NSDictionary *)findSessionAuthenticationCredentials; + +#pragma mark keychain storage + +// Save credentials for this request to the keychain +- (void)saveCredentialsToKeychain:(NSDictionary *)newCredentials; + +// Save credentials to the keychain ++ (void)saveCredentials:(NSURLCredential *)credentials forHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm; ++ (void)saveCredentials:(NSURLCredential *)credentials forProxy:(NSString *)host port:(int)port realm:(NSString *)realm; + +// Return credentials from the keychain ++ (NSURLCredential *)savedCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm; ++ (NSURLCredential *)savedCredentialsForProxy:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm; + +// Remove credentials from the keychain ++ (void)removeCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm; ++ (void)removeCredentialsForProxy:(NSString *)host port:(int)port realm:(NSString *)realm; + +// We keep track of any cookies we accept, so that we can remove them from the persistent store later ++ (void)setSessionCookies:(NSMutableArray *)newSessionCookies; ++ (NSMutableArray *)sessionCookies; + +// Adds a cookie to our list of cookies we've accepted, checking first for an old version of the same cookie and removing that ++ (void)addSessionCookie:(NSHTTPCookie *)newCookie; + +// Dump all session data (authentication and cookies) ++ (void)clearSession; + +#pragma mark gzip decompression + +// Uncompress gzipped data with zlib ++ (NSData *)uncompressZippedData:(NSData*)compressedData; + +// Uncompress gzipped data from a file into another file, used when downloading to a file ++ (int)uncompressZippedDataFromFile:(NSString *)sourcePath toFile:(NSString *)destinationPath; ++ (int)uncompressZippedDataFromSource:(FILE *)source toDestination:(FILE *)dest; + +#pragma mark gzip compression + +// Compress data with gzip using zlib ++ (NSData *)compressData:(NSData*)uncompressedData; + +// gzip compress data from a file, saving to another file, used for uploading when shouldCompressRequestBody is true ++ (int)compressDataFromFile:(NSString *)sourcePath toFile:(NSString *)destinationPath; ++ (int)compressDataFromSource:(FILE *)source toDestination:(FILE *)dest; + +#pragma mark get user agent + +// Will be used as a user agent if requests do not specify a custom user agent +// Is only used when you have specified a Bundle Display Name (CFDisplayBundleName) or Bundle Name (CFBundleName) in your plist ++ (NSString *)defaultUserAgentString; + +#pragma mark proxy autoconfiguration + +// Returns an array of proxies to use for a particular url, given the url of a PAC script ++ (NSArray *)proxiesForURL:(NSURL *)theURL fromPAC:(NSURL *)pacScriptURL; + +#pragma mark mime-type detection + +// Return the mime type for a file ++ (NSString *)mimeTypeForFileAtPath:(NSString *)path; + +#pragma mark bandwidth measurement / throttling + +// The maximum number of bytes ALL requests can send / receive in a second +// This is a rough figure. The actual amount used will be slightly more, this does not include HTTP headers ++ (unsigned long)maxBandwidthPerSecond; ++ (void)setMaxBandwidthPerSecond:(unsigned long)bytes; + +// Get a rough average (for the last 5 seconds) of how much bandwidth is being used, in bytes ++ (unsigned long)averageBandwidthUsedPerSecond; + +- (void)performThrottling; + +// Will return YES is bandwidth throttling is currently in use ++ (BOOL)isBandwidthThrottled; + +// Used internally to record bandwidth use, and by ASIInputStreams when uploading. It's probably best if you don't mess with this. ++ (void)incrementBandwidthUsedInLastSecond:(unsigned long)bytes; + +// On iPhone, ASIHTTPRequest can automatically turn throttling on and off as the connection type changes between WWAN and WiFi + +#if TARGET_OS_IPHONE +// Set to YES to automatically turn on throttling when WWAN is connected, and automatically turn it off when it isn't ++ (void)setShouldThrottleBandwidthForWWAN:(BOOL)throttle; + +// Turns on throttling automatically when WWAN is connected using a custom limit, and turns it off automatically when it isn't ++ (void)throttleBandwidthForWWANUsingLimit:(unsigned long)limit; + +#pragma mark reachability + +// Returns YES when an iPhone OS device is connected via WWAN, false when connected via WIFI or not connected ++ (BOOL)isNetworkReachableViaWWAN; + +#endif + +#pragma mark cache + ++ (void)setDefaultCache:(id )cache; ++ (id )defaultCache; + +// Returns the maximum amount of data we can read as part of the current measurement period, and sleeps this thread if our allowance is used up ++ (unsigned long)maxUploadReadLength; + +#pragma mark network activity + ++ (BOOL)isNetworkInUse; +#if TARGET_OS_IPHONE ++ (void)setShouldUpdateNetworkActivityIndicator:(BOOL)shouldUpdate; +#endif + +#pragma mark miscellany + +// Used for generating Authorization header when using basic authentication when shouldPresentCredentialsBeforeChallenge is true +// And also by ASIS3Request ++ (NSString *)base64forData:(NSData *)theData; + +// Returns a date from a string in RFC1123 format ++ (NSDate *)dateFromRFC1123String:(NSString *)string; + +#pragma mark threading behaviour + +// In the default implementation, all requests run in a single background thread +// Advanced users only: Override this method in a subclass for a different threading behaviour +// Eg: return [NSThread mainThread] to run all requests in the main thread +// Alternatively, you can create a thread on demand, or manage a pool of threads +// Threads returned by this method will need to run the runloop in default mode (eg CFRunLoopRun()) +// Requests will stop the runloop when they complete +// If you have multiple requests sharing the thread you'll need to restart the runloop when this happens ++ (NSThread *)threadForRequest:(ASIHTTPRequest *)request; + + +#pragma mark === + +@property (retain) NSString *username; +@property (retain) NSString *password; +@property (retain) NSString *domain; + +@property (retain) NSString *proxyUsername; +@property (retain) NSString *proxyPassword; +@property (retain) NSString *proxyDomain; + +@property (retain) NSString *proxyHost; +@property (assign) int proxyPort; +@property (retain) NSString *proxyType; + +@property (retain,setter=setURL:) NSURL *url; +@property (retain) NSURL *originalURL; +@property (assign, nonatomic) id delegate; +@property (assign, nonatomic) id queue; +@property (assign, nonatomic) id uploadProgressDelegate; +@property (assign, nonatomic) id downloadProgressDelegate; +@property (assign) BOOL useKeychainPersistence; +@property (assign) BOOL useSessionPersistence; +@property (retain) NSString *downloadDestinationPath; +@property (retain) NSString *temporaryFileDownloadPath; +@property (assign) SEL didStartSelector; +@property (assign) SEL didReceiveResponseHeadersSelector; +@property (assign) SEL didFinishSelector; +@property (assign) SEL didFailSelector; +@property (assign) SEL didReceiveDataSelector; +@property (retain,readonly) NSString *authenticationRealm; +@property (retain,readonly) NSString *proxyAuthenticationRealm; +@property (retain) NSError *error; +@property (assign,readonly) BOOL complete; +@property (retain) NSDictionary *responseHeaders; +@property (retain) NSMutableDictionary *requestHeaders; +@property (retain) NSMutableArray *requestCookies; +@property (retain,readonly) NSArray *responseCookies; +@property (assign) BOOL useCookiePersistence; +@property (retain) NSDictionary *requestCredentials; +@property (retain) NSDictionary *proxyCredentials; +@property (assign,readonly) int responseStatusCode; +@property (retain,readonly) NSString *responseStatusMessage; +@property (retain) NSMutableData *rawResponseData; +@property (assign) NSTimeInterval timeOutSeconds; +@property (retain) NSString *requestMethod; +@property (retain) NSMutableData *postBody; +@property (assign,readonly) unsigned long long contentLength; +@property (assign) unsigned long long postLength; +@property (assign) BOOL shouldResetDownloadProgress; +@property (assign) BOOL shouldResetUploadProgress; +@property (assign) ASIHTTPRequest *mainRequest; +@property (assign) BOOL showAccurateProgress; +@property (assign,readonly) unsigned long long totalBytesRead; +@property (assign,readonly) unsigned long long totalBytesSent; +@property (assign) NSStringEncoding defaultResponseEncoding; +@property (assign,readonly) NSStringEncoding responseEncoding; +@property (assign) BOOL allowCompressedResponse; +@property (assign) BOOL allowResumeForFileDownloads; +@property (retain) NSDictionary *userInfo; +@property (retain) NSString *postBodyFilePath; +@property (assign) BOOL shouldStreamPostDataFromDisk; +@property (assign) BOOL didCreateTemporaryPostDataFile; +@property (assign) BOOL useHTTPVersionOne; +@property (assign, readonly) unsigned long long partialDownloadSize; +@property (assign) BOOL shouldRedirect; +@property (assign) BOOL validatesSecureCertificate; +@property (assign) BOOL shouldCompressRequestBody; +@property (retain) NSURL *PACurl; +@property (retain) NSString *authenticationScheme; +@property (retain) NSString *proxyAuthenticationScheme; +@property (assign) BOOL shouldPresentAuthenticationDialog; +@property (assign) BOOL shouldPresentProxyAuthenticationDialog; +@property (assign, readonly) ASIAuthenticationState authenticationNeeded; +@property (assign) BOOL shouldPresentCredentialsBeforeChallenge; +@property (assign, readonly) int authenticationRetryCount; +@property (assign, readonly) int proxyAuthenticationRetryCount; +@property (assign) BOOL haveBuiltRequestHeaders; +@property (assign, nonatomic) BOOL haveBuiltPostBody; +@property (assign, readonly) BOOL inProgress; +@property (assign) int numberOfTimesToRetryOnTimeout; +@property (assign, readonly) int retryCount; +@property (assign) BOOL shouldAttemptPersistentConnection; +@property (assign) NSTimeInterval persistentConnectionTimeoutSeconds; +@property (assign) BOOL shouldUseRFC2616RedirectBehaviour; +@property (assign, readonly) BOOL connectionCanBeReused; +@property (retain, readonly) NSNumber *requestID; +@property (assign) id downloadCache; +@property (assign) ASICachePolicy cachePolicy; +@property (assign) ASICacheStoragePolicy cacheStoragePolicy; +@property (assign, readonly) BOOL didUseCachedResponse; +@property (assign) NSTimeInterval secondsToCache; +@end diff --git a/ASIHTTPRequest.m b/ASIHTTPRequest.m new file mode 100644 index 0000000..1f33924 --- /dev/null +++ b/ASIHTTPRequest.m @@ -0,0 +1,4059 @@ +// +// ASIHTTPRequest.m +// +// Created by Ben Copsey on 04/10/2007. +// Copyright 2007-2010 All-Seeing Interactive. All rights reserved. +// +// A guide to the main features is available at: +// http://allseeing-i.com/ASIHTTPRequest +// +// Portions are based on the ImageClient example from Apple: +// See: http://developer.apple.com/samplecode/ImageClient/listing37.html + +#import "ASIHTTPRequest.h" +#import +#if TARGET_OS_IPHONE +#import "Reachability.h" +#import "ASIAuthenticationDialog.h" +#import +#else +#import +#endif +#import "ASIInputStream.h" + + +// Automatically set on build + +NSString *ASIHTTPRequestVersion = @"v1.7-13 2010-07-02"; + +NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain"; + +static NSString *ASIHTTPRequestRunLoopMode = @"ASIHTTPRequestRunLoopMode"; + +static const CFOptionFlags kNetworkEvents = kCFStreamEventOpenCompleted | kCFStreamEventHasBytesAvailable | kCFStreamEventEndEncountered | kCFStreamEventErrorOccurred; + +// In memory caches of credentials, used on when useSessionPersistence is YES +static NSMutableArray *sessionCredentialsStore = nil; +static NSMutableArray *sessionProxyCredentialsStore = nil; + +// This lock mediates access to session credentials +static NSRecursiveLock *sessionCredentialsLock = nil; + +// We keep track of cookies we have received here so we can remove them from the sharedHTTPCookieStorage later +static NSMutableArray *sessionCookies = nil; + +// The number of times we will allow requests to redirect before we fail with a redirection error +const int RedirectionLimit = 5; + +// The default number of seconds to use for a timeout +static NSTimeInterval defaultTimeOutSeconds = 10; + +static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventType type, void *clientCallBackInfo) { + [((ASIHTTPRequest*)clientCallBackInfo) handleNetworkEvent: type]; +} + +// This lock prevents the operation from being cancelled while it is trying to update the progress, and vice versa +static NSRecursiveLock *progressLock; + +static NSError *ASIRequestCancelledError; +static NSError *ASIRequestTimedOutError; +static NSError *ASIAuthenticationError; +static NSError *ASIUnableToCreateRequestError; +static NSError *ASITooMuchRedirectionError; + +static NSMutableArray *bandwidthUsageTracker = nil; +static unsigned long averageBandwidthUsedPerSecond = 0; + +// These are used for queuing persistent connections on the same connection + +// Incremented every time we specify we want a new connection +static unsigned int nextConnectionNumberToCreate = 0; + +// An array of connectionInfo dictionaries. +// When attempting a persistent connection, we look here to try to find an existing connection to the same server that is currently not in use +static NSMutableArray *persistentConnectionsPool = nil; + +// Mediates access to the persistent connections pool +static NSRecursiveLock *connectionsLock = nil; + +// Each request gets a new id, we store this rather than a ref to the request itself in the connectionInfo dictionary. +// We do this so we don't have to keep the request around while we wait for the connection to expire +static unsigned int nextRequestID = 0; + +// Records how much bandwidth all requests combined have used in the last second +static unsigned long bandwidthUsedInLastSecond = 0; + +// A date one second in the future from the time it was created +static NSDate *bandwidthMeasurementDate = nil; + +// Since throttling variables are shared among all requests, we'll use a lock to mediate access +static NSLock *bandwidthThrottlingLock = nil; + +// the maximum number of bytes that can be transmitted in one second +static unsigned long maxBandwidthPerSecond = 0; + +// A default figure for throttling bandwidth on mobile devices +unsigned long const ASIWWANBandwidthThrottleAmount = 14800; + +#if TARGET_OS_IPHONE +// YES when bandwidth throttling is active +// This flag does not denote whether throttling is turned on - rather whether it is currently in use +// It will be set to NO when throttling was turned on with setShouldThrottleBandwidthForWWAN, but a WI-FI connection is active +static BOOL isBandwidthThrottled = NO; + +// When YES, bandwidth will be automatically throttled when using WWAN (3G/Edge/GPRS) +// Wifi will not be throttled +static BOOL shouldThrottleBandwithForWWANOnly = NO; +#endif + +// Mediates access to the session cookies so requests +static NSRecursiveLock *sessionCookiesLock = nil; + +// This lock ensures delegates only receive one notification that authentication is required at once +// When using ASIAuthenticationDialogs, it also ensures only one dialog is shown at once +// If a request can't aquire the lock immediately, it means a dialog is being shown or a delegate is handling the authentication challenge +// Once it gets the lock, it will try to look for existing credentials again rather than showing the dialog / notifying the delegate +// This is so it can make use of any credentials supplied for the other request, if they are appropriate +static NSRecursiveLock *delegateAuthenticationLock = nil; + +// When throttling bandwidth, Set to a date in future that we will allow all requests to wake up and reschedule their streams +static NSDate *throttleWakeUpTime = nil; + +static id defaultCache = nil; + + +// Used for tracking when requests are using the network +static unsigned int runningRequestCount = 0; + +#if TARGET_OS_IPHONE +// Use [ASIHTTPRequest setShouldUpdateNetworkActivityIndicator:NO] if you want to manage it yourself +static BOOL shouldUpdateNetworkActivityIndicator = YES; +#endif + +//**Queue stuff**/ + +// The thread all requests will run on +// Hangs around forever, but will be blocked unless there are requests underway +static NSThread *networkThread = nil; + +static NSOperationQueue *sharedQueue = nil; + +// Private stuff +@interface ASIHTTPRequest () + +- (void)cancelLoad; + +- (void)destroyReadStream; +- (void)scheduleReadStream; +- (void)unscheduleReadStream; + +- (BOOL)askDelegateForCredentials; +- (BOOL)askDelegateForProxyCredentials; ++ (void)measureBandwidthUsage; ++ (void)recordBandwidthUsage; +- (void)startRequest; +- (void)updateStatus:(NSTimer *)timer; +- (void)checkRequestStatus; + +- (void)markAsFinished; +- (void)performRedirect; +- (BOOL)shouldTimeOut; + + +- (BOOL)useDataFromCache; + +#if TARGET_OS_IPHONE ++ (void)registerForNetworkReachabilityNotifications; ++ (void)unsubscribeFromNetworkReachabilityNotifications; +// Called when the status of the network changes ++ (void)reachabilityChanged:(NSNotification *)note; + +#endif + +@property (assign) BOOL complete; +@property (retain) NSArray *responseCookies; +@property (assign) int responseStatusCode; +@property (retain, nonatomic) NSDate *lastActivityTime; +@property (assign) unsigned long long contentLength; +@property (assign) unsigned long long partialDownloadSize; +@property (assign, nonatomic) unsigned long long uploadBufferSize; +@property (assign) NSStringEncoding responseEncoding; +@property (retain, nonatomic) NSOutputStream *postBodyWriteStream; +@property (retain, nonatomic) NSInputStream *postBodyReadStream; +@property (assign) unsigned long long totalBytesRead; +@property (assign) unsigned long long totalBytesSent; +@property (assign, nonatomic) unsigned long long lastBytesRead; +@property (assign, nonatomic) unsigned long long lastBytesSent; +@property (retain) NSRecursiveLock *cancelledLock; +@property (retain, nonatomic) NSOutputStream *fileDownloadOutputStream; +@property (assign) int authenticationRetryCount; +@property (assign) int proxyAuthenticationRetryCount; +@property (assign, nonatomic) BOOL updatedProgress; +@property (assign, nonatomic) BOOL needsRedirect; +@property (assign, nonatomic) int redirectCount; +@property (retain, nonatomic) NSData *compressedPostBody; +@property (retain, nonatomic) NSString *compressedPostBodyFilePath; +@property (retain) NSString *authenticationRealm; +@property (retain) NSString *proxyAuthenticationRealm; +@property (retain) NSString *responseStatusMessage; +@property (assign) BOOL inProgress; +@property (assign) int retryCount; +@property (assign) BOOL connectionCanBeReused; +@property (retain, nonatomic) NSMutableDictionary *connectionInfo; +@property (retain, nonatomic) NSInputStream *readStream; +@property (assign) ASIAuthenticationState authenticationNeeded; +@property (assign, nonatomic) BOOL readStreamIsScheduled; +@property (assign, nonatomic) BOOL downloadComplete; +@property (retain) NSNumber *requestID; +@property (assign, nonatomic) NSString *runLoopMode; +@property (retain, nonatomic) NSTimer *statusTimer; +@property (assign) BOOL didUseCachedResponse; +@end + + +@implementation ASIHTTPRequest + +#pragma mark init / dealloc + ++ (void)initialize +{ + if (self == [ASIHTTPRequest class]) { + persistentConnectionsPool = [[NSMutableArray alloc] init]; + connectionsLock = [[NSRecursiveLock alloc] init]; + progressLock = [[NSRecursiveLock alloc] init]; + bandwidthThrottlingLock = [[NSLock alloc] init]; + sessionCookiesLock = [[NSRecursiveLock alloc] init]; + sessionCredentialsLock = [[NSRecursiveLock alloc] init]; + delegateAuthenticationLock = [[NSRecursiveLock alloc] init]; + bandwidthUsageTracker = [[NSMutableArray alloc] initWithCapacity:5]; + ASIRequestTimedOutError = [[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIRequestTimedOutErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"The request timed out",NSLocalizedDescriptionKey,nil]] retain]; + ASIAuthenticationError = [[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIAuthenticationErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Authentication needed",NSLocalizedDescriptionKey,nil]] retain]; + ASIRequestCancelledError = [[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIRequestCancelledErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"The request was cancelled",NSLocalizedDescriptionKey,nil]] retain]; + ASIUnableToCreateRequestError = [[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIUnableToCreateRequestErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to create request (bad url?)",NSLocalizedDescriptionKey,nil]] retain]; + ASITooMuchRedirectionError = [[NSError errorWithDomain:NetworkRequestErrorDomain code:ASITooMuchRedirectionErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"The request failed because it redirected too many times",NSLocalizedDescriptionKey,nil]] retain]; + + sharedQueue = [[NSOperationQueue alloc] init]; + [sharedQueue setMaxConcurrentOperationCount:4]; + + } +} + + +- (id)initWithURL:(NSURL *)newURL +{ + self = [self init]; + [self setRequestMethod:@"GET"]; + + [self setRunLoopMode:NSDefaultRunLoopMode]; + [self setShouldAttemptPersistentConnection:YES]; + [self setPersistentConnectionTimeoutSeconds:60.0]; + [self setShouldPresentCredentialsBeforeChallenge:YES]; + [self setShouldRedirect:YES]; + [self setShowAccurateProgress:YES]; + [self setShouldResetDownloadProgress:YES]; + [self setShouldResetUploadProgress:YES]; + [self setAllowCompressedResponse:YES]; + [self setDefaultResponseEncoding:NSISOLatin1StringEncoding]; + [self setShouldPresentProxyAuthenticationDialog:YES]; + + [self setTimeOutSeconds:[ASIHTTPRequest defaultTimeOutSeconds]]; + [self setUseSessionPersistence:YES]; + [self setUseCookiePersistence:YES]; + [self setValidatesSecureCertificate:YES]; + [self setRequestCookies:[[[NSMutableArray alloc] init] autorelease]]; + [self setDidStartSelector:@selector(requestStarted:)]; + [self setDidReceiveResponseHeadersSelector:@selector(requestReceivedResponseHeaders:)]; + [self setDidFinishSelector:@selector(requestFinished:)]; + [self setDidFailSelector:@selector(requestFailed:)]; + [self setDidReceiveDataSelector:@selector(request:didReceiveData:)]; + [self setURL:newURL]; + [self setCancelledLock:[[[NSRecursiveLock alloc] init] autorelease]]; + [self setDownloadCache:[[self class] defaultCache]]; + return self; +} + ++ (id)requestWithURL:(NSURL *)newURL +{ + return [[[self alloc] initWithURL:newURL] autorelease]; +} + ++ (id)requestWithURL:(NSURL *)newURL usingCache:(id )cache +{ + return [self requestWithURL:newURL usingCache:cache andCachePolicy:ASIDefaultCachePolicy]; +} + ++ (id)requestWithURL:(NSURL *)newURL usingCache:(id )cache andCachePolicy:(ASICachePolicy)policy +{ + ASIHTTPRequest *request = [[[self alloc] initWithURL:newURL] autorelease]; + [request setDownloadCache:cache]; + [request setCachePolicy:policy]; + return request; +} + +- (void)dealloc +{ + [self setAuthenticationNeeded:ASINoAuthenticationNeededYet]; + if (requestAuthentication) { + CFRelease(requestAuthentication); + } + if (proxyAuthentication) { + CFRelease(proxyAuthentication); + } + if (request) { + CFRelease(request); + } + [self cancelLoad]; + [userInfo release]; + [postBody release]; + [compressedPostBody release]; + [error release]; + [requestHeaders release]; + [requestCookies release]; + [downloadDestinationPath release]; + [temporaryFileDownloadPath release]; + [fileDownloadOutputStream release]; + [username release]; + [password release]; + [domain release]; + [authenticationRealm release]; + [authenticationScheme release]; + [requestCredentials release]; + [proxyHost release]; + [proxyType release]; + [proxyUsername release]; + [proxyPassword release]; + [proxyDomain release]; + [proxyAuthenticationRealm release]; + [proxyAuthenticationScheme release]; + [proxyCredentials release]; + [url release]; + [originalURL release]; + [lastActivityTime release]; + [responseCookies release]; + [rawResponseData release]; + [responseHeaders release]; + [requestMethod release]; + [cancelledLock release]; + [postBodyFilePath release]; + [compressedPostBodyFilePath release]; + [postBodyWriteStream release]; + [postBodyReadStream release]; + [PACurl release]; + [responseStatusMessage release]; + [connectionInfo release]; + [requestID release]; + [super dealloc]; +} + + +#pragma mark setup request + +- (void)addRequestHeader:(NSString *)header value:(NSString *)value +{ + if (!requestHeaders) { + [self setRequestHeaders:[NSMutableDictionary dictionaryWithCapacity:1]]; + } + [requestHeaders setObject:value forKey:header]; +} + +// This function will be called either just before a request starts, or when postLength is needed, whichever comes first +// postLength must be set by the time this function is complete +- (void)buildPostBody +{ + if ([self haveBuiltPostBody]) { + return; + } + + // Are we submitting the request body from a file on disk + if ([self postBodyFilePath]) { + + // If we were writing to the post body via appendPostData or appendPostDataFromFile, close the write stream + if ([self postBodyWriteStream]) { + [[self postBodyWriteStream] close]; + [self setPostBodyWriteStream:nil]; + } + + NSError *err = nil; + NSString *path; + if ([self shouldCompressRequestBody]) { + [self setCompressedPostBodyFilePath:[NSTemporaryDirectory() stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]]]; + [ASIHTTPRequest compressDataFromFile:[self postBodyFilePath] toFile:[self compressedPostBodyFilePath]]; + path = [self compressedPostBodyFilePath]; + } else { + path = [self postBodyFilePath]; + } + [self setPostLength:[[[NSFileManager defaultManager] attributesOfItemAtPath:path error:&err] fileSize]]; + if (err) { + [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to get attributes for file at path '%@'",path],NSLocalizedDescriptionKey,error,NSUnderlyingErrorKey,nil]]]; + return; + } + + // Otherwise, we have an in-memory request body + } else { + if ([self shouldCompressRequestBody]) { + [self setCompressedPostBody:[ASIHTTPRequest compressData:[self postBody]]]; + [self setPostLength:[[self compressedPostBody] length]]; + } else { + [self setPostLength:[[self postBody] length]]; + } + } + + if ([self postLength] > 0) { + if ([requestMethod isEqualToString:@"GET"] || [requestMethod isEqualToString:@"DELETE"] || [requestMethod isEqualToString:@"HEAD"]) { + [self setRequestMethod:@"POST"]; + } + [self addRequestHeader:@"Content-Length" value:[NSString stringWithFormat:@"%llu",[self postLength]]]; + } + [self setHaveBuiltPostBody:YES]; +} + +// Sets up storage for the post body +- (void)setupPostBody +{ + if ([self shouldStreamPostDataFromDisk]) { + if (![self postBodyFilePath]) { + [self setPostBodyFilePath:[NSTemporaryDirectory() stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]]]; + [self setDidCreateTemporaryPostDataFile:YES]; + } + if (![self postBodyWriteStream]) { + [self setPostBodyWriteStream:[[[NSOutputStream alloc] initToFileAtPath:[self postBodyFilePath] append:NO] autorelease]]; + [[self postBodyWriteStream] open]; + } + } else { + if (![self postBody]) { + [self setPostBody:[[[NSMutableData alloc] init] autorelease]]; + } + } +} + +- (void)appendPostData:(NSData *)data +{ + [self setupPostBody]; + if ([data length] == 0) { + return; + } + if ([self shouldStreamPostDataFromDisk]) { + [[self postBodyWriteStream] write:[data bytes] maxLength:[data length]]; + } else { + [[self postBody] appendData:data]; + } +} + +- (void)appendPostDataFromFile:(NSString *)file +{ + [self setupPostBody]; + NSInputStream *stream = [[[NSInputStream alloc] initWithFileAtPath:file] autorelease]; + [stream open]; + NSUInteger bytesRead; + while ([stream hasBytesAvailable]) { + + unsigned char buffer[1024*256]; + bytesRead = [stream read:buffer maxLength:sizeof(buffer)]; + if (bytesRead == 0) { + break; + } + if ([self shouldStreamPostDataFromDisk]) { + [[self postBodyWriteStream] write:buffer maxLength:bytesRead]; + } else { + [[self postBody] appendData:[NSData dataWithBytes:buffer length:bytesRead]]; + } + } + [stream close]; +} + +- (id)delegate +{ + [[self cancelledLock] lock]; + id d = [[delegate retain] autorelease]; + [[self cancelledLock] unlock]; + return d; +} + +- (void)setDelegate:(id)newDelegate +{ + [[self cancelledLock] lock]; + delegate = newDelegate; + [[self cancelledLock] unlock]; +} + +- (id)queue +{ + [[self cancelledLock] lock]; + id q = [[queue retain] autorelease]; + [[self cancelledLock] unlock]; + return q; +} + + +- (void)setQueue:(id)newQueue +{ + [[self cancelledLock] lock]; + queue = newQueue; + [[self cancelledLock] unlock]; +} + +#pragma mark get information about this request + +- (void)cancel +{ + #if DEBUG_REQUEST_STATUS + ////nslog(@"Request cancelled: %@",self); + #endif + + [[self cancelledLock] lock]; + + if ([self isCancelled] || [self complete]) { + [[self cancelledLock] unlock]; + return; + } + + [self failWithError:ASIRequestCancelledError]; + [self setComplete:YES]; + [self cancelLoad]; + + [[self retain] autorelease]; + [super cancel]; + + [[self cancelledLock] unlock]; +} + + +// Call this method to get the received data as an NSString. Don't use for binary data! +- (NSString *)responseString +{ + NSData *data = [self responseData]; + if (!data) { + return nil; + } + + return [[[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:[self responseEncoding]] autorelease]; +} + +- (BOOL)isResponseCompressed +{ + NSString *encoding = [[self responseHeaders] objectForKey:@"Content-Encoding"]; + return encoding && [encoding rangeOfString:@"gzip"].location != NSNotFound; +} + +- (NSData *)responseData +{ + if ([self isResponseCompressed]) { + return [ASIHTTPRequest uncompressZippedData:[self rawResponseData]]; + } else { + return [self rawResponseData]; + } +} + +#pragma mark running a request + +- (void)startSynchronous +{ +#if DEBUG_REQUEST_STATUS || DEBUG_THROTTLING + //nslog(@"Starting synchronous request %@",self); +#endif + [self setRunLoopMode:ASIHTTPRequestRunLoopMode]; + [self setInProgress:YES]; + + if (![self isCancelled] && ![self complete]) { + [self main]; + while (!complete) { + [[NSRunLoop currentRunLoop] runMode:[self runLoopMode] beforeDate:[NSDate distantFuture]]; + } + } + + [self setInProgress:NO]; +} + +- (void)start +{ + [self setInProgress:YES]; + [self performSelector:@selector(main) onThread:[[self class] threadForRequest:self] withObject:nil waitUntilDone:NO]; +} + +- (void)startAsynchronous +{ +#if DEBUG_REQUEST_STATUS || DEBUG_THROTTLING + //nslog(@"Starting asynchronous request %@",self); +#endif + [sharedQueue addOperation:self]; +} + +#pragma mark concurrency + +- (BOOL)isConcurrent +{ + return YES; +} + +- (BOOL)isFinished +{ + return [self complete]; +} + +- (BOOL)isExecuting { + return [self inProgress]; +} + +#pragma mark request logic + +// Create the request +- (void)main +{ + @try { + + [[self cancelledLock] lock]; + + // A HEAD request generated by an ASINetworkQueue may have set the error already. If so, we should not proceed. + if ([self error]) { + [self setComplete:YES]; + [self markAsFinished]; + return; + } + + [self setComplete:NO]; + + if (![self url]) { + [self failWithError:ASIUnableToCreateRequestError]; + return; + } + + // Must call before we create the request so that the request method can be set if needs be + if (![self mainRequest]) { + [self buildPostBody]; + } + + if (![[self requestMethod] isEqualToString:@"GET"]) { + [self setDownloadCache:nil]; + } + + + // If we're redirecting, we'll already have a CFHTTPMessageRef + if (request) { + CFRelease(request); + } + + // Create a new HTTP request. + request = CFHTTPMessageCreateRequest(kCFAllocatorDefault, (CFStringRef)[self requestMethod], (CFURLRef)[self url], [self useHTTPVersionOne] ? kCFHTTPVersion1_0 : kCFHTTPVersion1_1); + if (!request) { + [self failWithError:ASIUnableToCreateRequestError]; + return; + } + + //If this is a HEAD request generated by an ASINetworkQueue, we need to let the main request generate its headers first so we can use them + if ([self mainRequest]) { + [[self mainRequest] buildRequestHeaders]; + } + + // Even if this is a HEAD request with a mainRequest, we still need to call to give subclasses a chance to add their own to HEAD requests (ASIS3Request does this) + [self buildRequestHeaders]; + + if ([self downloadCache]) { + if ([self cachePolicy] == ASIDefaultCachePolicy) { + [self setCachePolicy:[[self downloadCache] defaultCachePolicy]]; + } + + // See if we should pull from the cache rather than fetching the data + if ([self cachePolicy] == ASIOnlyLoadIfNotCachedCachePolicy) { + if ([self useDataFromCache]) { + return; + } + } else if ([self cachePolicy] == ASIReloadIfDifferentCachePolicy) { + + // Force a conditional GET if we have a cached version of this content already + NSDictionary *cachedHeaders = [[self downloadCache] cachedHeadersForRequest:self]; + if (cachedHeaders) { + NSString *etag = [cachedHeaders objectForKey:@"Etag"]; + if (etag) { + [[self requestHeaders] setObject:etag forKey:@"If-None-Match"]; + } + NSString *lastModified = [cachedHeaders objectForKey:@"Last-Modified"]; + if (lastModified) { + [[self requestHeaders] setObject:lastModified forKey:@"If-Modified-Since"]; + } + } + } + } + + [self applyAuthorizationHeader]; + + + NSString *header; + for (header in [self requestHeaders]) { + CFHTTPMessageSetHeaderFieldValue(request, (CFStringRef)header, (CFStringRef)[[self requestHeaders] objectForKey:header]); + } + + [self startRequest]; + + } @catch (NSException *exception) { + NSError *underlyingError = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASIUnhandledExceptionError userInfo:[exception userInfo]]; + [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIUnhandledExceptionError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[exception name],NSLocalizedDescriptionKey,[exception reason],NSLocalizedFailureReasonErrorKey,underlyingError,NSUnderlyingErrorKey,nil]]]; + + } @finally { + [[self cancelledLock] unlock]; + } +} + +- (void)applyAuthorizationHeader +{ + // Do we want to send credentials before we are asked for them? + if (![self shouldPresentCredentialsBeforeChallenge]) { + return; + } + + // First, see if we have any credentials we can use in the session store + NSDictionary *credentials = nil; + if ([self useSessionPersistence]) { + credentials = [self findSessionAuthenticationCredentials]; + } + + + // Are any credentials set on this request that might be used for basic authentication? + if ([self username] && [self password] && ![self domain]) { + + // If we have stored credentials, is this server asking for basic authentication? If we don't have credentials, we'll assume basic + if (!credentials || (CFStringRef)[credentials objectForKey:@"AuthenticationScheme"] == kCFHTTPAuthenticationSchemeBasic) { + [self addBasicAuthenticationHeaderWithUsername:[self username] andPassword:[self password]]; + } + } + + if (credentials && ![[self requestHeaders] objectForKey:@"Authorization"]) { + + // When the Authentication key is set, the credentials were stored after an authentication challenge, so we can let CFNetwork apply them + // (credentials for Digest and NTLM will always be stored like this) + if ([credentials objectForKey:@"Authentication"]) { + + // If we've already talked to this server and have valid credentials, let's apply them to the request + if (!CFHTTPMessageApplyCredentialDictionary(request, (CFHTTPAuthenticationRef)[credentials objectForKey:@"Authentication"], (CFDictionaryRef)[credentials objectForKey:@"Credentials"], NULL)) { + [[self class] removeAuthenticationCredentialsFromSessionStore:[credentials objectForKey:@"Credentials"]]; + } + + // If the Authentication key is not set, these credentials were stored after a username and password set on a previous request passed basic authentication + // When this happens, we'll need to create the Authorization header ourselves + } else { + NSDictionary *usernameAndPassword = [credentials objectForKey:@"Credentials"]; + [self addBasicAuthenticationHeaderWithUsername:[usernameAndPassword objectForKey:(NSString *)kCFHTTPAuthenticationUsername] andPassword:[usernameAndPassword objectForKey:(NSString *)kCFHTTPAuthenticationPassword]]; + } + } + if ([self useSessionPersistence]) { + credentials = [self findSessionProxyAuthenticationCredentials]; + if (credentials) { + if (!CFHTTPMessageApplyCredentialDictionary(request, (CFHTTPAuthenticationRef)[credentials objectForKey:@"Authentication"], (CFDictionaryRef)[credentials objectForKey:@"Credentials"], NULL)) { + [[self class] removeProxyAuthenticationCredentialsFromSessionStore:[credentials objectForKey:@"Credentials"]]; + } + } + } +} + +- (void)applyCookieHeader +{ + // Add cookies from the persistent (mac os global) store + if ([self useCookiePersistence]) { + NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[[self url] absoluteURL]]; + if (cookies) { + [[self requestCookies] addObjectsFromArray:cookies]; + } + } + + // Apply request cookies + NSArray *cookies; + if ([self mainRequest]) { + cookies = [[self mainRequest] requestCookies]; + } else { + cookies = [self requestCookies]; + } + if ([cookies count] > 0) { + NSHTTPCookie *cookie; + NSString *cookieHeader = nil; + for (cookie in cookies) { + if (!cookieHeader) { + cookieHeader = [NSString stringWithFormat: @"%@=%@",[cookie name],[cookie value]]; + } else { + cookieHeader = [NSString stringWithFormat: @"%@; %@=%@",cookieHeader,[cookie name],[cookie value]]; + } + } + if (cookieHeader) { + [self addRequestHeader:@"Cookie" value:cookieHeader]; + } + } +} + +- (void)buildRequestHeaders +{ + if ([self haveBuiltRequestHeaders]) { + return; + } + [self setHaveBuiltRequestHeaders:YES]; + + if ([self mainRequest]) { + for (NSString *header in [[self mainRequest] requestHeaders]) { + [self addRequestHeader:header value:[[[self mainRequest] requestHeaders] valueForKey:header]]; + } + return; + } + + [self applyCookieHeader]; + + // Build and set the user agent string if the request does not already have a custom user agent specified + if (![[self requestHeaders] objectForKey:@"User-Agent"]) { + NSString *userAgentString = [ASIHTTPRequest defaultUserAgentString]; + if (userAgentString) { + [self addRequestHeader:@"User-Agent" value:userAgentString]; + } + } + + + // Accept a compressed response + if ([self allowCompressedResponse]) { + [self addRequestHeader:@"Accept-Encoding" value:@"gzip"]; + } + + // Configure a compressed request body + if ([self shouldCompressRequestBody]) { + [self addRequestHeader:@"Content-Encoding" value:@"gzip"]; + } + + // Should this request resume an existing download? + if ([self allowResumeForFileDownloads] && [self downloadDestinationPath] && [self temporaryFileDownloadPath] && [[NSFileManager defaultManager] fileExistsAtPath:[self temporaryFileDownloadPath]]) { + NSError *err = nil; + [self setPartialDownloadSize:[[[NSFileManager defaultManager] attributesOfItemAtPath:[self temporaryFileDownloadPath] error:&err] fileSize]]; + if (err) { + [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to get attributes for file at path '%@'",[self temporaryFileDownloadPath]],NSLocalizedDescriptionKey,error,NSUnderlyingErrorKey,nil]]]; + return; + } + [self addRequestHeader:@"Range" value:[NSString stringWithFormat:@"bytes=%llu-",[self partialDownloadSize]]]; + } +} + +- (void)startRequest +{ + if ([self isCancelled]) { + return; + } + + [self requestStarted]; + + [self setDownloadComplete:NO]; + [self setComplete:NO]; + [self setTotalBytesRead:0]; + [self setLastBytesRead:0]; + + if ([self redirectCount] == 0) { + [self setOriginalURL:[self url]]; + } + + // If we're retrying a request, let's remove any progress we made + if ([self lastBytesSent] > 0) { + [self removeUploadProgressSoFar]; + } + + [self setLastBytesSent:0]; + [self setContentLength:0]; + [self setResponseHeaders:nil]; + if (![self downloadDestinationPath]) { + [self setRawResponseData:[[[NSMutableData alloc] init] autorelease]]; + } + + + // + // Create the stream for the request + // + + [self setReadStreamIsScheduled:NO]; + + // Do we need to stream the request body from disk + if ([self shouldStreamPostDataFromDisk] && [self postBodyFilePath] && [[NSFileManager defaultManager] fileExistsAtPath:[self postBodyFilePath]]) { + + // Are we gzipping the request body? + if ([self compressedPostBodyFilePath] && [[NSFileManager defaultManager] fileExistsAtPath:[self compressedPostBodyFilePath]]) { + [self setPostBodyReadStream:[ASIInputStream inputStreamWithFileAtPath:[self compressedPostBodyFilePath] request:self]]; + } else { + [self setPostBodyReadStream:[ASIInputStream inputStreamWithFileAtPath:[self postBodyFilePath] request:self]]; + } + [self setReadStream:[(NSInputStream *)CFReadStreamCreateForStreamedHTTPRequest(kCFAllocatorDefault, request,(CFReadStreamRef)[self postBodyReadStream]) autorelease]]; + } else { + + // If we have a request body, we'll stream it from memory using our custom stream, so that we can measure bandwidth use and it can be bandwidth-throttled if nescessary + if ([self postBody] && [[self postBody] length] > 0) { + if ([self shouldCompressRequestBody] && [self compressedPostBody]) { + [self setPostBodyReadStream:[ASIInputStream inputStreamWithData:[self compressedPostBody] request:self]]; + } else if ([self postBody]) { + [self setPostBodyReadStream:[ASIInputStream inputStreamWithData:[self postBody] request:self]]; + } + [self setReadStream:[(NSInputStream *)CFReadStreamCreateForStreamedHTTPRequest(kCFAllocatorDefault, request,(CFReadStreamRef)[self postBodyReadStream]) autorelease]]; + + } else { + [self setReadStream:[(NSInputStream *)CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, request) autorelease]]; + } + } + + if (![self readStream]) { + [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to create read stream",NSLocalizedDescriptionKey,nil]]]; + return; + } + + // Tell CFNetwork not to validate SSL certificates + if (![self validatesSecureCertificate] && [[[[self url] scheme] lowercaseString] isEqualToString:@"https"]) { + CFReadStreamSetProperty((CFReadStreamRef)[self readStream], kCFStreamPropertySSLSettings, [NSMutableDictionary dictionaryWithObject:(NSString *)kCFBooleanFalse forKey:(NSString *)kCFStreamSSLValidatesCertificateChain]); + } + + // + // Handle proxy settings + // + + // Have details of the proxy been set on this request + if (![self proxyHost] && ![self proxyPort]) { + + // If not, we need to figure out what they'll be + + NSArray *proxies = nil; + + // Have we been given a proxy auto config file? + if ([self PACurl]) { + + proxies = [ASIHTTPRequest proxiesForURL:[self url] fromPAC:[self PACurl]]; + + // Detect proxy settings and apply them + } else { + +#if TARGET_OS_IPHONE + NSDictionary *proxySettings = NSMakeCollectable([(NSDictionary *)CFNetworkCopySystemProxySettings() autorelease]); +#else + NSDictionary *proxySettings = NSMakeCollectable([(NSDictionary *)SCDynamicStoreCopyProxies(NULL) autorelease]); +#endif + + proxies = NSMakeCollectable([(NSArray *)CFNetworkCopyProxiesForURL((CFURLRef)[self url], (CFDictionaryRef)proxySettings) autorelease]); + + // Now check to see if the proxy settings contained a PAC url, we need to run the script to get the real list of proxies if so + NSDictionary *settings = [proxies objectAtIndex:0]; + if ([settings objectForKey:(NSString *)kCFProxyAutoConfigurationURLKey]) { + proxies = [ASIHTTPRequest proxiesForURL:[self url] fromPAC:[settings objectForKey:(NSString *)kCFProxyAutoConfigurationURLKey]]; + } + } + + if (!proxies) { + [self setReadStream:nil]; + [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to obtain information on proxy servers needed for request",NSLocalizedDescriptionKey,nil]]]; + return; + } + // I don't really understand why the dictionary returned by CFNetworkCopyProxiesForURL uses different key names from CFNetworkCopySystemProxySettings/SCDynamicStoreCopyProxies + // and why its key names are documented while those we actually need to use don't seem to be (passing the kCF* keys doesn't seem to work) + if ([proxies count] > 0) { + NSDictionary *settings = [proxies objectAtIndex:0]; + [self setProxyHost:[settings objectForKey:(NSString *)kCFProxyHostNameKey]]; + [self setProxyPort:[[settings objectForKey:(NSString *)kCFProxyPortNumberKey] intValue]]; + [self setProxyType:[settings objectForKey:(NSString *)kCFProxyTypeKey]]; + } + } + if ([self proxyHost] && [self proxyPort]) { + NSString *hostKey; + NSString *portKey; + + if (![self proxyType]) { + [self setProxyType:(NSString *)kCFProxyTypeHTTP]; + } + + if ([[self proxyType] isEqualToString:(NSString *)kCFProxyTypeSOCKS]) { + hostKey = (NSString *)kCFStreamPropertySOCKSProxyHost; + portKey = (NSString *)kCFStreamPropertySOCKSProxyPort; + } else { + hostKey = (NSString *)kCFStreamPropertyHTTPProxyHost; + portKey = (NSString *)kCFStreamPropertyHTTPProxyPort; + if ([[[[self url] scheme] lowercaseString] isEqualToString:@"https"]) { + hostKey = (NSString *)kCFStreamPropertyHTTPSProxyHost; + portKey = (NSString *)kCFStreamPropertyHTTPSProxyPort; + } + } + NSMutableDictionary *proxyToUse = [NSMutableDictionary dictionaryWithObjectsAndKeys:[self proxyHost],hostKey,[NSNumber numberWithInt:[self proxyPort]],portKey,nil]; + + if ([[self proxyType] isEqualToString:(NSString *)kCFProxyTypeSOCKS]) { + CFReadStreamSetProperty((CFReadStreamRef)[self readStream], kCFStreamPropertySOCKSProxy, proxyToUse); + } else { + CFReadStreamSetProperty((CFReadStreamRef)[self readStream], kCFStreamPropertyHTTPProxy, proxyToUse); + } + } + + // + // Handle persistent connections + // + + [ASIHTTPRequest expirePersistentConnections]; + + [connectionsLock lock]; + + + if (![[self url] host] || ![[self url] scheme]) { + [self setConnectionInfo:nil]; + [self setShouldAttemptPersistentConnection:NO]; + } + + // Will store the old stream that was using this connection (if there was one) so we can clean it up once we've opened our own stream + NSInputStream *oldStream = nil; + + // Use a persistent connection if possible + if ([self shouldAttemptPersistentConnection]) { + + + // If we are redirecting, we will re-use the current connection only if we are connecting to the same server + if ([self connectionInfo]) { + + if (![[[self connectionInfo] objectForKey:@"host"] isEqualToString:[[self url] host]] || ![[[self connectionInfo] objectForKey:@"scheme"] isEqualToString:[[self url] scheme]] || [(NSNumber *)[[self connectionInfo] objectForKey:@"port"] intValue] != [[[self url] port] intValue]) { + [self setConnectionInfo:nil]; + + // Check if we should have expired this connection + } else if ([[[self connectionInfo] objectForKey:@"expires"] timeIntervalSinceNow] < 0) { + #if DEBUG_PERSISTENT_CONNECTIONS + //nslog(@"Not re-using connection #%hi because it has expired",[[[self connectionInfo] objectForKey:@"id"] intValue]); + #endif + [persistentConnectionsPool removeObject:[self connectionInfo]]; + [self setConnectionInfo:nil]; + } + } + + + + if (![self connectionInfo] && [[self url] host] && [[self url] scheme]) { // We must have a proper url with a host and scheme, or this will explode + + // Look for a connection to the same server in the pool + for (NSMutableDictionary *existingConnection in persistentConnectionsPool) { + if (![existingConnection objectForKey:@"request"] && [[existingConnection objectForKey:@"host"] isEqualToString:[[self url] host]] && [[existingConnection objectForKey:@"scheme"] isEqualToString:[[self url] scheme]] && [(NSNumber *)[existingConnection objectForKey:@"port"] intValue] == [[[self url] port] intValue]) { + [self setConnectionInfo:existingConnection]; + } + } + } + + if ([[self connectionInfo] objectForKey:@"stream"]) { + oldStream = [[[self connectionInfo] objectForKey:@"stream"] retain]; + + } + + // No free connection was found in the pool matching the server/scheme/port we're connecting to, we'll need to create a new one + if (![self connectionInfo]) { + [self setConnectionInfo:[NSMutableDictionary dictionary]]; + nextConnectionNumberToCreate++; + [[self connectionInfo] setObject:[NSNumber numberWithInt:nextConnectionNumberToCreate] forKey:@"id"]; + [[self connectionInfo] setObject:[[self url] host] forKey:@"host"]; + [[self connectionInfo] setObject:[NSNumber numberWithInt:[[[self url] port] intValue]] forKey:@"port"]; + [[self connectionInfo] setObject:[[self url] scheme] forKey:@"scheme"]; + [persistentConnectionsPool addObject:[self connectionInfo]]; + } + + // If we are retrying this request, it will already have a requestID + if (![self requestID]) { + nextRequestID++; + [self setRequestID:[NSNumber numberWithUnsignedInt:nextRequestID]]; + } + [[self connectionInfo] setObject:[self requestID] forKey:@"request"]; + [[self connectionInfo] setObject:[self readStream] forKey:@"stream"]; + CFReadStreamSetProperty((CFReadStreamRef)[self readStream], kCFStreamPropertyHTTPAttemptPersistentConnection, kCFBooleanTrue); + + #if DEBUG_PERSISTENT_CONNECTIONS + //nslog(@"Request #%@ will use connection #%hi",[self requestID],[[[self connectionInfo] objectForKey:@"id"] intValue]); + #endif + + + // Tag the stream with an id that tells it which connection to use behind the scenes + // See http://lists.apple.com/archives/macnetworkprog/2008/Dec/msg00001.html for details on this approach + + CFReadStreamSetProperty((CFReadStreamRef)[self readStream], CFSTR("ASIStreamID"), [[self connectionInfo] objectForKey:@"id"]); + + } + + [connectionsLock unlock]; + + // Schedule the stream + if (![self readStreamIsScheduled] && (!throttleWakeUpTime || [throttleWakeUpTime timeIntervalSinceDate:[NSDate date]] < 0)) { + [self scheduleReadStream]; + } + + BOOL streamSuccessfullyOpened = NO; + + + // Start the HTTP connection + CFStreamClientContext ctxt = {0, self, NULL, NULL, NULL}; + if (CFReadStreamSetClient((CFReadStreamRef)[self readStream], kNetworkEvents, ReadStreamClientCallBack, &ctxt)) { + if (CFReadStreamOpen((CFReadStreamRef)[self readStream])) { + streamSuccessfullyOpened = YES; + } + } + + // Here, we'll close the stream that was previously using this connection, if there was one + // We've kept it open until now (when we've just opened a new stream) so that the new stream can make use of the old connection + // http://lists.apple.com/archives/Macnetworkprog/2006/Mar/msg00119.html + if (oldStream) { + [oldStream close]; + [oldStream release]; + oldStream = nil; + } + + if (!streamSuccessfullyOpened) { + [self setConnectionCanBeReused:NO]; + [self destroyReadStream]; + [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to start HTTP connection",NSLocalizedDescriptionKey,nil]]]; + return; + } + + if (![self mainRequest]) { + if ([self shouldResetUploadProgress]) { + if ([self showAccurateProgress]) { + [self incrementUploadSizeBy:[self postLength]]; + } else { + [self incrementUploadSizeBy:1]; + } + [ASIHTTPRequest updateProgressIndicator:[self uploadProgressDelegate] withProgress:0 ofTotal:1]; + } + if ([self shouldResetDownloadProgress] && ![self partialDownloadSize]) { + [ASIHTTPRequest updateProgressIndicator:[self downloadProgressDelegate] withProgress:0 ofTotal:1]; + } + } + + + // Record when the request started, so we can timeout if nothing happens + [self setLastActivityTime:[NSDate date]]; + [self setStatusTimer:[NSTimer timerWithTimeInterval:0.25 target:self selector:@selector(updateStatus:) userInfo:nil repeats:YES]]; + [[NSRunLoop currentRunLoop] addTimer:[self statusTimer] forMode:[self runLoopMode]]; +} + +- (void)setStatusTimer:(NSTimer *)timer +{ + [self retain]; + // We must invalidate the old timer here, not before we've created and scheduled a new timer + // This is because the timer may be the only thing retaining an asynchronous request + if (statusTimer && timer != statusTimer) { + [statusTimer invalidate]; + [statusTimer release]; + } + statusTimer = [timer retain]; + [self release]; +} + +// This gets fired every 1/4 of a second to update the progress and work out if we need to timeout +- (void)updateStatus:(NSTimer*)timer +{ + [self checkRequestStatus]; + if (![self inProgress]) { + [self setStatusTimer:nil]; + } +} + +- (void)performRedirect +{ + [self setComplete:YES]; + [self setNeedsRedirect:NO]; + [self setRedirectCount:[self redirectCount]+1]; + + if ([self redirectCount] > RedirectionLimit) { + // Some naughty / badly coded website is trying to force us into a redirection loop. This is not cool. + [self failWithError:ASITooMuchRedirectionError]; + [self setComplete:YES]; + } else { + // Go all the way back to the beginning and build the request again, so that we can apply any new cookies + [self main]; + } +} + +- (BOOL)shouldTimeOut +{ + NSTimeInterval secondsSinceLastActivity = [[NSDate date] timeIntervalSinceDate:lastActivityTime]; + // See if we need to timeout + if ([self readStream] && [self readStreamIsScheduled] && [self lastActivityTime] && [self timeOutSeconds] > 0 && secondsSinceLastActivity > [self timeOutSeconds]) { + + // We have no body, or we've sent more than the upload buffer size,so we can safely time out here + if ([self postLength] == 0 || ([self uploadBufferSize] > 0 && [self totalBytesSent] > [self uploadBufferSize])) { + return YES; + + // ***Black magic warning*** + // We have a body, but we've taken longer than timeOutSeconds to upload the first small chunk of data + // Since there's no reliable way to track upload progress for the first 32KB (iPhone) or 128KB (Mac) with CFNetwork, we'll be slightly more forgiving on the timeout, as there's a strong chance our connection is just very slow. + } else if (secondsSinceLastActivity > [self timeOutSeconds]*1.5) { + return YES; + } + } + return NO; +} + +- (void)checkRequestStatus +{ + // We won't let the request cancel while we're updating progress / checking for a timeout + [[self cancelledLock] lock]; + + // See if our NSOperationQueue told us to cancel + if ([self isCancelled] || [self complete]) { + [[self cancelledLock] unlock]; + return; + } + + [self performThrottling]; + + if ([self shouldTimeOut]) { + // Do we need to auto-retry this request? + if ([self numberOfTimesToRetryOnTimeout] > [self retryCount]) { + [self setRetryCount:[self retryCount]+1]; + [self unscheduleReadStream]; + [[self cancelledLock] unlock]; + [self startRequest]; + return; + } + [self failWithError:ASIRequestTimedOutError]; + [self cancelLoad]; + [self setComplete:YES]; + [[self cancelledLock] unlock]; + return; + } + + // readStream will be null if we aren't currently running (perhaps we're waiting for a delegate to supply credentials) + if ([self readStream]) { + + // If we have a post body + if ([self postLength]) { + + [self setLastBytesSent:totalBytesSent]; + + // Find out how much data we've uploaded so far + [self setTotalBytesSent:[NSMakeCollectable([(NSNumber *)CFReadStreamCopyProperty((CFReadStreamRef)[self readStream], kCFStreamPropertyHTTPRequestBytesWrittenCount) autorelease]) unsignedLongLongValue]]; + if (totalBytesSent > lastBytesSent) { + + // We've uploaded more data, reset the timeout + [self setLastActivityTime:[NSDate date]]; + [ASIHTTPRequest incrementBandwidthUsedInLastSecond:(unsigned long)(totalBytesSent-lastBytesSent)]; + + #if DEBUG_REQUEST_STATUS + if ([self totalBytesSent] == [self postLength]) { + //nslog(@"Request %@ finished uploading data",self); + } + #endif + } + } + + [self updateProgressIndicators]; + + } + + [[self cancelledLock] unlock]; +} + + +// Cancel loading and clean up. DO NOT USE THIS TO CANCEL REQUESTS - use [request cancel] instead +- (void)cancelLoad +{ + [self destroyReadStream]; + + [[self postBodyReadStream] close]; + + if ([self rawResponseData]) { + [self setRawResponseData:nil]; + + // If we were downloading to a file + } else if ([self temporaryFileDownloadPath]) { + [[self fileDownloadOutputStream] close]; + + // If we haven't said we might want to resume, let's remove the temporary file too + if (![self allowResumeForFileDownloads]) { + [self removeTemporaryDownloadFile]; + } + } + + // Clean up any temporary file used to store request body for streaming + if (![self authenticationNeeded] && [self didCreateTemporaryPostDataFile]) { + [self removePostDataFile]; + [self setDidCreateTemporaryPostDataFile:NO]; + } + + [self setResponseHeaders:nil]; +} + + +- (void)removeTemporaryDownloadFile +{ + if ([self temporaryFileDownloadPath]) { + if ([[NSFileManager defaultManager] fileExistsAtPath:[self temporaryFileDownloadPath]]) { + NSError *removeError = nil; + [[NSFileManager defaultManager] removeItemAtPath:[self temporaryFileDownloadPath] error:&removeError]; + if (removeError) { + [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to delete file at path '%@'",[self temporaryFileDownloadPath]],NSLocalizedDescriptionKey,removeError,NSUnderlyingErrorKey,nil]]]; + } + } + [self setTemporaryFileDownloadPath:nil]; + } +} + +- (void)removePostDataFile +{ + if ([self postBodyFilePath]) { + NSError *removeError = nil; + [[NSFileManager defaultManager] removeItemAtPath:[self postBodyFilePath] error:&removeError]; + if (removeError) { + [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to delete file at path '%@'",[self postBodyFilePath]],NSLocalizedDescriptionKey,removeError,NSUnderlyingErrorKey,nil]]]; + } + [self setPostBodyFilePath:nil]; + } + if ([self compressedPostBodyFilePath]) { + NSError *removeError = nil; + [[NSFileManager defaultManager] removeItemAtPath:[self compressedPostBodyFilePath] error:&removeError]; + if (removeError) { + [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to delete file at path '%@'",[self compressedPostBodyFilePath]],NSLocalizedDescriptionKey,removeError,NSUnderlyingErrorKey,nil]]]; + } + [self setCompressedPostBodyFilePath:nil]; + } +} + +#pragma mark HEAD request + +// Used by ASINetworkQueue to create a HEAD request appropriate for this request with the same headers (though you can use it yourself) +- (ASIHTTPRequest *)HEADRequest +{ + ASIHTTPRequest *headRequest = [[self class] requestWithURL:[self url]]; + + // Copy the properties that make sense for a HEAD request + [headRequest setRequestHeaders:[[[self requestHeaders] mutableCopy] autorelease]]; + [headRequest setRequestCookies:[[[self requestCookies] mutableCopy] autorelease]]; + [headRequest setUseCookiePersistence:[self useCookiePersistence]]; + [headRequest setUseKeychainPersistence:[self useKeychainPersistence]]; + [headRequest setUseSessionPersistence:[self useSessionPersistence]]; + [headRequest setAllowCompressedResponse:[self allowCompressedResponse]]; + [headRequest setUsername:[self username]]; + [headRequest setPassword:[self password]]; + [headRequest setDomain:[self domain]]; + [headRequest setProxyUsername:[self proxyUsername]]; + [headRequest setProxyPassword:[self proxyPassword]]; + [headRequest setProxyDomain:[self proxyDomain]]; + [headRequest setProxyHost:[self proxyHost]]; + [headRequest setProxyPort:[self proxyPort]]; + [headRequest setProxyType:[self proxyType]]; + [headRequest setShouldPresentAuthenticationDialog:[self shouldPresentAuthenticationDialog]]; + [headRequest setShouldPresentProxyAuthenticationDialog:[self shouldPresentProxyAuthenticationDialog]]; + [headRequest setTimeOutSeconds:[self timeOutSeconds]]; + [headRequest setUseHTTPVersionOne:[self useHTTPVersionOne]]; + [headRequest setValidatesSecureCertificate:[self validatesSecureCertificate]]; + [headRequest setPACurl:[self PACurl]]; + [headRequest setShouldPresentCredentialsBeforeChallenge:[self shouldPresentCredentialsBeforeChallenge]]; + [headRequest setNumberOfTimesToRetryOnTimeout:[self numberOfTimesToRetryOnTimeout]]; + [headRequest setShouldUseRFC2616RedirectBehaviour:[self shouldUseRFC2616RedirectBehaviour]]; + [headRequest setShouldAttemptPersistentConnection:[self shouldAttemptPersistentConnection]]; + [headRequest setPersistentConnectionTimeoutSeconds:[self persistentConnectionTimeoutSeconds]]; + + [headRequest setMainRequest:self]; + [headRequest setRequestMethod:@"HEAD"]; + return headRequest; +} + + +#pragma mark upload/download progress + + +- (void)updateProgressIndicators +{ + //Only update progress if this isn't a HEAD request used to preset the content-length + if (![self mainRequest]) { + if ([self showAccurateProgress] || ([self complete] && ![self updatedProgress])) { + [self updateUploadProgress]; + [self updateDownloadProgress]; + } + } +} + +- (id)uploadProgressDelegate +{ + [[self cancelledLock] lock]; + id d = [[uploadProgressDelegate retain] autorelease]; + [[self cancelledLock] unlock]; + return d; +} + +- (void)setUploadProgressDelegate:(id)newDelegate +{ + [[self cancelledLock] lock]; + uploadProgressDelegate = newDelegate; + + #if !TARGET_OS_IPHONE + // If the uploadProgressDelegate is an NSProgressIndicator, we set its MaxValue to 1.0 so we can update it as if it were a UIProgressView + double max = 1.0; + [ASIHTTPRequest performSelector:@selector(setMaxValue:) onTarget:[self uploadProgressDelegate] withObject:nil amount:&max]; + #endif + [[self cancelledLock] unlock]; +} + +- (id)downloadProgressDelegate +{ + [[self cancelledLock] lock]; + id d = [[downloadProgressDelegate retain] autorelease]; + [[self cancelledLock] unlock]; + return d; +} + +- (void)setDownloadProgressDelegate:(id)newDelegate +{ + [[self cancelledLock] lock]; + downloadProgressDelegate = newDelegate; + + #if !TARGET_OS_IPHONE + // If the downloadProgressDelegate is an NSProgressIndicator, we set its MaxValue to 1.0 so we can update it as if it were a UIProgressView + double max = 1.0; + [ASIHTTPRequest performSelector:@selector(setMaxValue:) onTarget:[self downloadProgressDelegate] withObject:nil amount:&max]; + #endif + [[self cancelledLock] unlock]; +} + + +- (void)updateDownloadProgress +{ + // We won't update download progress until we've examined the headers, since we might need to authenticate + if (![self responseHeaders] || [self needsRedirect] || !([self contentLength] || [self complete])) { + return; + } + + unsigned long long bytesReadSoFar = [self totalBytesRead]+[self partialDownloadSize]; + unsigned long long value = 0; + + if ([self showAccurateProgress] && [self contentLength]) { + value = bytesReadSoFar-[self lastBytesRead]; + if (value == 0) { + return; + } + } else { + value = 1; + [self setUpdatedProgress:YES]; + } + if (!value) { + return; + } + + [ASIHTTPRequest performSelector:@selector(request:didReceiveBytes:) onTarget:[self queue] withObject:self amount:&value]; + [ASIHTTPRequest performSelector:@selector(request:didReceiveBytes:) onTarget:[self downloadProgressDelegate] withObject:self amount:&value]; + [ASIHTTPRequest updateProgressIndicator:[self downloadProgressDelegate] withProgress:[self totalBytesRead]+[self partialDownloadSize] ofTotal:[self contentLength]+[self partialDownloadSize]]; + + [self setLastBytesRead:bytesReadSoFar]; +} + + +- (void)updateUploadProgress +{ + if ([self isCancelled] || [self totalBytesSent] == 0) { + return; + } + + // If this is the first time we've written to the buffer, totalBytesSent will be the size of the buffer (currently seems to be 128KB on both Leopard and iPhone 2.2.1, 32KB on iPhone 3.0) + // If request body is less than the buffer size, totalBytesSent will be the total size of the request body + // We will remove this from any progress display, as kCFStreamPropertyHTTPRequestBytesWrittenCount does not tell us how much data has actually be written + if ([self uploadBufferSize] == 0 && [self totalBytesSent] != [self postLength]) { + [self setUploadBufferSize:[self totalBytesSent]]; + [self incrementUploadSizeBy:-[self uploadBufferSize]]; + } + + unsigned long long value = 0; + + if ([self showAccurateProgress]) { + if ([self totalBytesSent] == [self postLength] || [self lastBytesSent] > 0) { + value = [self totalBytesSent]-[self lastBytesSent]; + } else { + return; + } + } else { + value = 1; + [self setUpdatedProgress:YES]; + } + + if (!value) { + return; + } + + [ASIHTTPRequest performSelector:@selector(request:didSendBytes:) onTarget:[self queue] withObject:self amount:&value]; + [ASIHTTPRequest performSelector:@selector(request:didSendBytes:) onTarget:[self uploadProgressDelegate] withObject:self amount:&value]; + [ASIHTTPRequest updateProgressIndicator:[self uploadProgressDelegate] withProgress:[self totalBytesSent]-[self uploadBufferSize] ofTotal:[self postLength]-[self uploadBufferSize]]; +} + + +- (void)incrementDownloadSizeBy:(long long)length +{ + [ASIHTTPRequest performSelector:@selector(request:incrementDownloadSizeBy:) onTarget:[self queue] withObject:self amount:&length]; + [ASIHTTPRequest performSelector:@selector(request:incrementDownloadSizeBy:) onTarget:[self downloadProgressDelegate] withObject:self amount:&length]; +} + + +- (void)incrementUploadSizeBy:(long long)length +{ + [ASIHTTPRequest performSelector:@selector(request:incrementUploadSizeBy:) onTarget:[self queue] withObject:self amount:&length]; + [ASIHTTPRequest performSelector:@selector(request:incrementUploadSizeBy:) onTarget:[self uploadProgressDelegate] withObject:self amount:&length]; +} + + +-(void)removeUploadProgressSoFar +{ + long long progressToRemove = -[self totalBytesSent]; + [ASIHTTPRequest performSelector:@selector(request:didSendBytes:) onTarget:[self queue] withObject:self amount:&progressToRemove]; + [ASIHTTPRequest performSelector:@selector(request:didSendBytes:) onTarget:[self uploadProgressDelegate] withObject:self amount:&progressToRemove]; + [ASIHTTPRequest updateProgressIndicator:[self uploadProgressDelegate] withProgress:0 ofTotal:[self postLength]]; +} + + ++ (void)performSelector:(SEL)selector onTarget:(id)target withObject:(id)object amount:(void *)amount +{ + if ([target respondsToSelector:selector]) { + NSMethodSignature *signature = nil; + signature = [[target class] instanceMethodSignatureForSelector:selector]; + NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; + [invocation setTarget:target]; + [invocation setSelector:selector]; + + int argumentNumber = 2; + + // If we got an object parameter, we pass a pointer to the object pointer + if (object) { + [invocation setArgument:&object atIndex:argumentNumber]; + argumentNumber++; + } + + // For the amount we'll just pass the pointer directly so NSInvocation will call the method using the number itself rather than a pointer to it + if (amount) { + [invocation setArgument:amount atIndex:argumentNumber]; + } + + [invocation performSelectorOnMainThread:@selector(invokeWithTarget:) withObject:target waitUntilDone:[NSThread isMainThread]]; + } +} + + ++ (void)updateProgressIndicator:(id)indicator withProgress:(unsigned long long)progress ofTotal:(unsigned long long)total +{ + #if TARGET_OS_IPHONE + // Cocoa Touch: UIProgressView + SEL selector = @selector(setProgress:); + float progressAmount = (progress*1.0f)/(total*1.0f); + + #else + // Cocoa: NSProgressIndicator + double progressAmount = progressAmount = (progress*1.0)/(total*1.0); + SEL selector = @selector(setDoubleValue:); + #endif + + if (![indicator respondsToSelector:selector]) { + return; + } + + [progressLock lock]; + [ASIHTTPRequest performSelector:selector onTarget:indicator withObject:nil amount:&progressAmount]; + [progressLock unlock]; +} + + +#pragma mark handling request complete / failure + + + +- (void)requestReceivedResponseHeaders +{ + if ([self error] || [self mainRequest]) { + return; + } + // Let the delegate know we have started + if ([self didReceiveResponseHeadersSelector] && [[self delegate] respondsToSelector:[self didReceiveResponseHeadersSelector]]) { + [[self delegate] performSelectorOnMainThread:[self didReceiveResponseHeadersSelector] withObject:self waitUntilDone:[NSThread isMainThread]]; + } + + // Let the queue know we have started + if ([[self queue] respondsToSelector:@selector(requestReceivedResponseHeaders:)]) { + [[self queue] performSelectorOnMainThread:@selector(requestReceivedResponseHeaders:) withObject:self waitUntilDone:[NSThread isMainThread]]; + } +} + +- (void)requestStarted +{ + if ([self error] || [self mainRequest]) { + return; + } + // Let the delegate know we have started + if ([self didStartSelector] && [[self delegate] respondsToSelector:[self didStartSelector]]) { + [[self delegate] performSelectorOnMainThread:[self didStartSelector] withObject:self waitUntilDone:[NSThread isMainThread]]; + } + + // Let the queue know we have started + if ([[self queue] respondsToSelector:@selector(requestStarted:)]) { + [[self queue] performSelectorOnMainThread:@selector(requestStarted:) withObject:self waitUntilDone:[NSThread isMainThread]]; + } +} + +// Subclasses might override this method to process the result in the same thread +// If you do this, don't forget to call [super requestFinished] to let the queue / delegate know we're done +- (void)requestFinished +{ +#if DEBUG_REQUEST_STATUS || DEBUG_THROTTLING + //nslog(@"Request finished: %@",self); +#endif + if ([self error] || [self mainRequest]) { + return; + } + // Let the delegate know we are done + if ([self didFinishSelector] && [[self delegate] respondsToSelector:[self didFinishSelector]]) { + [[self delegate] performSelectorOnMainThread:[self didFinishSelector] withObject:self waitUntilDone:[NSThread isMainThread]]; + } + + // Let the queue know we are done + if ([[self queue] respondsToSelector:@selector(requestFinished:)]) { + [[self queue] performSelectorOnMainThread:@selector(requestFinished:) withObject:self waitUntilDone:[NSThread isMainThread]]; + } + +} + +// Subclasses might override this method to perform error handling in the same thread +// If you do this, don't forget to call [super failWithError:] to let the queue / delegate know we're done +- (void)failWithError:(NSError *)theError +{ +#if DEBUG_REQUEST_STATUS || DEBUG_THROTTLING + //nslog(@"Request %@: %@",self,(theError == ASIRequestCancelledError ? @"Cancelled" : @"Failed")); +#endif + [self setComplete:YES]; + + // Invalidate the current connection so subsequent requests don't attempt to reuse it + if (theError && [theError code] != ASIAuthenticationErrorType && [theError code] != ASITooMuchRedirectionErrorType) { + [connectionsLock lock]; + #if DEBUG_PERSISTENT_CONNECTIONS + //nslog(@"Request #%@ failed and will invalidate connection #%@",[self requestID],[[self connectionInfo] objectForKey:@"id"]); + #endif + [[self connectionInfo] removeObjectForKey:@"request"]; + [persistentConnectionsPool removeObject:[self connectionInfo]]; + [connectionsLock unlock]; + [self destroyReadStream]; + } + if ([self connectionCanBeReused]) { + [[self connectionInfo] setObject:[NSDate dateWithTimeIntervalSinceNow:[self persistentConnectionTimeoutSeconds]] forKey:@"expires"]; + } + + if ([self isCancelled] || [self error]) { + return; + } + + if ([self downloadCache] && [self cachePolicy] == ASIUseCacheIfLoadFailsCachePolicy) { + if ([self useDataFromCache]) { + return; + } + } + + + [self setError:theError]; + + ASIHTTPRequest *failedRequest = self; + + // If this is a HEAD request created by an ASINetworkQueue or compatible queue delegate, make the main request fail + if ([self mainRequest]) { + failedRequest = [self mainRequest]; + [failedRequest setError:theError]; + } + + // Let the delegate know something went wrong + if ([failedRequest didFailSelector] && [[failedRequest delegate] respondsToSelector:[failedRequest didFailSelector]]) { + [[failedRequest delegate] performSelectorOnMainThread:[failedRequest didFailSelector] withObject:failedRequest waitUntilDone:[NSThread isMainThread]]; + } + + // Let the queue know something went wrong + if ([[failedRequest queue] respondsToSelector:@selector(requestFailed:)]) { + [[failedRequest queue] performSelectorOnMainThread:@selector(requestFailed:) withObject:failedRequest waitUntilDone:[NSThread isMainThread]]; + } + + // markAsFinished may well cause this object to be dealloced + [self retain]; + [self markAsFinished]; + [self release]; +} + +#pragma mark parsing HTTP response headers + +- (void)readResponseHeaders +{ + [self setAuthenticationNeeded:ASINoAuthenticationNeededYet]; + + CFHTTPMessageRef message = (CFHTTPMessageRef)CFReadStreamCopyProperty((CFReadStreamRef)[self readStream], kCFStreamPropertyHTTPResponseHeader); + if (!message) { + return; + } + + // Make sure we've received all the headers + if (!CFHTTPMessageIsHeaderComplete(message)) { + CFRelease(message); + return; + } + + #if DEBUG_REQUEST_STATUS + if ([self totalBytesSent] == [self postLength]) { + //nslog(@"Request %@ received response headers",self); + } + #endif + + CFDictionaryRef headerFields = CFHTTPMessageCopyAllHeaderFields(message); + [self setResponseHeaders:(NSDictionary *)headerFields]; + + CFRelease(headerFields); + + [self setResponseStatusCode:(int)CFHTTPMessageGetResponseStatusCode(message)]; + [self setResponseStatusMessage:[(NSString *)CFHTTPMessageCopyResponseStatusLine(message) autorelease]]; + + if ([self downloadCache] && [self cachePolicy] == ASIReloadIfDifferentCachePolicy) { + if ([self useDataFromCache]) { + CFRelease(message); + return; + } + } + + // Is the server response a challenge for credentials? + if ([self responseStatusCode] == 401) { + [self setAuthenticationNeeded:ASIHTTPAuthenticationNeeded]; + } else if ([self responseStatusCode] == 407) { + [self setAuthenticationNeeded:ASIProxyAuthenticationNeeded]; + } + + // Authentication succeeded, or no authentication was required + if (![self authenticationNeeded]) { + + // Did we get here without an authentication challenge? (which can happen when shouldPresentCredentialsBeforeChallenge is YES and basic auth was successful) + if (!requestAuthentication && [self username] && [self password] && [self useSessionPersistence]) { + + NSMutableDictionary *newCredentials = [NSMutableDictionary dictionaryWithCapacity:2]; + [newCredentials setObject:[self username] forKey:(NSString *)kCFHTTPAuthenticationUsername]; + [newCredentials setObject:[self password] forKey:(NSString *)kCFHTTPAuthenticationPassword]; + + // Store the credentials in the session + NSMutableDictionary *sessionCredentials = [NSMutableDictionary dictionary]; + [sessionCredentials setObject:newCredentials forKey:@"Credentials"]; + [sessionCredentials setObject:[self url] forKey:@"URL"]; + [sessionCredentials setObject:(NSString *)kCFHTTPAuthenticationSchemeBasic forKey:@"AuthenticationScheme"]; + [[self class] storeAuthenticationCredentialsInSessionStore:sessionCredentials]; + } + } + + // Handle response text encoding + [self parseStringEncodingFromHeaders]; + + // Handle cookies + NSArray *newCookies = [NSHTTPCookie cookiesWithResponseHeaderFields:[self responseHeaders] forURL:[self url]]; + [self setResponseCookies:newCookies]; + + if ([self useCookiePersistence]) { + + // Store cookies in global persistent store + [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:newCookies forURL:[self url] mainDocumentURL:nil]; + + // We also keep any cookies in the sessionCookies array, so that we have a reference to them if we need to remove them later + NSHTTPCookie *cookie; + for (cookie in newCookies) { + [ASIHTTPRequest addSessionCookie:cookie]; + } + } + + // Do we need to redirect? + // Note that ASIHTTPRequest does not currently support 305 Use Proxy + if ([self shouldRedirect] && [responseHeaders valueForKey:@"Location"]) { + if (([self responseStatusCode] > 300 && [self responseStatusCode] < 304) || [self responseStatusCode] == 307) { + + // By default, we redirect 301 and 302 response codes as GET requests + // According to RFC 2616 this is wrong, but this is what most browsers do, so it's probably what you're expecting to happen + // See also: + // http://allseeing-i.lighthouseapp.com/projects/27881/tickets/27-302-redirection-issue + + if ([self responseStatusCode] != 307 && (![self shouldUseRFC2616RedirectBehaviour] || [self responseStatusCode] == 303)) { + [self setRequestMethod:@"GET"]; + [self setPostBody:nil]; + [self setPostLength:0]; + + // Perhaps there are other headers we should be preserving, but it's hard to know what we need to keep and what to throw away. + NSString *userAgent = [[self requestHeaders] objectForKey:@"User-Agent"]; + if (userAgent) { + [self setRequestHeaders:[NSMutableDictionary dictionaryWithObject:userAgent forKey:@"User-Agent"]]; + } else { + [self setRequestHeaders:nil]; + } + [self setHaveBuiltRequestHeaders:NO]; + } else { + + // Force rebuild the cookie header incase we got some new cookies from this request + // All other request headers will remain as they are for 301 / 302 redirects + [self applyCookieHeader]; + } + + // Force the redirected request to rebuild the request headers (if not a 303, it will re-use old ones, and add any new ones) + + [self setURL:[[NSURL URLWithString:[responseHeaders valueForKey:@"Location"] relativeToURL:[self url]] absoluteURL]]; + [self setNeedsRedirect:YES]; + + // Clear the request cookies + // This means manually added cookies will not be added to the redirect request - only those stored in the global persistent store + // But, this is probably the safest option - we might be redirecting to a different domain + [self setRequestCookies:[NSMutableArray array]]; + + #if DEBUG_REQUEST_STATUS + //nslog(@"Request will redirect (code: %hi): %@",[self responseStatusCode],self); + #endif + + } + } + + if (![self needsRedirect]) { + // See if we got a Content-length header + NSString *cLength = [responseHeaders valueForKey:@"Content-Length"]; + ASIHTTPRequest *theRequest = self; + if ([self mainRequest]) { + theRequest = [self mainRequest]; + } + + if (cLength) { + SInt32 length = CFStringGetIntValue((CFStringRef)cLength); + + // Workaround for Apache HEAD requests for dynamically generated content returning the wrong Content-Length when using gzip + if ([self mainRequest] && [self allowCompressedResponse] && length == 20 && [self showAccurateProgress] && [self shouldResetDownloadProgress]) { + [[self mainRequest] setShowAccurateProgress:NO]; + [[self mainRequest] incrementDownloadSizeBy:1]; + + } else { + [theRequest setContentLength:length]; + if ([self showAccurateProgress] && [self shouldResetDownloadProgress]) { + [theRequest incrementDownloadSizeBy:[theRequest contentLength]+[theRequest partialDownloadSize]]; + } + } + + } else if ([self showAccurateProgress] && [self shouldResetDownloadProgress]) { + [theRequest setShowAccurateProgress:NO]; + [theRequest incrementDownloadSizeBy:1]; + } + } + + // Handle connection persistence + if ([self shouldAttemptPersistentConnection]) { + + NSString *connectionHeader = [[[self responseHeaders] objectForKey:@"Connection"] lowercaseString]; + NSString *httpVersion = NSMakeCollectable([(NSString *)CFHTTPMessageCopyVersion(message) autorelease]); + + // Don't re-use the connection if the server is HTTP 1.0 and didn't send Connection: Keep-Alive + if (![httpVersion isEqualToString:(NSString *)kCFHTTPVersion1_0] || [connectionHeader isEqualToString:@"keep-alive"]) { + + // See if server explicitly told us to close the connection + if (![connectionHeader isEqualToString:@"close"]) { + + NSString *keepAliveHeader = [[self responseHeaders] objectForKey:@"Keep-Alive"]; + + // If we got a keep alive header, we'll reuse the connection for as long as the server tells us + if (keepAliveHeader) { + int timeout = 0; + int max = 0; + NSScanner *scanner = [NSScanner scannerWithString:keepAliveHeader]; + [scanner scanString:@"timeout=" intoString:NULL]; + [scanner scanInt:&timeout]; + [scanner scanUpToString:@"max=" intoString:NULL]; + [scanner scanString:@"max=" intoString:NULL]; + [scanner scanInt:&max]; + if (max > 5) { + [self setConnectionCanBeReused:YES]; + [self setPersistentConnectionTimeoutSeconds:timeout]; + #if DEBUG_PERSISTENT_CONNECTIONS + //nslog(@"Got a keep-alive header, will keep this connection open for %f seconds", [self persistentConnectionTimeoutSeconds]); + #endif + } + + // Otherwise, we'll assume we can keep this connection open + } else { + [self setConnectionCanBeReused:YES]; + #if DEBUG_PERSISTENT_CONNECTIONS + //nslog(@"Got no keep-alive header, will keep this connection open for %f seconds", [self persistentConnectionTimeoutSeconds]); + #endif + } + } + } + } + + CFRelease(message); + [self requestReceivedResponseHeaders]; +} + +// Handle response text encoding +// If the Content-Type header specified an encoding, we'll use that, otherwise we use defaultStringEncoding (which defaults to NSISOLatin1StringEncoding) +- (void)parseStringEncodingFromHeaders +{ + NSString *contentType = [[self responseHeaders] objectForKey:@"Content-Type"]; + NSStringEncoding encoding = [self defaultResponseEncoding]; + if (contentType) { + + NSString *charsetSeparator = @"charset="; + NSScanner *charsetScanner = [NSScanner scannerWithString: contentType]; + NSString *IANAEncoding = nil; + + if ([charsetScanner scanUpToString: charsetSeparator intoString: NULL] && [charsetScanner scanLocation] < [contentType length]) { + [charsetScanner setScanLocation: [charsetScanner scanLocation] + [charsetSeparator length]]; + [charsetScanner scanUpToString: @";" intoString: &IANAEncoding]; + } + + if (IANAEncoding) { + CFStringEncoding cfEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)IANAEncoding); + if (cfEncoding != kCFStringEncodingInvalidId) { + encoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding); + } + } + } + [self setResponseEncoding:encoding]; +} + +#pragma mark http authentication + +- (void)saveProxyCredentialsToKeychain:(NSDictionary *)newCredentials +{ + NSURLCredential *authenticationCredentials = [NSURLCredential credentialWithUser:[newCredentials objectForKey:(NSString *)kCFHTTPAuthenticationUsername] password:[newCredentials objectForKey:(NSString *)kCFHTTPAuthenticationPassword] persistence:NSURLCredentialPersistencePermanent]; + if (authenticationCredentials) { + [ASIHTTPRequest saveCredentials:authenticationCredentials forProxy:[self proxyHost] port:[self proxyPort] realm:[self proxyAuthenticationRealm]]; + } +} + + +- (void)saveCredentialsToKeychain:(NSDictionary *)newCredentials +{ + NSURLCredential *authenticationCredentials = [NSURLCredential credentialWithUser:[newCredentials objectForKey:(NSString *)kCFHTTPAuthenticationUsername] password:[newCredentials objectForKey:(NSString *)kCFHTTPAuthenticationPassword] persistence:NSURLCredentialPersistencePermanent]; + + if (authenticationCredentials) { + [ASIHTTPRequest saveCredentials:authenticationCredentials forHost:[[self url] host] port:[[[self url] port] intValue] protocol:[[self url] scheme] realm:[self authenticationRealm]]; + } +} + +- (BOOL)applyProxyCredentials:(NSDictionary *)newCredentials +{ + [self setProxyAuthenticationRetryCount:[self proxyAuthenticationRetryCount]+1]; + + if (newCredentials && proxyAuthentication && request) { + + // Apply whatever credentials we've built up to the old request + if (CFHTTPMessageApplyCredentialDictionary(request, proxyAuthentication, (CFMutableDictionaryRef)newCredentials, NULL)) { + + //If we have credentials and they're ok, let's save them to the keychain + if (useKeychainPersistence) { + [self saveProxyCredentialsToKeychain:newCredentials]; + } + if (useSessionPersistence) { + NSMutableDictionary *sessionProxyCredentials = [NSMutableDictionary dictionary]; + [sessionProxyCredentials setObject:(id)proxyAuthentication forKey:@"Authentication"]; + [sessionProxyCredentials setObject:newCredentials forKey:@"Credentials"]; + [sessionProxyCredentials setObject:[self proxyHost] forKey:@"Host"]; + [sessionProxyCredentials setObject:[NSNumber numberWithInt:[self proxyPort]] forKey:@"Port"]; + [sessionProxyCredentials setObject:[self proxyAuthenticationScheme] forKey:@"AuthenticationScheme"]; + [[self class] storeProxyAuthenticationCredentialsInSessionStore:sessionProxyCredentials]; + } + [self setProxyCredentials:newCredentials]; + return YES; + } else { + [[self class] removeProxyAuthenticationCredentialsFromSessionStore:newCredentials]; + } + } + return NO; +} + +- (BOOL)applyCredentials:(NSDictionary *)newCredentials +{ + [self setAuthenticationRetryCount:[self authenticationRetryCount]+1]; + + if (newCredentials && requestAuthentication && request) { + // Apply whatever credentials we've built up to the old request + if (CFHTTPMessageApplyCredentialDictionary(request, requestAuthentication, (CFMutableDictionaryRef)newCredentials, NULL)) { + + //If we have credentials and they're ok, let's save them to the keychain + if (useKeychainPersistence) { + [self saveCredentialsToKeychain:newCredentials]; + } + if (useSessionPersistence) { + + NSMutableDictionary *sessionCredentials = [NSMutableDictionary dictionary]; + [sessionCredentials setObject:(id)requestAuthentication forKey:@"Authentication"]; + [sessionCredentials setObject:newCredentials forKey:@"Credentials"]; + [sessionCredentials setObject:[self url] forKey:@"URL"]; + [sessionCredentials setObject:[self authenticationScheme] forKey:@"AuthenticationScheme"]; + if ([self authenticationRealm]) { + [sessionCredentials setObject:[self authenticationRealm] forKey:@"AuthenticationRealm"]; + } + [[self class] storeAuthenticationCredentialsInSessionStore:sessionCredentials]; + + } + [self setRequestCredentials:newCredentials]; + return YES; + } else { + [[self class] removeAuthenticationCredentialsFromSessionStore:newCredentials]; + } + } + return NO; +} + +- (NSMutableDictionary *)findProxyCredentials +{ + NSMutableDictionary *newCredentials = [[[NSMutableDictionary alloc] init] autorelease]; + + // Is an account domain needed? (used currently for NTLM only) + if (CFHTTPAuthenticationRequiresAccountDomain(proxyAuthentication)) { + if (![self proxyDomain]) { + [self setProxyDomain:@""]; + } + [newCredentials setObject:[self proxyDomain] forKey:(NSString *)kCFHTTPAuthenticationAccountDomain]; + } + + NSString *user = nil; + NSString *pass = nil; + + + // If this is a HEAD request generated by an ASINetworkQueue, we'll try to use the details from the main request + if ([self mainRequest] && [[self mainRequest] proxyUsername] && [[self mainRequest] proxyPassword]) { + user = [[self mainRequest] proxyUsername]; + pass = [[self mainRequest] proxyPassword]; + + // Let's try to use the ones set in this object + } else if ([self proxyUsername] && [self proxyPassword]) { + user = [self proxyUsername]; + pass = [self proxyPassword]; + } + + + // Ok, that didn't work, let's try the keychain + // For authenticating proxies, we'll look in the keychain regardless of the value of useKeychainPersistence + if ((!user || !pass)) { + NSURLCredential *authenticationCredentials = [ASIHTTPRequest savedCredentialsForProxy:[self proxyHost] port:[self proxyPort] protocol:[[self url] scheme] realm:[self proxyAuthenticationRealm]]; + if (authenticationCredentials) { + user = [authenticationCredentials user]; + pass = [authenticationCredentials password]; + } + + } + + // If we have a username and password, let's apply them to the request and continue + if (user && pass) { + + [newCredentials setObject:user forKey:(NSString *)kCFHTTPAuthenticationUsername]; + [newCredentials setObject:pass forKey:(NSString *)kCFHTTPAuthenticationPassword]; + return newCredentials; + } + return nil; +} + + +- (NSMutableDictionary *)findCredentials +{ + NSMutableDictionary *newCredentials = [[[NSMutableDictionary alloc] init] autorelease]; + + // Is an account domain needed? (used currently for NTLM only) + if (CFHTTPAuthenticationRequiresAccountDomain(requestAuthentication)) { + if (!domain) { + [self setDomain:@""]; + } + [newCredentials setObject:domain forKey:(NSString *)kCFHTTPAuthenticationAccountDomain]; + } + + // First, let's look at the url to see if the username and password were included + NSString *user = [[self url] user]; + NSString *pass = [[self url] password]; + + // If the username and password weren't in the url + if (!user || !pass) { + + // If this is a HEAD request generated by an ASINetworkQueue, we'll try to use the details from the main request + if ([self mainRequest] && [[self mainRequest] username] && [[self mainRequest] password]) { + user = [[self mainRequest] username]; + pass = [[self mainRequest] password]; + + // Let's try to use the ones set in this object + } else if ([self username] && [self password]) { + user = [self username]; + pass = [self password]; + } + + } + + // Ok, that didn't work, let's try the keychain + if ((!user || !pass) && useKeychainPersistence) { + NSURLCredential *authenticationCredentials = [ASIHTTPRequest savedCredentialsForHost:[[self url] host] port:[[[self url] port] intValue] protocol:[[self url] scheme] realm:[self authenticationRealm]]; + if (authenticationCredentials) { + user = [authenticationCredentials user]; + pass = [authenticationCredentials password]; + } + + } + + // If we have a username and password, let's apply them to the request and continue + if (user && pass) { + + [newCredentials setObject:user forKey:(NSString *)kCFHTTPAuthenticationUsername]; + [newCredentials setObject:pass forKey:(NSString *)kCFHTTPAuthenticationPassword]; + return newCredentials; + } + return nil; +} + +// Called by delegate or authentication dialog to resume loading once authentication info has been populated +- (void)retryUsingSuppliedCredentials +{ + [self attemptToApplyCredentialsAndResume]; +} + +// Called by delegate or authentication dialog to cancel authentication +- (void)cancelAuthentication +{ + [self failWithError:ASIAuthenticationError]; +} + +- (BOOL)showProxyAuthenticationDialog +{ +// Mac authentication dialog coming soon! +#if TARGET_OS_IPHONE + if ([self shouldPresentProxyAuthenticationDialog]) { + [ASIAuthenticationDialog performSelectorOnMainThread:@selector(presentAuthenticationDialogForRequest:) withObject:self waitUntilDone:[NSThread isMainThread]]; + return YES; + } + return NO; +#else + return NO; +#endif +} + + +- (BOOL)askDelegateForProxyCredentials +{ + + // If we have a delegate, we'll see if it can handle proxyAuthenticationNeededForRequest:. + // Otherwise, we'll try the queue (if this request is part of one) and it will pass the message on to its own delegate + id authenticationDelegate = [self delegate]; + if (!authenticationDelegate) { + authenticationDelegate = [self queue]; + } + + if ([authenticationDelegate respondsToSelector:@selector(proxyAuthenticationNeededForRequest:)]) { + [authenticationDelegate performSelectorOnMainThread:@selector(proxyAuthenticationNeededForRequest:) withObject:self waitUntilDone:[NSThread isMainThread]]; + return YES; + } + return NO; +} + +- (void)attemptToApplyProxyCredentialsAndResume +{ + + if ([self error] || [self isCancelled]) { + return; + } + + // Read authentication data + if (!proxyAuthentication) { + CFHTTPMessageRef responseHeader = (CFHTTPMessageRef) CFReadStreamCopyProperty((CFReadStreamRef)[self readStream],kCFStreamPropertyHTTPResponseHeader); + proxyAuthentication = CFHTTPAuthenticationCreateFromResponse(NULL, responseHeader); + CFRelease(responseHeader); + [self setProxyAuthenticationScheme:[(NSString *)CFHTTPAuthenticationCopyMethod(proxyAuthentication) autorelease]]; + } + + // If we haven't got a CFHTTPAuthenticationRef by now, something is badly wrong, so we'll have to give up + if (!proxyAuthentication) { + [self cancelLoad]; + [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileApplyingCredentialsType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Failed to get authentication object from response headers",NSLocalizedDescriptionKey,nil]]]; + return; + } + + // Get the authentication realm + [self setProxyAuthenticationRealm:nil]; + if (!CFHTTPAuthenticationRequiresAccountDomain(proxyAuthentication)) { + [self setProxyAuthenticationRealm:[(NSString *)CFHTTPAuthenticationCopyRealm(proxyAuthentication) autorelease]]; + } + + // See if authentication is valid + CFStreamError err; + if (!CFHTTPAuthenticationIsValid(proxyAuthentication, &err)) { + + CFRelease(proxyAuthentication); + proxyAuthentication = NULL; + + // check for bad credentials, so we can give the delegate a chance to replace them + if (err.domain == kCFStreamErrorDomainHTTP && (err.error == kCFStreamErrorHTTPAuthenticationBadUserName || err.error == kCFStreamErrorHTTPAuthenticationBadPassword)) { + + // Prevent more than one request from asking for credentials at once + [delegateAuthenticationLock lock]; + + // We know the credentials we just presented are bad, we should remove them from the session store too + [[self class] removeProxyAuthenticationCredentialsFromSessionStore:proxyCredentials]; + [self setProxyCredentials:nil]; + + + // If the user cancelled authentication via a dialog presented by another request, our queue may have cancelled us + if ([self error] || [self isCancelled]) { + [delegateAuthenticationLock unlock]; + return; + } + + + // Now we've acquired the lock, it may be that the session contains credentials we can re-use for this request + if ([self useSessionPersistence]) { + NSDictionary *credentials = [self findSessionProxyAuthenticationCredentials]; + if (credentials && [self applyProxyCredentials:[credentials objectForKey:@"Credentials"]]) { + [delegateAuthenticationLock unlock]; + [self startRequest]; + return; + } + } + + [self setLastActivityTime:nil]; + + if ([self askDelegateForProxyCredentials]) { + [self attemptToApplyProxyCredentialsAndResume]; + [delegateAuthenticationLock unlock]; + return; + } + if ([self showProxyAuthenticationDialog]) { + [self attemptToApplyProxyCredentialsAndResume]; + [delegateAuthenticationLock unlock]; + return; + } + [delegateAuthenticationLock unlock]; + } + [self cancelLoad]; + [self failWithError:ASIAuthenticationError]; + return; + } + + [self cancelLoad]; + + if (proxyCredentials) { + + // We use startRequest rather than starting all over again in load request because NTLM requires we reuse the request + if ((([self proxyAuthenticationScheme] != (NSString *)kCFHTTPAuthenticationSchemeNTLM) || [self proxyAuthenticationRetryCount] < 2) && [self applyProxyCredentials:proxyCredentials]) { + [self startRequest]; + + // We've failed NTLM authentication twice, we should assume our credentials are wrong + } else if ([self proxyAuthenticationScheme] == (NSString *)kCFHTTPAuthenticationSchemeNTLM && [self proxyAuthenticationRetryCount] == 2) { + [self failWithError:ASIAuthenticationError]; + + // Something went wrong, we'll have to give up + } else { + [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileApplyingCredentialsType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Failed to apply proxy credentials to request",NSLocalizedDescriptionKey,nil]]]; + } + + // Are a user name & password needed? + } else if (CFHTTPAuthenticationRequiresUserNameAndPassword(proxyAuthentication)) { + + // Prevent more than one request from asking for credentials at once + [delegateAuthenticationLock lock]; + + // If the user cancelled authentication via a dialog presented by another request, our queue may have cancelled us + if ([self error] || [self isCancelled]) { + [delegateAuthenticationLock unlock]; + return; + } + + // Now we've acquired the lock, it may be that the session contains credentials we can re-use for this request + if ([self useSessionPersistence]) { + NSDictionary *credentials = [self findSessionProxyAuthenticationCredentials]; + if (credentials && [self applyProxyCredentials:[credentials objectForKey:@"Credentials"]]) { + [delegateAuthenticationLock unlock]; + [self startRequest]; + return; + } + } + + NSMutableDictionary *newCredentials = [self findProxyCredentials]; + + //If we have some credentials to use let's apply them to the request and continue + if (newCredentials) { + + if ([self applyProxyCredentials:newCredentials]) { + [delegateAuthenticationLock unlock]; + [self startRequest]; + } else { + [delegateAuthenticationLock unlock]; + [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileApplyingCredentialsType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Failed to apply proxy credentials to request",NSLocalizedDescriptionKey,nil]]]; + } + + return; + } + + if ([self askDelegateForProxyCredentials]) { + [delegateAuthenticationLock unlock]; + return; + } + + if ([self showProxyAuthenticationDialog]) { + [delegateAuthenticationLock unlock]; + return; + } + [delegateAuthenticationLock unlock]; + + // The delegate isn't interested and we aren't showing the authentication dialog, we'll have to give up + [self failWithError:ASIAuthenticationError]; + return; + } + +} + +- (BOOL)showAuthenticationDialog +{ +// Mac authentication dialog coming soon! +#if TARGET_OS_IPHONE + if ([self shouldPresentAuthenticationDialog]) { + [ASIAuthenticationDialog performSelectorOnMainThread:@selector(presentAuthenticationDialogForRequest:) withObject:self waitUntilDone:[NSThread isMainThread]]; + return YES; + } + return NO; +#else + return NO; +#endif +} + +- (BOOL)askDelegateForCredentials +{ + // If we have a delegate, we'll see if it can handle proxyAuthenticationNeededForRequest:. + // Otherwise, we'll try the queue (if this request is part of one) and it will pass the message on to its own delegate + id authenticationDelegate = [self delegate]; + if (!authenticationDelegate) { + authenticationDelegate = [self queue]; + } + + if ([authenticationDelegate respondsToSelector:@selector(authenticationNeededForRequest:)]) { + [authenticationDelegate performSelectorOnMainThread:@selector(authenticationNeededForRequest:) withObject:self waitUntilDone:[NSThread isMainThread]]; + return YES; + } + return NO; +} + +- (void)attemptToApplyCredentialsAndResume +{ + if ([self error] || [self isCancelled]) { + return; + } + + if ([self authenticationNeeded] == ASIProxyAuthenticationNeeded) { + [self attemptToApplyProxyCredentialsAndResume]; + return; + } + + // Read authentication data + if (!requestAuthentication) { + CFHTTPMessageRef responseHeader = (CFHTTPMessageRef) CFReadStreamCopyProperty((CFReadStreamRef)[self readStream],kCFStreamPropertyHTTPResponseHeader); + requestAuthentication = CFHTTPAuthenticationCreateFromResponse(NULL, responseHeader); + CFRelease(responseHeader); + [self setAuthenticationScheme:[(NSString *)CFHTTPAuthenticationCopyMethod(requestAuthentication) autorelease]]; + } + + if (!requestAuthentication) { + [self cancelLoad]; + [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileApplyingCredentialsType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Failed to get authentication object from response headers",NSLocalizedDescriptionKey,nil]]]; + return; + } + + // Get the authentication realm + [self setAuthenticationRealm:nil]; + if (!CFHTTPAuthenticationRequiresAccountDomain(requestAuthentication)) { + [self setAuthenticationRealm:[(NSString *)CFHTTPAuthenticationCopyRealm(requestAuthentication) autorelease]]; + } + + // See if authentication is valid + CFStreamError err; + if (!CFHTTPAuthenticationIsValid(requestAuthentication, &err)) { + + CFRelease(requestAuthentication); + requestAuthentication = NULL; + + // check for bad credentials, so we can give the delegate a chance to replace them + if (err.domain == kCFStreamErrorDomainHTTP && (err.error == kCFStreamErrorHTTPAuthenticationBadUserName || err.error == kCFStreamErrorHTTPAuthenticationBadPassword)) { + + // Prevent more than one request from asking for credentials at once + [delegateAuthenticationLock lock]; + + // We know the credentials we just presented are bad, we should remove them from the session store too + [[self class] removeAuthenticationCredentialsFromSessionStore:requestCredentials]; + [self setRequestCredentials:nil]; + + // If the user cancelled authentication via a dialog presented by another request, our queue may have cancelled us + if ([self error] || [self isCancelled]) { + [delegateAuthenticationLock unlock]; + return; + } + + // Now we've acquired the lock, it may be that the session contains credentials we can re-use for this request + if ([self useSessionPersistence]) { + NSDictionary *credentials = [self findSessionAuthenticationCredentials]; + if (credentials && [self applyCredentials:[credentials objectForKey:@"Credentials"]]) { + [delegateAuthenticationLock unlock]; + [self startRequest]; + return; + } + } + + + + [self setLastActivityTime:nil]; + + if ([self askDelegateForCredentials]) { + [delegateAuthenticationLock unlock]; + return; + } + if ([self showAuthenticationDialog]) { + [delegateAuthenticationLock unlock]; + return; + } + [delegateAuthenticationLock unlock]; + } + [self cancelLoad]; + [self failWithError:ASIAuthenticationError]; + return; + } + + [self cancelLoad]; + + if (requestCredentials) { + + if ((([self authenticationScheme] != (NSString *)kCFHTTPAuthenticationSchemeNTLM) || [self authenticationRetryCount] < 2) && [self applyCredentials:requestCredentials]) { + [self startRequest]; + + // We've failed NTLM authentication twice, we should assume our credentials are wrong + } else if ([self authenticationScheme] == (NSString *)kCFHTTPAuthenticationSchemeNTLM && [self authenticationRetryCount ] == 2) { + [self failWithError:ASIAuthenticationError]; + + } else { + [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileApplyingCredentialsType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Failed to apply credentials to request",NSLocalizedDescriptionKey,nil]]]; + } + + // Are a user name & password needed? + } else if (CFHTTPAuthenticationRequiresUserNameAndPassword(requestAuthentication)) { + + // Prevent more than one request from asking for credentials at once + [delegateAuthenticationLock lock]; + + // If the user cancelled authentication via a dialog presented by another request, our queue may have cancelled us + if ([self error] || [self isCancelled]) { + [delegateAuthenticationLock unlock]; + return; + } + + // Now we've acquired the lock, it may be that the session contains credentials we can re-use for this request + if ([self useSessionPersistence]) { + NSDictionary *credentials = [self findSessionAuthenticationCredentials]; + if (credentials && [self applyCredentials:[credentials objectForKey:@"Credentials"]]) { + [delegateAuthenticationLock unlock]; + [self startRequest]; + return; + } + } + + + NSMutableDictionary *newCredentials = [self findCredentials]; + + //If we have some credentials to use let's apply them to the request and continue + if (newCredentials) { + + if ([self applyCredentials:newCredentials]) { + [delegateAuthenticationLock unlock]; + [self startRequest]; + } else { + [delegateAuthenticationLock unlock]; + [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileApplyingCredentialsType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Failed to apply credentials to request",NSLocalizedDescriptionKey,nil]]]; + } + return; + } + if ([self askDelegateForCredentials]) { + [delegateAuthenticationLock unlock]; + return; + } + + if ([self showAuthenticationDialog]) { + [delegateAuthenticationLock unlock]; + return; + } + [delegateAuthenticationLock unlock]; + + [self failWithError:ASIAuthenticationError]; + + return; + } + +} + +- (void)addBasicAuthenticationHeaderWithUsername:(NSString *)theUsername andPassword:(NSString *)thePassword +{ + [self addRequestHeader:@"Authorization" value:[NSString stringWithFormat:@"Basic %@",[ASIHTTPRequest base64forData:[[NSString stringWithFormat:@"%@:%@",theUsername,thePassword] dataUsingEncoding:NSUTF8StringEncoding]]]]; +} + + +#pragma mark stream status handlers + +- (void)handleNetworkEvent:(CFStreamEventType)type +{ + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + [[self retain] autorelease]; + + [[self cancelledLock] lock]; + + if ([self complete] || [self isCancelled]) { + [[self cancelledLock] unlock]; + [pool release]; + return; + } + + // Dispatch the stream events. + switch (type) { + case kCFStreamEventHasBytesAvailable: + [self handleBytesAvailable]; + break; + + case kCFStreamEventEndEncountered: + [self handleStreamComplete]; + break; + + case kCFStreamEventErrorOccurred: + [self handleStreamError]; + break; + + default: + break; + } + + [self performThrottling]; + + [[self cancelledLock] unlock]; + + if ([self downloadComplete] && [self needsRedirect]) { + [self performRedirect]; + } else if ([self downloadComplete] && [self authenticationNeeded]) { + [self attemptToApplyCredentialsAndResume]; + } + [pool release]; +} + + +- (void)handleBytesAvailable +{ + if (![self responseHeaders]) { + [self readResponseHeaders]; + } + + // If we've cancelled the load part way through (for example, after deciding to use a cached version) + if ([self complete]) { + return; + } + + // In certain (presumably very rare) circumstances, handleBytesAvailable seems to be called when there isn't actually any data available + // We'll check that there is actually data available to prevent blocking on CFReadStreamRead() + // So far, I've only seen this in the stress tests, so it might never happen in real-world situations. + if (!CFReadStreamHasBytesAvailable((CFReadStreamRef)[self readStream])) { + return; + } + + long long bufferSize = 16384; + if (contentLength > 262144) { + bufferSize = 262144; + } else if (contentLength > 65536) { + bufferSize = 65536; + } + + // Reduce the buffer size if we're receiving data too quickly when bandwidth throttling is active + // This just augments the throttling done in measureBandwidthUsage to reduce the amount we go over the limit + + if ([[self class] isBandwidthThrottled]) { + [bandwidthThrottlingLock lock]; + if (maxBandwidthPerSecond > 0) { + long long maxiumumSize = (long long)maxBandwidthPerSecond-(long long)bandwidthUsedInLastSecond; + if (maxiumumSize < 0) { + // We aren't supposed to read any more data right now, but we'll read a single byte anyway so the CFNetwork's buffer isn't full + bufferSize = 1; + } else if (maxiumumSize/4 < bufferSize) { + // We were going to fetch more data that we should be allowed, so we'll reduce the size of our read + bufferSize = maxiumumSize/4; + } + } + if (bufferSize < 1) { + bufferSize = 1; + } + [bandwidthThrottlingLock unlock]; + } + + + UInt8 buffer[bufferSize]; + NSInteger bytesRead = [[self readStream] read:buffer maxLength:sizeof(buffer)]; + + // Less than zero is an error + if (bytesRead < 0) { + [self handleStreamError]; + + // If zero bytes were read, wait for the EOF to come. + } else if (bytesRead) { + + [self setTotalBytesRead:[self totalBytesRead]+bytesRead]; + [self setLastActivityTime:[NSDate date]]; + + // For bandwidth measurement / throttling + [ASIHTTPRequest incrementBandwidthUsedInLastSecond:bytesRead]; + + // If we need to redirect, and have automatic redirect on, and might be resuming a download, let's do nothing with the content + if ([self needsRedirect] && [self shouldRedirect] && [self allowResumeForFileDownloads]) { + return; + } + + // Does the delegate want to handle the data manually? + if ([[self delegate] respondsToSelector:[self didReceiveDataSelector]]) { + NSMethodSignature *signature = [[[self delegate] class] instanceMethodSignatureForSelector:[self didReceiveDataSelector]]; + NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; + [invocation setTarget:[self delegate]]; + [invocation setSelector:[self didReceiveDataSelector]]; + [invocation setArgument:&self atIndex:2]; + NSData *data = [NSData dataWithBytes:buffer length:bytesRead]; + [invocation setArgument:&data atIndex:3]; + [invocation retainArguments]; + [invocation performSelectorOnMainThread:@selector(invokeWithTarget:) withObject:[self delegate] waitUntilDone:[NSThread isMainThread]]; + + // Are we downloading to a file? + } else if ([self downloadDestinationPath]) { + if (![self fileDownloadOutputStream]) { + BOOL append = NO; + if (![self temporaryFileDownloadPath]) { + [self setTemporaryFileDownloadPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]]]; + } else if ([self allowResumeForFileDownloads]) { + append = YES; + } + + [self setFileDownloadOutputStream:[[[NSOutputStream alloc] initToFileAtPath:[self temporaryFileDownloadPath] append:append] autorelease]]; + [[self fileDownloadOutputStream] open]; + } + [[self fileDownloadOutputStream] write:buffer maxLength:bytesRead]; + + //Otherwise, let's add the data to our in-memory store + } else { + [rawResponseData appendBytes:buffer length:bytesRead]; + } + } + +} + +- (void)handleStreamComplete +{ +#if DEBUG_REQUEST_STATUS + //nslog(@"Request %@ finished downloading data",self); +#endif + + [self setDownloadComplete:YES]; + + if (![self responseHeaders]) { + [self readResponseHeaders]; + } + + [progressLock lock]; + // Find out how much data we've uploaded so far + [self setLastBytesSent:totalBytesSent]; + [self setTotalBytesSent:[NSMakeCollectable([(NSNumber *)CFReadStreamCopyProperty((CFReadStreamRef)[self readStream], kCFStreamPropertyHTTPRequestBytesWrittenCount) autorelease]) unsignedLongLongValue]]; + [self setComplete:YES]; + [self updateProgressIndicators]; + + + [[self postBodyReadStream] close]; + + NSError *fileError = nil; + + // Delete up the request body temporary file, if it exists + if ([self didCreateTemporaryPostDataFile] && ![self authenticationNeeded]) { + [self removePostDataFile]; + } + + // Close the output stream as we're done writing to the file + if ([self temporaryFileDownloadPath]) { + [[self fileDownloadOutputStream] close]; + [self setFileDownloadOutputStream:nil]; + + // If we are going to redirect and we are resuming, let's ignore this download + if ([self shouldRedirect] && [self needsRedirect] && [self allowResumeForFileDownloads]) { + + // Decompress the file (if necessary) directly to the destination path + } else if ([self isResponseCompressed]) { + int decompressionStatus = [ASIHTTPRequest uncompressZippedDataFromFile:[self temporaryFileDownloadPath] toFile:[self downloadDestinationPath]]; + if (decompressionStatus != Z_OK) { + fileError = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Decompression of %@ failed with code %i",[self temporaryFileDownloadPath],decompressionStatus],NSLocalizedDescriptionKey,nil]]; + } + [self removeTemporaryDownloadFile]; + } else { + + + //Remove any file at the destination path + NSError *moveError = nil; + if ([[NSFileManager defaultManager] fileExistsAtPath:[self downloadDestinationPath]]) { + [[NSFileManager defaultManager] removeItemAtPath:[self downloadDestinationPath] error:&moveError]; + if (moveError) { + fileError = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Unable to remove file at path '%@'",[self downloadDestinationPath]],NSLocalizedDescriptionKey,moveError,NSUnderlyingErrorKey,nil]]; + } + } + + //Move the temporary file to the destination path + if (!fileError) { + [[NSFileManager defaultManager] moveItemAtPath:[self temporaryFileDownloadPath] toPath:[self downloadDestinationPath] error:&moveError]; + if (moveError) { + fileError = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to move file from '%@' to '%@'",[self temporaryFileDownloadPath],[self downloadDestinationPath]],NSLocalizedDescriptionKey,moveError,NSUnderlyingErrorKey,nil]]; + } + [self setTemporaryFileDownloadPath:nil]; + } + + } + } + + // Save to the cache + if ([self downloadCache]) { + [[self downloadCache] storeResponseForRequest:self maxAge:[self secondsToCache]]; + } + + [progressLock unlock]; + + + [connectionsLock lock]; + if (![self connectionCanBeReused]) { + [self unscheduleReadStream]; + } + #if DEBUG_PERSISTENT_CONNECTIONS + //nslog(@"Request #%@ finished using connection #%@",[self requestID], [[self connectionInfo] objectForKey:@"id"]); + #endif + [[self connectionInfo] removeObjectForKey:@"request"]; + [[self connectionInfo] setObject:[NSDate dateWithTimeIntervalSinceNow:[self persistentConnectionTimeoutSeconds]] forKey:@"expires"]; + [connectionsLock unlock]; + + if (![self authenticationNeeded]) { + [self destroyReadStream]; + } + + if (![self needsRedirect] && ![self authenticationNeeded]) { + + if (fileError) { + [self failWithError:fileError]; + } else { + [self requestFinished]; + } + + [self markAsFinished]; + + // If request has asked delegate or ASIAuthenticationDialog for credentials + } else if ([self authenticationNeeded]) { + [self setStatusTimer:nil]; + CFRunLoopStop(CFRunLoopGetCurrent()); + } +} + +- (void)markAsFinished +{ + // Autoreleased requests may well be dealloced here otherwise + [self retain]; + + // dealloc won't be called when running with GC, so we'll clean these up now + if (request) { + CFMakeCollectable(request); + } + if (requestAuthentication) { + CFMakeCollectable(requestAuthentication); + } + if (proxyAuthentication) { + CFMakeCollectable(proxyAuthentication); + } + + [self willChangeValueForKey:@"isFinished"]; + [self willChangeValueForKey:@"isExecuting"]; + [self setInProgress:NO]; + [self setStatusTimer:nil]; + + [self didChangeValueForKey:@"isExecuting"]; + [self didChangeValueForKey:@"isFinished"]; + + CFRunLoopStop(CFRunLoopGetCurrent()); + + [self release]; +} + +- (BOOL)useDataFromCache +{ + NSDictionary *headers = [[self downloadCache] cachedHeadersForRequest:self]; + if (!headers) { + return NO; + } + NSString *dataPath = [[self downloadCache] pathToCachedResponseDataForRequest:self]; + if (!dataPath) { + return NO; + } + + if ([self cachePolicy] == ASIReloadIfDifferentCachePolicy) { + if (![[self downloadCache] isCachedDataCurrentForRequest:self]) { + [[self downloadCache] removeCachedDataForRequest:self]; + return NO; + } + } + + [self setDidUseCachedResponse:YES]; + + ASIHTTPRequest *theRequest = self; + if ([self mainRequest]) { + theRequest = [self mainRequest]; + } + [theRequest setResponseHeaders:headers]; + if ([theRequest downloadDestinationPath]) { + [theRequest setDownloadDestinationPath:dataPath]; + } else { + [theRequest setRawResponseData:[NSMutableData dataWithContentsOfFile:dataPath]]; + } + [theRequest setContentLength:[[[self responseHeaders] objectForKey:@"Content-Length"] longLongValue]]; + [theRequest setTotalBytesRead:[self contentLength]]; + + [theRequest parseStringEncodingFromHeaders]; + + [theRequest setResponseCookies:[NSHTTPCookie cookiesWithResponseHeaderFields:headers forURL:[self url]]]; + + [theRequest setComplete:YES]; + [theRequest setDownloadComplete:YES]; + + [theRequest updateProgressIndicators]; + [theRequest requestFinished]; + [theRequest markAsFinished]; + if ([self mainRequest]) { + [self markAsFinished]; + } + return YES; +} + +- (BOOL)retryUsingNewConnection +{ + if ([self retryCount] == 0) { + #if DEBUG_PERSISTENT_CONNECTIONS + //nslog(@"Request attempted to use connection #%@, but it has been closed - will retry with a new connection", [[self connectionInfo] objectForKey:@"id"]); + #endif + [connectionsLock lock]; + [[self connectionInfo] removeObjectForKey:@"request"]; + [persistentConnectionsPool removeObject:[self connectionInfo]]; + [self setConnectionInfo:nil]; + [connectionsLock unlock]; + [self setRetryCount:[self retryCount]+1]; + [self startRequest]; + return YES; + } + #if DEBUG_PERSISTENT_CONNECTIONS + //nslog(@"Request attempted to use connection #%@, but it has been closed - we have already retried with a new connection, so we must give up", [[self connectionInfo] objectForKey:@"id"]); + #endif + return NO; +} + +- (void)handleStreamError + +{ + NSError *underlyingError = NSMakeCollectable([(NSError *)CFReadStreamCopyError((CFReadStreamRef)[self readStream]) autorelease]); + + [self cancelLoad]; + + if (![self error]) { // We may already have handled this error + + // First, check for a 'socket not connected', 'broken pipe' or 'connection lost' error + // This may occur when we've attempted to reuse a connection that should have been closed + // If we get this, we need to retry the request + // We'll only do this once - if it happens again on retry, we'll give up + // -1005 = kCFURLErrorNetworkConnectionLost - this doesn't seem to be declared on Mac OS 10.5 + if (([[underlyingError domain] isEqualToString:NSPOSIXErrorDomain] && ([underlyingError code] == ENOTCONN || [underlyingError code] == EPIPE)) + || ([[underlyingError domain] isEqualToString:(NSString *)kCFErrorDomainCFNetwork] && [underlyingError code] == -1005)) { + if ([self retryUsingNewConnection]) { + return; + } + } + + NSString *reason = @"A connection failure occurred"; + + // We'll use a custom error message for SSL errors, but you should always check underlying error if you want more details + // For some reason SecureTransport.h doesn't seem to be available on iphone, so error codes hard-coded + // Also, iPhone seems to handle errors differently from Mac OS X - a self-signed certificate returns a different error code on each platform, so we'll just provide a general error + if ([[underlyingError domain] isEqualToString:NSOSStatusErrorDomain]) { + if ([underlyingError code] <= -9800 && [underlyingError code] >= -9818) { + reason = [NSString stringWithFormat:@"%@: SSL problem (possibly a bad/expired/self-signed certificate)",reason]; + } + } + + [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIConnectionFailureErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:reason,NSLocalizedDescriptionKey,underlyingError,NSUnderlyingErrorKey,nil]]]; + } + [self checkRequestStatus]; +} + +#pragma mark managing the read stream + + + +- (void)destroyReadStream +{ + if ([self readStream]) { + CFReadStreamSetClient((CFReadStreamRef)[self readStream], kCFStreamEventNone, NULL, NULL); + [connectionsLock lock]; + + if ([self readStreamIsScheduled]) { + runningRequestCount--; + #if TARGET_OS_IPHONE + if (shouldUpdateNetworkActivityIndicator && runningRequestCount == 0) { + [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; + } + #endif + } + + [self setReadStreamIsScheduled:NO]; + + if (![self connectionCanBeReused]) { + [[self readStream] removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:[self runLoopMode]]; + [[self readStream] close]; + } + [self setReadStream:nil]; + [connectionsLock unlock]; + } +} + +- (void)scheduleReadStream +{ + if ([self readStream] && ![self readStreamIsScheduled]) { + + [connectionsLock lock]; + runningRequestCount++; + #if TARGET_OS_IPHONE + if (shouldUpdateNetworkActivityIndicator) { + [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; + } + #endif + [connectionsLock unlock]; + + // Reset the timeout + [self setLastActivityTime:[NSDate date]]; + [[self readStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:[self runLoopMode]]; + [self setReadStreamIsScheduled:YES]; + } +} + +- (void)unscheduleReadStream +{ + if ([self readStream] && [self readStreamIsScheduled]) { + + [connectionsLock lock]; + runningRequestCount--; + #if TARGET_OS_IPHONE + if (shouldUpdateNetworkActivityIndicator && runningRequestCount == 0) { + [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; + } + #endif + [connectionsLock unlock]; + + [[self readStream] removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:[self runLoopMode]]; + [self setReadStreamIsScheduled:NO]; + } +} + +#pragma mark persistent connections + +- (NSNumber *)connectionID +{ + + return [[self connectionInfo] objectForKey:@"id"]; +} + ++ (void)expirePersistentConnections +{ + [connectionsLock lock]; + NSUInteger i; + for (i=0; i<[persistentConnectionsPool count]; i++) { + NSDictionary *existingConnection = [persistentConnectionsPool objectAtIndex:i]; + if (![existingConnection objectForKey:@"request"] && [[existingConnection objectForKey:@"expires"] timeIntervalSinceNow] <= 0) { +#if DEBUG_PERSISTENT_CONNECTIONS + //nslog(@"Closing connection #%hi because it has expired",[[existingConnection objectForKey:@"id"] intValue]); +#endif + NSInputStream *stream = [existingConnection objectForKey:@"stream"]; + if (stream) { + [stream close]; + } + [persistentConnectionsPool removeObject:existingConnection]; + i--; + } + } + [connectionsLock unlock]; +} + +#pragma mark NSCopying + +- (id)copyWithZone:(NSZone *)zone +{ + // Don't forget - this will return a retained copy! + ASIHTTPRequest *newRequest = [[[self class] alloc] initWithURL:[self url]]; + [newRequest setDelegate:[self delegate]]; + [newRequest setRequestMethod:[self requestMethod]]; + [newRequest setPostBody:[self postBody]]; + [newRequest setShouldStreamPostDataFromDisk:[self shouldStreamPostDataFromDisk]]; + [newRequest setPostBodyFilePath:[self postBodyFilePath]]; + [newRequest setRequestHeaders:[[[self requestHeaders] mutableCopyWithZone:zone] autorelease]]; + [newRequest setRequestCookies:[[[self requestCookies] mutableCopyWithZone:zone] autorelease]]; + [newRequest setUseCookiePersistence:[self useCookiePersistence]]; + [newRequest setUseKeychainPersistence:[self useKeychainPersistence]]; + [newRequest setUseSessionPersistence:[self useSessionPersistence]]; + [newRequest setAllowCompressedResponse:[self allowCompressedResponse]]; + [newRequest setDownloadDestinationPath:[self downloadDestinationPath]]; + [newRequest setTemporaryFileDownloadPath:[self temporaryFileDownloadPath]]; + [newRequest setUsername:[self username]]; + [newRequest setPassword:[self password]]; + [newRequest setDomain:[self domain]]; + [newRequest setProxyUsername:[self proxyUsername]]; + [newRequest setProxyPassword:[self proxyPassword]]; + [newRequest setProxyDomain:[self proxyDomain]]; + [newRequest setProxyHost:[self proxyHost]]; + [newRequest setProxyPort:[self proxyPort]]; + [newRequest setProxyType:[self proxyType]]; + [newRequest setUploadProgressDelegate:[self uploadProgressDelegate]]; + [newRequest setDownloadProgressDelegate:[self downloadProgressDelegate]]; + [newRequest setShouldPresentAuthenticationDialog:[self shouldPresentAuthenticationDialog]]; + [newRequest setShouldPresentProxyAuthenticationDialog:[self shouldPresentProxyAuthenticationDialog]]; + [newRequest setPostLength:[self postLength]]; + [newRequest setHaveBuiltPostBody:[self haveBuiltPostBody]]; + [newRequest setDidStartSelector:[self didStartSelector]]; + [newRequest setDidFinishSelector:[self didFinishSelector]]; + [newRequest setDidFailSelector:[self didFailSelector]]; + [newRequest setTimeOutSeconds:[self timeOutSeconds]]; + [newRequest setShouldResetDownloadProgress:[self shouldResetDownloadProgress]]; + [newRequest setShouldResetUploadProgress:[self shouldResetUploadProgress]]; + [newRequest setShowAccurateProgress:[self showAccurateProgress]]; + [newRequest setDefaultResponseEncoding:[self defaultResponseEncoding]]; + [newRequest setAllowResumeForFileDownloads:[self allowResumeForFileDownloads]]; + [newRequest setUserInfo:[[[self userInfo] copyWithZone:zone] autorelease]]; + [newRequest setUseHTTPVersionOne:[self useHTTPVersionOne]]; + [newRequest setShouldRedirect:[self shouldRedirect]]; + [newRequest setValidatesSecureCertificate:[self validatesSecureCertificate]]; + [newRequest setPACurl:[self PACurl]]; + [newRequest setShouldPresentCredentialsBeforeChallenge:[self shouldPresentCredentialsBeforeChallenge]]; + [newRequest setNumberOfTimesToRetryOnTimeout:[self numberOfTimesToRetryOnTimeout]]; + [newRequest setShouldUseRFC2616RedirectBehaviour:[self shouldUseRFC2616RedirectBehaviour]]; + [newRequest setShouldAttemptPersistentConnection:[self shouldAttemptPersistentConnection]]; + [newRequest setPersistentConnectionTimeoutSeconds:[self persistentConnectionTimeoutSeconds]]; + return newRequest; +} + +#pragma mark default time out + ++ (NSTimeInterval)defaultTimeOutSeconds +{ + return defaultTimeOutSeconds; +} + ++ (void)setDefaultTimeOutSeconds:(NSTimeInterval)newTimeOutSeconds +{ + defaultTimeOutSeconds = newTimeOutSeconds; +} + +#pragma mark session credentials + ++ (NSMutableArray *)sessionProxyCredentialsStore +{ + [sessionCredentialsLock lock]; + if (!sessionProxyCredentialsStore) { + sessionProxyCredentialsStore = [[NSMutableArray alloc] init]; + } + [sessionCredentialsLock unlock]; + return sessionProxyCredentialsStore; +} + ++ (NSMutableArray *)sessionCredentialsStore +{ + [sessionCredentialsLock lock]; + if (!sessionCredentialsStore) { + sessionCredentialsStore = [[NSMutableArray alloc] init]; + } + [sessionCredentialsLock unlock]; + return sessionCredentialsStore; +} + ++ (void)storeProxyAuthenticationCredentialsInSessionStore:(NSDictionary *)credentials +{ + [sessionCredentialsLock lock]; + [self removeProxyAuthenticationCredentialsFromSessionStore:[credentials objectForKey:@"Credentials"]]; + [[[self class] sessionProxyCredentialsStore] addObject:credentials]; + [sessionCredentialsLock unlock]; +} + ++ (void)storeAuthenticationCredentialsInSessionStore:(NSDictionary *)credentials +{ + [sessionCredentialsLock lock]; + [self removeAuthenticationCredentialsFromSessionStore:[credentials objectForKey:@"Credentials"]]; + [[[self class] sessionCredentialsStore] addObject:credentials]; + [sessionCredentialsLock unlock]; +} + ++ (void)removeProxyAuthenticationCredentialsFromSessionStore:(NSDictionary *)credentials +{ + [sessionCredentialsLock lock]; + NSMutableArray *sessionCredentialsList = [[self class] sessionProxyCredentialsStore]; + NSUInteger i; + for (i=0; i<[sessionCredentialsList count]; i++) { + NSDictionary *theCredentials = [sessionCredentialsList objectAtIndex:i]; + if ([theCredentials objectForKey:@"Credentials"] == credentials) { + [sessionCredentialsList removeObjectAtIndex:i]; + [sessionCredentialsLock unlock]; + return; + } + } + [sessionCredentialsLock unlock]; +} + ++ (void)removeAuthenticationCredentialsFromSessionStore:(NSDictionary *)credentials +{ + [sessionCredentialsLock lock]; + NSMutableArray *sessionCredentialsList = [[self class] sessionCredentialsStore]; + NSUInteger i; + for (i=0; i<[sessionCredentialsList count]; i++) { + NSDictionary *theCredentials = [sessionCredentialsList objectAtIndex:i]; + if ([theCredentials objectForKey:@"Credentials"] == credentials) { + [sessionCredentialsList removeObjectAtIndex:i]; + [sessionCredentialsLock unlock]; + return; + } + } + [sessionCredentialsLock unlock]; +} + +- (NSDictionary *)findSessionProxyAuthenticationCredentials +{ + [sessionCredentialsLock lock]; + NSMutableArray *sessionCredentialsList = [[self class] sessionProxyCredentialsStore]; + for (NSDictionary *theCredentials in sessionCredentialsList) { + if ([[theCredentials objectForKey:@"Host"] isEqualToString:[self proxyHost]] && [[theCredentials objectForKey:@"Port"] intValue] == [self proxyPort]) { + [sessionCredentialsLock unlock]; + return theCredentials; + } + } + [sessionCredentialsLock unlock]; + return nil; +} + + +- (NSDictionary *)findSessionAuthenticationCredentials +{ + [sessionCredentialsLock lock]; + NSMutableArray *sessionCredentialsList = [[self class] sessionCredentialsStore]; + // Find an exact match (same url) + for (NSDictionary *theCredentials in sessionCredentialsList) { + if ([[theCredentials objectForKey:@"URL"] isEqual:[self url]]) { + // /Just a sanity check to ensure we never choose credentials from a different realm. Can't really do more than that, as either this request or the stored credentials may not have a realm when the other does + if (![self responseStatusCode] || (![theCredentials objectForKey:@"AuthenticationRealm"] || [[theCredentials objectForKey:@"AuthenticationRealm"] isEqualToString:[self authenticationRealm]])) { + [sessionCredentialsLock unlock]; + return theCredentials; + } + } + } + // Find a rough match (same host, port, scheme) + NSURL *requestURL = [self url]; + for (NSDictionary *theCredentials in sessionCredentialsList) { + NSURL *theURL = [theCredentials objectForKey:@"URL"]; + + // Port can be nil! + if ([[theURL host] isEqualToString:[requestURL host]] && ([theURL port] == [requestURL port] || ([requestURL port] && [[theURL port] isEqualToNumber:[requestURL port]])) && [[theURL scheme] isEqualToString:[requestURL scheme]]) { + if (![self responseStatusCode] || (![theCredentials objectForKey:@"AuthenticationRealm"] || [[theCredentials objectForKey:@"AuthenticationRealm"] isEqualToString:[self authenticationRealm]])) { + [sessionCredentialsLock unlock]; + return theCredentials; + } + } + } + [sessionCredentialsLock unlock]; + return nil; +} + +#pragma mark keychain storage + ++ (void)saveCredentials:(NSURLCredential *)credentials forHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm +{ + NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithHost:host port:port protocol:protocol realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; + [[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credentials forProtectionSpace:protectionSpace]; +} + ++ (void)saveCredentials:(NSURLCredential *)credentials forProxy:(NSString *)host port:(int)port realm:(NSString *)realm +{ + NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithProxyHost:host port:port type:NSURLProtectionSpaceHTTPProxy realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; + [[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credentials forProtectionSpace:protectionSpace]; +} + ++ (NSURLCredential *)savedCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm +{ + NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithHost:host port:port protocol:protocol realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; + return [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:protectionSpace]; +} + ++ (NSURLCredential *)savedCredentialsForProxy:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm +{ + NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithProxyHost:host port:port type:NSURLProtectionSpaceHTTPProxy realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; + return [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:protectionSpace]; +} + ++ (void)removeCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm +{ + NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithHost:host port:port protocol:protocol realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; + NSURLCredential *credential = [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:protectionSpace]; + if (credential) { + [[NSURLCredentialStorage sharedCredentialStorage] removeCredential:credential forProtectionSpace:protectionSpace]; + } +} + ++ (void)removeCredentialsForProxy:(NSString *)host port:(int)port realm:(NSString *)realm +{ + NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithProxyHost:host port:port type:NSURLProtectionSpaceHTTPProxy realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; + NSURLCredential *credential = [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:protectionSpace]; + if (credential) { + [[NSURLCredentialStorage sharedCredentialStorage] removeCredential:credential forProtectionSpace:protectionSpace]; + } +} + + ++ (NSMutableArray *)sessionCookies +{ + if (!sessionCookies) { + [ASIHTTPRequest setSessionCookies:[[[NSMutableArray alloc] init] autorelease]]; + } + return sessionCookies; +} + ++ (void)setSessionCookies:(NSMutableArray *)newSessionCookies +{ + [sessionCookiesLock lock]; + // Remove existing cookies from the persistent store + for (NSHTTPCookie *cookie in sessionCookies) { + [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie]; + } + [sessionCookies release]; + sessionCookies = [newSessionCookies retain]; + [sessionCookiesLock unlock]; +} + ++ (void)addSessionCookie:(NSHTTPCookie *)newCookie +{ + [sessionCookiesLock lock]; + NSHTTPCookie *cookie; + NSUInteger i; + NSUInteger max = [[ASIHTTPRequest sessionCookies] count]; + for (i=0; i= [decompressed length]) { + [decompressed increaseLengthBy: half_length]; + } + strm.next_out = [decompressed mutableBytes] + strm.total_out; + strm.avail_out = (unsigned int)([decompressed length] - strm.total_out); + + // Inflate another chunk. + status = inflate (&strm, Z_SYNC_FLUSH); + if (status == Z_STREAM_END) { + done = YES; + } else if (status != Z_OK) { + break; + } + } + if (inflateEnd (&strm) != Z_OK) return nil; + + // Set real length. + if (done) { + [decompressed setLength: strm.total_out]; + return [NSData dataWithData: decompressed]; + } else { + return nil; + } +} + +// NOTE: To debug this method, turn off Data Formatters in Xcode or you'll crash on closeFile ++ (int)uncompressZippedDataFromFile:(NSString *)sourcePath toFile:(NSString *)destinationPath +{ + // Create an empty file at the destination path + if (![[NSFileManager defaultManager] createFileAtPath:destinationPath contents:[NSData data] attributes:nil]) { + return 1; + } + + // Get a FILE struct for the source file + NSFileHandle *inputFileHandle = [NSFileHandle fileHandleForReadingAtPath:sourcePath]; + FILE *source = fdopen([inputFileHandle fileDescriptor], "r"); + + // Get a FILE struct for the destination path + NSFileHandle *outputFileHandle = [NSFileHandle fileHandleForWritingAtPath:destinationPath]; + FILE *dest = fdopen([outputFileHandle fileDescriptor], "w"); + + + // Uncompress data in source and save in destination + int status = [ASIHTTPRequest uncompressZippedDataFromSource:source toDestination:dest]; + + // Close the files + fclose(dest); + fclose(source); + [inputFileHandle closeFile]; + [outputFileHandle closeFile]; + return status; +} + +// +// From the zlib sample code by Mark Adler, code here: +// http://www.zlib.net/zpipe.c +// +#define CHUNK 16384 + ++ (int)uncompressZippedDataFromSource:(FILE *)source toDestination:(FILE *)dest +{ + int ret; + unsigned have; + z_stream strm; + unsigned char in[CHUNK]; + unsigned char out[CHUNK]; + + /* allocate inflate state */ + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + strm.avail_in = 0; + strm.next_in = Z_NULL; + ret = inflateInit2(&strm, (15+32)); + if (ret != Z_OK) + return ret; + + /* decompress until deflate stream ends or end of file */ + do { + strm.avail_in = (unsigned int)fread(in, 1, CHUNK, source); + if (ferror(source)) { + (void)inflateEnd(&strm); + return Z_ERRNO; + } + if (strm.avail_in == 0) + break; + strm.next_in = in; + + /* run inflate() on input until output buffer not full */ + do { + strm.avail_out = CHUNK; + strm.next_out = out; + ret = inflate(&strm, Z_NO_FLUSH); + assert(ret != Z_STREAM_ERROR); /* state not clobbered */ + switch (ret) { + case Z_NEED_DICT: + ret = Z_DATA_ERROR; /* and fall through */ + case Z_DATA_ERROR: + case Z_MEM_ERROR: + (void)inflateEnd(&strm); + return ret; + } + have = CHUNK - strm.avail_out; + if (fwrite(&out, 1, have, dest) != have || ferror(dest)) { + (void)inflateEnd(&strm); + return Z_ERRNO; + } + } while (strm.avail_out == 0); + + /* done when inflate() says it's done */ + } while (ret != Z_STREAM_END); + + /* clean up and return */ + (void)inflateEnd(&strm); + return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR; +} + + +#pragma mark gzip compression + +// Based on this from Robbie Hanson: http://deusty.blogspot.com/2007/07/gzip-compressiondecompression.html + ++ (NSData *)compressData:(NSData*)uncompressedData +{ + if ([uncompressedData length] == 0) return uncompressedData; + + z_stream strm; + + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + strm.total_out = 0; + strm.next_in=(Bytef *)[uncompressedData bytes]; + strm.avail_in = (unsigned int)[uncompressedData length]; + + // Compresssion Levels: + // Z_NO_COMPRESSION + // Z_BEST_SPEED + // Z_BEST_COMPRESSION + // Z_DEFAULT_COMPRESSION + + if (deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, (15+16), 8, Z_DEFAULT_STRATEGY) != Z_OK) return nil; + + NSMutableData *compressed = [NSMutableData dataWithLength:16384]; // 16K chunks for expansion + + do { + + if (strm.total_out >= [compressed length]) + [compressed increaseLengthBy: 16384]; + + strm.next_out = [compressed mutableBytes] + strm.total_out; + strm.avail_out = (unsigned int)([compressed length] - strm.total_out); + + deflate(&strm, Z_FINISH); + + } while (strm.avail_out == 0); + + deflateEnd(&strm); + + [compressed setLength: strm.total_out]; + return [NSData dataWithData:compressed]; +} + +// NOTE: To debug this method, turn off Data Formatters in Xcode or you'll crash on closeFile ++ (int)compressDataFromFile:(NSString *)sourcePath toFile:(NSString *)destinationPath +{ + // Create an empty file at the destination path + [[NSFileManager defaultManager] createFileAtPath:destinationPath contents:[NSData data] attributes:nil]; + + // Get a FILE struct for the source file + NSFileHandle *inputFileHandle = [NSFileHandle fileHandleForReadingAtPath:sourcePath]; + FILE *source = fdopen([inputFileHandle fileDescriptor], "r"); + + // Get a FILE struct for the destination path + NSFileHandle *outputFileHandle = [NSFileHandle fileHandleForWritingAtPath:destinationPath]; + FILE *dest = fdopen([outputFileHandle fileDescriptor], "w"); + + // compress data in source and save in destination + int status = [ASIHTTPRequest compressDataFromSource:source toDestination:dest]; + + // Close the files + fclose(dest); + fclose(source); + + // We have to close both of these explictly because CFReadStreamCreateForStreamedHTTPRequest() seems to go bonkers otherwise + [inputFileHandle closeFile]; + [outputFileHandle closeFile]; + + return status; +} + +// +// Also from the zlib sample code at http://www.zlib.net/zpipe.c +// ++ (int)compressDataFromSource:(FILE *)source toDestination:(FILE *)dest +{ + int ret, flush; + unsigned have; + z_stream strm; + unsigned char in[CHUNK]; + unsigned char out[CHUNK]; + + /* allocate deflate state */ + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, (15+16), 8, Z_DEFAULT_STRATEGY); + if (ret != Z_OK) + return ret; + + /* compress until end of file */ + do { + strm.avail_in = (unsigned int)fread(in, 1, CHUNK, source); + if (ferror(source)) { + (void)deflateEnd(&strm); + return Z_ERRNO; + } + flush = feof(source) ? Z_FINISH : Z_NO_FLUSH; + strm.next_in = in; + + /* run deflate() on input until output buffer not full, finish + compression if all of source has been read in */ + do { + strm.avail_out = CHUNK; + strm.next_out = out; + ret = deflate(&strm, flush); /* no bad return value */ + assert(ret != Z_STREAM_ERROR); /* state not clobbered */ + have = CHUNK - strm.avail_out; + if (fwrite(out, 1, have, dest) != have || ferror(dest)) { + (void)deflateEnd(&strm); + return Z_ERRNO; + } + } while (strm.avail_out == 0); + assert(strm.avail_in == 0); /* all input will be used */ + + /* done when last data in file processed */ + } while (flush != Z_FINISH); + assert(ret == Z_STREAM_END); /* stream will be complete */ + + /* clean up and return */ + (void)deflateEnd(&strm); + return Z_OK; +} + +#pragma mark get user agent + ++ (NSString *)defaultUserAgentString +{ + NSBundle *bundle = [NSBundle mainBundle]; + + // Attempt to find a name for this application + NSString *appName = [bundle objectForInfoDictionaryKey:@"CFBundleDisplayName"]; + if (!appName) { + appName = [bundle objectForInfoDictionaryKey:@"CFBundleName"]; + } + // If we couldn't find one, we'll give up (and ASIHTTPRequest will use the standard CFNetwork user agent) + if (!appName) { + return nil; + } + NSString *appVersion = nil; + NSString *marketingVersionNumber = [bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; + NSString *developmentVersionNumber = [bundle objectForInfoDictionaryKey:@"CFBundleVersion"]; + if (marketingVersionNumber && developmentVersionNumber) { + if ([marketingVersionNumber isEqualToString:developmentVersionNumber]) { + appVersion = marketingVersionNumber; + } else { + appVersion = [NSString stringWithFormat:@"%@ rv:%@",marketingVersionNumber,developmentVersionNumber]; + } + } else { + appVersion = (marketingVersionNumber ? marketingVersionNumber : developmentVersionNumber); + } + + + NSString *deviceName; + NSString *OSName; + NSString *OSVersion; + + NSString *locale = [[NSLocale currentLocale] localeIdentifier]; + +#if TARGET_OS_IPHONE + UIDevice *device = [UIDevice currentDevice]; + deviceName = [device model]; + OSName = [device systemName]; + OSVersion = [device systemVersion]; + +#else + deviceName = @"Macintosh"; + OSName = @"Mac OS X"; + + // From http://www.cocoadev.com/index.pl?DeterminingOSVersion + // We won't bother to check for systems prior to 10.4, since ASIHTTPRequest only works on 10.5+ + OSErr err; + SInt32 versionMajor, versionMinor, versionBugFix; + err = Gestalt(gestaltSystemVersionMajor, &versionMajor); + if (err != noErr) return nil; + err = Gestalt(gestaltSystemVersionMinor, &versionMinor); + if (err != noErr) return nil; + err = Gestalt(gestaltSystemVersionBugFix, &versionBugFix); + if (err != noErr) return nil; + OSVersion = [NSString stringWithFormat:@"%u.%u.%u", versionMajor, versionMinor, versionBugFix]; + +#endif + // Takes the form "My Application 1.0 (Macintosh; Mac OS X 10.5.7; en_GB)" + return [NSString stringWithFormat:@"%@ %@ (%@; %@ %@; %@)", appName, appVersion, deviceName, OSName, OSVersion, locale]; +} + +#pragma mark proxy autoconfiguration + +// Returns an array of proxies to use for a particular url, given the url of a PAC script ++ (NSArray *)proxiesForURL:(NSURL *)theURL fromPAC:(NSURL *)pacScriptURL +{ + // From: http://developer.apple.com/samplecode/CFProxySupportTool/listing1.html + // Work around . This dummy call to + // CFNetworkCopyProxiesForURL initialise some state within CFNetwork + // that is required by CFNetworkCopyProxiesForAutoConfigurationScript. + CFRelease(CFNetworkCopyProxiesForURL((CFURLRef)theURL, NULL)); + + NSStringEncoding encoding; + NSError *err = nil; + NSString *script = [NSString stringWithContentsOfURL:pacScriptURL usedEncoding:&encoding error:&err]; + if (err) { + // If we can't fetch the PAC, we'll assume no proxies + // Some people have a PAC configured that is not always available, so I think this is the best behaviour + return [NSArray array]; + } + // Obtain the list of proxies by running the autoconfiguration script + CFErrorRef err2 = NULL; + NSArray *proxies = NSMakeCollectable([(NSArray *)CFNetworkCopyProxiesForAutoConfigurationScript((CFStringRef)script,(CFURLRef)theURL, &err2) autorelease]); + if (err2) { + return nil; + } + return proxies; +} + +#pragma mark mime-type detection + ++ (NSString *)mimeTypeForFileAtPath:(NSString *)path +{ + if (![[NSFileManager defaultManager] fileExistsAtPath:path]) { + return nil; + } + // Borrowed from http://stackoverflow.com/questions/2439020/wheres-the-iphone-mime-type-database + CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)[path pathExtension], NULL); + CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType); + CFRelease(UTI); + if (!MIMEType) { + return @"application/octet-stream"; + } + return NSMakeCollectable([(NSString *)MIMEType autorelease]); +} + +#pragma mark bandwidth measurement / throttling + +- (void)performThrottling +{ + if (![self readStream]) { + return; + } + [ASIHTTPRequest measureBandwidthUsage]; + if ([ASIHTTPRequest isBandwidthThrottled]) { + [bandwidthThrottlingLock lock]; + // Handle throttling + if (throttleWakeUpTime) { + if ([throttleWakeUpTime timeIntervalSinceDate:[NSDate date]] > 0) { + if ([self readStreamIsScheduled]) { + [self unscheduleReadStream]; + #if DEBUG_THROTTLING + //nslog(@"Sleeping request %@ until after %@",self,throttleWakeUpTime); + #endif + } + } else { + if (![self readStreamIsScheduled]) { + [self scheduleReadStream]; + #if DEBUG_THROTTLING + //nslog(@"Waking up request %@",self); + #endif + } + } + } + [bandwidthThrottlingLock unlock]; + + // Bandwidth throttling must have been turned off since we last looked, let's re-schedule the stream + } else if (![self readStreamIsScheduled]) { + [self scheduleReadStream]; + } +} + ++ (BOOL)isBandwidthThrottled +{ +#if TARGET_OS_IPHONE + [bandwidthThrottlingLock lock]; + + BOOL throttle = isBandwidthThrottled || (!shouldThrottleBandwithForWWANOnly && (maxBandwidthPerSecond)); + [bandwidthThrottlingLock unlock]; + return throttle; +#else + [bandwidthThrottlingLock lock]; + BOOL throttle = (maxBandwidthPerSecond); + [bandwidthThrottlingLock unlock]; + return throttle; +#endif +} + ++ (unsigned long)maxBandwidthPerSecond +{ + [bandwidthThrottlingLock lock]; + unsigned long amount = maxBandwidthPerSecond; + [bandwidthThrottlingLock unlock]; + return amount; +} + ++ (void)setMaxBandwidthPerSecond:(unsigned long)bytes +{ + [bandwidthThrottlingLock lock]; + maxBandwidthPerSecond = bytes; + [bandwidthThrottlingLock unlock]; +} + ++ (void)incrementBandwidthUsedInLastSecond:(unsigned long)bytes +{ + [bandwidthThrottlingLock lock]; + bandwidthUsedInLastSecond += bytes; + [bandwidthThrottlingLock unlock]; +} + ++ (void)recordBandwidthUsage +{ + if (bandwidthUsedInLastSecond == 0) { + [bandwidthUsageTracker removeAllObjects]; + } else { + NSTimeInterval interval = [bandwidthMeasurementDate timeIntervalSinceNow]; + while ((interval < 0 || [bandwidthUsageTracker count] > 5) && [bandwidthUsageTracker count] > 0) { + [bandwidthUsageTracker removeObjectAtIndex:0]; + interval++; + } + } + #if DEBUG_THROTTLING + //nslog(@"===Used: %u bytes of bandwidth in last measurement period===",bandwidthUsedInLastSecond); + #endif + [bandwidthUsageTracker addObject:[NSNumber numberWithUnsignedLong:bandwidthUsedInLastSecond]]; + [bandwidthMeasurementDate release]; + bandwidthMeasurementDate = [[NSDate dateWithTimeIntervalSinceNow:1] retain]; + bandwidthUsedInLastSecond = 0; + + NSUInteger measurements = [bandwidthUsageTracker count]; + unsigned long totalBytes = 0; + for (NSNumber *bytes in bandwidthUsageTracker) { + totalBytes += [bytes unsignedLongValue]; + } + averageBandwidthUsedPerSecond = totalBytes/measurements; +} + ++ (unsigned long)averageBandwidthUsedPerSecond +{ + [bandwidthThrottlingLock lock]; + unsigned long amount = averageBandwidthUsedPerSecond; + [bandwidthThrottlingLock unlock]; + return amount; +} + ++ (void)measureBandwidthUsage +{ + // Other requests may have to wait for this lock if we're sleeping, but this is fine, since in that case we already know they shouldn't be sending or receiving data + [bandwidthThrottlingLock lock]; + + if (!bandwidthMeasurementDate || [bandwidthMeasurementDate timeIntervalSinceNow] < -0) { + [ASIHTTPRequest recordBandwidthUsage]; + } + + // Are we performing bandwidth throttling? + if ( + #if TARGET_OS_IPHONE + isBandwidthThrottled || (!shouldThrottleBandwithForWWANOnly && (maxBandwidthPerSecond)) + #else + maxBandwidthPerSecond + #endif + ) { + // How much data can we still send or receive this second? + long long bytesRemaining = (long long)maxBandwidthPerSecond - (long long)bandwidthUsedInLastSecond; + + // Have we used up our allowance? + if (bytesRemaining < 0) { + + // Yes, put this request to sleep until a second is up, with extra added punishment sleeping time for being very naughty (we have used more bandwidth than we were allowed) + double extraSleepyTime = (-bytesRemaining/(maxBandwidthPerSecond*1.0)); + [throttleWakeUpTime release]; + throttleWakeUpTime = [[NSDate alloc] initWithTimeInterval:extraSleepyTime sinceDate:bandwidthMeasurementDate]; + } + } + [bandwidthThrottlingLock unlock]; +} + ++ (unsigned long)maxUploadReadLength +{ + + [bandwidthThrottlingLock lock]; + + // We'll split our bandwidth allowance into 4 (which is the default for an ASINetworkQueue's max concurrent operations count) to give all running requests a fighting chance of reading data this cycle + long long toRead = maxBandwidthPerSecond/4; + if (maxBandwidthPerSecond > 0 && (bandwidthUsedInLastSecond + toRead > maxBandwidthPerSecond)) { + toRead = (long long)maxBandwidthPerSecond-(long long)bandwidthUsedInLastSecond; + if (toRead < 0) { + toRead = 0; + } + } + + if (toRead == 0 || !bandwidthMeasurementDate || [bandwidthMeasurementDate timeIntervalSinceNow] < -0) { + [throttleWakeUpTime release]; + throttleWakeUpTime = [bandwidthMeasurementDate retain]; + } + [bandwidthThrottlingLock unlock]; + return (unsigned long)toRead; +} + + +#if TARGET_OS_IPHONE ++ (void)setShouldThrottleBandwidthForWWAN:(BOOL)throttle +{ + if (throttle) { + [ASIHTTPRequest throttleBandwidthForWWANUsingLimit:ASIWWANBandwidthThrottleAmount]; + } else { + [ASIHTTPRequest unsubscribeFromNetworkReachabilityNotifications]; + [ASIHTTPRequest setMaxBandwidthPerSecond:0]; + [bandwidthThrottlingLock lock]; + isBandwidthThrottled = NO; + shouldThrottleBandwithForWWANOnly = NO; + [bandwidthThrottlingLock unlock]; + } +} + ++ (void)throttleBandwidthForWWANUsingLimit:(unsigned long)limit +{ + [bandwidthThrottlingLock lock]; + shouldThrottleBandwithForWWANOnly = YES; + maxBandwidthPerSecond = limit; + [ASIHTTPRequest registerForNetworkReachabilityNotifications]; + [bandwidthThrottlingLock unlock]; + [ASIHTTPRequest reachabilityChanged:nil]; +} + +#pragma mark reachability + ++ (void)registerForNetworkReachabilityNotifications +{ + [[Reachability reachabilityForInternetConnection] startNotifier]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:@"kNetworkReachabilityChangedNotification" object:nil]; +} + + ++ (void)unsubscribeFromNetworkReachabilityNotifications +{ + [[NSNotificationCenter defaultCenter] removeObserver:self name:@"kNetworkReachabilityChangedNotification" object:nil]; +} + ++ (BOOL)isNetworkReachableViaWWAN +{ + return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == ReachableViaWWAN); +} + ++ (void)reachabilityChanged:(NSNotification *)note +{ + [bandwidthThrottlingLock lock]; + isBandwidthThrottled = [ASIHTTPRequest isNetworkReachableViaWWAN]; + [bandwidthThrottlingLock unlock]; +} +#endif + +#pragma mark cache + ++ (void)setDefaultCache:(id )cache +{ + [defaultCache release]; + defaultCache = [cache retain]; +} + ++ (id )defaultCache +{ + return defaultCache; +} + + +#pragma mark network activity + ++ (BOOL)isNetworkInUse +{ + [connectionsLock lock]; + BOOL inUse = (runningRequestCount > 0); + [connectionsLock unlock]; + return inUse; +} +#if TARGET_OS_IPHONE ++ (void)setShouldUpdateNetworkActivityIndicator:(BOOL)shouldUpdate +{ + [connectionsLock lock]; + shouldUpdateNetworkActivityIndicator = shouldUpdate; + [connectionsLock unlock]; +} +#endif + + +#pragma mark threading behaviour + +// In the default implementation, all requests run in a single background thread +// Advanced users only: Override this method in a subclass for a different threading behaviour +// Eg: return [NSThread mainThread] to run all requests in the main thread +// Alternatively, you can create a thread on demand, or manage a pool of threads +// Threads returned by this method will need to run the runloop in default mode (eg CFRunLoopRun()) +// Requests will stop the runloop when they complete +// If you have multiple requests sharing the thread or you want to re-use the thread, you'll need to restart the runloop ++ (NSThread *)threadForRequest:(ASIHTTPRequest *)request +{ + if (!networkThread) { + networkThread = [[NSThread alloc] initWithTarget:self selector:@selector(runRequests) object:nil]; + [networkThread start]; + } + return networkThread; +} + ++ (void)runRequests +{ + // Should keep the runloop from exiting + CFRunLoopSourceContext context = {0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; + CFRunLoopSourceRef source = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context); + CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode); + + while (1) { + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + CFRunLoopRun(); + [pool release]; + } + + // Should never be called, but anyway + CFRunLoopRemoveSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode); + CFRelease(source); +} + + + +#pragma mark miscellany + +// From: http://www.cocoadev.com/index.pl?BaseSixtyFour + ++ (NSString*)base64forData:(NSData*)theData { + + const uint8_t* input = (const uint8_t*)[theData bytes]; + NSInteger length = [theData length]; + + static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; + + NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4]; + uint8_t* output = (uint8_t*)data.mutableBytes; + + NSInteger i; + for (i=0; i < length; i += 3) { + NSInteger value = 0; + NSInteger j; + for (j = i; j < (i + 3); j++) { + value <<= 8; + + if (j < length) { + value |= (0xFF & input[j]); + } + } + + NSInteger theIndex = (i / 3) * 4; + output[theIndex + 0] = table[(value >> 18) & 0x3F]; + output[theIndex + 1] = table[(value >> 12) & 0x3F]; + output[theIndex + 2] = (i + 1) < length ? table[(value >> 6) & 0x3F] : '='; + output[theIndex + 3] = (i + 2) < length ? table[(value >> 0) & 0x3F] : '='; + } + + return [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease]; +} + +// Based on hints from http://stackoverflow.com/questions/1850824/parsing-a-rfc-822-date-with-nsdateformatter ++ (NSDate *)dateFromRFC1123String:(NSString *)string +{ + NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; + [formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]]; + // Does the string include a week day? + NSString *day = @""; + if ([string rangeOfString:@","].location != NSNotFound) { + day = @"EEE, "; + } + // Does the string include seconds? + NSString *seconds = @""; + if ([[string componentsSeparatedByString:@":"] count] == 3) { + seconds = @":ss"; + } + [formatter setDateFormat:[NSString stringWithFormat:@"%@dd MMM yyyy HH:mm%@ z",day,seconds]]; + return [formatter dateFromString:string]; +} + +#pragma mark === + +@synthesize username; +@synthesize password; +@synthesize domain; +@synthesize proxyUsername; +@synthesize proxyPassword; +@synthesize proxyDomain; +@synthesize url; +@synthesize originalURL; +@synthesize delegate; +@synthesize queue; +@synthesize uploadProgressDelegate; +@synthesize downloadProgressDelegate; +@synthesize useKeychainPersistence; +@synthesize useSessionPersistence; +@synthesize useCookiePersistence; +@synthesize downloadDestinationPath; +@synthesize temporaryFileDownloadPath; +@synthesize didStartSelector; +@synthesize didReceiveResponseHeadersSelector; +@synthesize didFinishSelector; +@synthesize didFailSelector; +@synthesize didReceiveDataSelector; +@synthesize authenticationRealm; +@synthesize proxyAuthenticationRealm; +@synthesize error; +@synthesize complete; +@synthesize requestHeaders; +@synthesize responseHeaders; +@synthesize responseCookies; +@synthesize requestCookies; +@synthesize requestCredentials; +@synthesize responseStatusCode; +@synthesize rawResponseData; +@synthesize lastActivityTime; +@synthesize timeOutSeconds; +@synthesize requestMethod; +@synthesize postBody; +@synthesize compressedPostBody; +@synthesize contentLength; +@synthesize partialDownloadSize; +@synthesize postLength; +@synthesize shouldResetDownloadProgress; +@synthesize shouldResetUploadProgress; +@synthesize mainRequest; +@synthesize totalBytesRead; +@synthesize totalBytesSent; +@synthesize showAccurateProgress; +@synthesize uploadBufferSize; +@synthesize defaultResponseEncoding; +@synthesize responseEncoding; +@synthesize allowCompressedResponse; +@synthesize allowResumeForFileDownloads; +@synthesize userInfo; +@synthesize postBodyFilePath; +@synthesize compressedPostBodyFilePath; +@synthesize postBodyWriteStream; +@synthesize postBodyReadStream; +@synthesize shouldStreamPostDataFromDisk; +@synthesize didCreateTemporaryPostDataFile; +@synthesize useHTTPVersionOne; +@synthesize lastBytesRead; +@synthesize lastBytesSent; +@synthesize cancelledLock; +@synthesize haveBuiltPostBody; +@synthesize fileDownloadOutputStream; +@synthesize authenticationRetryCount; +@synthesize proxyAuthenticationRetryCount; +@synthesize updatedProgress; +@synthesize shouldRedirect; +@synthesize validatesSecureCertificate; +@synthesize needsRedirect; +@synthesize redirectCount; +@synthesize shouldCompressRequestBody; +@synthesize proxyCredentials; +@synthesize proxyHost; +@synthesize proxyPort; +@synthesize proxyType; +@synthesize PACurl; +@synthesize authenticationScheme; +@synthesize proxyAuthenticationScheme; +@synthesize shouldPresentAuthenticationDialog; +@synthesize shouldPresentProxyAuthenticationDialog; +@synthesize authenticationNeeded; +@synthesize responseStatusMessage; +@synthesize shouldPresentCredentialsBeforeChallenge; +@synthesize haveBuiltRequestHeaders; +@synthesize inProgress; +@synthesize numberOfTimesToRetryOnTimeout; +@synthesize retryCount; +@synthesize shouldAttemptPersistentConnection; +@synthesize persistentConnectionTimeoutSeconds; +@synthesize connectionCanBeReused; +@synthesize connectionInfo; +@synthesize readStream; +@synthesize readStreamIsScheduled; +@synthesize shouldUseRFC2616RedirectBehaviour; +@synthesize downloadComplete; +@synthesize requestID; +@synthesize runLoopMode; +@synthesize statusTimer; +@synthesize downloadCache; +@synthesize cachePolicy; +@synthesize cacheStoragePolicy; +@synthesize didUseCachedResponse; +@synthesize secondsToCache; +@end diff --git a/ASIHTTPRequestConfig.h b/ASIHTTPRequestConfig.h new file mode 100644 index 0000000..e9171cd --- /dev/null +++ b/ASIHTTPRequestConfig.h @@ -0,0 +1,32 @@ +// +// ASIHTTPRequestConfig.h +// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest +// +// Created by Ben Copsey on 14/12/2009. +// Copyright 2009 All-Seeing Interactive. All rights reserved. +// + + +// ====== +// Debug output configuration options +// ====== + +// When set to 1 ASIHTTPRequests will print information about what a request is doing +#ifndef DEBUG_REQUEST_STATUS + #define DEBUG_REQUEST_STATUS 0 +#endif + +// When set to 1, ASIFormDataRequests will print information about the request body to the console +#ifndef DEBUG_FORM_DATA_REQUEST + #define DEBUG_FORM_DATA_REQUEST 0 +#endif + +// When set to 1, ASIHTTPRequests will print information about bandwidth throttling to the console +#ifndef DEBUG_THROTTLING + #define DEBUG_THROTTLING 1 +#endif + +// When set to 1, ASIHTTPRequests will print information about persistent connections to the console +#ifndef DEBUG_PERSISTENT_CONNECTIONS + #define DEBUG_PERSISTENT_CONNECTIONS 0 +#endif diff --git a/ASIHTTPRequestDelegate.h b/ASIHTTPRequestDelegate.h new file mode 100644 index 0000000..82ce352 --- /dev/null +++ b/ASIHTTPRequestDelegate.h @@ -0,0 +1,33 @@ +// +// ASIHTTPRequestDelegate.h +// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest +// +// Created by Ben Copsey on 13/04/2010. +// Copyright 2010 All-Seeing Interactive. All rights reserved. +// + +@class ASIHTTPRequest; + +@protocol ASIHTTPRequestDelegate + +@optional + +// These are the default delegate methods for request status +// You can use different ones by setting didStartSelector / didFinishSelector / didFailSelector +- (void)requestStarted:(ASIHTTPRequest *)request; +- (void)requestReceivedResponseHeaders:(ASIHTTPRequest *)request; +- (void)requestFinished:(ASIHTTPRequest *)request; +- (void)requestFailed:(ASIHTTPRequest *)request; + +// When a delegate implements this method, it is expected to process all incoming data itself +// This means that responseData / responseString / downloadDestinationPath etc are ignored +// You can have the request call a different method by setting didReceiveDataSelector +- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data; + +// If a delegate implements one of these, it will be asked to supply credentials when none are available +// The delegate can then either restart the request ([request retryUsingSuppliedCredentials]) once credentials have been set +// or cancel it ([request cancelAuthentication]) +- (void)authenticationNeededForRequest:(ASIHTTPRequest *)request; +- (void)proxyAuthenticationNeededForRequest:(ASIHTTPRequest *)request; + +@end diff --git a/ASIInputStream.h b/ASIInputStream.h new file mode 100644 index 0000000..7b9f93e --- /dev/null +++ b/ASIInputStream.h @@ -0,0 +1,26 @@ +// +// ASIInputStream.h +// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest +// +// Created by Ben Copsey on 10/08/2009. +// Copyright 2009 All-Seeing Interactive. All rights reserved. +// + +#import + +@class ASIHTTPRequest; + +// This is a wrapper for NSInputStream that pretends to be an NSInputStream itself +// Subclassing NSInputStream seems to be tricky, and may involve overriding undocumented methods, so we'll cheat instead. +// It is used by ASIHTTPRequest whenever we have a request body, and handles measuring and throttling the bandwidth used for uploading + +@interface ASIInputStream : NSObject { + NSInputStream *stream; + ASIHTTPRequest *request; +} ++ (id)inputStreamWithFileAtPath:(NSString *)path request:(ASIHTTPRequest *)request; ++ (id)inputStreamWithData:(NSData *)data request:(ASIHTTPRequest *)request; + +@property (retain, nonatomic) NSInputStream *stream; +@property (assign, nonatomic) ASIHTTPRequest *request; +@end diff --git a/ASIInputStream.m b/ASIInputStream.m new file mode 100644 index 0000000..bd85e0b --- /dev/null +++ b/ASIInputStream.m @@ -0,0 +1,136 @@ +// +// ASIInputStream.m +// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest +// +// Created by Ben Copsey on 10/08/2009. +// Copyright 2009 All-Seeing Interactive. All rights reserved. +// + +#import "ASIInputStream.h" +#import "ASIHTTPRequest.h" + +// Used to ensure only one request can read data at once +static NSLock *readLock = nil; + +@implementation ASIInputStream + ++ (void)initialize +{ + if (self == [ASIInputStream class]) { + readLock = [[NSLock alloc] init]; + } +} + ++ (id)inputStreamWithFileAtPath:(NSString *)path request:(ASIHTTPRequest *)request +{ + ASIInputStream *stream = [[[self alloc] init] autorelease]; + [stream setRequest:request]; + [stream setStream:[NSInputStream inputStreamWithFileAtPath:path]]; + return stream; +} + ++ (id)inputStreamWithData:(NSData *)data request:(ASIHTTPRequest *)request +{ + ASIInputStream *stream = [[[self alloc] init] autorelease]; + [stream setRequest:request]; + [stream setStream:[NSInputStream inputStreamWithData:data]]; + return stream; +} + +- (void)dealloc +{ + [stream release]; + [super dealloc]; +} + +// Called when CFNetwork wants to read more of our request body +// When throttling is on, we ask ASIHTTPRequest for the maximum amount of data we can read +- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len +{ + [readLock lock]; + unsigned long toRead = len; + if ([ASIHTTPRequest isBandwidthThrottled]) { + toRead = [ASIHTTPRequest maxUploadReadLength]; + if (toRead > len) { + toRead = len; + } else if (toRead == 0) { + toRead = 1; + } + [request performThrottling]; + } + [ASIHTTPRequest incrementBandwidthUsedInLastSecond:toRead]; + [readLock unlock]; + return [stream read:buffer maxLength:toRead]; +} + +/* + * Implement NSInputStream mandatory methods to make sure they are implemented + * (necessary for MacRuby for example) and avoir the overhead of method + * forwarding for these common methods. + */ +- (void)open +{ + [stream open]; +} + +- (void)close +{ + [stream close]; +} + +- (id)delegate +{ + return [stream delegate]; +} + +- (void)setDelegate:(id)delegate +{ + [stream setDelegate:delegate]; +} + +- (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode +{ + [stream scheduleInRunLoop:aRunLoop forMode:mode]; +} + +- (void)removeFromRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode +{ + [stream removeFromRunLoop:aRunLoop forMode:mode]; +} + +- (id)propertyForKey:(NSString *)key +{ + return [stream propertyForKey:key]; +} + +- (BOOL)setProperty:(id)property forKey:(NSString *)key +{ + return [stream setProperty:property forKey:key]; +} + +- (NSStreamStatus)streamStatus +{ + return [stream streamStatus]; +} + +- (NSError *)streamError +{ + return [stream streamError]; +} + +// If we get asked to perform a method we don't have (probably internal ones), +// we'll just forward the message to our stream + +- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector +{ + return [stream methodSignatureForSelector:aSelector]; +} + +- (void)forwardInvocation:(NSInvocation *)anInvocation +{ + [anInvocation invokeWithTarget:stream]; +} + +@synthesize stream; +@synthesize request; +@end diff --git a/ASINetworkQueue.h b/ASINetworkQueue.h new file mode 100644 index 0000000..b0398a7 --- /dev/null +++ b/ASINetworkQueue.h @@ -0,0 +1,102 @@ +// +// ASINetworkQueue.h +// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest +// +// Created by Ben Copsey on 07/11/2008. +// Copyright 2008-2009 All-Seeing Interactive. All rights reserved. +// + +#import +#import "ASIHTTPRequestDelegate.h" +#import "ASIProgressDelegate.h" + +@interface ASINetworkQueue : NSOperationQueue { + + // Delegate will get didFail + didFinish messages (if set) + id delegate; + + // Will be called when a request starts with the request as the argument + SEL requestDidStartSelector; + + // Will be called when a request receives response headers with the request as the argument + SEL requestDidReceiveResponseHeadersSelector; + + // Will be called when a request completes with the request as the argument + SEL requestDidFinishSelector; + + // Will be called when a request fails with the request as the argument + SEL requestDidFailSelector; + + // Will be called when the queue finishes with the queue as the argument + SEL queueDidFinishSelector; + + // Upload progress indicator, probably an NSProgressIndicator or UIProgressView + id uploadProgressDelegate; + + // Total amount uploaded so far for all requests in this queue + unsigned long long bytesUploadedSoFar; + + // Total amount to be uploaded for all requests in this queue - requests add to this figure as they work out how much data they have to transmit + unsigned long long totalBytesToUpload; + + // Download progress indicator, probably an NSProgressIndicator or UIProgressView + id downloadProgressDelegate; + + // Total amount downloaded so far for all requests in this queue + unsigned long long bytesDownloadedSoFar; + + // Total amount to be downloaded for all requests in this queue - requests add to this figure as they receive Content-Length headers + unsigned long long totalBytesToDownload; + + // When YES, the queue will cancel all requests when a request fails. Default is YES + BOOL shouldCancelAllRequestsOnFailure; + + //Number of real requests (excludes HEAD requests created to manage showAccurateProgress) + int requestsCount; + + // When NO, this request will only update the progress indicator when it completes + // When YES, this request will update the progress indicator according to how much data it has received so far + // When YES, the queue will first perform HEAD requests for all GET requests in the queue, so it can calculate the total download size before it starts + // NO means better performance, because it skips this step for GET requests, and it won't waste time updating the progress indicator until a request completes + // Set to YES if the size of a requests in the queue varies greatly for much more accurate results + // Default for requests in the queue is NO + BOOL showAccurateProgress; + + // Storage container for additional queue information. + NSDictionary *userInfo; + +} + +// Convenience constructor ++ (id)queue; + +// Call this to reset a queue - it will cancel all operations, clear delegates, and suspend operation +- (void)reset; + +// Used internally to manage HEAD requests when showAccurateProgress is YES, do not use! +- (void)addHEADOperation:(NSOperation *)operation; + +// All ASINetworkQueues are paused when created so that total size can be calculated before the queue starts +// This method will start the queue +- (void)go; + +@property (assign, nonatomic, setter=setUploadProgressDelegate:) id uploadProgressDelegate; +@property (assign, nonatomic, setter=setDownloadProgressDelegate:) id downloadProgressDelegate; + +@property (assign) SEL requestDidStartSelector; +@property (assign) SEL requestDidReceiveResponseHeadersSelector; +@property (assign) SEL requestDidFinishSelector; +@property (assign) SEL requestDidFailSelector; +@property (assign) SEL queueDidFinishSelector; +@property (assign) BOOL shouldCancelAllRequestsOnFailure; +@property (assign) id delegate; +@property (assign) BOOL showAccurateProgress; +@property (assign, readonly) int requestsCount; +@property (retain) NSDictionary *userInfo; + +@property (assign) unsigned long long bytesUploadedSoFar; +@property (assign) unsigned long long totalBytesToUpload; +@property (assign) unsigned long long bytesDownloadedSoFar; +@property (assign) unsigned long long totalBytesToDownload; + +@end diff --git a/ASINetworkQueue.m b/ASINetworkQueue.m new file mode 100644 index 0000000..5f65d49 --- /dev/null +++ b/ASINetworkQueue.m @@ -0,0 +1,330 @@ +// +// ASINetworkQueue.m +// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest +// +// Created by Ben Copsey on 07/11/2008. +// Copyright 2008-2009 All-Seeing Interactive. All rights reserved. +// + +#import "ASINetworkQueue.h" +#import "ASIHTTPRequest.h" + +// Private stuff +@interface ASINetworkQueue () + - (void)resetProgressDelegate:(id)progressDelegate; + @property (assign) int requestsCount; +@end + +@implementation ASINetworkQueue + +- (id)init +{ + self = [super init]; + [self setShouldCancelAllRequestsOnFailure:YES]; + [self setMaxConcurrentOperationCount:4]; + [self setSuspended:YES]; + + return self; +} + ++ (id)queue +{ + return [[[self alloc] init] autorelease]; +} + +- (void)dealloc +{ + //We need to clear the queue on any requests that haven't got around to cleaning up yet, as otherwise they'll try to let us know if something goes wrong, and we'll be long gone by then + for (ASIHTTPRequest *request in [self operations]) { + [request setQueue:nil]; + } + [userInfo release]; + [super dealloc]; +} + +- (void)setSuspended:(BOOL)suspend +{ + [super setSuspended:suspend]; +} + +- (void)reset +{ + [self cancelAllOperations]; + [self setDelegate:nil]; + [self setDownloadProgressDelegate:nil]; + [self setUploadProgressDelegate:nil]; + [self setRequestDidStartSelector:NULL]; + [self setRequestDidReceiveResponseHeadersSelector:NULL]; + [self setRequestDidFailSelector:NULL]; + [self setRequestDidFinishSelector:NULL]; + [self setQueueDidFinishSelector:NULL]; + [self setSuspended:YES]; +} + + +- (void)go +{ + [self setSuspended:NO]; +} + +- (void)cancelAllOperations +{ + [self setBytesUploadedSoFar:0]; + [self setTotalBytesToUpload:0]; + [self setBytesDownloadedSoFar:0]; + [self setTotalBytesToDownload:0]; + + [super cancelAllOperations]; +} + +- (void)setUploadProgressDelegate:(id)newDelegate +{ + + uploadProgressDelegate = newDelegate; + [self resetProgressDelegate:newDelegate]; + +} + +- (void)setDownloadProgressDelegate:(id)newDelegate +{ + downloadProgressDelegate = newDelegate; + [self resetProgressDelegate:newDelegate]; +} + +- (void)resetProgressDelegate:(id)progressDelegate +{ +#if !TARGET_OS_IPHONE + // If the uploadProgressDelegate is an NSProgressIndicator, we set its MaxValue to 1.0 so we can treat it similarly to UIProgressViews + + + SEL selector = @selector(setMaxValue:); + if ([progressDelegate respondsToSelector:selector]) { + double max = 1.0; + [ASIHTTPRequest performSelector:selector onTarget:progressDelegate withObject:nil amount:&max]; + } + selector = @selector(setDoubleValue:); + if ([progressDelegate respondsToSelector:selector]) { + double value = 0.0; + [ASIHTTPRequest performSelector:selector onTarget:progressDelegate withObject:nil amount:&value]; + } +#else + SEL selector = @selector(setProgress:); + if ([progressDelegate respondsToSelector:selector]) { + float value = 0.0f; + [ASIHTTPRequest performSelector:selector onTarget:progressDelegate withObject:nil amount:&value]; + } +#endif +} + +- (void)addHEADOperation:(NSOperation *)operation +{ + if ([operation isKindOfClass:[ASIHTTPRequest class]]) { + + ASIHTTPRequest *request = (ASIHTTPRequest *)operation; + [request setRequestMethod:@"HEAD"]; + [request setQueuePriority:10]; + [request setShowAccurateProgress:YES]; + [request setQueue:self]; + + // Important - we are calling NSOperation's add method - we don't want to add this as a normal request! + [super addOperation:request]; + } +} + +// Only add ASIHTTPRequests to this queue!! +- (void)addOperation:(NSOperation *)operation +{ + if (![operation isKindOfClass:[ASIHTTPRequest class]]) { + [NSException raise:@"AttemptToAddInvalidRequest" format:@"Attempted to add an object that was not an ASIHTTPRequest to an ASINetworkQueue"]; + } + + [self setRequestsCount:[self requestsCount]+1]; + + ASIHTTPRequest *request = (ASIHTTPRequest *)operation; + + if ([self showAccurateProgress]) { + + // Force the request to build its body (this may change requestMethod) + [request buildPostBody]; + + // If this is a GET request and we want accurate progress, perform a HEAD request first to get the content-length + // We'll only do this before the queue is started + // If requests are added after the queue is started they will probably move the overall progress backwards anyway, so there's no value performing the HEAD requests first + // Instead, they'll update the total progress if and when they receive a content-length header + if ([[request requestMethod] isEqualToString:@"GET"]) { + if ([self isSuspended]) { + ASIHTTPRequest *HEADRequest = [request HEADRequest]; + [self addHEADOperation:HEADRequest]; + [request addDependency:HEADRequest]; + if ([request shouldResetDownloadProgress]) { + [self resetProgressDelegate:[request downloadProgressDelegate]]; + [request setShouldResetDownloadProgress:NO]; + } + } + } + [request buildPostBody]; + [self request:nil incrementUploadSizeBy:[request postLength]]; + + + } else { + [self request:nil incrementDownloadSizeBy:1]; + [self request:nil incrementUploadSizeBy:1]; + } + // Tell the request not to increment the upload size when it starts, as we've already added its length + if ([request shouldResetUploadProgress]) { + [self resetProgressDelegate:[request uploadProgressDelegate]]; + [request setShouldResetUploadProgress:NO]; + } + + [request setShowAccurateProgress:[self showAccurateProgress]]; + + [request setQueue:self]; + [super addOperation:request]; + +} + +- (void)requestStarted:(ASIHTTPRequest *)request +{ + if ([self requestDidStartSelector]) { + [[self delegate] performSelector:[self requestDidStartSelector] withObject:request]; + } +} + +- (void)requestReceivedResponseHeaders:(ASIHTTPRequest *)request +{ + if ([self requestDidReceiveResponseHeadersSelector]) { + [[self delegate] performSelector:[self requestDidReceiveResponseHeadersSelector] withObject:request]; + } +} + + +- (void)requestFinished:(ASIHTTPRequest *)request +{ + [self setRequestsCount:[self requestsCount]-1]; + if ([self requestDidFinishSelector]) { + [[self delegate] performSelector:[self requestDidFinishSelector] withObject:request]; + } + if ([self requestsCount] == 0) { + if ([self queueDidFinishSelector]) { + [[self delegate] performSelector:[self queueDidFinishSelector] withObject:self]; + } + } +} + +- (void)requestFailed:(ASIHTTPRequest *)request +{ + [self setRequestsCount:[self requestsCount]-1]; + if ([self requestDidFailSelector]) { + [[self delegate] performSelector:[self requestDidFailSelector] withObject:request]; + } + if ([self requestsCount] == 0) { + if ([self queueDidFinishSelector]) { + [[self delegate] performSelector:[self queueDidFinishSelector] withObject:self]; + } + } + if ([self shouldCancelAllRequestsOnFailure] && [self requestsCount] > 0) { + [self cancelAllOperations]; + } + +} + + +- (void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes +{ + //nslog(@"YES didReceiveBytes"); + + [self setBytesDownloadedSoFar:[self bytesDownloadedSoFar]+bytes]; + if ([self downloadProgressDelegate]) { + [ASIHTTPRequest updateProgressIndicator:[self downloadProgressDelegate] withProgress:[self bytesDownloadedSoFar] ofTotal:[self totalBytesToDownload]]; + } +} + +- (void)request:(ASIHTTPRequest *)request didSendBytes:(long long)bytes +{ + //nslog(@"YES didSendBytes"); + + [self setBytesUploadedSoFar:[self bytesUploadedSoFar]+bytes]; + if ([self uploadProgressDelegate]) { + [ASIHTTPRequest updateProgressIndicator:[self uploadProgressDelegate] withProgress:[self bytesUploadedSoFar] ofTotal:[self totalBytesToUpload]]; + } +} + +- (void)request:(ASIHTTPRequest *)request incrementDownloadSizeBy:(long long)newLength +{ + [self setTotalBytesToDownload:[self totalBytesToDownload]+newLength]; +} + +- (void)request:(ASIHTTPRequest *)request incrementUploadSizeBy:(long long)newLength +{ + [self setTotalBytesToUpload:[self totalBytesToUpload]+newLength]; +} + + +// Since this queue takes over as the delegate for all requests it contains, it should forward authorisation requests to its own delegate +- (void)authenticationNeededForRequest:(ASIHTTPRequest *)request +{ + if ([[self delegate] respondsToSelector:@selector(authenticationNeededForRequest:)]) { + [[self delegate] performSelector:@selector(authenticationNeededForRequest:) withObject:request]; + } +} + +- (void)proxyAuthenticationNeededForRequest:(ASIHTTPRequest *)request +{ + if ([[self delegate] respondsToSelector:@selector(proxyAuthenticationNeededForRequest:)]) { + [[self delegate] performSelector:@selector(proxyAuthenticationNeededForRequest:) withObject:request]; + } +} + + +- (BOOL)respondsToSelector:(SEL)selector +{ + if (selector == @selector(authenticationNeededForRequest:)) { + if ([[self delegate] respondsToSelector:@selector(authenticationNeededForRequest:)]) { + return YES; + } + return NO; + } else if (selector == @selector(proxyAuthenticationNeededForRequest:)) { + if ([[self delegate] respondsToSelector:@selector(proxyAuthenticationNeededForRequest:)]) { + return YES; + } + return NO; + } + return [super respondsToSelector:selector]; +} + +#pragma mark NSCopying + +- (id)copyWithZone:(NSZone *)zone +{ + ASINetworkQueue *newQueue = [[[self class] alloc] init]; + [newQueue setDelegate:[self delegate]]; + [newQueue setRequestDidStartSelector:[self requestDidStartSelector]]; + [newQueue setRequestDidFinishSelector:[self requestDidFinishSelector]]; + [newQueue setRequestDidFailSelector:[self requestDidFailSelector]]; + [newQueue setQueueDidFinishSelector:[self queueDidFinishSelector]]; + [newQueue setUploadProgressDelegate:[self uploadProgressDelegate]]; + [newQueue setDownloadProgressDelegate:[self downloadProgressDelegate]]; + [newQueue setShouldCancelAllRequestsOnFailure:[self shouldCancelAllRequestsOnFailure]]; + [newQueue setShowAccurateProgress:[self showAccurateProgress]]; + [newQueue setUserInfo:[[[self userInfo] copyWithZone:zone] autorelease]]; + return newQueue; +} + + +@synthesize requestsCount; +@synthesize bytesUploadedSoFar; +@synthesize totalBytesToUpload; +@synthesize bytesDownloadedSoFar; +@synthesize totalBytesToDownload; +@synthesize shouldCancelAllRequestsOnFailure; +@synthesize uploadProgressDelegate; +@synthesize downloadProgressDelegate; +@synthesize requestDidStartSelector; +@synthesize requestDidReceiveResponseHeadersSelector; +@synthesize requestDidFinishSelector; +@synthesize requestDidFailSelector; +@synthesize queueDidFinishSelector; +@synthesize delegate; +@synthesize showAccurateProgress; +@synthesize userInfo; +@end diff --git a/ASIProgressDelegate.h b/ASIProgressDelegate.h new file mode 100644 index 0000000..e2bb0cf --- /dev/null +++ b/ASIProgressDelegate.h @@ -0,0 +1,38 @@ +// +// ASIProgressDelegate.h +// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest +// +// Created by Ben Copsey on 13/04/2010. +// Copyright 2010 All-Seeing Interactive. All rights reserved. +// + +@class ASIHTTPRequest; + +@protocol ASIProgressDelegate + +@optional + +// These methods are used to update UIProgressViews (iPhone OS) or NSProgressIndicators (Mac OS X) +// If you are using a custom progress delegate, you may find it easier to implement didReceiveBytes / didSendBytes instead +#if TARGET_OS_IPHONE +- (void)setProgress:(float)newProgress; +#else +- (void)setDoubleValue:(double)newProgress; +- (void)setMaxValue:(double)newMax; +#endif + +// Called when the request receives some data - bytes is the length of that data +- (void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes; + +// Called when the request sends some data +// The first 32KB (128KB on older platforms) of data sent is not included in this amount because of limitations with the CFNetwork API +// bytes may be less than zero if a request needs to remove upload progress (probably because the request needs to run again) +- (void)request:(ASIHTTPRequest *)request didSendBytes:(long long)bytes; + +// Called when a request needs to change the length of the content to download +- (void)request:(ASIHTTPRequest *)request incrementDownloadSizeBy:(long long)newLength; + +// Called when a request needs to change the length of the content to upload +// newLength may be less than zero when a request needs to remove the size of the internal buffer from progress tracking +- (void)request:(ASIHTTPRequest *)request incrementUploadSizeBy:(long long)newLength; +@end diff --git a/JSON.h b/JSON.h new file mode 100644 index 0000000..1e58c9a --- /dev/null +++ b/JSON.h @@ -0,0 +1,50 @@ +/* + Copyright (C) 2009 Stig Brautaset. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name of the author nor the names of its contributors may be used + to endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + @mainpage A strict JSON parser and generator for Objective-C + + JSON (JavaScript Object Notation) is a lightweight data-interchange + format. This framework provides two apis for parsing and generating + JSON. One standard object-based and a higher level api consisting of + categories added to existing Objective-C classes. + + Learn more on the http://code.google.com/p/json-framework project site. + + This framework does its best to be as strict as possible, both in what it + accepts and what it generates. For example, it does not support trailing commas + in arrays or objects. Nor does it support embedded comments, or + anything else not in the JSON specification. This is considered a feature. + +*/ + +#import "SBJSON.h" +#import "NSObject+SBJSON.h" +#import "NSString+SBJSON.h" + diff --git a/NSObject+SBJSON.h b/NSObject+SBJSON.h new file mode 100644 index 0000000..ecf0ee4 --- /dev/null +++ b/NSObject+SBJSON.h @@ -0,0 +1,68 @@ +/* + Copyright (C) 2009 Stig Brautaset. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name of the author nor the names of its contributors may be used + to endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#import + + +/** + @brief Adds JSON generation to Foundation classes + + This is a category on NSObject that adds methods for returning JSON representations + of standard objects to the objects themselves. This means you can call the + -JSONRepresentation method on an NSArray object and it'll do what you want. + */ +@interface NSObject (NSObject_SBJSON) + +/** + @brief Returns a string containing the receiver encoded as a JSON fragment. + + This method is added as a category on NSObject but is only actually + supported for the following objects: + @li NSDictionary + @li NSArray + @li NSString + @li NSNumber (also used for booleans) + @li NSNull + + @deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed. + */ +- (NSString *)JSONFragment; + +/** + @brief Returns a string containing the receiver encoded in JSON. + + This method is added as a category on NSObject but is only actually + supported for the following objects: + @li NSDictionary + @li NSArray + */ +- (NSString *)JSONRepresentation; + +@end + diff --git a/NSObject+SBJSON.m b/NSObject+SBJSON.m new file mode 100644 index 0000000..1097ff1 --- /dev/null +++ b/NSObject+SBJSON.m @@ -0,0 +1,58 @@ +/* + Copyright (C) 2009 Stig Brautaset. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name of the author nor the names of its contributors may be used + to endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#import "NSObject+SBJSON.h" +#import "SBJsonWriter.h" + +@implementation NSObject (NSObject_SBJSON) + +- (NSString *)JSONFragment { + SBJsonWriter *jsonWriter = [SBJsonWriter new]; + NSString *json = [jsonWriter stringWithFragment:self]; + if (!json) + { + + } + + [jsonWriter release]; + return json; +} + +- (NSString *)JSONRepresentation { + SBJsonWriter *jsonWriter = [SBJsonWriter new]; + NSString *json = [jsonWriter stringWithObject:self]; + if (!json) + { + + } + [jsonWriter release]; + return json; +} + +@end diff --git a/NSString+SBJSON.h b/NSString+SBJSON.h new file mode 100644 index 0000000..fad7179 --- /dev/null +++ b/NSString+SBJSON.h @@ -0,0 +1,58 @@ +/* + Copyright (C) 2009 Stig Brautaset. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name of the author nor the names of its contributors may be used + to endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#import + +/** + @brief Adds JSON parsing methods to NSString + +This is a category on NSString that adds methods for parsing the target string. +*/ +@interface NSString (NSString_SBJSON) + + +/** + @brief Returns the object represented in the receiver, or nil on error. + + Returns a a scalar object represented by the string's JSON fragment representation. + + @deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed. + */ +- (id)JSONFragmentValue; + +/** + @brief Returns the NSDictionary or NSArray represented by the current string's JSON representation. + + Returns the dictionary or array represented in the receiver, or nil on error. + + Returns the NSDictionary or NSArray represented by the current string's JSON representation. + */ +- (id)JSONValue; + +@end diff --git a/NSString+SBJSON.m b/NSString+SBJSON.m new file mode 100644 index 0000000..b8e21a4 --- /dev/null +++ b/NSString+SBJSON.m @@ -0,0 +1,59 @@ +/* + Copyright (C) 2007-2009 Stig Brautaset. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name of the author nor the names of its contributors may be used + to endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#import "NSString+SBJSON.h" +#import "SBJsonParser.h" + +@implementation NSString (NSString_SBJSON) + +- (id)JSONFragmentValue +{ + SBJsonParser *jsonParser = [SBJsonParser new]; + id repr = [jsonParser fragmentWithString:self]; + if (!repr) + { + //nslog(@"-JSONFragmentValue failed. Error trace is: %@", [jsonParser errorTrace]); + } + [jsonParser release]; + return repr; +} + +- (id)JSONValue +{ + SBJsonParser *jsonParser = [SBJsonParser new]; + id repr = [jsonParser objectWithString:self]; + if (!repr) + { + //nslog(@"-JSONValue failed. Error trace is: %@", [jsonParser errorTrace]); + } + [jsonParser release]; + return repr; +} + +@end diff --git a/SBJSON.h b/SBJSON.h new file mode 100644 index 0000000..43d63c3 --- /dev/null +++ b/SBJSON.h @@ -0,0 +1,75 @@ +/* + Copyright (C) 2007-2009 Stig Brautaset. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name of the author nor the names of its contributors may be used + to endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#import +#import "SBJsonParser.h" +#import "SBJsonWriter.h" + +/** + @brief Facade for SBJsonWriter/SBJsonParser. + + Requests are forwarded to instances of SBJsonWriter and SBJsonParser. + */ +@interface SBJSON : SBJsonBase { + +@private + SBJsonParser *jsonParser; + SBJsonWriter *jsonWriter; +} + + +/// Return the fragment represented by the given string +- (id)fragmentWithString:(NSString*)jsonrep + error:(NSError**)error; + +/// Return the object represented by the given string +- (id)objectWithString:(NSString*)jsonrep + error:(NSError**)error; + +/// Parse the string and return the represented object (or scalar) +- (id)objectWithString:(id)value + allowScalar:(BOOL)x + error:(NSError**)error; + + +/// Return JSON representation of an array or dictionary +- (NSString*)stringWithObject:(id)value + error:(NSError**)error; + +/// Return JSON representation of any legal JSON value +- (NSString*)stringWithFragment:(id)value + error:(NSError**)error; + +/// Return JSON representation (or fragment) for the given object +- (NSString*)stringWithObject:(id)value + allowScalar:(BOOL)x + error:(NSError**)error; + + +@end diff --git a/SBJSON.m b/SBJSON.m new file mode 100644 index 0000000..2a30f1a --- /dev/null +++ b/SBJSON.m @@ -0,0 +1,212 @@ +/* + Copyright (C) 2007-2009 Stig Brautaset. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name of the author nor the names of its contributors may be used + to endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#import "SBJSON.h" + +@implementation SBJSON + +- (id)init { + self = [super init]; + if (self) { + jsonWriter = [SBJsonWriter new]; + jsonParser = [SBJsonParser new]; + [self setMaxDepth:512]; + + } + return self; +} + +- (void)dealloc { + [jsonWriter release]; + [jsonParser release]; + [super dealloc]; +} + +#pragma mark Writer + + +- (NSString *)stringWithObject:(id)obj { + NSString *repr = [jsonWriter stringWithObject:obj]; + if (repr) + return repr; + + [errorTrace release]; + errorTrace = [[jsonWriter errorTrace] mutableCopy]; + return nil; +} + +/** + Returns a string containing JSON representation of the passed in value, or nil on error. + If nil is returned and @p error is not NULL, @p *error can be interrogated to find the cause of the error. + + @param value any instance that can be represented as a JSON fragment + @param allowScalar wether to return json fragments for scalar objects + @param error used to return an error by reference (pass NULL if this is not desired) + +@deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed. + */ +- (NSString*)stringWithObject:(id)value allowScalar:(BOOL)allowScalar error:(NSError**)error { + + NSString *json = allowScalar ? [jsonWriter stringWithFragment:value] : [jsonWriter stringWithObject:value]; + if (json) + return json; + + [errorTrace release]; + errorTrace = [[jsonWriter errorTrace] mutableCopy]; + + if (error) + *error = [errorTrace lastObject]; + return nil; +} + +/** + Returns a string containing JSON representation of the passed in value, or nil on error. + If nil is returned and @p error is not NULL, @p error can be interrogated to find the cause of the error. + + @param value any instance that can be represented as a JSON fragment + @param error used to return an error by reference (pass NULL if this is not desired) + + @deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed. + */ +- (NSString*)stringWithFragment:(id)value error:(NSError**)error { + return [self stringWithObject:value + allowScalar:YES + error:error]; +} + +/** + Returns a string containing JSON representation of the passed in value, or nil on error. + If nil is returned and @p error is not NULL, @p error can be interrogated to find the cause of the error. + + @param value a NSDictionary or NSArray instance + @param error used to return an error by reference (pass NULL if this is not desired) + */ +- (NSString*)stringWithObject:(id)value error:(NSError**)error { + return [self stringWithObject:value + allowScalar:NO + error:error]; +} + +#pragma mark Parsing + +- (id)objectWithString:(NSString *)repr { + id obj = [jsonParser objectWithString:repr]; + if (obj) + return obj; + + [errorTrace release]; + errorTrace = [[jsonParser errorTrace] mutableCopy]; + + return nil; +} + +/** + Returns the object represented by the passed-in string or nil on error. The returned object can be + a string, number, boolean, null, array or dictionary. + + @param value the json string to parse + @param allowScalar whether to return objects for JSON fragments + @param error used to return an error by reference (pass NULL if this is not desired) + + @deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed. + */ +- (id)objectWithString:(id)value allowScalar:(BOOL)allowScalar error:(NSError**)error { + + id obj = allowScalar ? [jsonParser fragmentWithString:value] : [jsonParser objectWithString:value]; + if (obj) + return obj; + + [errorTrace release]; + errorTrace = [[jsonParser errorTrace] mutableCopy]; + + if (error) + *error = [errorTrace lastObject]; + return nil; +} + +/** + Returns the object represented by the passed-in string or nil on error. The returned object can be + a string, number, boolean, null, array or dictionary. + + @param repr the json string to parse + @param error used to return an error by reference (pass NULL if this is not desired) + + @deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed. + */ +- (id)fragmentWithString:(NSString*)repr error:(NSError**)error { + return [self objectWithString:repr + allowScalar:YES + error:error]; +} + +/** + Returns the object represented by the passed-in string or nil on error. The returned object + will be either a dictionary or an array. + + @param repr the json string to parse + @param error used to return an error by reference (pass NULL if this is not desired) + */ +- (id)objectWithString:(NSString*)repr error:(NSError**)error { + return [self objectWithString:repr + allowScalar:NO + error:error]; +} + + + +#pragma mark Properties - parsing + +- (NSUInteger)maxDepth { + return jsonParser.maxDepth; +} + +- (void)setMaxDepth:(NSUInteger)d { + jsonWriter.maxDepth = jsonParser.maxDepth = d; +} + + +#pragma mark Properties - writing + +- (BOOL)humanReadable { + return jsonWriter.humanReadable; +} + +- (void)setHumanReadable:(BOOL)x { + jsonWriter.humanReadable = x; +} + +- (BOOL)sortKeys { + return jsonWriter.sortKeys; +} + +- (void)setSortKeys:(BOOL)x { + jsonWriter.sortKeys = x; +} + +@end diff --git a/SBJsonBase.h b/SBJsonBase.h new file mode 100644 index 0000000..7b10844 --- /dev/null +++ b/SBJsonBase.h @@ -0,0 +1,86 @@ +/* + Copyright (C) 2009 Stig Brautaset. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name of the author nor the names of its contributors may be used + to endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#import + +extern NSString * SBJSONErrorDomain; + + +enum { + EUNSUPPORTED = 1, + EPARSENUM, + EPARSE, + EFRAGMENT, + ECTRL, + EUNICODE, + EDEPTH, + EESCAPE, + ETRAILCOMMA, + ETRAILGARBAGE, + EEOF, + EINPUT +}; + +/** + @brief Common base class for parsing & writing. + + This class contains the common error-handling code and option between the parser/writer. + */ +@interface SBJsonBase : NSObject { + NSMutableArray *errorTrace; + +@protected + NSUInteger depth, maxDepth; +} + +/** + @brief The maximum recursing depth. + + Defaults to 512. If the input is nested deeper than this the input will be deemed to be + malicious and the parser returns nil, signalling an error. ("Nested too deep".) You can + turn off this security feature by setting the maxDepth value to 0. + */ +@property NSUInteger maxDepth; + +/** + @brief Return an error trace, or nil if there was no errors. + + Note that this method returns the trace of the last method that failed. + You need to check the return value of the call you're making to figure out + if the call actually failed, before you know call this method. + */ + @property(copy,readonly) NSArray* errorTrace; + +/// @internal for use in subclasses to add errors to the stack trace +- (void)addErrorWithCode:(NSUInteger)code description:(NSString*)str; + +/// @internal for use in subclasess to clear the error before a new parsing attempt +- (void)clearErrorTrace; + +@end diff --git a/SBJsonBase.m b/SBJsonBase.m new file mode 100644 index 0000000..6684325 --- /dev/null +++ b/SBJsonBase.m @@ -0,0 +1,78 @@ +/* + Copyright (C) 2009 Stig Brautaset. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name of the author nor the names of its contributors may be used + to endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#import "SBJsonBase.h" +NSString * SBJSONErrorDomain = @"org.brautaset.JSON.ErrorDomain"; + + +@implementation SBJsonBase + +@synthesize errorTrace; +@synthesize maxDepth; + +- (id)init { + self = [super init]; + if (self) + self.maxDepth = 512; + return self; +} + +- (void)dealloc { + [errorTrace release]; + [super dealloc]; +} + +- (void)addErrorWithCode:(NSUInteger)code description:(NSString*)str { + NSDictionary *userInfo; + if (!errorTrace) { + errorTrace = [NSMutableArray new]; + userInfo = [NSDictionary dictionaryWithObject:str forKey:NSLocalizedDescriptionKey]; + + } else { + userInfo = [NSDictionary dictionaryWithObjectsAndKeys: + str, NSLocalizedDescriptionKey, + [errorTrace lastObject], NSUnderlyingErrorKey, + nil]; + } + + NSError *error = [NSError errorWithDomain:SBJSONErrorDomain code:code userInfo:userInfo]; + + [self willChangeValueForKey:@"errorTrace"]; + [errorTrace addObject:error]; + [self didChangeValueForKey:@"errorTrace"]; +} + +- (void)clearErrorTrace { + [self willChangeValueForKey:@"errorTrace"]; + [errorTrace release]; + errorTrace = nil; + [self didChangeValueForKey:@"errorTrace"]; +} + +@end diff --git a/SBJsonParser.h b/SBJsonParser.h new file mode 100644 index 0000000..e95304d --- /dev/null +++ b/SBJsonParser.h @@ -0,0 +1,87 @@ +/* + Copyright (C) 2009 Stig Brautaset. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name of the author nor the names of its contributors may be used + to endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#import +#import "SBJsonBase.h" + +/** + @brief Options for the parser class. + + This exists so the SBJSON facade can implement the options in the parser without having to re-declare them. + */ +@protocol SBJsonParser + +/** + @brief Return the object represented by the given string. + + Returns the object represented by the passed-in string or nil on error. The returned object can be + a string, number, boolean, null, array or dictionary. + + @param repr the json string to parse + */ +- (id)objectWithString:(NSString *)repr; + +@end + + +/** + @brief The JSON parser class. + + JSON is mapped to Objective-C types in the following way: + + @li Null -> NSNull + @li String -> NSMutableString + @li Array -> NSMutableArray + @li Object -> NSMutableDictionary + @li Boolean -> NSNumber (initialised with -initWithBool:) + @li Number -> NSDecimalNumber + + Since Objective-C doesn't have a dedicated class for boolean values, these turns into NSNumber + instances. These are initialised with the -initWithBool: method, and + round-trip back to JSON properly. (They won't silently suddenly become 0 or 1; they'll be + represented as 'true' and 'false' again.) + + JSON numbers turn into NSDecimalNumber instances, + as we can thus avoid any loss of precision. (JSON allows ridiculously large numbers.) + + */ +@interface SBJsonParser : SBJsonBase { + +@private + const char *c; +} + +@end + +// don't use - exists for backwards compatibility with 2.1.x only. Will be removed in 2.3. +@interface SBJsonParser (Private) +- (id)fragmentWithString:(id)repr; +@end + + diff --git a/SBJsonParser.m b/SBJsonParser.m new file mode 100644 index 0000000..0022bd4 --- /dev/null +++ b/SBJsonParser.m @@ -0,0 +1,475 @@ +/* + Copyright (C) 2009 Stig Brautaset. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name of the author nor the names of its contributors may be used + to endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#import "SBJsonParser.h" + +@interface SBJsonParser () + +- (BOOL)scanValue:(NSObject **)o; + +- (BOOL)scanRestOfArray:(NSMutableArray **)o; +- (BOOL)scanRestOfDictionary:(NSMutableDictionary **)o; +- (BOOL)scanRestOfNull:(NSNull **)o; +- (BOOL)scanRestOfFalse:(NSNumber **)o; +- (BOOL)scanRestOfTrue:(NSNumber **)o; +- (BOOL)scanRestOfString:(NSMutableString **)o; + +// Cannot manage without looking at the first digit +- (BOOL)scanNumber:(NSNumber **)o; + +- (BOOL)scanHexQuad:(unichar *)x; +- (BOOL)scanUnicodeChar:(unichar *)x; + +- (BOOL)scanIsAtEnd; + +@end + +#define skipWhitespace(c) while (isspace(*c)) c++ +#define skipDigits(c) while (isdigit(*c)) c++ + + +@implementation SBJsonParser + +static char ctrl[0x22]; + + ++ (void)initialize { + ctrl[0] = '\"'; + ctrl[1] = '\\'; + for (int i = 1; i < 0x20; i++) + ctrl[i+1] = i; + ctrl[0x21] = 0; +} + +/** + @deprecated This exists in order to provide fragment support in older APIs in one more version. + It should be removed in the next major version. + */ +- (id)fragmentWithString:(id)repr { + [self clearErrorTrace]; + + if (!repr) { + [self addErrorWithCode:EINPUT description:@"Input was 'nil'"]; + return nil; + } + + depth = 0; + c = [repr UTF8String]; + + id o; + if (![self scanValue:&o]) { + return nil; + } + + // We found some valid JSON. But did it also contain something else? + if (![self scanIsAtEnd]) { + [self addErrorWithCode:ETRAILGARBAGE description:@"Garbage after JSON"]; + return nil; + } + + NSAssert1(o, @"Should have a valid object from %@", repr); + return o; +} + +- (id)objectWithString:(NSString *)repr { + + id o = [self fragmentWithString:repr]; + if (!o) + return nil; + + // Check that the object we've found is a valid JSON container. + if (![o isKindOfClass:[NSDictionary class]] && ![o isKindOfClass:[NSArray class]]) { + [self addErrorWithCode:EFRAGMENT description:@"Valid fragment, but not JSON"]; + return nil; + } + + return o; +} + +/* + In contrast to the public methods, it is an error to omit the error parameter here. + */ +- (BOOL)scanValue:(NSObject **)o +{ + skipWhitespace(c); + + switch (*c++) { + case '{': + return [self scanRestOfDictionary:(NSMutableDictionary **)o]; + break; + case '[': + return [self scanRestOfArray:(NSMutableArray **)o]; + break; + case '"': + return [self scanRestOfString:(NSMutableString **)o]; + break; + case 'f': + return [self scanRestOfFalse:(NSNumber **)o]; + break; + case 't': + return [self scanRestOfTrue:(NSNumber **)o]; + break; + case 'n': + return [self scanRestOfNull:(NSNull **)o]; + break; + case '-': + case '0'...'9': + c--; // cannot verify number correctly without the first character + return [self scanNumber:(NSNumber **)o]; + break; + case '+': + [self addErrorWithCode:EPARSENUM description: @"Leading + disallowed in number"]; + return NO; + break; + case 0x0: + [self addErrorWithCode:EEOF description:@"Unexpected end of string"]; + return NO; + break; + default: + [self addErrorWithCode:EPARSE description: @"Unrecognised leading character"]; + return NO; + break; + } + + NSAssert(0, @"Should never get here"); + return NO; +} + +- (BOOL)scanRestOfTrue:(NSNumber **)o +{ + if (!strncmp(c, "rue", 3)) { + c += 3; + *o = [NSNumber numberWithBool:YES]; + return YES; + } + [self addErrorWithCode:EPARSE description:@"Expected 'true'"]; + return NO; +} + +- (BOOL)scanRestOfFalse:(NSNumber **)o +{ + if (!strncmp(c, "alse", 4)) { + c += 4; + *o = [NSNumber numberWithBool:NO]; + return YES; + } + [self addErrorWithCode:EPARSE description: @"Expected 'false'"]; + return NO; +} + +- (BOOL)scanRestOfNull:(NSNull **)o { + if (!strncmp(c, "ull", 3)) { + c += 3; + *o = [NSNull null]; + return YES; + } + [self addErrorWithCode:EPARSE description: @"Expected 'null'"]; + return NO; +} + +- (BOOL)scanRestOfArray:(NSMutableArray **)o { + if (maxDepth && ++depth > maxDepth) { + [self addErrorWithCode:EDEPTH description: @"Nested too deep"]; + return NO; + } + + *o = [NSMutableArray arrayWithCapacity:8]; + + for (; *c ;) { + id v; + + skipWhitespace(c); + if (*c == ']' && c++) { + depth--; + return YES; + } + + if (![self scanValue:&v]) { + [self addErrorWithCode:EPARSE description:@"Expected value while parsing array"]; + return NO; + } + + [*o addObject:v]; + + skipWhitespace(c); + if (*c == ',' && c++) { + skipWhitespace(c); + if (*c == ']') { + [self addErrorWithCode:ETRAILCOMMA description: @"Trailing comma disallowed in array"]; + return NO; + } + } + } + + [self addErrorWithCode:EEOF description: @"End of input while parsing array"]; + return NO; +} + +- (BOOL)scanRestOfDictionary:(NSMutableDictionary **)o +{ + if (maxDepth && ++depth > maxDepth) { + [self addErrorWithCode:EDEPTH description: @"Nested too deep"]; + return NO; + } + + *o = [NSMutableDictionary dictionaryWithCapacity:7]; + + for (; *c ;) { + id k, v; + + skipWhitespace(c); + if (*c == '}' && c++) { + depth--; + return YES; + } + + if (!(*c == '\"' && c++ && [self scanRestOfString:&k])) { + [self addErrorWithCode:EPARSE description: @"Object key string expected"]; + return NO; + } + + skipWhitespace(c); + if (*c != ':') { + [self addErrorWithCode:EPARSE description: @"Expected ':' separating key and value"]; + return NO; + } + + c++; + if (![self scanValue:&v]) { + NSString *string = [NSString stringWithFormat:@"Object value expected for key: %@", k]; + [self addErrorWithCode:EPARSE description: string]; + return NO; + } + + [*o setObject:v forKey:k]; + + skipWhitespace(c); + if (*c == ',' && c++) { + skipWhitespace(c); + if (*c == '}') { + [self addErrorWithCode:ETRAILCOMMA description: @"Trailing comma disallowed in object"]; + return NO; + } + } + } + + [self addErrorWithCode:EEOF description: @"End of input while parsing object"]; + return NO; +} + +- (BOOL)scanRestOfString:(NSMutableString **)o +{ + *o = [NSMutableString stringWithCapacity:16]; + do { + // First see if there's a portion we can grab in one go. + // Doing this caused a massive speedup on the long string. + size_t len = strcspn(c, ctrl); + if (len) { + // check for + id t = [[NSString alloc] initWithBytesNoCopy:(char*)c + length:len + encoding:NSUTF8StringEncoding + freeWhenDone:NO]; + if (t) { + [*o appendString:t]; + [t release]; + c += len; + } + } + + if (*c == '"') { + c++; + return YES; + + } else if (*c == '\\') { + unichar uc = *++c; + switch (uc) { + case '\\': + case '/': + case '"': + break; + + case 'b': uc = '\b'; break; + case 'n': uc = '\n'; break; + case 'r': uc = '\r'; break; + case 't': uc = '\t'; break; + case 'f': uc = '\f'; break; + + case 'u': + c++; + if (![self scanUnicodeChar:&uc]) { + [self addErrorWithCode:EUNICODE description: @"Broken unicode character"]; + return NO; + } + c--; // hack. + break; + default: + [self addErrorWithCode:EESCAPE description: [NSString stringWithFormat:@"Illegal escape sequence '0x%x'", uc]]; + return NO; + break; + } + CFStringAppendCharacters((CFMutableStringRef)*o, &uc, 1); + c++; + + } else if (*c < 0x20) { + [self addErrorWithCode:ECTRL description: [NSString stringWithFormat:@"Unescaped control character '0x%x'", *c]]; + return NO; + + } else { + //nslog(@"should not be able to get here"); + } + } while (*c); + + [self addErrorWithCode:EEOF description:@"Unexpected EOF while parsing string"]; + return NO; +} + +- (BOOL)scanUnicodeChar:(unichar *)x +{ + unichar hi, lo; + + if (![self scanHexQuad:&hi]) { + [self addErrorWithCode:EUNICODE description: @"Missing hex quad"]; + return NO; + } + + if (hi >= 0xd800) { // high surrogate char? + if (hi < 0xdc00) { // yes - expect a low char + + if (!(*c == '\\' && ++c && *c == 'u' && ++c && [self scanHexQuad:&lo])) { + [self addErrorWithCode:EUNICODE description: @"Missing low character in surrogate pair"]; + return NO; + } + + if (lo < 0xdc00 || lo >= 0xdfff) { + [self addErrorWithCode:EUNICODE description:@"Invalid low surrogate char"]; + return NO; + } + + hi = (hi - 0xd800) * 0x400 + (lo - 0xdc00) + 0x10000; + + } else if (hi < 0xe000) { + [self addErrorWithCode:EUNICODE description:@"Invalid high character in surrogate pair"]; + return NO; + } + } + + *x = hi; + return YES; +} + +- (BOOL)scanHexQuad:(unichar *)x +{ + *x = 0; + for (int i = 0; i < 4; i++) { + unichar uc = *c; + c++; + int d = (uc >= '0' && uc <= '9') + ? uc - '0' : (uc >= 'a' && uc <= 'f') + ? (uc - 'a' + 10) : (uc >= 'A' && uc <= 'F') + ? (uc - 'A' + 10) : -1; + if (d == -1) { + [self addErrorWithCode:EUNICODE description:@"Missing hex digit in quad"]; + return NO; + } + *x *= 16; + *x += d; + } + return YES; +} + +- (BOOL)scanNumber:(NSNumber **)o +{ + const char *ns = c; + + // The logic to test for validity of the number formatting is relicensed + // from JSON::XS with permission from its author Marc Lehmann. + // (Available at the CPAN: http://search.cpan.org/dist/JSON-XS/ .) + + if ('-' == *c) + c++; + + if ('0' == *c && c++) { + if (isdigit(*c)) { + [self addErrorWithCode:EPARSENUM description: @"Leading 0 disallowed in number"]; + return NO; + } + + } else if (!isdigit(*c) && c != ns) { + [self addErrorWithCode:EPARSENUM description: @"No digits after initial minus"]; + return NO; + + } else { + skipDigits(c); + } + + // Fractional part + if ('.' == *c && c++) { + + if (!isdigit(*c)) { + [self addErrorWithCode:EPARSENUM description: @"No digits after decimal point"]; + return NO; + } + skipDigits(c); + } + + // Exponential part + if ('e' == *c || 'E' == *c) { + c++; + + if ('-' == *c || '+' == *c) + c++; + + if (!isdigit(*c)) { + [self addErrorWithCode:EPARSENUM description: @"No digits after exponent"]; + return NO; + } + skipDigits(c); + } + + id str = [[NSString alloc] initWithBytesNoCopy:(char*)ns + length:c - ns + encoding:NSUTF8StringEncoding + freeWhenDone:NO]; + [str autorelease]; + if (str && (*o = [NSDecimalNumber decimalNumberWithString:str])) + return YES; + + [self addErrorWithCode:EPARSENUM description: @"Failed creating decimal instance"]; + return NO; +} + +- (BOOL)scanIsAtEnd +{ + skipWhitespace(c); + return !*c; +} + + +@end diff --git a/SBJsonWriter.h b/SBJsonWriter.h new file mode 100644 index 0000000..f6f5e17 --- /dev/null +++ b/SBJsonWriter.h @@ -0,0 +1,129 @@ +/* + Copyright (C) 2009 Stig Brautaset. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name of the author nor the names of its contributors may be used + to endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#import +#import "SBJsonBase.h" + +/** + @brief Options for the writer class. + + This exists so the SBJSON facade can implement the options in the writer without having to re-declare them. + */ +@protocol SBJsonWriter + +/** + @brief Whether we are generating human-readable (multiline) JSON. + + Set whether or not to generate human-readable JSON. The default is NO, which produces + JSON without any whitespace. (Except inside strings.) If set to YES, generates human-readable + JSON with linebreaks after each array value and dictionary key/value pair, indented two + spaces per nesting level. + */ +@property BOOL humanReadable; + +/** + @brief Whether or not to sort the dictionary keys in the output. + + If this is set to YES, the dictionary keys in the JSON output will be in sorted order. + (This is useful if you need to compare two structures, for example.) The default is NO. + */ +@property BOOL sortKeys; + +/** + @brief Return JSON representation (or fragment) for the given object. + + Returns a string containing JSON representation of the passed in value, or nil on error. + If nil is returned and @p error is not NULL, @p *error can be interrogated to find the cause of the error. + + @param value any instance that can be represented as a JSON fragment + + */ +- (NSString*)stringWithObject:(id)value; + +@end + + +/** + @brief The JSON writer class. + + Objective-C types are mapped to JSON types in the following way: + + @li NSNull -> Null + @li NSString -> String + @li NSArray -> Array + @li NSDictionary -> Object + @li NSNumber (-initWithBool:) -> Boolean + @li NSNumber -> Number + + In JSON the keys of an object must be strings. NSDictionary keys need + not be, but attempting to convert an NSDictionary with non-string keys + into JSON will throw an exception. + + NSNumber instances created with the +initWithBool: method are + converted into the JSON boolean "true" and "false" values, and vice + versa. Any other NSNumber instances are converted to a JSON number the + way you would expect. + + */ +@interface SBJsonWriter : SBJsonBase { + +@private + BOOL sortKeys, humanReadable; +} + +@end + +// don't use - exists for backwards compatibility. Will be removed in 2.3. +@interface SBJsonWriter (Private) +- (NSString*)stringWithFragment:(id)value; +@end + +/** + @brief Allows generation of JSON for otherwise unsupported classes. + + If you have a custom class that you want to create a JSON representation for you can implement + this method in your class. It should return a representation of your object defined + in terms of objects that can be translated into JSON. For example, a Person + object might implement it like this: + + @code + - (id)jsonProxyObject { + return [NSDictionary dictionaryWithObjectsAndKeys: + name, @"name", + phone, @"phone", + email, @"email", + nil]; + } + @endcode + + */ +@interface NSObject (SBProxyForJson) +- (id)proxyForJson; +@end + diff --git a/SBJsonWriter.m b/SBJsonWriter.m new file mode 100644 index 0000000..0f32904 --- /dev/null +++ b/SBJsonWriter.m @@ -0,0 +1,237 @@ +/* + Copyright (C) 2009 Stig Brautaset. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name of the author nor the names of its contributors may be used + to endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#import "SBJsonWriter.h" + +@interface SBJsonWriter () + +- (BOOL)appendValue:(id)fragment into:(NSMutableString*)json; +- (BOOL)appendArray:(NSArray*)fragment into:(NSMutableString*)json; +- (BOOL)appendDictionary:(NSDictionary*)fragment into:(NSMutableString*)json; +- (BOOL)appendString:(NSString*)fragment into:(NSMutableString*)json; + +- (NSString*)indent; + +@end + +@implementation SBJsonWriter + +static NSMutableCharacterSet *kEscapeChars; + ++ (void)initialize { + kEscapeChars = [[NSMutableCharacterSet characterSetWithRange: NSMakeRange(0,32)] retain]; + [kEscapeChars addCharactersInString: @"\"\\"]; +} + + +@synthesize sortKeys; +@synthesize humanReadable; + +/** + @deprecated This exists in order to provide fragment support in older APIs in one more version. + It should be removed in the next major version. + */ +- (NSString*)stringWithFragment:(id)value { + [self clearErrorTrace]; + depth = 0; + NSMutableString *json = [NSMutableString stringWithCapacity:128]; + + if ([self appendValue:value into:json]) + return json; + + return nil; +} + + +- (NSString*)stringWithObject:(id)value { + + if ([value isKindOfClass:[NSDictionary class]] || [value isKindOfClass:[NSArray class]]) { + return [self stringWithFragment:value]; + } + + if ([value respondsToSelector:@selector(proxyForJson)]) { + NSString *tmp = [self stringWithObject:[value proxyForJson]]; + if (tmp) + return tmp; + } + + + [self clearErrorTrace]; + [self addErrorWithCode:EFRAGMENT description:@"Not valid type for JSON"]; + return nil; +} + + +- (NSString*)indent { + return [@"\n" stringByPaddingToLength:1 + 2 * depth withString:@" " startingAtIndex:0]; +} + +- (BOOL)appendValue:(id)fragment into:(NSMutableString*)json { + if ([fragment isKindOfClass:[NSDictionary class]]) { + if (![self appendDictionary:fragment into:json]) + return NO; + + } else if ([fragment isKindOfClass:[NSArray class]]) { + if (![self appendArray:fragment into:json]) + return NO; + + } else if ([fragment isKindOfClass:[NSString class]]) { + if (![self appendString:fragment into:json]) + return NO; + + } else if ([fragment isKindOfClass:[NSNumber class]]) { + if ('c' == *[fragment objCType]) + [json appendString:[fragment boolValue] ? @"true" : @"false"]; + else + [json appendString:[fragment stringValue]]; + + } else if ([fragment isKindOfClass:[NSNull class]]) { + [json appendString:@"null"]; + } else if ([fragment respondsToSelector:@selector(proxyForJson)]) { + [self appendValue:[fragment proxyForJson] into:json]; + + } else { + [self addErrorWithCode:EUNSUPPORTED description:[NSString stringWithFormat:@"JSON serialisation not supported for %@", [fragment class]]]; + return NO; + } + return YES; +} + +- (BOOL)appendArray:(NSArray*)fragment into:(NSMutableString*)json { + if (maxDepth && ++depth > maxDepth) { + [self addErrorWithCode:EDEPTH description: @"Nested too deep"]; + return NO; + } + [json appendString:@"["]; + + BOOL addComma = NO; + for (id value in fragment) { + if (addComma) + [json appendString:@","]; + else + addComma = YES; + + if ([self humanReadable]) + [json appendString:[self indent]]; + + if (![self appendValue:value into:json]) { + return NO; + } + } + + depth--; + if ([self humanReadable] && [fragment count]) + [json appendString:[self indent]]; + [json appendString:@"]"]; + return YES; +} + +- (BOOL)appendDictionary:(NSDictionary*)fragment into:(NSMutableString*)json { + if (maxDepth && ++depth > maxDepth) { + [self addErrorWithCode:EDEPTH description: @"Nested too deep"]; + return NO; + } + [json appendString:@"{"]; + + NSString *colon = [self humanReadable] ? @" : " : @":"; + BOOL addComma = NO; + NSArray *keys = [fragment allKeys]; + if (self.sortKeys) + keys = [keys sortedArrayUsingSelector:@selector(compare:)]; + + for (id value in keys) { + if (addComma) + [json appendString:@","]; + else + addComma = YES; + + if ([self humanReadable]) + [json appendString:[self indent]]; + + if (![value isKindOfClass:[NSString class]]) { + [self addErrorWithCode:EUNSUPPORTED description: @"JSON object key must be string"]; + return NO; + } + + if (![self appendString:value into:json]) + return NO; + + [json appendString:colon]; + if (![self appendValue:[fragment objectForKey:value] into:json]) { + [self addErrorWithCode:EUNSUPPORTED description:[NSString stringWithFormat:@"Unsupported value for key %@ in object", value]]; + return NO; + } + } + + depth--; + if ([self humanReadable] && [fragment count]) + [json appendString:[self indent]]; + [json appendString:@"}"]; + return YES; +} + +- (BOOL)appendString:(NSString*)fragment into:(NSMutableString*)json { + + [json appendString:@"\""]; + + NSRange esc = [fragment rangeOfCharacterFromSet:kEscapeChars]; + if ( !esc.length ) { + // No special chars -- can just add the raw string: + [json appendString:fragment]; + + } else { + NSUInteger length = [fragment length]; + for (NSUInteger i = 0; i < length; i++) { + unichar uc = [fragment characterAtIndex:i]; + switch (uc) { + case '"': [json appendString:@"\\\""]; break; + case '\\': [json appendString:@"\\\\"]; break; + case '\t': [json appendString:@"\\t"]; break; + case '\n': [json appendString:@"\\n"]; break; + case '\r': [json appendString:@"\\r"]; break; + case '\b': [json appendString:@"\\b"]; break; + case '\f': [json appendString:@"\\f"]; break; + default: + if (uc < 0x20) { + [json appendFormat:@"\\u%04x", uc]; + } else { + CFStringAppendCharacters((CFMutableStringRef)json, &uc, 1); + } + break; + + } + } + } + + [json appendString:@"\""]; + return YES; +} + + +@end diff --git a/UIImageView+AFNetworking.h b/UIImageView+AFNetworking.h new file mode 100644 index 0000000..bafb790 --- /dev/null +++ b/UIImageView+AFNetworking.h @@ -0,0 +1,78 @@ +// UIImageView+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 +#import "AFImageRequestOperation.h" + +#import + +#if __IPHONE_OS_VERSION_MIN_REQUIRED +#import + +/** + This category adds methods to the UIKit framework's `UIImageView` class. The methods in this category provide support for loading remote images asynchronously from a URL. + */ +@interface UIImageView (AFNetworking) + +/** + Creates and enqueues an image request operation, which asynchronously downloads the image from the specified URL, and sets it the request is finished. Any previous image request for the receiver will be cancelled. If the image is cached locally, the image is set immediately, otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished. + + By default, URL requests have a cache policy of `NSURLCacheStorageAllowed` and a timeout interval of 30 seconds, and are set not handle cookies. To configure URL requests differently, use `setImageWithURLRequest:placeholderImage:success:failure:` + + @param url The URL used for the image request. + */ +- (void)setImageWithURL:(NSURL *)url; + +/** + Creates and enqueues an image request operation, which asynchronously downloads the image from the specified URL. Any previous image request for the receiver will be cancelled. If the image is cached locally, the image is set immediately, otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished. + + By default, URL requests have a cache policy of `NSURLCacheStorageAllowed` and a timeout interval of 30 seconds, and are set not handle cookies. To configure URL requests differently, use `setImageWithURLRequest:placeholderImage:success:failure:` + + @param url The URL used for the image request. + @param placeholderImage The image to be set initially, until the image request finishes. If `nil`, the image view will not change its image until the image request finishes. + */ +- (void)setImageWithURL:(NSURL *)url + placeholderImage:(UIImage *)placeholderImage; + +/** + Creates and enqueues an image request operation, which asynchronously downloads the image with the specified URL request object. Any previous image request for the receiver will be cancelled. If the image is cached locally, the image is set immediately, otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished. + + If a success block is specified, it is the responsibility of the block to set the image of the image view before returning. If no success block is specified, the default behavior of setting the image with `self.image = image` is executed. + + @param urlRequest The URL request used for the image request. + @param placeholderImage The image to be set initially, until the image request finishes. If `nil`, the image view will not change its image until the image request finishes. + @param success A block to be executed when the image request operation finishes successfully, with a status code in the 2xx range, and with an acceptable content type (e.g. `image/png`). This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the image created from the response data of request. If the image was returned from cache, the request and response parameters will be `nil`. + @param failure A block object to be executed when the image request operation finishes unsuccessfully, or that finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the error object describing the network or parsing error that occurred. + */ +- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest + placeholderImage:(UIImage *)placeholderImage + success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success + failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure; + +/** + Cancels any executing image request operation for the receiver, if one exists. + */ +- (void)cancelImageRequestOperation; + +@end + +#endif diff --git a/UIImageView+AFNetworking.m b/UIImageView+AFNetworking.m new file mode 100644 index 0000000..b577cf1 --- /dev/null +++ b/UIImageView+AFNetworking.m @@ -0,0 +1,186 @@ +// UIImageView+AFNetworking.m +// +// 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 +#import + +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +#import "UIImageView+AFNetworking.h" + +@interface AFImageCache : NSCache +- (UIImage *)cachedImageForRequest:(NSURLRequest *)request; +- (void)cacheImage:(UIImage *)image + forRequest:(NSURLRequest *)request; +@end + +#pragma mark - + +static char kAFImageRequestOperationObjectKey; + +@interface UIImageView (_AFNetworking) +@property (readwrite, nonatomic, strong, setter = af_setImageRequestOperation:) AFImageRequestOperation *af_imageRequestOperation; +@end + +@implementation UIImageView (_AFNetworking) +@dynamic af_imageRequestOperation; +@end + +#pragma mark - + +@implementation UIImageView (AFNetworking) + +- (AFHTTPRequestOperation *)af_imageRequestOperation { + return (AFHTTPRequestOperation *)objc_getAssociatedObject(self, &kAFImageRequestOperationObjectKey); +} + +- (void)af_setImageRequestOperation:(AFImageRequestOperation *)imageRequestOperation { + objc_setAssociatedObject(self, &kAFImageRequestOperationObjectKey, imageRequestOperation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + ++ (NSOperationQueue *)af_sharedImageRequestOperationQueue { + static NSOperationQueue *_af_imageRequestOperationQueue = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + _af_imageRequestOperationQueue = [[NSOperationQueue alloc] init]; + [_af_imageRequestOperationQueue setMaxConcurrentOperationCount:NSOperationQueueDefaultMaxConcurrentOperationCount]; + }); + + return _af_imageRequestOperationQueue; +} + ++ (AFImageCache *)af_sharedImageCache { + static AFImageCache *_af_imageCache = nil; + static dispatch_once_t oncePredicate; + dispatch_once(&oncePredicate, ^{ + _af_imageCache = [[AFImageCache alloc] init]; + }); + + return _af_imageCache; +} + +#pragma mark - + +- (void)setImageWithURL:(NSURL *)url { + [self setImageWithURL:url placeholderImage:nil]; +} + +- (void)setImageWithURL:(NSURL *)url + placeholderImage:(UIImage *)placeholderImage +{ + NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; + [request addValue:@"image/*" forHTTPHeaderField:@"Accept"]; + + [self setImageWithURLRequest:request placeholderImage:placeholderImage success:nil failure:nil]; +} + +- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest + placeholderImage:(UIImage *)placeholderImage + success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success + failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure +{ + [self cancelImageRequestOperation]; + + UIImage *cachedImage = [[[self class] af_sharedImageCache] cachedImageForRequest:urlRequest]; + if (cachedImage) { + if (success) { + success(nil, nil, cachedImage); + } else { + self.image = cachedImage; + } + + self.af_imageRequestOperation = nil; + } else { + if (placeholderImage) { + self.image = placeholderImage; + } + + AFImageRequestOperation *requestOperation = [[AFImageRequestOperation alloc] initWithRequest:urlRequest]; + [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { + if ([urlRequest isEqual:[self.af_imageRequestOperation request]]) { + if (success) { + success(operation.request, operation.response, responseObject); + } else if (responseObject) { + self.image = responseObject; + } + + if (self.af_imageRequestOperation == operation) { + self.af_imageRequestOperation = nil; + } + } + + [[[self class] af_sharedImageCache] cacheImage:responseObject forRequest:urlRequest]; + } failure:^(AFHTTPRequestOperation *operation, NSError *error) { + if ([urlRequest isEqual:[self.af_imageRequestOperation request]]) { + if (failure) { + failure(operation.request, operation.response, error); + } + + if (self.af_imageRequestOperation == operation) { + self.af_imageRequestOperation = nil; + } + } + }]; + + self.af_imageRequestOperation = requestOperation; + + [[[self class] af_sharedImageRequestOperationQueue] addOperation:self.af_imageRequestOperation]; + } +} + +- (void)cancelImageRequestOperation { + [self.af_imageRequestOperation cancel]; + self.af_imageRequestOperation = nil; +} + +@end + +#pragma mark - + +static inline NSString * AFImageCacheKeyFromURLRequest(NSURLRequest *request) { + return [[request URL] absoluteString]; +} + +@implementation AFImageCache + +- (UIImage *)cachedImageForRequest:(NSURLRequest *)request { + switch ([request cachePolicy]) { + case NSURLRequestReloadIgnoringCacheData: + case NSURLRequestReloadIgnoringLocalAndRemoteCacheData: + return nil; + default: + break; + } + + return [self objectForKey:AFImageCacheKeyFromURLRequest(request)]; +} + +- (void)cacheImage:(UIImage *)image + forRequest:(NSURLRequest *)request +{ + if (image && request) { + [self setObject:image forKey:AFImageCacheKeyFromURLRequest(request)]; + } +} + +@end + +#endif