Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 182 lines (134 sloc) 4.356 kb
9ae54ee @eczarny Initial commit.
authored
1 #import "XMLRPCRequest.h"
2 #import "XMLRPCEncoder.h"
83326c5 @Milyardo Implement support for user specified xml encoders.
Milyardo authored
3 #import "XMLRPCDefaultEncoder.h"
9ae54ee @eczarny Initial commit.
authored
4
94968e0 @eczarny ARC should now be enabled by default.
authored
5 #define kDefaultTimeoutInterval 240;
6
7 #pragma mark -
69951ce @corristo actually added timeout support. NSURLConnection ignores timeouts less th...
corristo authored
8
9ae54ee @eczarny Initial commit.
authored
9 @implementation XMLRPCRequest
10
83326c5 @Milyardo Implement support for user specified xml encoders.
Milyardo authored
11 - (id)initWithURL: (NSURL *)URL withEncoder: (id<XMLRPCEncoder>)encoder {
a11816d fixed a few warnings
Thomas Post authored
12 self = [super init];
13 if (self) {
9ae54ee @eczarny Initial commit.
authored
14 if (URL) {
15 myRequest = [[NSMutableURLRequest alloc] initWithURL: URL];
16 } else {
17 myRequest = [[NSMutableURLRequest alloc] init];
18 }
19
b975ead @drbobbeaty Added ARC Support
drbobbeaty authored
20 myXMLEncoder = encoder;
21 #if ! __has_feature(objc_arc)
22 [myXMLEncoder retain];
23 #endif
69951ce @corristo actually added timeout support. NSURLConnection ignores timeouts less th...
corristo authored
24
94968e0 @eczarny ARC should now be enabled by default.
authored
25 myTimeoutInterval = kDefaultTimeoutInterval;
9ae54ee @eczarny Initial commit.
authored
26 }
27
28 return self;
29 }
30
83326c5 @Milyardo Implement support for user specified xml encoders.
Milyardo authored
31 - (id)initWithURL: (NSURL *)URL {
b975ead @drbobbeaty Added ARC Support
drbobbeaty authored
32 #if ! __has_feature(objc_arc)
a1345d5 @eczarny Forgot to include this minor style change in my last commit.
authored
33 return [self initWithURL: URL withEncoder: [[[XMLRPCDefaultEncoder alloc] init] autorelease]];
b975ead @drbobbeaty Added ARC Support
drbobbeaty authored
34 #else
a1345d5 @eczarny Forgot to include this minor style change in my last commit.
authored
35 return [self initWithURL: URL withEncoder: [[XMLRPCDefaultEncoder alloc] init]];
b975ead @drbobbeaty Added ARC Support
drbobbeaty authored
36 #endif
83326c5 @Milyardo Implement support for user specified xml encoders.
Milyardo authored
37 }
38
9ae54ee @eczarny Initial commit.
authored
39 #pragma mark -
40
41 - (void)setURL: (NSURL *)URL {
42 [myRequest setURL: URL];
43 }
44
45 - (NSURL *)URL {
46 return [myRequest URL];
47 }
48
49 #pragma mark -
50
51 - (void)setUserAgent: (NSString *)userAgent {
52 if (![self userAgent]) {
53 [myRequest addValue: userAgent forHTTPHeaderField: @"User-Agent"];
54 } else {
55 [myRequest setValue: userAgent forHTTPHeaderField: @"User-Agent"];
56 }
57 }
58
59 - (NSString *)userAgent {
60 return [myRequest valueForHTTPHeaderField: @"User-Agent"];
61 }
62
63 #pragma mark -
64
83326c5 @Milyardo Implement support for user specified xml encoders.
Milyardo authored
65 - (void)setEncoder:(id<XMLRPCEncoder>)encoder {
66 NSString *method = [myXMLEncoder method];
67 NSArray *parameters = [myXMLEncoder parameters];
b975ead @drbobbeaty Added ARC Support
drbobbeaty authored
68 #if ! __has_feature(objc_arc)
83326c5 @Milyardo Implement support for user specified xml encoders.
Milyardo authored
69 [myXMLEncoder release];
e436dae @eczarny Cleaned up some code, fixed a couple warnings, and upgraded to the 10.7 ...
authored
70
83326c5 @Milyardo Implement support for user specified xml encoders.
Milyardo authored
71 myXMLEncoder = [encoder retain];
b975ead @drbobbeaty Added ARC Support
drbobbeaty authored
72 #else
73 myXMLEncoder = encoder;
74 #endif
e436dae @eczarny Cleaned up some code, fixed a couple warnings, and upgraded to the 10.7 ...
authored
75
76 [myXMLEncoder setMethod: method withParameters: parameters];
83326c5 @Milyardo Implement support for user specified xml encoders.
Milyardo authored
77 }
78
9ae54ee @eczarny Initial commit.
authored
79 - (void)setMethod: (NSString *)method {
80 [myXMLEncoder setMethod: method withParameters: nil];
81 }
82
83 - (void)setMethod: (NSString *)method withParameter: (id)parameter {
5a91241 @eczarny Prevent crashing if nil is provided as an XML-RPC request parameter.
authored
84 NSArray *parameters = nil;
85
86 if (parameter) {
87 parameters = [NSArray arrayWithObject: parameter];
88 }
89
90 [myXMLEncoder setMethod: method withParameters: parameters];
9ae54ee @eczarny Initial commit.
authored
91 }
92
93 - (void)setMethod: (NSString *)method withParameters: (NSArray *)parameters {
94 [myXMLEncoder setMethod: method withParameters: parameters];
95 }
96
94968e0 @eczarny ARC should now be enabled by default.
authored
97 - (void)setTimeoutInterval: (NSTimeInterval)timeoutInterval {
98 myTimeoutInterval = timeoutInterval;
b9cb20c @corristo Now one can set timeout on XMLRPCRequest
corristo authored
99 }
100
9ae54ee @eczarny Initial commit.
authored
101 #pragma mark -
102
103 - (NSString *)method {
104 return [myXMLEncoder method];
105 }
106
107 - (NSArray *)parameters {
108 return [myXMLEncoder parameters];
109 }
110
94968e0 @eczarny ARC should now be enabled by default.
authored
111 - (NSTimeInterval)timeoutInterval {
112 return myTimeoutInterval;
69951ce @corristo actually added timeout support. NSURLConnection ignores timeouts less th...
corristo authored
113 }
114
9ae54ee @eczarny Initial commit.
authored
115 #pragma mark -
116
117 - (NSString *)body {
118 return [myXMLEncoder encode];
119 }
120
121 #pragma mark -
122
123 - (NSURLRequest *)request {
124 NSData *content = [[self body] dataUsingEncoding: NSUTF8StringEncoding];
125 NSNumber *contentLength = [NSNumber numberWithInt: [content length]];
126
127 if (!myRequest) {
128 return nil;
129 }
130
131 [myRequest setHTTPMethod: @"POST"];
132
7d80136 @eczarny Fixed a typo.
authored
133 if (![myRequest valueForHTTPHeaderField: @"Content-Type"]) {
9ae54ee @eczarny Initial commit.
authored
134 [myRequest addValue: @"text/xml" forHTTPHeaderField: @"Content-Type"];
135 } else {
136 [myRequest setValue: @"text/xml" forHTTPHeaderField: @"Content-Type"];
137 }
138
139 if (![myRequest valueForHTTPHeaderField: @"Content-Length"]) {
140 [myRequest addValue: [contentLength stringValue] forHTTPHeaderField: @"Content-Length"];
141 } else {
142 [myRequest setValue: [contentLength stringValue] forHTTPHeaderField: @"Content-Length"];
143 }
144
22affe5 @koke Added Accept header to XMLRPCRequest
koke authored
145 if (![myRequest valueForHTTPHeaderField: @"Accept"]) {
146 [myRequest addValue: @"text/xml" forHTTPHeaderField: @"Accept"];
147 } else {
148 [myRequest setValue: @"text/xml" forHTTPHeaderField: @"Accept"];
149 }
150
19be12b @koke Use default User-Agent
koke authored
151 if (![self userAgent]) {
152 NSString *userAgent = [[NSUserDefaults standardUserDefaults] objectForKey:@"UserAgent"];
e436dae @eczarny Cleaned up some code, fixed a couple warnings, and upgraded to the 10.7 ...
authored
153
19be12b @koke Use default User-Agent
koke authored
154 if (userAgent) {
e436dae @eczarny Cleaned up some code, fixed a couple warnings, and upgraded to the 10.7 ...
authored
155 [self setUserAgent: userAgent];
19be12b @koke Use default User-Agent
koke authored
156 }
157 }
158
9ae54ee @eczarny Initial commit.
authored
159 [myRequest setHTTPBody: content];
160
161 return (NSURLRequest *)myRequest;
162 }
163
64219a3 @koke Added support for custom headers
koke authored
164 #pragma mark -
165
5bd8ed0 @eczarny Stripping out the redundant copies of the license.
authored
166 - (void)setValue: (NSString *)value forHTTPHeaderField: (NSString *)header {
167 [myRequest setValue: value forHTTPHeaderField: header];
168 }
169
9ae54ee @eczarny Initial commit.
authored
170 #pragma mark -
171
172 - (void)dealloc {
b975ead @drbobbeaty Added ARC Support
drbobbeaty authored
173 #if ! __has_feature(objc_arc)
9ae54ee @eczarny Initial commit.
authored
174 [myRequest release];
175 [myXMLEncoder release];
176
177 [super dealloc];
b975ead @drbobbeaty Added ARC Support
drbobbeaty authored
178 #endif
9ae54ee @eczarny Initial commit.
authored
179 }
180
181 @end
Something went wrong with that request. Please try again.