Permalink
Browse files

Add in extensions by Alberto Hierro (http://fi.am/)

  • Loading branch information...
1 parent 1274318 commit 01ce025e01c89a84364d21b0b32de6907c80d666 @jdg jdg committed Oct 30, 2008
@@ -29,7 +29,9 @@
@interface NSMutableURLRequest (OAParameterAdditions)
-- (NSArray *)parameters;
-- (void)setParameters:(NSArray *)parameters;
+@property(nonatomic, retain) NSArray *parameters;
+
+- (void)setHTTPBodyWithString:(NSString *)body;
+- (void)attachFileWithName:(NSString *)name filename:(NSString*)filename contentType:(NSString *)contentType data:(NSData*)data;
@end
@@ -25,25 +25,31 @@
#import "NSMutableURLRequest+Parameters.h"
+static NSString *Boundary = @"-----------------------------------0xCoCoaouTHeBouNDaRy";
@implementation NSMutableURLRequest (OAParameterAdditions)
+- (BOOL)isMultipart {
+ return [[self valueForHTTPHeaderField:@"Content-Type"] hasPrefix:@"multipart/form-data"];
+}
+
- (NSArray *)parameters {
- NSString *encodedParameters;
+ NSString *encodedParameters = nil;
- if ([[self HTTPMethod] isEqualToString:@"GET"] || [[self HTTPMethod] isEqualToString:@"DELETE"]) {
- encodedParameters = [[self URL] query];
- } else {
- // POST, PUT
- encodedParameters = [[NSString alloc] initWithData:[self HTTPBody] encoding:NSASCIIStringEncoding];
- }
+ if (![self isMultipart]) {
+ if ([[self HTTPMethod] isEqualToString:@"GET"] || [[self HTTPMethod] isEqualToString:@"DELETE"]) {
+ encodedParameters = [[self URL] query];
+ } else {
+ encodedParameters = [[[NSString alloc] initWithData:[self HTTPBody] encoding:NSASCIIStringEncoding] autorelease];
+ }
+ }
- if ((encodedParameters == nil) || ([encodedParameters isEqualToString:@""])) {
+ if (encodedParameters == nil || [encodedParameters isEqualToString:@""]) {
return nil;
}
-
+ NSLog(@"raw parameters %@", encodedParameters);
NSArray *encodedParameterPairs = [encodedParameters componentsSeparatedByString:@"&"];
- NSMutableArray *requestParameters = [[NSMutableArray alloc] initWithCapacity:16];
+ NSMutableArray *requestParameters = [NSMutableArray arrayWithCapacity:[encodedParameterPairs count]];
for (NSString *encodedPair in encodedParameterPairs) {
NSArray *encodedPairElements = [encodedPair componentsSeparatedByString:@"="];
@@ -55,27 +61,52 @@ - (NSArray *)parameters {
return requestParameters;
}
-- (void)setParameters:(NSArray *)parameters {
- NSMutableString *encodedParameterPairs = [[NSMutableString alloc] initWithCapacity:256];
+- (void)setParameters:(NSArray *)parameters
+{
+ NSMutableArray *pairs = [[[NSMutableArray alloc] initWithCapacity:[parameters count]] autorelease];
+ for (OARequestParameter *requestParameter in parameters) {
+ [pairs addObject:[requestParameter URLEncodedNameValuePair]];
+ }
+
+ NSString *encodedParameterPairs = [pairs componentsJoinedByString:@"&"];
- int position = 1;
- for (OARequestParameter *requestParameter in parameters) {
- [encodedParameterPairs appendString:[requestParameter URLEncodedNameValuePair]];
- if (position < [parameters count]) {
- [encodedParameterPairs appendString:@"&"];
- }
- position++;
- }
-
- if ([[self HTTPMethod] isEqualToString:@"GET"] || [[self HTTPMethod] isEqualToString:@"DELETE"]) {
- [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@", [[self URL] URLStringWithoutQuery], encodedParameterPairs]]];
- } else {
- // POST, PUT
- NSData *postData = [encodedParameterPairs dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
- [self setHTTPBody:postData];
- [self setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
- [self setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
- }
+ if ([[self HTTPMethod] isEqualToString:@"GET"] || [[self HTTPMethod] isEqualToString:@"DELETE"]) {
+ [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@", [[self URL] URLStringWithoutQuery], encodedParameterPairs]]];
+ } else {
+ // POST, PUT
+ [self setHTTPBodyWithString:encodedParameterPairs];
+ [self setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
+ }
+}
+
+- (void)setHTTPBodyWithString:(NSString *)body {
+ NSData *bodyData = [body dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
+ [self setValue:[NSString stringWithFormat:@"%d", [bodyData length]] forHTTPHeaderField:@"Content-Length"];
+ [self setHTTPBody:bodyData];
+}
+
+- (void)attachFileWithName:(NSString *)name filename:(NSString*)filename contentType:(NSString *)contentType data:(NSData*)data {
+
+ NSArray *parameters = [self parameters];
+ [self setValue:[@"multipart/form-data; boundary=" stringByAppendingString:Boundary] forHTTPHeaderField:@"Content-type"];
+
+ NSMutableData *bodyData = [NSMutableData new];
+ for (OARequestParameter *parameter in parameters) {
+ NSString *param = [NSString stringWithFormat:@"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n%@\r\n",
+ Boundary, [parameter URLEncodedName], [parameter value]];
+
+ [bodyData appendData:[param dataUsingEncoding:NSUTF8StringEncoding]];
+ }
+
+ NSString *filePrefix = [NSString stringWithFormat:@"--%@\r\nContent-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\nContent-Type: %@\r\n\r\n",
+ Boundary, name, filename, contentType];
+ [bodyData appendData:[filePrefix dataUsingEncoding:NSUTF8StringEncoding]];
+ [bodyData appendData:data];
+
+ [bodyData appendData:[[[@"--" stringByAppendingString:Boundary] stringByAppendingString:@"--"] dataUsingEncoding:NSUTF8StringEncoding]];
+ [self setValue:[NSString stringWithFormat:@"%d", [bodyData length]] forHTTPHeaderField:@"Content-Length"];
+ [self setHTTPBody:bodyData];
+ [bodyData release];
}
@end
@@ -30,5 +30,6 @@
- (NSString *)encodedURLString;
- (NSString *)encodedURLParameterString;
-
+- (NSString *)decodedURLString;
+- (NSString *)removeQuotes;
@end
@@ -34,7 +34,7 @@ - (NSString *)encodedURLString {
NULL, // characters to leave unescaped (NULL = all escaped sequences are replaced)
CFSTR("?=&+"), // legal URL characters to be escaped (NULL = all legal characters are replaced)
kCFStringEncodingUTF8); // encoding
- return result;
+ return [result autorelease];
}
- (NSString *)encodedURLParameterString {
@@ -43,7 +43,31 @@ - (NSString *)encodedURLParameterString {
NULL,
CFSTR(":/=,!$&'()*+;[]@#?"),
kCFStringEncodingUTF8);
- return result;
+ return [result autorelease];
+}
+
+- (NSString *)decodedURLString {
+ NSString *result = (NSString*)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault,
+ (CFStringRef)self,
+ CFSTR(""),
+ kCFStringEncodingUTF8);
+
+ return [result autorelease];
+
+}
+
+-(NSString *)removeQuotes
+{
+ NSUInteger length = [self length];
+ NSString *ret = self;
+ if ([self characterAtIndex:0] == '"') {
+ ret = [ret substringFromIndex:1];
+ }
+ if ([self characterAtIndex:length - 1] == '"') {
+ ret = [ret substringToIndex:length - 2];
+ }
+
+ return ret;
}
@end
View
@@ -0,0 +1,63 @@
+//
+// OACall.h
+// OAuthConsumer
+//
+// Created by Alberto García Hierro on 04/09/08.
+// Copyright 2008 Alberto García Hierro. All rights reserved.
+// bynotes.com
+
+#import <Foundation/Foundation.h>
+
+@class OAProblem;
+@class OACall;
+
+@protocol OACallDelegate
+
+- (void)call:(OACall *)call failedWithError:(NSError *)error;
+- (void)call:(OACall *)call failedWithProblem:(OAProblem *)problem;
+
+@end
+
+@class OAConsumer;
+@class OAToken;
+@class OADataFetcher;
+@class OAMutableURLRequest;
+@class OAServiceTicket;
+
+@interface OACall : NSObject {
+ NSURL *url;
+ NSString *method;
+ NSArray *parameters;
+ NSDictionary *files;
+ NSObject <OACallDelegate> *delegate;
+ SEL finishedSelector;
+ OADataFetcher *fetcher;
+ OAMutableURLRequest *request;
+ OAServiceTicket *ticket;
+}
+
+@property(readonly) NSURL *url;
+@property(readonly) NSString *method;
+@property(readonly) NSArray *parameters;
+@property(readonly) NSDictionary *files;
+@property(nonatomic, retain) OAServiceTicket *ticket;
+
+- (id)init;
+- (id)initWithURL:(NSURL *)aURL;
+- (id)initWithURL:(NSURL *)aURL method:(NSString *)aMethod;
+- (id)initWithURL:(NSURL *)aURL parameters:(NSArray *)theParameters;
+- (id)initWithURL:(NSURL *)aURL method:(NSString *)aMethod parameters:(NSArray *)theParameters;
+- (id)initWithURL:(NSURL *)aURL parameters:(NSArray *)theParameters files:(NSDictionary*)theFiles;
+
+- (id)initWithURL:(NSURL *)aURL
+ method:(NSString *)aMethod
+ parameters:(NSArray *)theParameters
+ files:(NSDictionary*)theFiles;
+
+- (void)perform:(OAConsumer *)consumer
+ token:(OAToken *)token
+ realm:(NSString *)realm
+ delegate:(NSObject <OACallDelegate> *)aDelegate
+ didFinish:(SEL)finished;
+
+@end
Oops, something went wrong. Retry.

0 comments on commit 01ce025

Please sign in to comment.