/
XMLRPCRequest.m
173 lines (138 loc) · 3.78 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
//
// Cocoa XML-RPC Client Framework
// XMLRPCConnection.m
//
// Created by Eric J. Czarny on Thu Jan 15 2004.
// Copyright (c) 2004 Divisible by Zero.
//
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#import "XMLRPCRequest.h"
#import "XMLRPCEncoder.h"
@implementation XMLRPCRequest
- (id)initWithHost: (NSURL *)host
{
if (self = [super init])
{
if (host != nil)
{
_request = [[NSMutableURLRequest alloc] initWithURL: host];
}
else
{
_request = [[NSMutableURLRequest alloc] init];
}
_encoder = [[XMLRPCEncoder alloc] init];
[_request setTimeoutInterval:120.0f];
// TODO: this should not be kept in the generic classes.
NSString *version = [[[NSBundle mainBundle] infoDictionary] valueForKey:[NSString stringWithFormat:@"CFBundleVersion"]];
[self setUserAgent:[NSString stringWithFormat:@"wp-iphone/%@",version]];
}
return self;
}
#pragma mark -
- (void)setHost: (NSURL *)host
{
[_request setURL: host];
}
- (NSURL *)host
{
return [_request URL];
}
#pragma mark -
- (void)setUserAgent: (NSString *)agent
{
if ([self userAgent] == nil)
{
[_request addValue: agent forHTTPHeaderField: @"User-Agent"];
}
else
{
[_request setValue: agent forHTTPHeaderField: @"User-Agent"];
}
}
- (NSString *)userAgent
{
return [_request valueForHTTPHeaderField: @"User-Agent"];
}
#pragma mark -
- (void)setMethod: (NSString *)method withObject: (id)object
{
[_encoder setMethod: method withObjects: [NSArray arrayWithObject: object]];
}
- (void)setMethod: (NSString *)method withObjects: (NSArray *)objects
{
[_encoder setMethod: method withObjects: objects];
}
#pragma mark -
- (NSString *)method
{
return [_encoder method];
}
- (NSArray *)objects
{
return [_encoder objects];
}
#pragma mark -
- (NSString *)source
{
return [_encoder source];
}
#pragma mark -
- (NSURLRequest *)request
{
NSData *request = [[_encoder encode] dataUsingEncoding: NSUTF8StringEncoding];
NSNumber *length = [NSNumber numberWithInt: [request length]];
if (request == nil)
{
return nil;
}
[_request setHTTPMethod: @"POST"];
if ([_request valueForHTTPHeaderField: @"Content-Length"] == nil)
{
[_request addValue: @"text/xml" forHTTPHeaderField: @"Content-Type"];
}
else
{
[_request setValue: @"text/xml" forHTTPHeaderField: @"Content-Type"];
}
if ([_request valueForHTTPHeaderField: @"Content-Length"] == nil)
{
[_request addValue: [length stringValue] forHTTPHeaderField: @"Content-Length"];
}
else
{
[_request setValue: [length stringValue] forHTTPHeaderField: @"Content-Length"];
}
[_request setHTTPBody: request];
return (NSURLRequest *)_request;
}
- (NSString *)description {
return [NSString stringWithFormat:@"\nhost: %@\nuser-agent: %@\nmethod: %@\nobjects:%@\nsource:%@",
self.host, self.userAgent, self.method, self.objects, self.source];
}
#pragma mark -
- (void)dealloc
{
[_request release];
[_encoder release];
[super dealloc];
}
@end