libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

commit 20be47629397dc69192a270ca171e24afb4f7365
parent c5ad835c7cf6c02bc5445f8b3ed47925bded90f7
Author: Sebastian Gerhardt <sebgerhardt@gmx.net>
Date:   Thu, 14 Aug 2008 18:07:41 +0000

improved adherence to GNU coding standards (responseheaders-example)


Diffstat:
Mdoc/examples/logging.c | 2+-
Mdoc/examples/responseheaders.c | 94++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
Mdoc/exploringrequests.inc | 2+-
Mdoc/responseheaders.inc | 66++++++++++++++++++++++++++++++++++++------------------------------
4 files changed, 91 insertions(+), 73 deletions(-)

diff --git a/doc/examples/logging.c b/doc/examples/logging.c @@ -12,7 +12,7 @@ int print_out_key (void *cls, enum MHD_ValueKind kind, const char *key, const ch return MHD_YES; } -int answer_to_connection(void *cls, struct MHD_Connection *connection, const char *url, +int answer_to_connection (void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, unsigned int *upload_data_size, void **con_cls) { diff --git a/doc/examples/responseheaders.c b/doc/examples/responseheaders.c @@ -9,70 +9,82 @@ #define MIMETYPE "image/png" -long GetFileSize(const char *filename) +long get_file_size (const char *filename) { FILE *fp; - fp = fopen(filename, "rb"); + fp = fopen (filename, "rb"); if (fp) - { - long size; + { + long size; - if ( (0!=fseek(fp, 0, SEEK_END)) || (-1==(size=ftell(fp))) ) - size = 0; + if ( (0 != fseek (fp, 0, SEEK_END)) + || (-1 == (size = ftell (fp))) ) + size = 0; - fclose(fp); - return size; - } else return 0; + fclose (fp); + + return size; + } + else + return 0; } -int AnswerToConnection(void *cls, struct MHD_Connection *connection, const char *url, - const char *method, const char *version, const char *upload_data, - unsigned int *upload_data_size, void **con_cls) +int answer_to_connection (void *cls, struct MHD_Connection *connection, const char *url, + const char *method, const char *version, const char *upload_data, + unsigned int *upload_data_size, void **con_cls) { - unsigned char *buffer; + unsigned char *buffer = NULL; struct MHD_Response *response; long size; FILE *fp; - int ret=0; + int ret = 0; if (0 != strcmp(method, "GET")) return MHD_NO; - size = GetFileSize(FILENAME); + size = get_file_size (FILENAME); if (size != 0) - { - fp = fopen(FILENAME, "rb"); - if (fp) { - buffer = malloc(size); - if (buffer) - if (size == fread(buffer, 1, size, fp)) ret=1; - } + fp = fopen (FILENAME, "rb"); + if (fp) + { + buffer = malloc (size); + + if (buffer) + if (size == fread (buffer, 1, size, fp)) ret = 1; - fclose(fp); - } + fclose(fp); + } + } if (!ret) - { - const char *errorstr = "<html><body>An internal server error has occured!\ - </body></html>"; + { + const char *errorstr = "<html><body>An internal server error has occured!\ + </body></html>"; + + if (buffer) free(buffer); + + response = MHD_create_response_from_data(strlen(errorstr), (void*)errorstr, + MHD_NO, MHD_NO); - if (buffer) free(buffer); - response = MHD_create_response_from_data(strlen(errorstr), (void*)errorstr, - MHD_NO, MHD_NO); + if (response) + { + ret = MHD_queue_response (connection, MHD_HTTP_INTERNAL_SERVER_ERROR, response); + MHD_destroy_response (response); - ret = MHD_queue_response (connection, MHD_HTTP_INTERNAL_SERVER_ERROR, response); - MHD_destroy_response (response); - return MHD_YES; - } + return MHD_YES; + } + else return MHD_NO; + } - response = MHD_create_response_from_data(size, (void*)buffer, MHD_YES, MHD_NO); + response = MHD_create_response_from_data (size, (void*)buffer, MHD_YES, MHD_NO); - MHD_add_response_header(response, "Content-Type", MIMETYPE); + MHD_add_response_header (response, "Content-Type", MIMETYPE); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); MHD_destroy_response (response); + return ret; } @@ -81,14 +93,14 @@ int main () { struct MHD_Daemon *daemon; - daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL, - &AnswerToConnection, NULL, MHD_OPTION_END); + daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL, + &answer_to_connection, NULL, MHD_OPTION_END); + if (NULL == daemon) return 1; - if (daemon == NULL) return 1; + getchar (); - getchar(); + MHD_stop_daemon (daemon); - MHD_stop_daemon(daemon); return 0; } diff --git a/doc/exploringrequests.inc b/doc/exploringrequests.inc @@ -9,7 +9,7 @@ just return MHD_NO after we have probed the request. This way, the connection is without much ado by the server. @verbatim -int answer_to_connection(void *cls, struct MHD_Connection *connection, const char *url, +int answer_to_connection (void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, unsigned int *upload_data_size, void **con_cls) { diff --git a/doc/responseheaders.inc b/doc/responseheaders.inc @@ -23,36 +23,37 @@ Once again, we can base the new example on the @code{hellobrowser} program. #define FILENAME "picture.png" #define MIMETYPE "image/png" -int AnswerToConnection(void *cls, struct MHD_Connection *connection, - const char *url, const char *method, const char *version, - const char *upload_data, unsigned int *upload_data_size, void **con_cls) +int answer_to_connection (void *cls, struct MHD_Connection *connection, const char *url, + const char *method, const char *version, const char *upload_data, + unsigned int *upload_data_size, void **con_cls) { + unsigned char *buffer = NULL; struct MHD_Response *response; - int ret=0; @end verbatim @noindent We want the program to load the graphics file into memory: @verbatim -long size; + long size; FILE *fp; - int ret=0; + int ret = 0; if (0 != strcmp(method, "GET")) return MHD_NO; - size = GetFileSize(FILENAME); + size = get_file_size (FILENAME); if (size != 0) - { - fp = fopen(FILENAME, "rb"); - if (fp) { - buffer = malloc(size); - if (buffer) - if (size == fread(buffer, 1, size, fp)) ret=1; - } + fp = fopen (FILENAME, "rb"); + if (fp) + { + buffer = malloc (size); - fclose(fp); - } + if (buffer) + if (size == fread (buffer, 1, size, fp)) ret = 1; + + fclose(fp); + } + } @end verbatim @noindent @@ -64,19 +65,24 @@ server side and if so, the client should be informed with @code{MHD_HTTP_INTERNA @verbatim if (!ret) - { - const char *errorstr = "<html><body>An internal server error\ - has occured!</body></html>"; - - if (buffer) free(buffer); - response = MHD_create_response_from_data(strlen(errorstr), - (void*)errorstr, MHD_NO, MHD_NO); - - ret = MHD_queue_response (connection, - MHD_HTTP_INTERNAL_SERVER_ERROR, response); - - return MHD_YES; - } + { + const char *errorstr = "<html><body>An internal server error has occured!\ + </body></html>"; + + if (buffer) free(buffer); + + response = MHD_create_response_from_data(strlen(errorstr), (void*)errorstr, + MHD_NO, MHD_NO); + + if (response) + { + ret = MHD_queue_response (connection, MHD_HTTP_INTERNAL_SERVER_ERROR, response); + MHD_destroy_response (response); + + return MHD_YES; + } + else return MHD_NO; + } @end verbatim @noindent @@ -88,7 +94,7 @@ But in the case of success a response will be constructed that contains the buff file's content. @verbatim -response = MHD_create_response_from_data(size, (void*)buffer, MHD_YES, MHD_NO); +response = MHD_create_response_from_data (size, (void*)buffer, MHD_YES, MHD_NO); @end verbatim @noindent