From eb16317dff47b6b353c0c03cec4413ab251fe83f Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Sun, 20 Jan 2013 17:46:25 +0100 Subject: [PATCH] Moved content generators from connection handler to separate functions --- main.c | 103 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 57 insertions(+), 46 deletions(-) diff --git a/main.c b/main.c index 8da970a..3859fef 100644 --- a/main.c +++ b/main.c @@ -53,6 +53,61 @@ void request_completed(void *cls, free(connstate); } +int serv_regular_file(struct MHD_Connection *connection, struct ConnectionState *connstate) { + struct MHD_Response *response; + int ret; + + LOG(LVL_DEBUG, "Serving %s as a regular file.", connstate->localFileName); + + // open a file descriptor to the file + int fd = open(connstate->localFileName, O_RDONLY); + + if(fd == -1) { + LOG(LVL_ERR, "Cannot open %s: %s", + connstate->localFileName, strerror(errno)); + + return MHD_queue_response(connection, MHD_HTTP_FORBIDDEN, error403Response); + } + + // no error so far -> serve the file + response = MHD_create_response_from_fd( + connstate->targetStat.st_size, + fd); + +#ifdef HAVE_MAGIC + // if libmagic is available, determine the correct MIME type for the file + const char *mimeType = magic_file(magicCookie, connstate->localFileName); + LOG(LVL_DEBUG, "MIME type for %s: %s", connstate->localFileName, mimeType); + MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimeType); +#endif + + ret = MHD_queue_response(connection, MHD_HTTP_OK, response); + MHD_destroy_response(response); + return ret; +} + +int serv_directory(struct MHD_Connection *connection, struct ConnectionState *connstate) { + struct MHD_Response *response; + int ret; + + LOG(LVL_DEBUG, "Generating directory listing for %s.", connstate->localFileName); + + char *result = create_dirlisting(connstate->cleanedURL, connstate->localFileName); + if(!result) { + LOG(LVL_ERR, "Failed to generate dirlisting for %s.", connstate->localFileName); + return MHD_queue_response(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, error500Response); + } + + response = MHD_create_response_from_buffer( + strlen(result), result, MHD_RESPMEM_MUST_FREE); + + MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, "text/html"); + + ret = MHD_queue_response(connection, MHD_HTTP_OK, response); + MHD_destroy_response(response); + return ret; +} + static int connection_handler(void * cls, struct MHD_Connection *connection, const char *url, @@ -63,9 +118,6 @@ static int connection_handler(void * cls, void **ptr) { struct ConnectionState *connstate; - struct MHD_Response *response; - int ret; - if (0 != strcmp(method, "GET")) { LOG(LVL_WARN, "Unexpected method: %s.", method); return MHD_NO; @@ -118,50 +170,9 @@ static int connection_handler(void * cls, } if(S_ISREG(connstate->targetStat.st_mode)) { - LOG(LVL_DEBUG, "Serving %s as a regular file.", connstate->localFileName); - - // open a file descriptor to the file - int fd = open(connstate->localFileName, O_RDONLY); - - if(fd == -1) { - LOG(LVL_ERR, "Cannot open %s: %s", - connstate->localFileName, strerror(errno)); - - return MHD_queue_response(connection, MHD_HTTP_FORBIDDEN, error403Response); - } - - // no error so far -> serve the file - response = MHD_create_response_from_fd( - connstate->targetStat.st_size, - fd); - -#ifdef HAVE_MAGIC - // if libmagic is available, determine the correct MIME type for the file - const char *mimeType = magic_file(magicCookie, connstate->localFileName); - LOG(LVL_DEBUG, "MIME type for %s: %s", connstate->localFileName, mimeType); - MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimeType); -#endif - - ret = MHD_queue_response(connection, MHD_HTTP_OK, response); - MHD_destroy_response(response); - return ret; + return serv_regular_file(connection, connstate); } else if(S_ISDIR(connstate->targetStat.st_mode)) { - LOG(LVL_DEBUG, "Generating directory listing for %s.", connstate->localFileName); - - char *result = create_dirlisting(connstate->cleanedURL, connstate->localFileName); - if(!result) { - LOG(LVL_ERR, "Failed to generate dirlisting for %s.", connstate->localFileName); - return MHD_queue_response(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, error500Response); - } - - response = MHD_create_response_from_buffer( - strlen(result), result, MHD_RESPMEM_MUST_FREE); - - MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, "text/html"); - - ret = MHD_queue_response(connection, MHD_HTTP_OK, response); - MHD_destroy_response(response); - return ret; + return serv_directory(connection, connstate); } else { LOG(LVL_WARN, "%s is neither a directory nor a regular file. Don't allow the access.",