aboutsummaryrefslogtreecommitdiff
path: root/src/examples
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2017-07-26 15:37:53 +0300
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2017-07-26 15:39:33 +0300
commitbb1699636bcc51526f2cd7ba268ae500d6a54a96 (patch)
treeb63d31a6015b51d1d2cd789c0e17c80e8ebd6340 /src/examples
parent09ccb6f7da257cfd6217c9254f90bf759187348e (diff)
downloadlibmicrohttpd-bb1699636bcc51526f2cd7ba268ae500d6a54a96.tar.gz
libmicrohttpd-bb1699636bcc51526f2cd7ba268ae500d6a54a96.zip
Furthermore extended chunked_example.c to illustrate situation with
parameter provided for content callback.
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/chunked_example.c41
1 files changed, 31 insertions, 10 deletions
diff --git a/src/examples/chunked_example.c b/src/examples/chunked_example.c
index 3cd008df..d8c2b7a7 100644
--- a/src/examples/chunked_example.c
+++ b/src/examples/chunked_example.c
@@ -26,6 +26,11 @@
26#include "platform.h" 26#include "platform.h"
27#include <microhttpd.h> 27#include <microhttpd.h>
28 28
29struct responseContentCallbackParam
30{
31 const char *response_data;
32 size_t response_size;
33};
29 34
30static ssize_t 35static ssize_t
31callback (void *cls, 36callback (void *cls,
@@ -33,14 +38,13 @@ callback (void *cls,
33 char *buf, 38 char *buf,
34 size_t buf_size) 39 size_t buf_size)
35{ 40{
36 static const char response_data[] = "<html><head><title>Simple response</title></head>" \
37 "<body>Simple response text</body></html>";
38 static const uint64_t response_size = (sizeof(response_data)/sizeof(char)) - 1;
39 size_t size_to_copy; 41 size_t size_to_copy;
42 struct responseContentCallbackParam * const param =
43 (struct responseContentCallbackParam *)cls;
40 44
41 /* Note: 'pos' will never exceed size of transmitted data. */ 45 /* Note: 'pos' will never exceed size of transmitted data. */
42 /* You can use 'pos == response_size' in next check. */ 46 /* You can use 'pos == param->response_size' in next check. */
43 if (pos >= response_size) 47 if (pos >= param->response_size)
44 { /* Whole response was sent. Signal end of response. */ 48 { /* Whole response was sent. Signal end of response. */
45 return MHD_CONTENT_READER_END_OF_STREAM; 49 return MHD_CONTENT_READER_END_OF_STREAM;
46 } 50 }
@@ -54,12 +58,12 @@ callback (void *cls,
54 } 58 }
55 * End of pseudo code. */ 59 * End of pseudo code. */
56 60
57 if (buf_size < (response_size - pos) ) 61 if (buf_size < (param->response_size - pos))
58 size_to_copy = buf_size; 62 size_to_copy = buf_size;
59 else 63 else
60 size_to_copy = response_size - pos; 64 size_to_copy = param->response_size - pos;
61 65
62 memcpy (buf, response_data + pos, size_to_copy); 66 memcpy (buf, param->response_data + pos, size_to_copy);
63 67
64 /* Pseudo code. * 68 /* Pseudo code. *
65 if (error_preparing_response) 69 if (error_preparing_response)
@@ -73,6 +77,14 @@ callback (void *cls,
73 return size_to_copy; 77 return size_to_copy;
74} 78}
75 79
80void
81free_callback_param(void *cls)
82{
83 free(cls);
84}
85
86static const char simple_response_text[] = "<html><head><title>Simple response</title></head>"
87 "<body>Simple response text</body></html>";
76 88
77 89
78static int 90static int
@@ -84,6 +96,7 @@ ahc_echo (void *cls,
84 const char *upload_data, size_t *upload_data_size, void **ptr) 96 const char *upload_data, size_t *upload_data_size, void **ptr)
85{ 97{
86 static int aptr; 98 static int aptr;
99 struct responseContentCallbackParam * callback_param;
87 struct MHD_Response *response; 100 struct MHD_Response *response;
88 int ret; 101 int ret;
89 102
@@ -95,12 +108,20 @@ ahc_echo (void *cls,
95 *ptr = &aptr; 108 *ptr = &aptr;
96 return MHD_YES; 109 return MHD_YES;
97 } 110 }
111
112 callback_param = malloc (sizeof(struct responseContentCallbackParam));
113 if (NULL == callback_param)
114 return MHD_NO; /* Not enough memory. */
115
116 callback_param->response_data = simple_response_text;
117 callback_param->response_size = (sizeof(simple_response_text)/sizeof(char)) - 1;
118
98 *ptr = NULL; /* reset when done */ 119 *ptr = NULL; /* reset when done */
99 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 120 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
100 1024, 121 1024,
101 &callback, 122 &callback,
102 NULL, 123 callback_param,
103 NULL); 124 &free_callback_param);
104 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 125 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
105 MHD_destroy_response (response); 126 MHD_destroy_response (response);
106 return ret; 127 return ret;