forked from omz/AppSales-Mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyHTTPConnection.m
190 lines (149 loc) · 6.62 KB
/
MyHTTPConnection.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
//
// This class was created by Nonnus,
// who graciously decided to share it with the CocoaHTTPServer community.
//
#import "MyHTTPConnection.h"
#import "HTTPServer.h"
#import "HTTPResponse.h"
#import "AsyncSocket.h"
#import "SFHFKeychainUtils.h"
@implementation MyHTTPConnection
- (BOOL)isBrowseable:(NSString *)path
{
return YES;
}
- (NSString *)createBrowseableIndex:(NSString *)path
{
//TODO: Localize the import/export html
NSMutableString *page = [NSMutableString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ImportTemplate" ofType:@"html"] usedEncoding:NULL error:NULL];
NSMutableString *reportsList = [NSMutableString string];
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
if ([files count] == 0) {
[reportsList appendString:NSLocalizedString(@"<i>No reports downloaded yet</i>",nil)];
} else {
for (NSString *file in files) {
[reportsList appendFormat:@"<a href=\"%@\">%@</a><br/>", file, file];
}
}
[page replaceOccurrencesOfString:@"[[[REPORTFILES]]]" withString:reportsList options:0 range:NSMakeRange(0,[page length])];
return [NSString stringWithString:page];
}
- (BOOL)supportsMethod:(NSString *)method atPath:(NSString *)relativePath
{
if ([method isEqual:@"POST"])
return [self supportsPOST:relativePath withSize:0];
return [super supportsMethod:method atPath:relativePath];
}
- (BOOL)supportsPOST:(NSString *)path withSize:(UInt64)contentLength
{
dataStartIndex = 0;
multipartData = [[NSMutableArray alloc] init];
postHeaderOK = FALSE;
return YES;
}
- (BOOL)isPasswordProtected:(NSString *)path
{
return YES;
}
- (NSString *)passwordForUser:(NSString *)username
{
// Security Note:
// A nil password means no access at all. (Such as for user doesn't exist)
// An empty string password is allowed, and will be treated as any other password. (To support anonymous access)
NSString *iTunesConnectUsername = [[NSUserDefaults standardUserDefaults] stringForKey:@"iTunesConnectUsername"];
NSString *password = nil;
if (iTunesConnectUsername) {
password = [SFHFKeychainUtils getPasswordForUsername:username andServiceName:@"omz:software AppSales Mobile Service" error:NULL];
return password;
}
return nil;
}
- (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path
{
if (requestContentLength > 0) { // Process POST data
if ([multipartData count] < 2) return nil;
NSString *postInfo = [[[NSString alloc] initWithData:[multipartData objectAtIndex:1] encoding:NSUTF8StringEncoding] autorelease];
if (!postInfo) postInfo = [[[NSString alloc] initWithData:[multipartData objectAtIndex:1] encoding:NSASCIIStringEncoding] autorelease];
NSArray* postInfoComponents = [postInfo componentsSeparatedByString:@"; filename="];
postInfoComponents = [[postInfoComponents lastObject] componentsSeparatedByString:@"\""];
postInfoComponents = [[postInfoComponents objectAtIndex:1] componentsSeparatedByString:@"\\"];
NSString* filename = [postInfoComponents lastObject];
if (![filename isEqualToString:@""]) //this makes sure we did not submitted upload form without selecting file
{
UInt16 separatorBytes = 0x0A0D;
NSMutableData* separatorData = [NSMutableData dataWithBytes:&separatorBytes length:2];
[separatorData appendData:[multipartData objectAtIndex:0]];
int l = [separatorData length];
int count = 2; //number of times the separator shows up at the end of file data
NSFileHandle* dataToTrim = [multipartData lastObject];
for (unsigned long long i = [dataToTrim offsetInFile] - l; i > 0; i--) {
[dataToTrim seekToFileOffset:i];
if ([[dataToTrim readDataOfLength:l] isEqualToData:separatorData])
{
[dataToTrim truncateFileAtOffset:i];
i -= l;
if (--count == 0) break;
}
}
[[NSNotificationCenter defaultCenter] postNotificationName:NewFileUploadedNotification object:filename];
}
[multipartData release];
requestContentLength = 0;
}
NSString *filePath = [self filePathForURI:path];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
return [[[HTTPFileResponse alloc] initWithFilePath:filePath] autorelease];
}
else {
NSString *folder = [path isEqualToString:@"/"] ? [[server documentRoot] path] : [NSString stringWithFormat: @"%@%@", [[server documentRoot] path], path];
if ([self isBrowseable:folder]) {
NSData *browseData = [[self createBrowseableIndex:folder] dataUsingEncoding:NSUTF8StringEncoding];
return [[[HTTPDataResponse alloc] initWithData:browseData] autorelease];
}
}
return nil;
}
- (void)processDataChunk:(NSData *)postDataChunk
{
if (!postHeaderOK) {
UInt16 separatorBytes = 0x0A0D;
NSData* separatorData = [NSData dataWithBytes:&separatorBytes length:2];
int l = [separatorData length];
for (unsigned int i = 0; i < [postDataChunk length] - l; i++) {
NSRange searchRange = {i, l};
if ([[postDataChunk subdataWithRange:searchRange] isEqualToData:separatorData]) {
NSRange newDataRange = {dataStartIndex, i - dataStartIndex};
dataStartIndex = i + l;
i += l - 1;
NSData *newData = [postDataChunk subdataWithRange:newDataRange];
if ([newData length]) {
[multipartData addObject:newData];
}
else {
postHeaderOK = TRUE;
NSString* postInfo = [[[NSString alloc] initWithBytes:[[multipartData objectAtIndex:1] bytes] length:[[multipartData objectAtIndex:1] length] encoding:NSUTF8StringEncoding] autorelease];
if (!postInfo) {
postInfo = [[[NSString alloc] initWithData:[multipartData objectAtIndex:1] encoding:NSUTF8StringEncoding] autorelease];
postInfo = [[[NSString alloc] initWithBytes:[[multipartData objectAtIndex:1] bytes] length:[[multipartData objectAtIndex:1] length] encoding:NSASCIIStringEncoding] autorelease];
}
NSArray* postInfoComponents = [postInfo componentsSeparatedByString:@"; filename="];
postInfoComponents = [[postInfoComponents lastObject] componentsSeparatedByString:@"\""];
postInfoComponents = [[postInfoComponents objectAtIndex:1] componentsSeparatedByString:@"\\"];
NSString* filename = [server.uploadPath stringByAppendingPathComponent:[postInfoComponents lastObject]];
NSRange fileDataRange = {dataStartIndex, [postDataChunk length] - dataStartIndex};
[[NSFileManager defaultManager] createFileAtPath:filename contents:[postDataChunk subdataWithRange:fileDataRange] attributes:nil];
NSFileHandle *file = [NSFileHandle fileHandleForUpdatingAtPath:filename];
if (file) {
[file seekToEndOfFile];
[multipartData addObject:file];
}
break;
}
}
}
}
else {
[(NSFileHandle*)[multipartData lastObject] writeData:postDataChunk];
}
}
@end