aboutsummaryrefslogtreecommitdiff
path: root/src/examples
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2017-10-05 23:16:19 +0200
committerChristian Grothoff <christian@grothoff.org>2017-10-05 23:16:19 +0200
commit1a23dd25c36bdb0e0be264730e9088a49a1e8152 (patch)
tree0df4a2329e32c5908b6ffd9d5aa9eda2cd075a4c /src/examples
parent111e08fbe3f436bd21f78ab8c6c4c3b79e728bba (diff)
downloadlibmicrohttpd-1a23dd25c36bdb0e0be264730e9088a49a1e8152.tar.gz
libmicrohttpd-1a23dd25c36bdb0e0be264730e9088a49a1e8152.zip
misc style improvements, fixing some tiny rare memory leaks in examples
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/chunked_example.c35
-rw-r--r--src/examples/demo.c6
-rw-r--r--src/examples/https_fileserver_example.c42
-rw-r--r--src/examples/post_example.c22
4 files changed, 71 insertions, 34 deletions
diff --git a/src/examples/chunked_example.c b/src/examples/chunked_example.c
index a661216c..9bf73e34 100644
--- a/src/examples/chunked_example.c
+++ b/src/examples/chunked_example.c
@@ -32,6 +32,7 @@ struct ResponseContentCallbackParam
32 size_t response_size; 32 size_t response_size;
33}; 33};
34 34
35
35static ssize_t 36static ssize_t
36callback (void *cls, 37callback (void *cls,
37 uint64_t pos, 38 uint64_t pos,
@@ -77,12 +78,14 @@ callback (void *cls,
77 return size_to_copy; 78 return size_to_copy;
78} 79}
79 80
80void 81
81free_callback_param(void *cls) 82static void
83free_callback_param (void *cls)
82{ 84{
83 free(cls); 85 free(cls);
84} 86}
85 87
88
86static const char simple_response_text[] = "<html><head><title>Simple response</title></head>" 89static const char simple_response_text[] = "<html><head><title>Simple response</title></head>"
87 "<body>Simple response text</body></html>"; 90 "<body>Simple response text</body></html>";
88 91
@@ -93,10 +96,12 @@ ahc_echo (void *cls,
93 const char *url, 96 const char *url,
94 const char *method, 97 const char *method,
95 const char *version, 98 const char *version,
96 const char *upload_data, size_t *upload_data_size, void **ptr) 99 const char *upload_data,
100 size_t *upload_data_size,
101 void **ptr)
97{ 102{
98 static int aptr; 103 static int aptr;
99 struct ResponseContentCallbackParam * callback_param; 104 struct ResponseContentCallbackParam *callback_param;
100 struct MHD_Response *response; 105 struct MHD_Response *response;
101 int ret; 106 int ret;
102 (void)cls; /* Unused. Silent compiler warning. */ 107 (void)cls; /* Unused. Silent compiler warning. */
@@ -127,31 +132,47 @@ ahc_echo (void *cls,
127 &callback, 132 &callback,
128 callback_param, 133 callback_param,
129 &free_callback_param); 134 &free_callback_param);
135 if (NULL == response)
136 {
137 free (callback_param);
138 return MHD_NO;
139 }
130 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 140 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
131 MHD_destroy_response (response); 141 MHD_destroy_response (response);
132 return ret; 142 return ret;
133} 143}
134 144
145
135int 146int
136main (int argc, char *const *argv) 147main (int argc, char *const *argv)
137{ 148{
138 struct MHD_Daemon *d; 149 struct MHD_Daemon *d;
150 int port;
139 151
140 if (argc != 2) 152 if (argc != 2)
141 { 153 {
142 printf ("%s PORT\n", argv[0]); 154 printf ("%s PORT\n", argv[0]);
143 return 1; 155 return 1;
144 } 156 }
157 port = atoi (argv[1]);
158 if ( (1 > port) ||
159 (port > UINT16_MAX) )
160 {
161 fprintf (stderr,
162 "Port must be a number between 1 and 65535\n");
163 return 1;
164 }
145 d = MHD_start_daemon (// MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 165 d = MHD_start_daemon (// MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
146 MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 166 MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
147 // MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, 167 // MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL,
148 // MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, 168 // MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL,
149 // MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 169 // MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
150 atoi (argv[1]), 170 (uint16_t) port,
151 NULL, NULL, &ahc_echo, NULL, 171 NULL, NULL,
172 &ahc_echo, NULL,
152 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, 173 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
153 MHD_OPTION_END); 174 MHD_OPTION_END);
154 if (d == NULL) 175 if (NULL == d)
155 return 1; 176 return 1;
156 (void) getc (stdin); 177 (void) getc (stdin);
157 MHD_stop_daemon (d); 178 MHD_stop_daemon (d);
diff --git a/src/examples/demo.c b/src/examples/demo.c
index edf38e98..0b177357 100644
--- a/src/examples/demo.c
+++ b/src/examples/demo.c
@@ -254,7 +254,7 @@ struct ResponseDataContext
254 * 254 *
255 * @param rdc where to store the list of files 255 * @param rdc where to store the list of files
256 * @param dirname name of the directory to list 256 * @param dirname name of the directory to list
257 * @return MHD_YES on success, MHD_NO on error 257 * @return #MHD_YES on success, #MHD_NO on error
258 */ 258 */
259static int 259static int
260list_directory (struct ResponseDataContext *rdc, 260list_directory (struct ResponseDataContext *rdc,
@@ -271,7 +271,7 @@ list_directory (struct ResponseDataContext *rdc,
271 { 271 {
272 if ('.' == de->d_name[0]) 272 if ('.' == de->d_name[0])
273 continue; 273 continue;
274 if (sizeof (fullname) <= (size_t) 274 if (sizeof (fullname) <= (unsigned int)
275 snprintf (fullname, sizeof (fullname), 275 snprintf (fullname, sizeof (fullname),
276 "%s/%s", 276 "%s/%s",
277 dirname, de->d_name)) 277 dirname, de->d_name))
@@ -555,7 +555,7 @@ process_upload_data (void *cls,
555 uc->category, 555 uc->category,
556 filename); 556 filename);
557 for (i=strlen (fn)-1;i>=0;i--) 557 for (i=strlen (fn)-1;i>=0;i--)
558 if (! isprint ((int) fn[i])) 558 if (! isprint ((unsigned char) fn[i]))
559 fn[i] = '_'; 559 fn[i] = '_';
560 uc->fd = open (fn, 560 uc->fd = open (fn,
561 O_CREAT | O_EXCL 561 O_CREAT | O_EXCL
diff --git a/src/examples/https_fileserver_example.c b/src/examples/https_fileserver_example.c
index 453ca2ff..818c8d55 100644
--- a/src/examples/https_fileserver_example.c
+++ b/src/examples/https_fileserver_example.c
@@ -191,38 +191,44 @@ int
191main (int argc, char *const *argv) 191main (int argc, char *const *argv)
192{ 192{
193 struct MHD_Daemon *TLS_daemon; 193 struct MHD_Daemon *TLS_daemon;
194 int port;
194 195
195 if (argc == 2) 196 if (argc != 2)
196 { 197 {
197 /* TODO check if this is truly necessary - disallow usage of the blocking /dev/random */ 198 printf ("%s PORT\n", argv[0]);
198 /* gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM, 0); */ 199 return 1;
199 TLS_daemon =
200 MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG |
201 MHD_USE_TLS, atoi (argv[1]), NULL, NULL, &http_ahc,
202 NULL, MHD_OPTION_CONNECTION_TIMEOUT, 256,
203 MHD_OPTION_HTTPS_MEM_KEY, key_pem,
204 MHD_OPTION_HTTPS_MEM_CERT, cert_pem,
205 MHD_OPTION_END);
206 } 200 }
207 else 201 port = atoi (argv[1]);
202 if ( (1 > port) ||
203 (port > UINT16_MAX) )
208 { 204 {
209 printf ("Usage: %s HTTP-PORT\n", argv[0]); 205 fprintf (stderr,
206 "Port must be a number between 1 and 65535\n");
210 return 1; 207 return 1;
211 } 208 }
212 209
213 if (TLS_daemon == NULL) 210 /* TODO check if this is truly necessary - disallow usage of the blocking /dev/random */
211 /* gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM, 0); */
212 TLS_daemon =
213 MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG |
214 MHD_USE_TLS,
215 (uint16_t) port,
216 NULL, NULL,
217 &http_ahc, NULL,
218 MHD_OPTION_CONNECTION_TIMEOUT, 256,
219 MHD_OPTION_HTTPS_MEM_KEY, key_pem,
220 MHD_OPTION_HTTPS_MEM_CERT, cert_pem,
221 MHD_OPTION_END);
222 if (NULL == TLS_daemon)
214 { 223 {
215 fprintf (stderr, "Error: failed to start TLS_daemon\n"); 224 fprintf (stderr, "Error: failed to start TLS_daemon\n");
216 return 1; 225 return 1;
217 } 226 }
218 else 227 printf ("MHD daemon listening on port %u\n",
219 { 228 (unsigned int) port);
220 printf ("MHD daemon listening on port %d\n", atoi (argv[1]));
221 }
222 229
223 (void) getc (stdin); 230 (void) getc (stdin);
224 231
225 MHD_stop_daemon (TLS_daemon); 232 MHD_stop_daemon (TLS_daemon);
226
227 return 0; 233 return 0;
228} 234}
diff --git a/src/examples/post_example.c b/src/examples/post_example.c
index 8b92956d..6c9a3f4b 100644
--- a/src/examples/post_example.c
+++ b/src/examples/post_example.c
@@ -313,23 +313,28 @@ fill_v1_form (const void *cls,
313 struct MHD_Connection *connection) 313 struct MHD_Connection *connection)
314{ 314{
315 int ret; 315 int ret;
316 size_t slen;
316 char *reply; 317 char *reply;
317 struct MHD_Response *response; 318 struct MHD_Response *response;
318 (void)cls; /* Unused. Silent compiler warning. */ 319 (void)cls; /* Unused. Silent compiler warning. */
319 320
320 reply = malloc (strlen (MAIN_PAGE) + strlen (session->value_1) + 1); 321 slen = strlen (MAIN_PAGE) + strlen (session->value_1);
322 reply = malloc (slen + 1);
321 if (NULL == reply) 323 if (NULL == reply)
322 return MHD_NO; 324 return MHD_NO;
323 snprintf (reply, 325 snprintf (reply,
324 strlen (MAIN_PAGE) + strlen (session->value_1) + 1, 326 slen + 1,
325 MAIN_PAGE, 327 MAIN_PAGE,
326 session->value_1); 328 session->value_1);
327 /* return static form */ 329 /* return static form */
328 response = MHD_create_response_from_buffer (strlen (reply), 330 response = MHD_create_response_from_buffer (slen,
329 (void *) reply, 331 (void *) reply,
330 MHD_RESPMEM_MUST_FREE); 332 MHD_RESPMEM_MUST_FREE);
331 if (NULL == response) 333 if (NULL == response)
334 {
335 free (reply);
332 return MHD_NO; 336 return MHD_NO;
337 }
333 add_session_cookie (session, response); 338 add_session_cookie (session, response);
334 MHD_add_response_header (response, 339 MHD_add_response_header (response,
335 MHD_HTTP_HEADER_CONTENT_ENCODING, 340 MHD_HTTP_HEADER_CONTENT_ENCODING,
@@ -359,22 +364,27 @@ fill_v1_v2_form (const void *cls,
359 int ret; 364 int ret;
360 char *reply; 365 char *reply;
361 struct MHD_Response *response; 366 struct MHD_Response *response;
367 size_t slen;
362 (void)cls; /* Unused. Silent compiler warning. */ 368 (void)cls; /* Unused. Silent compiler warning. */
363 369
364 reply = malloc (strlen (SECOND_PAGE) + strlen (session->value_1) + strlen (session->value_2) + 1); 370 slen = strlen (SECOND_PAGE) + strlen (session->value_1) + strlen (session->value_2);
371 reply = malloc (slen + 1);
365 if (NULL == reply) 372 if (NULL == reply)
366 return MHD_NO; 373 return MHD_NO;
367 snprintf (reply, 374 snprintf (reply,
368 strlen (SECOND_PAGE) + strlen (session->value_1) + strlen (session->value_2) + 1, 375 slen + 1,
369 SECOND_PAGE, 376 SECOND_PAGE,
370 session->value_1, 377 session->value_1,
371 session->value_2); 378 session->value_2);
372 /* return static form */ 379 /* return static form */
373 response = MHD_create_response_from_buffer (strlen (reply), 380 response = MHD_create_response_from_buffer (slen,
374 (void *) reply, 381 (void *) reply,
375 MHD_RESPMEM_MUST_FREE); 382 MHD_RESPMEM_MUST_FREE);
376 if (NULL == response) 383 if (NULL == response)
384 {
385 free (reply);
377 return MHD_NO; 386 return MHD_NO;
387 }
378 add_session_cookie (session, response); 388 add_session_cookie (session, response);
379 MHD_add_response_header (response, 389 MHD_add_response_header (response,
380 MHD_HTTP_HEADER_CONTENT_ENCODING, 390 MHD_HTTP_HEADER_CONTENT_ENCODING,