|
@@ -25,11 +25,22 @@
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
+#import <mach-o/getsect.h>
|
|
|
|
+
|
|
#import "GCDWebServer.h"
|
|
#import "GCDWebServer.h"
|
|
|
|
|
|
|
|
+static NSData* _DataFromTEXTSection(const char* name) {
|
|
|
|
+ unsigned long size = 0;
|
|
|
|
+ char* ptr = getsectdata("__TEXT", name, &size);
|
|
|
|
+ if (!ptr || !size) {
|
|
|
|
+ abort();
|
|
|
|
+ }
|
|
|
|
+ return [NSData dataWithBytesNoCopy:ptr length:size freeWhenDone:NO];
|
|
|
|
+}
|
|
|
|
+
|
|
int main(int argc, const char* argv[]) {
|
|
int main(int argc, const char* argv[]) {
|
|
BOOL success = NO;
|
|
BOOL success = NO;
|
|
- int mode = (argc == 2 ? MIN(MAX(atoi(argv[1]), 0), 2) : 0);
|
|
|
|
|
|
+ int mode = (argc == 2 ? MIN(MAX(atoi(argv[1]), 0), 3) : 0);
|
|
@autoreleasepool {
|
|
@autoreleasepool {
|
|
GCDWebServer* webServer = [[GCDWebServer alloc] init];
|
|
GCDWebServer* webServer = [[GCDWebServer alloc] init];
|
|
switch (mode) {
|
|
switch (mode) {
|
|
@@ -83,6 +94,33 @@ int main(int argc, const char* argv[]) {
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // Implements drag & drop file upload using http://filedropjs.org (requires Chrome 13+, Firefox 3.6+, IE 10+ or Safari 6+)
|
|
|
|
+ case 3: {
|
|
|
|
+ [webServer addGETHandlerForPath:@"/"
|
|
|
|
+ staticData:_DataFromTEXTSection("_index_html_")
|
|
|
|
+ contentType:@"text/html; charset=utf-8"
|
|
|
|
+ cacheAge:0];
|
|
|
|
+ [webServer addGETHandlerForPath:@"/filedrop-min.js"
|
|
|
|
+ staticData:_DataFromTEXTSection("_filedrop_js_")
|
|
|
|
+ contentType:@"application/javascript; charset=utf-8"
|
|
|
|
+ cacheAge:0];
|
|
|
|
+ [webServer addHandlerForMethod:@"POST" path:@"/ajax-upload" requestClass:[GCDWebServerFileRequest class] processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
|
|
|
|
+
|
|
|
|
+ NSString* fileName = GCDWebServerUnescapeURLString([request.headers objectForKey:@"X-File-Name"]);
|
|
|
|
+ NSString* inPath = [(GCDWebServerFileRequest*)request filePath];
|
|
|
|
+ NSString* outPath = [@"/tmp" stringByAppendingPathComponent:fileName];
|
|
|
|
+ [[NSFileManager defaultManager] removeItemAtPath:outPath error:NULL];
|
|
|
|
+ if ([[NSFileManager defaultManager] moveItemAtPath:inPath toPath:outPath error:NULL]) {
|
|
|
|
+ NSString* message = [NSString stringWithFormat:@"File uploaded to \"%@\"", outPath];
|
|
|
|
+ return [GCDWebServerDataResponse responseWithText:message];
|
|
|
|
+ } else {
|
|
|
|
+ return [GCDWebServerResponse responseWithStatusCode:500];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }];
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|
|
success = [webServer runWithPort:8080];
|
|
success = [webServer runWithPort:8080];
|
|
#if !__has_feature(objc_arc)
|
|
#if !__has_feature(objc_arc)
|