forked from eczarny/xmlrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLRPCRequest.m
165 lines (126 loc) · 4.01 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
#import "XMLRPCRequest.h"
#import "XMLRPCEncoder.h"
#import "XMLRPCDefaultEncoder.h"
@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
}
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 {
//Copy the old method and parameters to the new 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];
}
#pragma mark -
- (NSString *)method {
return [myXMLEncoder method];
}
- (NSArray *)parameters {
return [myXMLEncoder parameters];
}
#pragma mark -
- (NSString *)body {
return [myXMLEncoder encode];
}
#pragma mark -
- (NSURLRequest *)request {
NSData *content = [[self body] dataUsingEncoding: NSUTF8StringEncoding];
NSNumber *contentLength = [NSNumber numberWithInt: [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 -
- (void)dealloc {
#if ! __has_feature(objc_arc)
[myRequest release];
[myXMLEncoder release];
[super dealloc];
#endif
}
@end