Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 93 lines (76 sloc) 3.779 kB
ac9c63a @macbury Working with pin code
authored
1 //
2 // NSMutableURLRequest+Parameters.m
3 //
4 // Created by Jon Crosby on 10/19/07.
5 // Copyright 2007 Kaboomerang LLC. All rights reserved.
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 // THE SOFTWARE.
24
25
26 #import "NSMutableURLRequest+Parameters.h"
27
28
29 @implementation NSMutableURLRequest (OAParameterAdditions)
30
31 - (NSArray *)parameters
32 {
33 NSString *encodedParameters;
34 BOOL shouldfree = NO;
35
36 if ([[self HTTPMethod] isEqualToString:@"GET"] || [[self HTTPMethod] isEqualToString:@"DELETE"])
37 encodedParameters = [[self URL] query];
38 else
39 {
40 // POST, PUT
41 shouldfree = YES;
42 encodedParameters = [[NSString alloc] initWithData:[self HTTPBody] encoding:NSASCIIStringEncoding];
43 }
44
45 if ((encodedParameters == nil) || ([encodedParameters isEqualToString:@""]))
46 return nil;
47
48 NSArray *encodedParameterPairs = [encodedParameters componentsSeparatedByString:@"&"];
49 NSMutableArray *requestParameters = [[NSMutableArray alloc] initWithCapacity:16];
50
51 for (NSString *encodedPair in encodedParameterPairs)
52 {
53 NSArray *encodedPairElements = [encodedPair componentsSeparatedByString:@"="];
54 OARequestParameter *parameter = [OARequestParameter requestParameterWithName:[[encodedPairElements objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
55 value:[[encodedPairElements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
56 [requestParameters addObject:parameter];
57 }
58
59 // Cleanup
60 if (shouldfree)
61 [encodedParameters release];
62
63 return [requestParameters autorelease];
64 }
65
66 - (void)setParameters:(NSArray *)parameters
67 {
68 NSMutableString *encodedParameterPairs = [NSMutableString stringWithCapacity:256];
69
70 int position = 1;
71 for (OARequestParameter *requestParameter in parameters)
72 {
73 [encodedParameterPairs appendString:[requestParameter URLEncodedNameValuePair]];
74 if (position < [parameters count])
75 [encodedParameterPairs appendString:@"&"];
76
77 position++;
78 }
79
80 if ([[self HTTPMethod] isEqualToString:@"GET"] || [[self HTTPMethod] isEqualToString:@"DELETE"])
81 [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@", [[self URL] URLStringWithoutQuery], encodedParameterPairs]]];
82 else
83 {
84 // POST, PUT
85 NSData *postData = [encodedParameterPairs dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
86 [self setHTTPBody:postData];
87 [self setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
88 [self setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
89 }
90 }
91
92 @end
Something went wrong with that request. Please try again.