This repository has been archived by the owner on Apr 15, 2019. It is now read-only.
forked from eczarny/xmlrpc
-
Notifications
You must be signed in to change notification settings - Fork 29
/
XMLRPCRequest.m
195 lines (147 loc) · 4.41 KB
/
XMLRPCRequest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#import "XMLRPCRequest.h"
#import "XMLRPCEncoder.h"
#import "XMLRPCDefaultEncoder.h"
static const NSTimeInterval DEFAULT_TIMEOUT = 240;
@implementation XMLRPCRequest
- (id)initWithURL: (NSURL *)URL withEncoder: (id<XMLRPCEncoder>)encoder {
self = [super init];
if (self) {
if (URL) {
myRequest = [[NSMutableURLRequest alloc] initWithURL: URL];
} else {
myRequest = [[NSMutableURLRequest alloc] init];
}
myXMLEncoder = encoder;
#if ! __has_feature(objc_arc)
[myXMLEncoder retain];
#endif
myTimeout = DEFAULT_TIMEOUT;
}
return self;
}
- (id)initWithURL: (NSURL *)URL {
#if ! __has_feature(objc_arc)
return [self initWithURL:URL withEncoder:[[[XMLRPCDefaultEncoder alloc] init] autorelease]];
#else
return [self initWithURL:URL withEncoder:[[XMLRPCDefaultEncoder alloc] init]];
#endif
}
#pragma mark -
- (void)setURL: (NSURL *)URL {
[myRequest setURL: URL];
}
- (NSURL *)URL {
return [myRequest URL];
}
#pragma mark -
- (void)setUserAgent: (NSString *)userAgent {
if (![self userAgent]) {
[myRequest addValue: userAgent forHTTPHeaderField: @"User-Agent"];
} else {
[myRequest setValue: userAgent forHTTPHeaderField: @"User-Agent"];
}
}
- (NSString *)userAgent {
return [myRequest valueForHTTPHeaderField: @"User-Agent"];
}
#pragma mark -
- (void)setEncoder:(id<XMLRPCEncoder>)encoder {
NSString *method = [myXMLEncoder method];
NSArray *parameters = [myXMLEncoder parameters];
#if ! __has_feature(objc_arc)
[myXMLEncoder release];
myXMLEncoder = [encoder retain];
#else
myXMLEncoder = encoder;
#endif
[myXMLEncoder setMethod: method withParameters: parameters];
}
- (void)setMethod: (NSString *)method {
[myXMLEncoder setMethod: method withParameters: nil];
}
- (void)setMethod: (NSString *)method withParameter: (id)parameter {
NSArray *parameters = nil;
if (parameter) {
parameters = [NSArray arrayWithObject: parameter];
}
[myXMLEncoder setMethod: method withParameters: parameters];
}
- (void)setMethod: (NSString *)method withParameters: (NSArray *)parameters {
[myXMLEncoder setMethod: method withParameters: parameters];
}
- (void)setTimeoutInterval: (NSTimeInterval)timeout
{
myTimeout = timeout;
}
#pragma mark -
- (NSString *)method {
return [myXMLEncoder method];
}
- (NSArray *)parameters {
return [myXMLEncoder parameters];
}
- (NSTimeInterval)timeout
{
return myTimeout;
}
#pragma mark -
- (NSString *)body {
return [myXMLEncoder encode];
}
#pragma mark -
- (NSURLRequest *)request {
NSData *content = [[self body] dataUsingEncoding: NSUTF8StringEncoding];
NSNumber *contentLength = [NSNumber numberWithUnsignedInteger:[content length]];
if (!myRequest) {
return nil;
}
[myRequest setHTTPMethod: @"POST"];
if (![myRequest valueForHTTPHeaderField: @"Content-Type"]) {
[myRequest addValue: @"text/xml" forHTTPHeaderField: @"Content-Type"];
} else {
[myRequest setValue: @"text/xml" forHTTPHeaderField: @"Content-Type"];
}
if (![myRequest valueForHTTPHeaderField: @"Content-Length"]) {
[myRequest addValue: [contentLength stringValue] forHTTPHeaderField: @"Content-Length"];
} else {
[myRequest setValue: [contentLength stringValue] forHTTPHeaderField: @"Content-Length"];
}
if (![myRequest valueForHTTPHeaderField: @"Accept"]) {
[myRequest addValue: @"text/xml" forHTTPHeaderField: @"Accept"];
} else {
[myRequest setValue: @"text/xml" forHTTPHeaderField: @"Accept"];
}
if (![self userAgent]) {
NSString *userAgent = [[NSUserDefaults standardUserDefaults] objectForKey:@"UserAgent"];
if (userAgent) {
[self setUserAgent: userAgent];
}
}
[myRequest setHTTPBody: content];
return (NSURLRequest *)myRequest;
}
#pragma mark -
- (void)setValue: (NSString *)value forHTTPHeaderField: (NSString *)header {
[myRequest setValue: value forHTTPHeaderField: header];
}
#pragma mark -
- (id) extra {
return extra;
}
- (void) setExtra:(id) extraObject {
#if ! __has_feature(objc_arc)
[extra release];
extra = [extraObject retain];
#else
extra = extraObject;
#endif
}
#pragma mark -
- (void)dealloc {
#if ! __has_feature(objc_arc)
[myRequest release];
[myXMLEncoder release];
[super dealloc];
#endif
}
@end