main.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. Copyright (c) 2012-2014, Pierre-Olivier Latour
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * The name of Pierre-Olivier Latour may not be used to endorse
  12. or promote products derived from this software without specific
  13. prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL PIERRE-OLIVIER LATOUR BE LIABLE FOR ANY
  18. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #import <libgen.h>
  26. #import "GCDWebServer.h"
  27. #import "GCDWebServerDataRequest.h"
  28. #import "GCDWebServerURLEncodedFormRequest.h"
  29. #import "GCDWebServerDataResponse.h"
  30. #import "GCDWebServerStreamedResponse.h"
  31. #import "GCDWebDAVServer.h"
  32. #import "GCDWebUploader.h"
  33. #ifndef __GCDWEBSERVER_ENABLE_TESTING__
  34. #error __GCDWEBSERVER_ENABLE_TESTING__ must be defined
  35. #endif
  36. typedef enum {
  37. kMode_WebServer = 0,
  38. kMode_HTMLPage,
  39. kMode_HTMLForm,
  40. kMode_WebDAV,
  41. kMode_WebUploader,
  42. kMode_StreamingResponse
  43. } Mode;
  44. @interface Delegate : NSObject <GCDWebServerDelegate, GCDWebDAVServerDelegate, GCDWebUploaderDelegate>
  45. @end
  46. @implementation Delegate
  47. - (void)_logDelegateCall:(SEL)selector {
  48. fprintf(stdout, "<DELEGATE METHOD \"%s\" CALLED>\n", [NSStringFromSelector(selector) UTF8String]);
  49. }
  50. - (void)webServerDidStart:(GCDWebServer*)server {
  51. [self _logDelegateCall:_cmd];
  52. }
  53. - (void)webServerDidCompleteBonjourRegistration:(GCDWebServer*)server {
  54. [self _logDelegateCall:_cmd];
  55. }
  56. - (void)webServerDidConnect:(GCDWebServer*)server {
  57. [self _logDelegateCall:_cmd];
  58. }
  59. - (void)webServerDidDisconnect:(GCDWebServer*)server {
  60. [self _logDelegateCall:_cmd];
  61. }
  62. - (void)webServerDidStop:(GCDWebServer*)server {
  63. [self _logDelegateCall:_cmd];
  64. }
  65. - (void)davServer:(GCDWebDAVServer*)server didDownloadFileAtPath:(NSString*)path {
  66. [self _logDelegateCall:_cmd];
  67. }
  68. - (void)davServer:(GCDWebDAVServer*)server didUploadFileAtPath:(NSString*)path {
  69. [self _logDelegateCall:_cmd];
  70. }
  71. - (void)davServer:(GCDWebDAVServer*)server didMoveItemFromPath:(NSString*)fromPath toPath:(NSString*)toPath {
  72. [self _logDelegateCall:_cmd];
  73. }
  74. - (void)davServer:(GCDWebDAVServer*)server didCopyItemFromPath:(NSString*)fromPath toPath:(NSString*)toPath {
  75. [self _logDelegateCall:_cmd];
  76. }
  77. - (void)davServer:(GCDWebDAVServer*)server didDeleteItemAtPath:(NSString*)path {
  78. [self _logDelegateCall:_cmd];
  79. }
  80. - (void)davServer:(GCDWebDAVServer*)server didCreateDirectoryAtPath:(NSString*)path {
  81. [self _logDelegateCall:_cmd];
  82. }
  83. - (void)webUploader:(GCDWebUploader*)uploader didDownloadFileAtPath:(NSString*)path {
  84. [self _logDelegateCall:_cmd];
  85. }
  86. - (void)webUploader:(GCDWebUploader*)uploader didUploadFileAtPath:(NSString*)path {
  87. [self _logDelegateCall:_cmd];
  88. }
  89. - (void)webUploader:(GCDWebUploader*)uploader didMoveItemFromPath:(NSString*)fromPath toPath:(NSString*)toPath {
  90. [self _logDelegateCall:_cmd];
  91. }
  92. - (void)webUploader:(GCDWebUploader*)uploader didDeleteItemAtPath:(NSString*)path {
  93. [self _logDelegateCall:_cmd];
  94. }
  95. - (void)webUploader:(GCDWebUploader*)uploader didCreateDirectoryAtPath:(NSString*)path {
  96. [self _logDelegateCall:_cmd];
  97. }
  98. @end
  99. int main(int argc, const char* argv[]) {
  100. int result = -1;
  101. @autoreleasepool {
  102. Mode mode = kMode_WebServer;
  103. BOOL recording = NO;
  104. NSString* rootDirectory = NSHomeDirectory();
  105. NSString* testDirectory = nil;
  106. NSString* authenticationMethod = nil;
  107. NSString* authenticationRealm = nil;
  108. NSString* authenticationUser = nil;
  109. NSString* authenticationPassword = nil;
  110. if (argc == 1) {
  111. fprintf(stdout, "Usage: %s [-mode webServer | htmlPage | htmlForm | webDAV | webUploader | streamingResponse] [-record] [-root directory] [-tests directory] [-authenticationMethod Basic | Digest] [-authenticationRealm realm] [-authenticationUser user] [-authenticationPassword password]\n\n", basename((char*)argv[0]));
  112. } else {
  113. for (int i = 1; i < argc; ++i) {
  114. if (argv[i][0] != '-') {
  115. continue;
  116. }
  117. if (!strcmp(argv[i], "-mode") && (i + 1 < argc)) {
  118. ++i;
  119. if (!strcmp(argv[i], "webServer")) {
  120. mode = kMode_WebServer;
  121. } else if (!strcmp(argv[i], "htmlPage")) {
  122. mode = kMode_HTMLPage;
  123. } else if (!strcmp(argv[i], "htmlForm")) {
  124. mode = kMode_HTMLForm;
  125. } else if (!strcmp(argv[i], "webDAV")) {
  126. mode = kMode_WebDAV;
  127. } else if (!strcmp(argv[i], "webUploader")) {
  128. mode = kMode_WebUploader;
  129. } else if (!strcmp(argv[i], "streamingResponse")) {
  130. mode = kMode_StreamingResponse;
  131. }
  132. } else if (!strcmp(argv[i], "-record")) {
  133. recording = YES;
  134. } else if (!strcmp(argv[i], "-root") && (i + 1 < argc)) {
  135. ++i;
  136. rootDirectory = [[[NSFileManager defaultManager] stringWithFileSystemRepresentation:argv[i] length:strlen(argv[i])] stringByStandardizingPath];
  137. } else if (!strcmp(argv[i], "-tests") && (i + 1 < argc)) {
  138. ++i;
  139. testDirectory = [[[NSFileManager defaultManager] stringWithFileSystemRepresentation:argv[i] length:strlen(argv[i])] stringByStandardizingPath];
  140. } else if (!strcmp(argv[i], "-authenticationMethod") && (i + 1 < argc)) {
  141. ++i;
  142. authenticationMethod = [NSString stringWithUTF8String:argv[i]];
  143. } else if (!strcmp(argv[i], "-authenticationRealm") && (i + 1 < argc)) {
  144. ++i;
  145. authenticationRealm = [NSString stringWithUTF8String:argv[i]];
  146. } else if (!strcmp(argv[i], "-authenticationUser") && (i + 1 < argc)) {
  147. ++i;
  148. authenticationUser = [NSString stringWithUTF8String:argv[i]];
  149. } else if (!strcmp(argv[i], "-authenticationPassword") && (i + 1 < argc)) {
  150. ++i;
  151. authenticationPassword = [NSString stringWithUTF8String:argv[i]];
  152. }
  153. }
  154. }
  155. GCDWebServer* webServer = nil;
  156. switch (mode) {
  157. // Simply serve contents of home directory
  158. case kMode_WebServer: {
  159. fprintf(stdout, "Running in Web Server mode from \"%s\"", [rootDirectory UTF8String]);
  160. webServer = [[GCDWebServer alloc] init];
  161. [webServer addGETHandlerForBasePath:@"/" directoryPath:rootDirectory indexFilename:nil cacheAge:0 allowRangeRequests:YES];
  162. break;
  163. }
  164. // Renders a HTML page
  165. case kMode_HTMLPage: {
  166. fprintf(stdout, "Running in HTML Page mode");
  167. webServer = [[GCDWebServer alloc] init];
  168. [webServer addDefaultHandlerForMethod:@"GET"
  169. requestClass:[GCDWebServerRequest class]
  170. processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
  171. return [GCDWebServerDataResponse responseWithHTML:@"<html><body><p>Hello World</p></body></html>"];
  172. }];
  173. break;
  174. }
  175. // Implements an HTML form
  176. case kMode_HTMLForm: {
  177. fprintf(stdout, "Running in HTML Form mode");
  178. webServer = [[GCDWebServer alloc] init];
  179. [webServer addHandlerForMethod:@"GET"
  180. path:@"/"
  181. requestClass:[GCDWebServerRequest class]
  182. processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
  183. NSString* html = @" \
  184. <html><body> \
  185. <form name=\"input\" action=\"/\" method=\"post\" enctype=\"application/x-www-form-urlencoded\"> \
  186. Value: <input type=\"text\" name=\"value\"> \
  187. <input type=\"submit\" value=\"Submit\"> \
  188. </form> \
  189. </body></html> \
  190. ";
  191. return [GCDWebServerDataResponse responseWithHTML:html];
  192. }];
  193. [webServer addHandlerForMethod:@"POST"
  194. path:@"/"
  195. requestClass:[GCDWebServerURLEncodedFormRequest class]
  196. processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
  197. NSString* value = [[(GCDWebServerURLEncodedFormRequest*)request arguments] objectForKey:@"value"];
  198. NSString* html = [NSString stringWithFormat:@"<html><body><p>%@</p></body></html>", value];
  199. return [GCDWebServerDataResponse responseWithHTML:html];
  200. }];
  201. break;
  202. }
  203. // Serve home directory through WebDAV
  204. case kMode_WebDAV: {
  205. fprintf(stdout, "Running in WebDAV mode from \"%s\"", [rootDirectory UTF8String]);
  206. webServer = [[GCDWebDAVServer alloc] initWithUploadDirectory:rootDirectory];
  207. break;
  208. }
  209. // Serve home directory through web uploader
  210. case kMode_WebUploader: {
  211. fprintf(stdout, "Running in Web Uploader mode from \"%s\"", [rootDirectory UTF8String]);
  212. webServer = [[GCDWebUploader alloc] initWithUploadDirectory:rootDirectory];
  213. break;
  214. }
  215. // Test streaming responses
  216. case kMode_StreamingResponse: {
  217. fprintf(stdout, "Running in Streaming Response mode");
  218. webServer = [[GCDWebServer alloc] init];
  219. [webServer addHandlerForMethod:@"GET"
  220. path:@"/"
  221. requestClass:[GCDWebServerRequest class]
  222. processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
  223. __block int countDown = 10;
  224. return [GCDWebServerStreamedResponse responseWithContentType:@"text/plain" streamBlock:^NSData *(NSError** error) {
  225. usleep(100 * 1000);
  226. if (countDown) {
  227. return [[NSString stringWithFormat:@"%i\n", countDown--] dataUsingEncoding:NSUTF8StringEncoding];
  228. } else {
  229. return [NSData data];
  230. }
  231. }];
  232. }];
  233. break;
  234. }
  235. }
  236. #if __has_feature(objc_arc)
  237. fprintf(stdout, " (ARC is ON)\n");
  238. #else
  239. fprintf(stdout, " (ARC is OFF)\n");
  240. #endif
  241. if (webServer) {
  242. Delegate* delegate = [[Delegate alloc] init];
  243. webServer.delegate = delegate;
  244. if (testDirectory) {
  245. fprintf(stdout, "<RUNNING TESTS FROM \"%s\">\n\n", [testDirectory UTF8String]);
  246. result = (int)[webServer runTestsWithOptions:@{GCDWebServerOption_Port: @8080} inDirectory:testDirectory];
  247. } else {
  248. if (recording) {
  249. fprintf(stdout, "<RECORDING ENABLED>\n");
  250. webServer.recordingEnabled = YES;
  251. }
  252. fprintf(stdout, "\n");
  253. NSMutableDictionary* options = [NSMutableDictionary dictionary];
  254. [options setObject:@8080 forKey:GCDWebServerOption_Port];
  255. [options setObject:@"" forKey:GCDWebServerOption_BonjourName];
  256. if (authenticationUser && authenticationPassword) {
  257. [options setValue:authenticationRealm forKey:GCDWebServerOption_AuthenticationRealm];
  258. [options setObject:@{authenticationUser: authenticationPassword} forKey:GCDWebServerOption_AuthenticationAccounts];
  259. if ([authenticationMethod isEqualToString:@"Basic"]) {
  260. [options setObject:GCDWebServerAuthenticationMethod_Basic forKey:GCDWebServerOption_AuthenticationMethod];
  261. } else if ([authenticationMethod isEqualToString:@"Digest"]) {
  262. [options setObject:GCDWebServerAuthenticationMethod_DigestAccess forKey:GCDWebServerOption_AuthenticationMethod];
  263. }
  264. }
  265. if ([webServer runWithOptions:options]) {
  266. result = 0;
  267. }
  268. }
  269. #if !__has_feature(objc_arc)
  270. [webServer release];
  271. [delegate release];
  272. #endif
  273. }
  274. }
  275. return result;
  276. }