Skip to content
Browse files

URL query escaping is in

  • Loading branch information...
1 parent 2763939 commit 5f2dbc73e88236aaead0fc46f393d4181d668ce2 @probablycorey committed Jun 29, 2010
Showing with 104 additions and 5 deletions.
  1. +16 −0 Seriously.xcodeproj/project.pbxproj
  2. +15 −5 src/Seriously.m
  3. +18 −0 src/SeriouslyUtils.h
  4. +55 −0 src/SeriouslyUtils.m
View
16 Seriously.xcodeproj/project.pbxproj
@@ -34,6 +34,8 @@
87E24AEE11D952E600B9C33B /* SeriouslyOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 87E24AD311D952E600B9C33B /* SeriouslyOperation.m */; };
87E24B5511D9566500B9C33B /* SeriouslyResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = 87E24B5311D9566500B9C33B /* SeriouslyResponse.h */; };
87E24B5611D9566500B9C33B /* SeriouslyResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = 87E24B5411D9566500B9C33B /* SeriouslyResponse.m */; };
+ 87E5E0DD11DA75D100D334D5 /* SeriouslyUtils.h in Headers */ = {isa = PBXBuildFile; fileRef = 87E5E0DB11DA75D100D334D5 /* SeriouslyUtils.h */; };
+ 87E5E0DE11DA75D100D334D5 /* SeriouslyUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 87E5E0DC11DA75D100D334D5 /* SeriouslyUtils.m */; };
AA747D9F0F9514B9006C5449 /* Seriously_Prefix.pch in Headers */ = {isa = PBXBuildFile; fileRef = AA747D9E0F9514B9006C5449 /* Seriously_Prefix.pch */; };
AACBBE4A0F95108600F1A2B1 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AACBBE490F95108600F1A2B1 /* Foundation.framework */; };
/* End PBXBuildFile section */
@@ -66,6 +68,9 @@
87E24AD311D952E600B9C33B /* SeriouslyOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SeriouslyOperation.m; sourceTree = "<group>"; };
87E24B5311D9566500B9C33B /* SeriouslyResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SeriouslyResponse.h; sourceTree = "<group>"; };
87E24B5411D9566500B9C33B /* SeriouslyResponse.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SeriouslyResponse.m; sourceTree = "<group>"; };
+ 87E24DFA11DA6B9C00B9C33B /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
+ 87E5E0DB11DA75D100D334D5 /* SeriouslyUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SeriouslyUtils.h; sourceTree = "<group>"; };
+ 87E5E0DC11DA75D100D334D5 /* SeriouslyUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SeriouslyUtils.m; sourceTree = "<group>"; };
AA747D9E0F9514B9006C5449 /* Seriously_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Seriously_Prefix.pch; sourceTree = SOURCE_ROOT; };
AACBBE490F95108600F1A2B1 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
D2AAC07E0554694100DB518D /* libSeriously.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libSeriously.a; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -87,6 +92,7 @@
isa = PBXGroup;
children = (
D2AAC07E0554694100DB518D /* libSeriously.a */,
+ 87E24DFA11DA6B9C00B9C33B /* Info.plist */,
);
name = Products;
sourceTree = "<group>";
@@ -128,6 +134,8 @@
87E24B5411D9566500B9C33B /* SeriouslyResponse.m */,
87E24AD211D952E600B9C33B /* SeriouslyOperation.h */,
87E24AD311D952E600B9C33B /* SeriouslyOperation.m */,
+ 87E5E0DB11DA75D100D334D5 /* SeriouslyUtils.h */,
+ 87E5E0DC11DA75D100D334D5 /* SeriouslyUtils.m */,
);
name = Classes;
path = src;
@@ -201,6 +209,7 @@
87E24AEB11D952E600B9C33B /* Seriously.h in Headers */,
87E24AED11D952E600B9C33B /* SeriouslyOperation.h in Headers */,
87E24B5511D9566500B9C33B /* SeriouslyResponse.h in Headers */,
+ 87E5E0DD11DA75D100D334D5 /* SeriouslyUtils.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -258,6 +267,7 @@
87E24AEC11D952E600B9C33B /* Seriously.m in Sources */,
87E24AEE11D952E600B9C33B /* SeriouslyOperation.m in Sources */,
87E24B5611D9566500B9C33B /* SeriouslyResponse.m in Sources */,
+ 87E5E0DE11DA75D100D334D5 /* SeriouslyUtils.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -270,6 +280,7 @@
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
COPY_PHASE_STRIP = NO;
+ CURRENT_PROJECT_VERSION = 0.1;
DSTROOT = /tmp/Seriously.dst;
GCC_DYNAMIC_NO_PIC = NO;
GCC_ENABLE_FIX_AND_CONTINUE = YES;
@@ -288,6 +299,7 @@
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
+ CURRENT_PROJECT_VERSION = 0.1;
DSTROOT = /tmp/Seriously.dst;
GCC_MODEL_TUNING = G5;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
@@ -306,9 +318,11 @@
GCC_OPTIMIZATION_LEVEL = 0;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
+ INFOPLIST_FILE = Info.plist;
OTHER_LDFLAGS = "-ObjC";
PREBINDING = NO;
SDKROOT = iphoneos4.0;
+ VERSIONING_SYSTEM = "apple-generic";
};
name = Debug;
};
@@ -319,9 +333,11 @@
GCC_C_LANGUAGE_STANDARD = c99;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
+ INFOPLIST_FILE = Info.plist;
OTHER_LDFLAGS = "-ObjC";
PREBINDING = NO;
SDKROOT = iphoneos4.0;
+ VERSIONING_SYSTEM = "apple-generic";
};
name = Release;
};
View
20 src/Seriously.m
@@ -8,6 +8,7 @@
#import "Seriously.h"
#import "SeriouslyOperation.h"
+#import "SeriouslyUtils.h"
const NSString *kSeriouslyMethod = @"kSeriouslyMethod";
const NSString *kSeriouslyTimeout = @"kSeriouslyTimeout";
@@ -18,18 +19,27 @@
@implementation Seriously
+ (SeriouslyOperation *)request:(id)url options:(NSDictionary *)userOptions handler:(SeriouslyHandler)handler {
- if ([url isKindOfClass:[NSString class]]) url = [NSURL URLWithString:url];
-
NSMutableDictionary *options = [self options];
[options addEntriesFromDictionary:userOptions];
NSURLRequestCachePolicy cachePolicy = NSURLRequestUseProtocolCachePolicy;
NSTimeInterval timeout = 60;
- NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:cachePolicy timeoutInterval:timeout];
+ NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:nil cachePolicy:cachePolicy timeoutInterval:timeout];
+
[urlRequest setHTTPMethod:[[options objectForKey:kSeriouslyMethod] uppercaseString]];
[urlRequest setTimeoutInterval:[[options objectForKey:kSeriouslyTimeout] doubleValue]];
[urlRequest setAllHTTPHeaderFields:[options objectForKey:kSeriouslyHeaders]];
- [urlRequest setHTTPBody:[options objectForKey:kSeriouslyBody]];
+
+ if ([[urlRequest HTTPMethod] isEqual:@"POST"] || [[urlRequest HTTPMethod] isEqual:@"PUT"]) {
+ url = [SeriouslyUtils url:url params:nil];
+ [urlRequest setHTTPBody:[options objectForKey:kSeriouslyBody]];
+ }
+ else {
+ url = [SeriouslyUtils url:url params:[options objectForKey:kSeriouslyBody]];
+ }
+
+ [urlRequest setURL:url];
+
SeriouslyProgressHandler progressHandler = [options objectForKey:kSeriouslyProgressHandler];
SeriouslyOperation *operation = [SeriouslyOperation operationWithRequest:urlRequest handler:handler progressHandler:progressHandler];
@@ -52,7 +62,7 @@ + (NSOperationQueue *)operationQueue {
if (!operationQueue) {
operationQueue = [[NSOperationQueue alloc] init];
- operationQueue.maxConcurrentOperationCount = 1;
+ operationQueue.maxConcurrentOperationCount = 3;
}
return operationQueue;
View
18 src/SeriouslyUtils.h
@@ -0,0 +1,18 @@
+//
+// SeriouslyUtils.h
+// Seriously
+//
+// Created by Corey Johnson on 6/29/10.
+// Copyright 2010 __MyCompanyName__. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+
+@interface SeriouslyUtils : NSObject {}
+
++ (NSURL *)url:(id)url params:(id)params;
++ (NSString *)formatQueryParams:(id)params;
++ (NSString *)escapeQueryParam:(NSString *)string;
+
+@end
View
55 src/SeriouslyUtils.m
@@ -0,0 +1,55 @@
+//
+// SeriouslyUtils.m
+// Seriously
+//
+// Created by Corey Johnson on 6/29/10.
+// Copyright 2010 __MyCompanyName__. All rights reserved.
+//
+
+#import "SeriouslyUtils.h"
+
+
+@implementation SeriouslyUtils
+
++ (NSURL *)url:(id)url params:(id)params {
+ if ([url isKindOfClass:[NSString class]]) url = [NSURL URLWithString:url];
+ if (!params) {
+ return url;
+ }
+
+ NSString *urlString = [NSString stringWithFormat:@"%@?%@", [url absoluteString], params];
+ return [NSURL URLWithString:urlString];
+}
+
++ (NSString *)formatQueryParams:(id)params {
+ if (![params isKindOfClass:[NSDictionary class]]) return params;
+
+ NSMutableArray *pairs = [NSMutableArray array];
+ for (id key in params) {
+ id value = [params objectForKey:key];
+
+ if ([value isKindOfClass:[NSArray class]]) {
+ for (id v in value) {
+ [pairs addObject:[NSString stringWithFormat:@"%@[]=%@", key, [self escapeQueryParam:v]]];
+ }
+ }
+ else {
+ [pairs addObject:[NSString stringWithFormat:@"%@=%@",key, [self escapeQueryParam:value]]];
+ }
+ }
+
+ return [pairs componentsJoinedByString:@"&"];
+}
+
++ (NSString *)escapeQueryParam:(NSString *)string {
+ CFStringRef escaped = CFURLCreateStringByAddingPercentEscapes(
+ kCFAllocatorDefault,
+ (CFStringRef)string,
+ NULL,
+ (CFStringRef)@":/?=,!$&'()*+;[]@#",
+ CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
+
+ return (NSString *)escaped;
+}
+
+@end

0 comments on commit 5f2dbc7

Please sign in to comment.
Something went wrong with that request. Please try again.