From 6dfd30e508eb0f7ceca4f294ba1113c22eac4418 Mon Sep 17 00:00:00 2001
From: Thomas Kolb You can upload a file to the current directory here. " \
+ " " \
+ " Upload
" \
+ "
Upload a file to this directory
"); + } + strcat(result, FOOTER); return result; } -char* create_dirlisting(const char *url, const char *localpath) { +char* create_dirlisting(const char *url, const char *localpath, int uploadEnabled) { char *result; struct Entry *entries; @@ -288,7 +294,7 @@ char* create_dirlisting(const char *url, const char *localpath) { qsort(entries, numentries, sizeof(struct Entry), compare_entries); - result = gen_html(url, entries, numentries); + result = gen_html(url, entries, numentries, uploadEnabled); for(i = 0; i < numentries; i++) { if(entries[i].flags & FLG_USED) free(entries[i].name); diff --git a/src/main.c b/src/main.c index ae8f64c..cb2172b 100644 --- a/src/main.c +++ b/src/main.c @@ -55,6 +55,7 @@ magic_t magicCookie; #endif int running = 1; +int uploadEnabled = 1; void request_completed(void *cls, struct MHD_Connection * connection, @@ -208,7 +209,7 @@ int serv_directory(struct MHD_Connection *connection, struct ConnectionState *co LOG(LVL_DEBUG, "Generating directory listing for %s.", connstate->localFileName); - char *result = create_dirlisting(connstate->cleanedURL, connstate->localFileName); + char *result = create_dirlisting(connstate->cleanedURL, connstate->localFileName, uploadEnabled); if(!result) { LOG(LVL_ERR, "Failed to generate dirlisting for %s.", connstate->localFileName); return MHD_queue_response(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, error500Response); @@ -259,7 +260,13 @@ int serv_result_page(struct MHD_Connection *connection, struct ConnectionState * char *buffer = malloc(strlen(HEADER1 HEADER2 FOOTER) + 1024); - sprintf(buffer, HEADER1 "%s
" FOOTER, + sprintf(buffer, + HEADER1 + "%s
" + "" + FOOTER, title, title, message); response = MHD_create_response_from_buffer( @@ -333,6 +340,24 @@ static int post_processor(void *cls, return MHD_NO; } +int key_value_iterator(void *cls, + enum MHD_ValueKind kind, + const char *key, const char *value) { + struct ConnectionState *connstate = (struct ConnectionState*)cls; + + if(kind == MHD_GET_ARGUMENT_KIND) { + if(strcmp(key, "upload") == 0) { + connstate->uploadRequest = 1; + return MHD_NO; // this is the only argument we search for + } + + return MHD_YES; + } else { + return MHD_NO; + } +} + + static int connection_handler(void * cls, struct MHD_Connection *connection, const char *url, @@ -351,6 +376,7 @@ static int connection_handler(void * cls, connstate->result = RC_OK; connstate->upload_fd = NULL; + connstate->uploadRequest = 0; // set the local file name strncpy(connstate->localFileName, shareRoot, PATH_MAX); @@ -366,6 +392,9 @@ static int connection_handler(void * cls, method, url, connstate->localFileName); if (0 == strcmp(method, "GET") || 0 == strcmp(method, "HEAD")) { + // process GET arguments + MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND, key_value_iterator, connstate); + connstate->postProcessor = NULL; connstate->requestType = GET; } else if(0 == strcmp(method, "POST")) { @@ -413,7 +442,11 @@ static int connection_handler(void * cls, if(S_ISREG(connstate->targetStat.st_mode)) { return serv_regular_file(connection, connstate); } else if(S_ISDIR(connstate->targetStat.st_mode)) { - return serv_directory(connection, connstate); + if(connstate->uploadRequest) { + return MHD_queue_response(connection, MHD_HTTP_OK, uploadFormResponse); + } else { + return serv_directory(connection, connstate); + } } else { LOG(LVL_WARN, "%s is neither a directory nor a regular file. Don't allow the access.",