libmicrohttpd

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

commit bb1699636bcc51526f2cd7ba268ae500d6a54a96
parent 09ccb6f7da257cfd6217c9254f90bf759187348e
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date:   Wed, 26 Jul 2017 15:37:53 +0300

Furthermore extended chunked_example.c to illustrate situation with
parameter provided for content callback.

Diffstat:
Msrc/examples/chunked_example.c | 41+++++++++++++++++++++++++++++++----------
1 file changed, 31 insertions(+), 10 deletions(-)

diff --git a/src/examples/chunked_example.c b/src/examples/chunked_example.c @@ -26,6 +26,11 @@ #include "platform.h" #include <microhttpd.h> +struct responseContentCallbackParam +{ + const char *response_data; + size_t response_size; +}; static ssize_t callback (void *cls, @@ -33,14 +38,13 @@ callback (void *cls, char *buf, size_t buf_size) { - static const char response_data[] = "<html><head><title>Simple response</title></head>" \ - "<body>Simple response text</body></html>"; - static const uint64_t response_size = (sizeof(response_data)/sizeof(char)) - 1; size_t size_to_copy; + struct responseContentCallbackParam * const param = + (struct responseContentCallbackParam *)cls; /* Note: 'pos' will never exceed size of transmitted data. */ - /* You can use 'pos == response_size' in next check. */ - if (pos >= response_size) + /* You can use 'pos == param->response_size' in next check. */ + if (pos >= param->response_size) { /* Whole response was sent. Signal end of response. */ return MHD_CONTENT_READER_END_OF_STREAM; } @@ -54,12 +58,12 @@ callback (void *cls, } * End of pseudo code. */ - if (buf_size < (response_size - pos) ) + if (buf_size < (param->response_size - pos)) size_to_copy = buf_size; else - size_to_copy = response_size - pos; + size_to_copy = param->response_size - pos; - memcpy (buf, response_data + pos, size_to_copy); + memcpy (buf, param->response_data + pos, size_to_copy); /* Pseudo code. * if (error_preparing_response) @@ -73,6 +77,14 @@ callback (void *cls, return size_to_copy; } +void +free_callback_param(void *cls) +{ + free(cls); +} + +static const char simple_response_text[] = "<html><head><title>Simple response</title></head>" + "<body>Simple response text</body></html>"; static int @@ -84,6 +96,7 @@ ahc_echo (void *cls, const char *upload_data, size_t *upload_data_size, void **ptr) { static int aptr; + struct responseContentCallbackParam * callback_param; struct MHD_Response *response; int ret; @@ -95,12 +108,20 @@ ahc_echo (void *cls, *ptr = &aptr; return MHD_YES; } + + callback_param = malloc (sizeof(struct responseContentCallbackParam)); + if (NULL == callback_param) + return MHD_NO; /* Not enough memory. */ + + callback_param->response_data = simple_response_text; + callback_param->response_size = (sizeof(simple_response_text)/sizeof(char)) - 1; + *ptr = NULL; /* reset when done */ response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 1024, &callback, - NULL, - NULL); + callback_param, + &free_callback_param); ret = MHD_queue_response (connection, MHD_HTTP_OK, response); MHD_destroy_response (response); return ret;