main.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "GCDWebUploader.h"
  26. int main(int argc, const char* argv[]) {
  27. BOOL success = NO;
  28. int mode = (argc == 2 ? MIN(MAX(atoi(argv[1]), 0), 3) : 0);
  29. @autoreleasepool {
  30. GCDWebServer* webServer = nil;
  31. switch (mode) {
  32. // Simply serve contents of home directory
  33. case 0: {
  34. webServer = [[GCDWebServer alloc] init];
  35. [webServer addGETHandlerForBasePath:@"/" directoryPath:NSHomeDirectory() indexFilename:nil cacheAge:0 allowRangeRequests:YES];
  36. break;
  37. }
  38. // Renders a HTML page
  39. case 1: {
  40. webServer = [[GCDWebServer alloc] init];
  41. [webServer addDefaultHandlerForMethod:@"GET"
  42. requestClass:[GCDWebServerRequest class]
  43. processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
  44. return [GCDWebServerDataResponse responseWithHTML:@"<html><body><p>Hello World</p></body></html>"];
  45. }];
  46. break;
  47. }
  48. // Implements an HTML form
  49. case 2: {
  50. webServer = [[GCDWebServer alloc] init];
  51. [webServer addHandlerForMethod:@"GET"
  52. path:@"/"
  53. requestClass:[GCDWebServerRequest class]
  54. processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
  55. NSString* html = @" \
  56. <html><body> \
  57. <form name=\"input\" action=\"/\" method=\"post\" enctype=\"application/x-www-form-urlencoded\"> \
  58. Value: <input type=\"text\" name=\"value\"> \
  59. <input type=\"submit\" value=\"Submit\"> \
  60. </form> \
  61. </body></html> \
  62. ";
  63. return [GCDWebServerDataResponse responseWithHTML:html];
  64. }];
  65. [webServer addHandlerForMethod:@"POST"
  66. path:@"/"
  67. requestClass:[GCDWebServerURLEncodedFormRequest class]
  68. processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
  69. NSString* value = [[(GCDWebServerURLEncodedFormRequest*)request arguments] objectForKey:@"value"];
  70. NSString* html = [NSString stringWithFormat:@"<html><body><p>%@</p></body></html>", value];
  71. return [GCDWebServerDataResponse responseWithHTML:html];
  72. }];
  73. break;
  74. }
  75. case 3: {
  76. webServer = [[GCDWebUploader alloc] initWithUploadDirectory:@"/tmp"];
  77. break;
  78. }
  79. }
  80. success = [webServer runWithPort:8080];
  81. #if !__has_feature(objc_arc)
  82. [webServer release];
  83. #endif
  84. }
  85. return success ? 0 : -1;
  86. }