This repository has been archived by the owner on May 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
/
WebServer.m
218 lines (183 loc) · 8.33 KB
/
WebServer.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
// Copyright (C) 2010-2016 Pierre-Olivier Latour <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#import "AppDelegate.h"
#import "Defaults.h"
#define kDisconnectLatency 1.0
@interface WebsiteServer : GCDWebUploader
@end
@interface WebDAVServer : GCDWebDAVServer
@end
@implementation WebsiteServer
- (BOOL) shouldUploadFileAtPath:(NSString*)path withTemporaryFile:(NSString*)tempPath {
if ([[NSUserDefaults standardUserDefaults] integerForKey:kDefaultKey_ServerMode] == kServerMode_Limited) {
XLOG_ERROR(@"Upload rejected: web server is in limited mode");
return NO;
}
return YES;
}
@end
@implementation WebDAVServer
- (BOOL) shouldUploadFileAtPath:(NSString*)path withTemporaryFile:(NSString*)tempPath {
if ([[NSUserDefaults standardUserDefaults] integerForKey:kDefaultKey_ServerMode] == kServerMode_Limited) {
XLOG_ERROR(@"Upload rejected: web server is in limited mode");
return NO;
}
return YES;
}
@end
@implementation WebServer
@synthesize delegate=_delegate, type=_type;
+ (void) initialize {
NSMutableDictionary* defaults = [[NSMutableDictionary alloc] init];
[defaults setObject:[NSNumber numberWithInteger:kWebServerType_Website] forKey:kDefaultKey_ServerType];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
[defaults release];
}
+ (WebServer*) sharedWebServer {
XLOG_DEBUG_CHECK([NSThread isMainThread]);
static WebServer* server = nil;
if (server == nil) {
server = [[WebServer alloc] init];
server.type = (WebServerType)[[NSUserDefaults standardUserDefaults] integerForKey:kDefaultKey_ServerType];
}
return server;
}
- (void) setType:(WebServerType)type {
if (type != _type) {
if (_type != kWebServerType_Off) {
[_webServer stop];
[_webServer release];
_webServer = nil;
_type = kWebServerType_Off;
}
if (type != kWebServerType_Off) {
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSArray* fileExtensions = [NSArray arrayWithObjects:@"pdf", @"zip", @"cbz", @"rar", @"cbr", nil];
if (type == kWebServerType_Website) {
_webServer = [[WebsiteServer alloc] initWithUploadDirectory:documentsPath];
[(WebsiteServer*)_webServer setAllowedFileExtensions:fileExtensions];
[(WebsiteServer*)_webServer setTitle:NSLocalizedString(@"SERVER_TITLE", nil)];
[(WebsiteServer*)_webServer setPrologue:[NSString stringWithFormat:NSLocalizedString(@"SERVER_CONTENT", nil), [fileExtensions componentsJoinedByString:@", "]]];
if ([[NSUserDefaults standardUserDefaults] integerForKey:kDefaultKey_ServerMode] != kServerMode_Full) {
[(WebsiteServer*)_webServer setPrologue:[[(WebsiteServer*)_webServer prologue] stringByAppendingFormat:NSLocalizedString(@"SERVER_LIMITED_CONTENT", nil), kTrialMaxUploads]];
}
[(WebsiteServer*)_webServer setFooter:[NSString stringWithFormat:NSLocalizedString(@"SERVER_FOOTER_FORMAT", nil),
[[UIDevice currentDevice] name],
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]]];
} else if (type == kWebServerType_WebDAV) {
_webServer = [[WebDAVServer alloc] initWithUploadDirectory:documentsPath];
[(WebDAVServer*)_webServer setAllowedFileExtensions:fileExtensions];
}
if (_webServer) {
_webServer.delegate = self;
NSMutableDictionary* options = [NSMutableDictionary dictionary];
#if TARGET_IPHONE_SIMULATOR
[options setObject:[NSNumber numberWithInteger:8080] forKey:GCDWebServerOption_Port];
#else
[options setObject:[NSNumber numberWithInteger:80] forKey:GCDWebServerOption_Port];
#endif
[options setObject:@"" forKey:GCDWebServerOption_BonjourName];
NSString* name = [NSString stringWithFormat:NSLocalizedString(@"SERVER_NAME_FORMAT", nil),
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"],
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]];
[options setObject:name forKey:GCDWebServerOption_ServerName];
[options setObject:[NSNumber numberWithDouble:kDisconnectLatency] forKey:GCDWebServerOption_ConnectedStateCoalescingInterval];
NSError* error = nil;
BOOL success = [_webServer startWithOptions:options error:&error];
#if !TARGET_IPHONE_SIMULATOR
if (!success && [error.domain isEqualToString:NSPOSIXErrorDomain] && (error.code == 48)) {
XLOG_WARNING(@"Server port 80 is busy, trying alternate port 8080");
[options setObject:@8080 forKey:GCDWebServerOption_Port];
success = [_webServer startWithOptions:options error:&error];
}
#endif
if (success) {
_type = type;
} else {
[_webServer release];
_webServer = nil;
}
}
}
[[NSUserDefaults standardUserDefaults] setInteger:_type forKey:kDefaultKey_ServerType];
}
}
- (NSString*) addressLabel {
NSURL* serverURL = _webServer.serverURL;
NSURL* bonjourServerURL = _webServer.bonjourServerURL;
switch (_type) {
case kWebServerType_Off:
break;
case kWebServerType_Website:
if (serverURL) {
if (bonjourServerURL) {
return [NSString stringWithFormat:NSLocalizedString(@"ADDRESS_WEBSITE_BONJOUR", nil), [bonjourServerURL absoluteString], [serverURL absoluteString]];
} else {
return [NSString stringWithFormat:NSLocalizedString(@"ADDRESS_WEBSITE_IP", nil), [serverURL absoluteString]];
}
}
break;
case kWebServerType_WebDAV:
if (serverURL) {
if (bonjourServerURL) {
return [NSString stringWithFormat:NSLocalizedString(@"ADDRESS_WEBDAV_BONJOUR", nil), [bonjourServerURL absoluteString], [serverURL absoluteString]];
} else {
return [NSString stringWithFormat:NSLocalizedString(@"ADDRESS_WEBDAV_IP", nil), [serverURL absoluteString]];
}
}
break;
}
return NSLocalizedString(@"ADDRESS_UNAVAILABLE", nil);
}
- (void) webServerDidConnect:(GCDWebServer*)server {
[_delegate webServerDidConnect:self];
}
- (void) webServerDidDisconnect:(GCDWebServer*)server {
[_delegate webServerDidDisconnect:self];
}
- (void) webUploader:(GCDWebUploader*)uploader didDownloadFileAtPath:(NSString*)path {
[_delegate webServerDidDownloadComic:self];
}
- (void) webUploader:(GCDWebUploader*)uploader didUploadFileAtPath:(NSString*)path {
[_delegate webServerDidUploadComic:self];
}
- (void) webUploader:(GCDWebUploader*)uploader didMoveItemFromPath:(NSString*)fromPath toPath:(NSString*)toPath {
[_delegate webServerDidUpdate:self];
}
- (void) webUploader:(GCDWebUploader*)uploader didDeleteItemAtPath:(NSString*)path {
[_delegate webServerDidUpdate:self];
}
- (void) webUploader:(GCDWebUploader*)uploader didCreateDirectoryAtPath:(NSString*)path {
[_delegate webServerDidUpdate:self];
}
- (void) davServer:(GCDWebDAVServer*)server didDownloadFileAtPath:(NSString*)path {
[_delegate webServerDidDownloadComic:self];
}
- (void) davServer:(GCDWebDAVServer*)server didUploadFileAtPath:(NSString*)path {
[_delegate webServerDidUploadComic:self];
}
- (void) davServer:(GCDWebDAVServer*)server didMoveItemFromPath:(NSString*)fromPath toPath:(NSString*)toPath {
[_delegate webServerDidUpdate:self];
}
- (void) davServer:(GCDWebDAVServer*)server didCopyItemFromPath:(NSString*)fromPath toPath:(NSString*)toPath {
[_delegate webServerDidUpdate:self];
}
- (void) davServer:(GCDWebDAVServer*)server didDeleteItemAtPath:(NSString*)path {
[_delegate webServerDidUpdate:self];
}
- (void) davServer:(GCDWebDAVServer*)server didCreateDirectoryAtPath:(NSString*)path {
[_delegate webServerDidUpdate:self];
}
@end