main.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <mach-o/getsect.h>
  26. #import "GCDWebServer.h"
  27. static NSData* _DataFromTEXTSection(const char* name) {
  28. unsigned long size = 0;
  29. char* ptr = getsectdata("__TEXT", name, &size);
  30. if (!ptr || !size) {
  31. abort();
  32. }
  33. return [NSData dataWithBytesNoCopy:ptr length:size freeWhenDone:NO];
  34. }
  35. int main(int argc, const char* argv[]) {
  36. BOOL success = NO;
  37. int mode = (argc == 2 ? MIN(MAX(atoi(argv[1]), 0), 3) : 0);
  38. @autoreleasepool {
  39. GCDWebServer* webServer = [[GCDWebServer alloc] init];
  40. switch (mode) {
  41. // Simply serve contents of home directory
  42. case 0: {
  43. [webServer addGETHandlerForBasePath:@"/" directoryPath:NSHomeDirectory() indexFilename:nil cacheAge:0 allowRangeRequests:YES];
  44. break;
  45. }
  46. // Renders a HTML page
  47. case 1: {
  48. [webServer addDefaultHandlerForMethod:@"GET"
  49. requestClass:[GCDWebServerRequest class]
  50. processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
  51. return [GCDWebServerDataResponse responseWithHTML:@"<html><body><p>Hello World</p></body></html>"];
  52. }];
  53. break;
  54. }
  55. // Implements an HTML form
  56. case 2: {
  57. [webServer addHandlerForMethod:@"GET"
  58. path:@"/"
  59. requestClass:[GCDWebServerRequest class]
  60. processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
  61. NSString* html = @" \
  62. <html><body> \
  63. <form name=\"input\" action=\"/\" method=\"post\" enctype=\"application/x-www-form-urlencoded\"> \
  64. Value: <input type=\"text\" name=\"value\"> \
  65. <input type=\"submit\" value=\"Submit\"> \
  66. </form> \
  67. </body></html> \
  68. ";
  69. return [GCDWebServerDataResponse responseWithHTML:html];
  70. }];
  71. [webServer addHandlerForMethod:@"POST"
  72. path:@"/"
  73. requestClass:[GCDWebServerURLEncodedFormRequest class]
  74. processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
  75. NSString* value = [[(GCDWebServerURLEncodedFormRequest*)request arguments] objectForKey:@"value"];
  76. NSString* html = [NSString stringWithFormat:@"<html><body><p>%@</p></body></html>", value];
  77. return [GCDWebServerDataResponse responseWithHTML:html];
  78. }];
  79. break;
  80. }
  81. // Implements drag & drop file upload using http://filedropjs.org (requires Chrome 13+, Firefox 3.6+, IE 10+ or Safari 6+)
  82. case 3: {
  83. [webServer addGETHandlerForPath:@"/"
  84. staticData:_DataFromTEXTSection("_index_html_")
  85. contentType:@"text/html; charset=utf-8"
  86. cacheAge:0];
  87. [webServer addGETHandlerForPath:@"/filedrop-min.js"
  88. staticData:_DataFromTEXTSection("_filedrop_js_")
  89. contentType:@"application/javascript; charset=utf-8"
  90. cacheAge:0];
  91. [webServer addHandlerForMethod:@"POST" path:@"/ajax-upload" requestClass:[GCDWebServerFileRequest class] processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
  92. NSString* fileName = GCDWebServerUnescapeURLString([request.headers objectForKey:@"X-File-Name"]);
  93. NSString* inPath = [(GCDWebServerFileRequest*)request filePath];
  94. NSString* outPath = [@"/tmp" stringByAppendingPathComponent:fileName];
  95. [[NSFileManager defaultManager] removeItemAtPath:outPath error:NULL];
  96. if ([[NSFileManager defaultManager] moveItemAtPath:inPath toPath:outPath error:NULL]) {
  97. NSString* message = [NSString stringWithFormat:@"File uploaded to \"%@\"", outPath];
  98. return [GCDWebServerDataResponse responseWithText:message];
  99. } else {
  100. return [GCDWebServerResponse responseWithStatusCode:500];
  101. }
  102. }];
  103. break;
  104. }
  105. }
  106. success = [webServer runWithPort:8080];
  107. #if !__has_feature(objc_arc)
  108. [webServer release];
  109. #endif
  110. }
  111. return success ? 0 : -1;
  112. }