-
Notifications
You must be signed in to change notification settings - Fork 19
/
PKMultipartInputStream.m
executable file
·326 lines (302 loc) · 12.3 KB
/
PKMultipartInputStream.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// PKMultipartInputStream.h
#import <UIKit/UIKit.h>
#import <MobileCoreServices/UTType.h>
#import "PKMultipartInputStream.h"
#define kHeaderStringFormat @"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n"
#define kHeaderDataFormat @"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\nContent-Type: %@\r\n\r\n"
#define kHeaderPathFormat @"--%@\r\nContent-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\nContent-Type: %@\r\n\r\n"
#define kFooterFormat @"--%@--\r\n"
static NSString * MIMETypeForExtension(NSString * extension) {
CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)extension, NULL);
if (uti != NULL)
{
CFStringRef mime = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType);
CFRelease(uti);
if (mime != NULL)
{
NSString *type = [NSString stringWithString:(__bridge NSString *)mime];
CFRelease(mime);
return type;
}
}
return @"application/octet-stream";
}
@interface PKMultipartElement : NSObject
@property (nonatomic, strong) NSData *headers;
@property (nonatomic, strong) NSInputStream *body;
@property (nonatomic) NSUInteger headersLength, bodyLength, length, delivered;
@end
@implementation PKMultipartElement
- (void)updateLength
{
self.length = self.headersLength + self.bodyLength + 2;
[self.body open];
}
- (id)initWithName:(NSString *)name boundary:(NSString *)boundary string:(NSString *)string
{
self = [super init];
self.headers = [[NSString stringWithFormat:kHeaderStringFormat, boundary, name] dataUsingEncoding:NSUTF8StringEncoding];
self.headersLength = [self.headers length];
NSData *stringData = [string dataUsingEncoding:NSUTF8StringEncoding];
self.body = [NSInputStream inputStreamWithData:stringData];
self.bodyLength = stringData.length;
[self updateLength];
return self;
}
- (id)initWithName:(NSString *)name boundary:(NSString *)boundary data:(NSData *)data contentType:(NSString *)contentType
{
self = [super init];
self.headers = [[NSString stringWithFormat:kHeaderDataFormat, boundary, name, contentType] dataUsingEncoding:NSUTF8StringEncoding];
self.headersLength = [self.headers length];
self.body = [NSInputStream inputStreamWithData:data];
self.bodyLength = [data length];
[self updateLength];
return self;
}
- (id)initWithName:(NSString *)name boundary:(NSString *)boundary data:(NSData *)data contentType:(NSString *)contentType filename:(NSString*)filename
{
self = [super init];
self.headers = [[NSString stringWithFormat:kHeaderPathFormat, boundary, name, filename, contentType] dataUsingEncoding:NSUTF8StringEncoding];
self.headersLength = [self.headers length];
self.body = [NSInputStream inputStreamWithData:data];
self.bodyLength = [data length];
[self updateLength];
return self;
}
- (id)initWithName:(NSString *)name filename:(NSString *)filename boundary:(NSString *)boundary path:(NSString *)path
{
return [self initWithName:name filename:filename boundary:boundary path:path contentType:MIMETypeForExtension(path.pathExtension)];
}
- (id)initWithName:(NSString *)name filename:(NSString *)filename boundary:(NSString *)boundary path:(NSString *)path contentType:(NSString *)contentType
{
self = [super init];
if (self) {
if (!filename)
{
filename = path.lastPathComponent;
}
self.headers = [[NSString stringWithFormat:kHeaderPathFormat, boundary, name, filename, contentType] dataUsingEncoding:NSUTF8StringEncoding];
self.headersLength = [self.headers length];
self.body = [NSInputStream inputStreamWithFileAtPath:path];
self.bodyLength = [[[[NSFileManager defaultManager] attributesOfItemAtPath:path error:NULL] objectForKey:NSFileSize] unsignedIntegerValue];
[self updateLength];
}
return self;
}
- (id)initWithName:(NSString *)name filename:(NSString *)filename boundary:(NSString *)boundary stream:(NSInputStream *)stream streamLength:(NSUInteger)streamLength
{
self = [super init];
if (self) {
self.headers = [[NSString stringWithFormat:kHeaderPathFormat, boundary, name, filename, MIMETypeForExtension(filename.pathExtension)] dataUsingEncoding:NSUTF8StringEncoding];
self.headersLength = [self.headers length];
self.body = stream;
self.bodyLength = streamLength;
[self updateLength];
}
return self;
}
- (id)initWithHeaders:(NSDictionary *)headers string:(NSString *)string boundary:(NSString *)boundary
{
self = [super init];
if (self) {
_headers = [self makeHeadersDataFromHeadersDict:headers boundary:boundary];
_headersLength = _headers.length;
NSData *stringData = [string dataUsingEncoding:NSUTF8StringEncoding];
_body = [NSInputStream inputStreamWithData:stringData];
_bodyLength = stringData.length;
[self updateLength];
}
return self;
}
- (id)initWithHeaders:(NSDictionary *)headers path:(NSString *)path boundary:(NSString *)boundary
{
self = [super init];
if (self) {
_headers = [self makeHeadersDataFromHeadersDict:headers boundary:boundary];
_headersLength = _headers.length;
_body = [NSInputStream inputStreamWithFileAtPath:path];
_bodyLength = [[[[NSFileManager defaultManager] attributesOfItemAtPath:path error:NULL] objectForKey:NSFileSize] unsignedIntegerValue];
[self updateLength];
}
return self;
}
- (NSData *)makeHeadersDataFromHeadersDict:(NSDictionary *)headers boundary:(NSString *)boundary
{
NSMutableString *headersString = [[NSMutableString alloc] initWithFormat:@"--%@", boundary];
[self appendNewLine:headersString];
for (NSString *key in headers.allKeys) {
[headersString appendString:[[NSString alloc] initWithFormat:@"%@: %@", key, headers[key]]];
[self appendNewLine:headersString];
}
[self appendNewLine:headersString];
NSData *result = [headersString dataUsingEncoding:NSUTF8StringEncoding];
return result;
}
- (void)appendNewLine:(NSMutableString *)string {
[string appendString:@"\r\n"];
}
- (NSUInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len
{
NSUInteger sent = 0, read;
if (self.delivered >= self.length)
{
return 0;
}
if (self.delivered < self.headersLength && sent < len)
{
read = MIN(self.headersLength - self.delivered, len - sent);
[self.headers getBytes:buffer + sent range:NSMakeRange(self.delivered, read)];
sent += read;
self.delivered += sent;
}
while (self.delivered >= self.headersLength && self.delivered < (self.length - 2) && sent < len)
{
if ((read = [self.body read:buffer + sent maxLength:len - sent]) == 0)
{
break;
}
sent += read;
self.delivered += read;
}
if (self.delivered >= (self.length - 2) && sent < len)
{
if (self.delivered == (self.length - 2))
{
*(buffer + sent) = '\r';
sent ++; self.delivered ++;
}
*(buffer + sent) = '\n';
sent ++; self.delivered ++;
}
return sent;
}
@end
@interface PKMultipartInputStream()
@property (nonatomic, strong) NSMutableArray *parts;
@property (nonatomic, strong, nonnull) NSString *boundary;
@property (nonatomic, strong) NSData *footer;
@property (nonatomic) NSUInteger currentPart, delivered, length;
@property (nonatomic) NSStreamStatus status;
@end
@implementation PKMultipartInputStream
@synthesize delegate;
- (void)updateLength
{
self.length = self.footer.length + [[self.parts valueForKeyPath:@"@sum.length"] unsignedIntegerValue];
}
- (id)init
{
self = [super init];
if (self)
{
self.parts = [NSMutableArray array];
self.boundary = [[NSProcessInfo processInfo] globallyUniqueString];
self.footer = [[NSString stringWithFormat:kFooterFormat, self.boundary] dataUsingEncoding:NSUTF8StringEncoding];
[self updateLength];
}
return self;
}
- (void)addPartWithName:(NSString *)name string:(NSString *)string
{
[self.parts addObject:[[PKMultipartElement alloc] initWithName:name boundary:self.boundary string:string]];
[self updateLength];
}
- (void)addPartWithName:(NSString *)name data:(NSData *)data
{
[self.parts addObject:[[PKMultipartElement alloc] initWithName:name boundary:self.boundary data:data contentType:@"application/octet-stream"]];
[self updateLength];
}
- (void)addPartWithName:(NSString *)name data:(NSData *)data contentType:(NSString *)type
{
[self.parts addObject:[[PKMultipartElement alloc] initWithName:name boundary:self.boundary data:data contentType:type]];
[self updateLength];
}
- (void)addPartWithName:(NSString *)name filename:(NSString*)filename data:(NSData *)data contentType:(NSString *)type
{
[self.parts addObject:[[PKMultipartElement alloc] initWithName:name boundary:self.boundary data:data contentType:type filename:filename]];
[self updateLength];
}
- (void)addPartWithName:(NSString *)name path:(NSString *)path
{
[self.parts addObject:[[PKMultipartElement alloc] initWithName:name filename:nil boundary:self.boundary path:path]];
[self updateLength];
}
- (void)addPartWithName:(NSString *)name filename:(NSString *)filename path:(NSString *)path
{
[self.parts addObject:[[PKMultipartElement alloc] initWithName:name filename:filename boundary:self.boundary path:path]];
[self updateLength];
}
- (void)addPartWithName:(NSString *)name filename:(NSString *)filename path:(NSString *)path contentType:(NSString *)type
{
[self.parts addObject:[[PKMultipartElement alloc] initWithName:name filename:filename boundary:self.boundary path:path contentType:type]];
[self updateLength];
}
- (void)addPartWithName:(NSString *)name filename:(NSString *)filename stream:(NSInputStream *)stream streamLength:(NSUInteger)streamLength
{
[self.parts addObject:[[PKMultipartElement alloc] initWithName:name filename:filename boundary:self.boundary stream:stream streamLength:streamLength]];
[self updateLength];
}
- (void)addPartWithHeaders:(NSDictionary *)headers string:(NSString *)string
{
[self.parts addObject:[[PKMultipartElement alloc] initWithHeaders:headers string:string boundary:self.boundary]];
[self updateLength];
}
- (void)addPartWithHeaders:(NSDictionary *)headers path:(NSString *)path
{
[self.parts addObject:[[PKMultipartElement alloc] initWithHeaders:headers path:path boundary:self.boundary]];
[self updateLength];
}
- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len
{
NSUInteger sent = 0, read;
self.status = NSStreamStatusReading;
while (self.delivered < self.length && sent < len && self.currentPart < self.parts.count)
{
if ((read = [[self.parts objectAtIndex:self.currentPart] read:(buffer + sent) maxLength:(len - sent)]) == 0)
{
self.currentPart ++;
continue;
}
sent += read;
self.delivered += read;
}
if (self.delivered >= (self.length - self.footer.length) && sent < len)
{
read = MIN(self.footer.length - (self.delivered - (self.length - self.footer.length)), len - sent);
[self.footer getBytes:buffer + sent range:NSMakeRange(self.delivered - (self.length - self.footer.length), read)];
sent += read;
self.delivered += read;
}
return sent;
}
- (BOOL)hasBytesAvailable
{
return self.delivered < self.length;
}
- (void)open
{
self.status = NSStreamStatusOpen;
}
- (void)close
{
self.status = NSStreamStatusClosed;
}
- (NSStreamStatus)streamStatus
{
if (self.status != NSStreamStatusClosed && self.delivered >= self.length)
{
self.status = NSStreamStatusAtEnd;
}
return self.status;
}
- (void)_scheduleInCFRunLoop:(NSRunLoop *)runLoop forMode:(id)mode {}
- (void)_setCFClientFlags:(CFOptionFlags)flags callback:(CFReadStreamClientCallBack)callback context:(CFStreamClientContext)context {}
- (void)removeFromRunLoop:(__unused NSRunLoop *)aRunLoop forMode:(__unused NSString *)mode {}
- (id)propertyForKey:(NSStreamPropertyKey)key {
return nil;
}
- (BOOL)setProperty:(id)property forKey:(NSStreamPropertyKey)key {
return NO;
}
@end