aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-10-17 16:56:41 +0200
committerChristian Grothoff <christian@grothoff.org>2019-10-17 16:56:41 +0200
commit972103dc288e2a2396e060018e7c3733f29a940d (patch)
treeaeb0b3a182d5168f12752928d3204342c9581e91
parentcbbfd0591fc639ddd21a5850f54f403fb7394174 (diff)
downloadlibmicrohttpd-972103dc288e2a2396e060018e7c3733f29a940d.tar.gz
libmicrohttpd-972103dc288e2a2396e060018e7c3733f29a940d.zip
applying uncrustify to ensure uniform indentation
-rw-r--r--.gitignore1
-rwxr-xr-xbootstrap25
-rw-r--r--contrib/uncrustify.cfg2
-rwxr-xr-xcontrib/uncrustify_precommit35
-rw-r--r--src/examples/authorization_example.c59
-rw-r--r--src/examples/benchmark.c79
-rw-r--r--src/examples/benchmark_https.c168
-rw-r--r--src/examples/chunked_example.c76
-rw-r--r--src/examples/connection_close.c76
-rw-r--r--src/examples/demo.c748
-rw-r--r--src/examples/demo_https.c846
-rw-r--r--src/examples/digest_auth_example.c142
-rw-r--r--src/examples/dual_stack_example.c44
-rw-r--r--src/examples/fileserver_example_dirs.c162
-rw-r--r--src/examples/fileserver_example_external_select.c137
-rw-r--r--src/examples/http_chunked_compression.c139
-rw-r--r--src/examples/http_compression.c63
-rw-r--r--src/examples/https_fileserver_example.c114
-rw-r--r--src/examples/minimal_example_comet.c35
-rw-r--r--src/examples/msgs_i18n.c47
-rw-r--r--src/examples/post_example.c509
-rw-r--r--src/examples/querystring_example.c50
-rw-r--r--src/examples/refuse_post_example.c59
-rw-r--r--src/examples/suspend_resume_epoll.c64
-rw-r--r--src/examples/timeout.c39
-rw-r--r--src/examples/upgrade_example.c118
-rw-r--r--src/gnutls/check_record_pending.c8
-rw-r--r--src/gnutls/handshake.c24
-rw-r--r--src/gnutls/idle_ready.c18
-rw-r--r--src/gnutls/init.c38
-rw-r--r--src/gnutls/init.h2
-rw-r--r--src/gnutls/recv.c6
-rw-r--r--src/gnutls/send.c16
-rw-r--r--src/gnutls/setup_connection.c83
-rw-r--r--src/gnutls/shutdown_connection.c8
-rw-r--r--src/gnutls/strerror.c6
-rw-r--r--src/gnutls/update_event_loop_info.c36
-rw-r--r--src/include/autoinit_funcs.h105
-rw-r--r--src/include/mhd_options.h14
-rw-r--r--src/include/microhttpd.h390
-rw-r--r--src/include/microhttpd2.h488
-rw-r--r--src/include/microhttpd_tls.h56
-rw-r--r--src/include/platform.h16
-rw-r--r--src/lib/action_continue.c4
-rw-r--r--src/lib/action_from_response.c53
-rw-r--r--src/lib/action_parse_post.c4
-rw-r--r--src/lib/action_process_upload.c8
-rw-r--r--src/lib/action_suspend.c70
-rw-r--r--src/lib/base64.c55
-rw-r--r--src/lib/base64.h2
-rw-r--r--src/lib/connection_add.c1204
-rw-r--r--src/lib/connection_add.h4
-rw-r--r--src/lib/connection_call_handlers.c4026
-rw-r--r--src/lib/connection_call_handlers.h12
-rw-r--r--src/lib/connection_cleanup.c132
-rw-r--r--src/lib/connection_cleanup.h6
-rw-r--r--src/lib/connection_close.c58
-rw-r--r--src/lib/connection_close.h4
-rw-r--r--src/lib/connection_finish_forward.c46
-rw-r--r--src/lib/connection_finish_forward.h2
-rw-r--r--src/lib/connection_info.c12
-rw-r--r--src/lib/connection_options.c70
-rw-r--r--src/lib/connection_update_last_activity.c11
-rw-r--r--src/lib/connection_update_last_activity.h2
-rw-r--r--src/lib/daemon_close_all_connections.c179
-rw-r--r--src/lib/daemon_close_all_connections.h2
-rw-r--r--src/lib/daemon_create.c50
-rw-r--r--src/lib/daemon_destroy.c158
-rw-r--r--src/lib/daemon_epoll.c533
-rw-r--r--src/lib/daemon_epoll.h4
-rw-r--r--src/lib/daemon_get_timeout.c78
-rw-r--r--src/lib/daemon_info.c98
-rw-r--r--src/lib/daemon_ip_limit.c120
-rw-r--r--src/lib/daemon_ip_limit.h12
-rw-r--r--src/lib/daemon_options.c140
-rw-r--r--src/lib/daemon_poll.c368
-rw-r--r--src/lib/daemon_poll.h14
-rw-r--r--src/lib/daemon_quiesce.c103
-rw-r--r--src/lib/daemon_run.c36
-rw-r--r--src/lib/daemon_select.c699
-rw-r--r--src/lib/daemon_select.h6
-rw-r--r--src/lib/daemon_start.c1013
-rw-r--r--src/lib/init.c40
-rw-r--r--src/lib/init.h2
-rw-r--r--src/lib/internal.c288
-rw-r--r--src/lib/internal.h161
-rw-r--r--src/lib/md5.c226
-rw-r--r--src/lib/md5.h24
-rw-r--r--src/lib/memorypool.c118
-rw-r--r--src/lib/memorypool.h12
-rw-r--r--src/lib/mhd_assert.h16
-rw-r--r--src/lib/mhd_byteorder.h47
-rw-r--r--src/lib/mhd_compat.c24
-rw-r--r--src/lib/mhd_compat.h18
-rw-r--r--src/lib/mhd_itc.c6
-rw-r--r--src/lib/mhd_itc.h103
-rw-r--r--src/lib/mhd_itc_types.h2
-rw-r--r--src/lib/mhd_limits.h38
-rw-r--r--src/lib/mhd_locks.h47
-rw-r--r--src/lib/mhd_mono_clock.c178
-rw-r--r--src/lib/mhd_mono_clock.h6
-rw-r--r--src/lib/mhd_sockets.c239
-rw-r--r--src/lib/mhd_sockets.h239
-rw-r--r--src/lib/mhd_str.c347
-rw-r--r--src/lib/mhd_str.h103
-rw-r--r--src/lib/mhd_threads.c162
-rw-r--r--src/lib/mhd_threads.h78
-rw-r--r--src/lib/reason_phrase.c2
-rw-r--r--src/lib/request.c72
-rw-r--r--src/lib/request_info.c12
-rw-r--r--src/lib/request_resume.c185
-rw-r--r--src/lib/request_resume.h2
-rw-r--r--src/lib/response.c126
-rw-r--r--src/lib/response_for_upgrade.c20
-rw-r--r--src/lib/response_from_buffer.c46
-rw-r--r--src/lib/response_from_callback.c14
-rw-r--r--src/lib/response_from_fd.c85
-rw-r--r--src/lib/response_options.c5
-rw-r--r--src/lib/sysfdsetsize.c2
-rw-r--r--src/lib/tsearch.c122
-rw-r--r--src/lib/tsearch.h24
-rw-r--r--src/lib/upgrade_process.c491
-rw-r--r--src/lib/upgrade_process.h2
-rw-r--r--src/lib/version.c155
-rw-r--r--src/microhttpd/base64.c55
-rw-r--r--src/microhttpd/base64.h2
-rw-r--r--src/microhttpd/connection.h2
-rw-r--r--src/microhttpd/connection_https.c127
-rw-r--r--src/microhttpd/connection_https.h2
-rw-r--r--src/microhttpd/daemon.c7140
-rw-r--r--src/microhttpd/digestauth.c936
-rw-r--r--src/microhttpd/internal.c290
-rw-r--r--src/microhttpd/internal.h161
-rw-r--r--src/microhttpd/md5.c181
-rw-r--r--src/microhttpd/md5.h18
-rw-r--r--src/microhttpd/memorypool.h10
-rw-r--r--src/microhttpd/mhd_assert.h16
-rw-r--r--src/microhttpd/mhd_bithelpers.h152
-rw-r--r--src/microhttpd/mhd_byteorder.h47
-rw-r--r--src/microhttpd/mhd_compat.c24
-rw-r--r--src/microhttpd/mhd_compat.h18
-rw-r--r--src/microhttpd/mhd_itc.c6
-rw-r--r--src/microhttpd/mhd_itc.h103
-rw-r--r--src/microhttpd/mhd_itc_types.h2
-rw-r--r--src/microhttpd/mhd_limits.h38
-rw-r--r--src/microhttpd/mhd_locks.h47
-rw-r--r--src/microhttpd/mhd_mono_clock.c178
-rw-r--r--src/microhttpd/mhd_mono_clock.h6
-rw-r--r--src/microhttpd/mhd_send.c347
-rw-r--r--src/microhttpd/mhd_send.h6
-rw-r--r--src/microhttpd/mhd_sockets.h239
-rw-r--r--src/microhttpd/mhd_str.c369
-rw-r--r--src/microhttpd/mhd_str.h109
-rw-r--r--src/microhttpd/mhd_threads.h78
-rw-r--r--src/microhttpd/postprocessor.c1205
-rw-r--r--src/microhttpd/reason_phrase.c184
-rw-r--r--src/microhttpd/response.c611
-rw-r--r--src/microhttpd/sha256.c254
-rw-r--r--src/microhttpd/sha256.h4
-rw-r--r--src/microhttpd/sysfdsetsize.c2
-rw-r--r--src/microhttpd/test_daemon.c69
-rw-r--r--src/microhttpd/test_helpers.h34
-rw-r--r--src/microhttpd/test_http_reasons.c124
-rw-r--r--src/microhttpd/test_options.c32
-rw-r--r--src/microhttpd/test_postprocessor.c68
-rw-r--r--src/microhttpd/test_postprocessor_amp.c30
-rw-r--r--src/microhttpd/test_postprocessor_large.c16
-rw-r--r--src/microhttpd/test_shutdown_select.c285
-rw-r--r--src/microhttpd/test_start_stop.c50
-rw-r--r--src/microhttpd/test_str.c4627
-rw-r--r--src/microhttpd/test_str_token.c127
-rw-r--r--src/microhttpd/tsearch.c122
-rw-r--r--src/microhttpd/tsearch.h24
-rw-r--r--src/testcurl/curl_version_check.c108
-rw-r--r--src/testcurl/gauger.h114
-rw-r--r--src/testcurl/https/test_empty_response.c170
-rw-r--r--src/testcurl/https/test_https_get.c55
-rw-r--r--src/testcurl/https/test_https_get_parallel.c67
-rw-r--r--src/testcurl/https/test_https_get_parallel_threads.c72
-rw-r--r--src/testcurl/https/test_https_get_select.c185
-rw-r--r--src/testcurl/https/test_https_multi_daemon.c106
-rw-r--r--src/testcurl/https/test_https_session_info.c113
-rw-r--r--src/testcurl/https/test_https_sni.c156
-rw-r--r--src/testcurl/https/test_https_time_out.c75
-rw-r--r--src/testcurl/https/test_tls_authentication.c67
-rw-r--r--src/testcurl/https/test_tls_extensions.c144
-rw-r--r--src/testcurl/https/test_tls_options.c114
-rw-r--r--src/testcurl/https/tls_test_common.c317
-rw-r--r--src/testcurl/https/tls_test_common.h35
-rw-r--r--src/testcurl/https/tls_test_keys.h260
-rw-r--r--src/testcurl/mhd_has_in_name.h20
-rw-r--r--src/testcurl/perf_get.c526
-rw-r--r--src/testcurl/perf_get_concurrent.c368
-rw-r--r--src/testcurl/test_callback.c232
-rw-r--r--src/testcurl/test_concurrent_stop.c188
-rw-r--r--src/testcurl/test_delete.c287
-rw-r--r--src/testcurl/test_digestauth.c157
-rw-r--r--src/testcurl/test_digestauth_sha256.c159
-rw-r--r--src/testcurl/test_digestauth_with_arguments.c164
-rw-r--r--src/testcurl/test_get.c808
-rw-r--r--src/testcurl/test_get_chunked.c289
-rw-r--r--src/testcurl/test_get_response_cleanup.c259
-rw-r--r--src/testcurl/test_get_sendfile.c394
-rw-r--r--src/testcurl/test_iplimit.c338
-rw-r--r--src/testcurl/test_large_put.c522
-rw-r--r--src/testcurl/test_long_header.c168
-rw-r--r--src/testcurl/test_parse_cookies.c173
-rw-r--r--src/testcurl/test_post.c560
-rw-r--r--src/testcurl/test_post_loop.c692
-rw-r--r--src/testcurl/test_postform.c385
-rw-r--r--src/testcurl/test_process_arguments.c176
-rw-r--r--src/testcurl/test_process_headers.c325
-rw-r--r--src/testcurl/test_put.c335
-rw-r--r--src/testcurl/test_put_chunked.c305
-rw-r--r--src/testcurl/test_quiesce.c510
-rw-r--r--src/testcurl/test_quiesce_stream.c80
-rw-r--r--src/testcurl/test_termination.c62
-rw-r--r--src/testcurl/test_timeout.c178
-rw-r--r--src/testcurl/test_urlparse.c77
-rw-r--r--src/testzzuf/socat.c50
-rw-r--r--src/testzzuf/test_get.c262
-rw-r--r--src/testzzuf/test_get_chunked.c246
-rw-r--r--src/testzzuf/test_long_header.c164
-rw-r--r--src/testzzuf/test_post.c348
-rw-r--r--src/testzzuf/test_post_form.c342
-rw-r--r--src/testzzuf/test_put.c310
-rw-r--r--src/testzzuf/test_put_chunked.c275
-rw-r--r--src/testzzuf/test_put_large.c324
228 files changed, 25846 insertions, 24385 deletions
diff --git a/.gitignore b/.gitignore
index 88f8ab32..46beb093 100644
--- a/.gitignore
+++ b/.gitignore
@@ -48,3 +48,4 @@ po/configargs.stamp
48**~ 48**~
49doc/libmicrohttpd.log 49doc/libmicrohttpd.log
50src/examples/suspend_resume_epoll 50src/examples/suspend_resume_epoll
51uncrustify.cfg
diff --git a/bootstrap b/bootstrap
index dce3249e..7e150794 100755
--- a/bootstrap
+++ b/bootstrap
@@ -8,4 +8,27 @@ fi
8 8
9test -n "$bs_scrdir" && cd "$bs_scrdir" || echo "Warning: cannot get sources directory" 1>&2 9test -n "$bs_scrdir" && cd "$bs_scrdir" || echo "Warning: cannot get sources directory" 1>&2
10 10
11autoreconf -I m4 -i ${1+"$@"} 11# This is more portable than `which' but comes with
12# the caveat of not(?) properly working on busybox's ash:
13existence()
14{
15 command -v "$1" >/dev/null 2>&1
16}
17
18
19if existence uncrustify; then
20 echo "Installing uncrustify hook and configuration"
21 # Install uncrustify format symlink (if possible)
22 ln -s contrib/uncrustify.cfg uncrustify.cfg 2> /dev/null
23 # Install pre-commit hook (if possible)
24 ln -s ../../contrib/uncrustify_precommit .git/hooks/pre-commit 2> /dev/null
25else
26 echo "Uncrustify not detected, hook not installed. Please install uncrustify if you plan on doing development"
27fi
28
29if existence libtool || existence libtoolize || existence glibtoolize || existence slibtool; then
30 autoreconf -I m4 -i ${1+"$@"}
31else
32 echo "*** No libtoolize (libtool) or libtool found, please install it ***" >&2;
33 exit 1
34fi
diff --git a/contrib/uncrustify.cfg b/contrib/uncrustify.cfg
index 6ced05d7..f56c8e73 100644
--- a/contrib/uncrustify.cfg
+++ b/contrib/uncrustify.cfg
@@ -4,7 +4,7 @@ output_tab_size = 2
4indent_columns = 2 4indent_columns = 2
5indent_with_tabs = 0 5indent_with_tabs = 0
6indent_case_brace = 2 6indent_case_brace = 2
7indent_label=0 7indent_label=-16
8 8
9code_width=80 9code_width=80
10#cmd_width=80 10#cmd_width=80
diff --git a/contrib/uncrustify_precommit b/contrib/uncrustify_precommit
new file mode 100755
index 00000000..fd29998c
--- /dev/null
+++ b/contrib/uncrustify_precommit
@@ -0,0 +1,35 @@
1#!/bin/sh
2
3# use as .git/hooks/pre-commit
4
5exec 1>&2
6
7RET=0
8changed=$(git diff --cached --name-only)
9crustified=""
10
11for f in $changed;
12do
13 if echo $f | grep \\.[c,h]\$ > /dev/null
14 then
15 # compare result of uncrustify with changes
16 #
17 # only change any of the invocations here if
18 # they are portable across all cmp and shell
19 # implementations !
20 uncrustify -q -c uncrustify.cfg -f $f | cmp -s $f -
21 if test $? = 1 ;
22 then
23 crustified=" $crustified $f"
24 RET=1
25 fi
26 fi
27done
28
29if [ $RET = 1 ];
30then
31 echo "Run"
32 echo "uncrustify --no-backup -c uncrustify.cfg ${crustified}"
33 echo "before commiting."
34fi
35exit $RET
diff --git a/src/examples/authorization_example.c b/src/examples/authorization_example.c
index d8a88203..c07d7827 100644
--- a/src/examples/authorization_example.c
+++ b/src/examples/authorization_example.c
@@ -32,9 +32,11 @@
32#include <windows.h> 32#include <windows.h>
33#endif 33#endif
34 34
35#define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>" 35#define PAGE \
36 "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>"
36 37
37#define DENIED "<html><head><title>Access denied</title></head><body>Access denied</body></html>" 38#define DENIED \
39 "<html><head><title>Access denied</title></head><body>Access denied</body></html>"
38 40
39 41
40 42
@@ -53,19 +55,19 @@ ahc_echo (void *cls,
53 char *user; 55 char *user;
54 char *pass; 56 char *pass;
55 int fail; 57 int fail;
56 (void)url; /* Unused. Silent compiler warning. */ 58 (void) url; /* Unused. Silent compiler warning. */
57 (void)version; /* Unused. Silent compiler warning. */ 59 (void) version; /* Unused. Silent compiler warning. */
58 (void)upload_data; /* Unused. Silent compiler warning. */ 60 (void) upload_data; /* Unused. Silent compiler warning. */
59 (void)upload_data_size; /* Unused. Silent compiler warning. */ 61 (void) upload_data_size; /* Unused. Silent compiler warning. */
60 62
61 if (0 != strcmp (method, "GET")) 63 if (0 != strcmp (method, "GET"))
62 return MHD_NO; /* unexpected method */ 64 return MHD_NO; /* unexpected method */
63 if (&aptr != *ptr) 65 if (&aptr != *ptr)
64 { 66 {
65 /* do never respond on first call */ 67 /* do never respond on first call */
66 *ptr = &aptr; 68 *ptr = &aptr;
67 return MHD_YES; 69 return MHD_YES;
68 } 70 }
69 *ptr = NULL; /* reset when done */ 71 *ptr = NULL; /* reset when done */
70 72
71 /* require: "Aladdin" with password "open sesame" */ 73 /* require: "Aladdin" with password "open sesame" */
@@ -77,18 +79,18 @@ ahc_echo (void *cls,
77 (0 != strcmp (pass, "open sesame") ) ); 79 (0 != strcmp (pass, "open sesame") ) );
78 if (fail) 80 if (fail)
79 { 81 {
80 response = MHD_create_response_from_buffer (strlen (DENIED), 82 response = MHD_create_response_from_buffer (strlen (DENIED),
81 (void *) DENIED, 83 (void *) DENIED,
82 MHD_RESPMEM_PERSISTENT); 84 MHD_RESPMEM_PERSISTENT);
83 ret = MHD_queue_basic_auth_fail_response (connection,"TestRealm",response); 85 ret = MHD_queue_basic_auth_fail_response (connection,"TestRealm",response);
84 } 86 }
85 else 87 else
86 { 88 {
87 response = MHD_create_response_from_buffer (strlen (me), 89 response = MHD_create_response_from_buffer (strlen (me),
88 (void *) me, 90 (void *) me,
89 MHD_RESPMEM_PERSISTENT); 91 MHD_RESPMEM_PERSISTENT);
90 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 92 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
91 } 93 }
92 if (NULL != user) 94 if (NULL != user)
93 MHD_free (user); 95 MHD_free (user);
94 if (NULL != pass) 96 if (NULL != pass)
@@ -107,13 +109,14 @@ main (int argc, char *const *argv)
107 if ( (argc != 2) || 109 if ( (argc != 2) ||
108 (1 != sscanf (argv[1], "%u", &port)) || 110 (1 != sscanf (argv[1], "%u", &port)) ||
109 (UINT16_MAX < port) ) 111 (UINT16_MAX < port) )
110 { 112 {
111 fprintf (stderr, 113 fprintf (stderr,
112 "%s PORT\n", argv[0]); 114 "%s PORT\n", argv[0]);
113 return 1; 115 return 1;
114 } 116 }
115 117
116 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 118 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
119 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
117 atoi (argv[1]), 120 atoi (argv[1]),
118 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); 121 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END);
119 if (d == NULL) 122 if (d == NULL)
diff --git a/src/examples/benchmark.c b/src/examples/benchmark.c
index 9512b9bf..2a8fb179 100644
--- a/src/examples/benchmark.c
+++ b/src/examples/benchmark.c
@@ -25,14 +25,15 @@
25#include "platform.h" 25#include "platform.h"
26#include <microhttpd.h> 26#include <microhttpd.h>
27 27
28#if defined(CPU_COUNT) && (CPU_COUNT+0) < 2 28#if defined(CPU_COUNT) && (CPU_COUNT + 0) < 2
29#undef CPU_COUNT 29#undef CPU_COUNT
30#endif 30#endif
31#if !defined(CPU_COUNT) 31#if ! defined(CPU_COUNT)
32#define CPU_COUNT 2 32#define CPU_COUNT 2
33#endif 33#endif
34 34
35#define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>" 35#define PAGE \
36 "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>"
36 37
37 38
38#define SMALL (1024 * 128) 39#define SMALL (1024 * 128)
@@ -62,16 +63,16 @@ static struct MHD_Response *response;
62 */ 63 */
63static void 64static void
64completed_callback (void *cls, 65completed_callback (void *cls,
65 struct MHD_Connection *connection, 66 struct MHD_Connection *connection,
66 void **con_cls, 67 void **con_cls,
67 enum MHD_RequestTerminationCode toe) 68 enum MHD_RequestTerminationCode toe)
68{ 69{
69 struct timeval *tv = *con_cls; 70 struct timeval *tv = *con_cls;
70 struct timeval tve; 71 struct timeval tve;
71 uint64_t delta; 72 uint64_t delta;
72 (void)cls; /* Unused. Silent compiler warning. */ 73 (void) cls; /* Unused. Silent compiler warning. */
73 (void)connection; /* Unused. Silent compiler warning. */ 74 (void) connection; /* Unused. Silent compiler warning. */
74 (void)toe; /* Unused. Silent compiler warning. */ 75 (void) toe; /* Unused. Silent compiler warning. */
75 76
76 if (NULL == tv) 77 if (NULL == tv)
77 return; 78 return;
@@ -80,10 +81,10 @@ completed_callback (void *cls,
80 delta = 0; 81 delta = 0;
81 if (tve.tv_usec >= tv->tv_usec) 82 if (tve.tv_usec >= tv->tv_usec)
82 delta += (tve.tv_sec - tv->tv_sec) * 1000000LL 83 delta += (tve.tv_sec - tv->tv_sec) * 1000000LL
83 + (tve.tv_usec - tv->tv_usec); 84 + (tve.tv_usec - tv->tv_usec);
84 else 85 else
85 delta += (tve.tv_sec - tv->tv_sec) * 1000000LL 86 delta += (tve.tv_sec - tv->tv_sec) * 1000000LL
86 - tv->tv_usec + tve.tv_usec; 87 - tv->tv_usec + tve.tv_usec;
87 if (delta < SMALL) 88 if (delta < SMALL)
88 small_deltas[delta]++; 89 small_deltas[delta]++;
89 else 90 else
@@ -94,11 +95,11 @@ completed_callback (void *cls,
94 95
95static void * 96static void *
96uri_logger_cb (void *cls, 97uri_logger_cb (void *cls,
97 const char *uri) 98 const char *uri)
98{ 99{
99 struct timeval *tv = malloc (sizeof (struct timeval)); 100 struct timeval *tv = malloc (sizeof (struct timeval));
100 (void)cls; /* Unused. Silent compiler warning. */ 101 (void) cls; /* Unused. Silent compiler warning. */
101 (void)uri; /* Unused. Silent compiler warning. */ 102 (void) uri; /* Unused. Silent compiler warning. */
102 103
103 if (NULL != tv) 104 if (NULL != tv)
104 gettimeofday (tv, NULL); 105 gettimeofday (tv, NULL);
@@ -114,12 +115,12 @@ ahc_echo (void *cls,
114 const char *version, 115 const char *version,
115 const char *upload_data, size_t *upload_data_size, void **ptr) 116 const char *upload_data, size_t *upload_data_size, void **ptr)
116{ 117{
117 (void)cls; /* Unused. Silent compiler warning. */ 118 (void) cls; /* Unused. Silent compiler warning. */
118 (void)url; /* Unused. Silent compiler warning. */ 119 (void) url; /* Unused. Silent compiler warning. */
119 (void)version; /* Unused. Silent compiler warning. */ 120 (void) version; /* Unused. Silent compiler warning. */
120 (void)upload_data; /* Unused. Silent compiler warning. */ 121 (void) upload_data; /* Unused. Silent compiler warning. */
121 (void)upload_data_size; /* Unused. Silent compiler warning. */ 122 (void) upload_data_size; /* Unused. Silent compiler warning. */
122 (void)ptr; /* Unused. Silent compiler warning. */ 123 (void) ptr; /* Unused. Silent compiler warning. */
123 124
124 if (0 != strcmp (method, "GET")) 125 if (0 != strcmp (method, "GET"))
125 return MHD_NO; /* unexpected method */ 126 return MHD_NO; /* unexpected method */
@@ -134,37 +135,39 @@ main (int argc, char *const *argv)
134 unsigned int i; 135 unsigned int i;
135 136
136 if (argc != 2) 137 if (argc != 2)
137 { 138 {
138 printf ("%s PORT\n", argv[0]); 139 printf ("%s PORT\n", argv[0]);
139 return 1; 140 return 1;
140 } 141 }
141 response = MHD_create_response_from_buffer (strlen (PAGE), 142 response = MHD_create_response_from_buffer (strlen (PAGE),
142 (void *) PAGE, 143 (void *) PAGE,
143 MHD_RESPMEM_PERSISTENT); 144 MHD_RESPMEM_PERSISTENT);
144#if 0 145#if 0
145 (void) MHD_add_response_header (response, 146 (void) MHD_add_response_header (response,
146 MHD_HTTP_HEADER_CONNECTION, 147 MHD_HTTP_HEADER_CONNECTION,
147 "close"); 148 "close");
148#endif 149#endif
149 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_SUPPRESS_DATE_NO_CLOCK 150 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD
151 | MHD_USE_SUPPRESS_DATE_NO_CLOCK
150#ifdef EPOLL_SUPPORT 152#ifdef EPOLL_SUPPORT
151 | MHD_USE_EPOLL | MHD_USE_TURBO 153 | MHD_USE_EPOLL | MHD_USE_TURBO
152#endif 154#endif
153 , 155 ,
154 atoi (argv[1]), 156 atoi (argv[1]),
155 NULL, NULL, &ahc_echo, NULL, 157 NULL, NULL, &ahc_echo, NULL,
156 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, 158 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
157 MHD_OPTION_THREAD_POOL_SIZE, (unsigned int) NUMBER_OF_THREADS, 159 MHD_OPTION_THREAD_POOL_SIZE, (unsigned
158 MHD_OPTION_URI_LOG_CALLBACK, &uri_logger_cb, NULL, 160 int) NUMBER_OF_THREADS,
159 MHD_OPTION_NOTIFY_COMPLETED, &completed_callback, NULL, 161 MHD_OPTION_URI_LOG_CALLBACK, &uri_logger_cb, NULL,
160 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 1000, 162 MHD_OPTION_NOTIFY_COMPLETED, &completed_callback, NULL,
161 MHD_OPTION_END); 163 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 1000,
164 MHD_OPTION_END);
162 if (d == NULL) 165 if (d == NULL)
163 return 1; 166 return 1;
164 (void) getc (stdin); 167 (void) getc (stdin);
165 MHD_stop_daemon (d); 168 MHD_stop_daemon (d);
166 MHD_destroy_response (response); 169 MHD_destroy_response (response);
167 for (i=0;i<SMALL;i++) 170 for (i = 0; i<SMALL; i++)
168 if (0 != small_deltas[i]) 171 if (0 != small_deltas[i])
169 fprintf (stdout, "D: %d %u\n", i, small_deltas[i]); 172 fprintf (stdout, "D: %d %u\n", i, small_deltas[i]);
170 return 0; 173 return 0;
diff --git a/src/examples/benchmark_https.c b/src/examples/benchmark_https.c
index 64eb11b7..9506e562 100644
--- a/src/examples/benchmark_https.c
+++ b/src/examples/benchmark_https.c
@@ -25,14 +25,15 @@
25#include "platform.h" 25#include "platform.h"
26#include <microhttpd.h> 26#include <microhttpd.h>
27 27
28#if defined(CPU_COUNT) && (CPU_COUNT+0) < 2 28#if defined(CPU_COUNT) && (CPU_COUNT + 0) < 2
29#undef CPU_COUNT 29#undef CPU_COUNT
30#endif 30#endif
31#if !defined(CPU_COUNT) 31#if ! defined(CPU_COUNT)
32#define CPU_COUNT 2 32#define CPU_COUNT 2
33#endif 33#endif
34 34
35#define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>" 35#define PAGE \
36 "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>"
36 37
37 38
38#define SMALL (1024 * 128) 39#define SMALL (1024 * 128)
@@ -62,16 +63,16 @@ static struct MHD_Response *response;
62 */ 63 */
63static void 64static void
64completed_callback (void *cls, 65completed_callback (void *cls,
65 struct MHD_Connection *connection, 66 struct MHD_Connection *connection,
66 void **con_cls, 67 void **con_cls,
67 enum MHD_RequestTerminationCode toe) 68 enum MHD_RequestTerminationCode toe)
68{ 69{
69 struct timeval *tv = *con_cls; 70 struct timeval *tv = *con_cls;
70 struct timeval tve; 71 struct timeval tve;
71 uint64_t delta; 72 uint64_t delta;
72 (void)cls; /* Unused. Silent compiler warning. */ 73 (void) cls; /* Unused. Silent compiler warning. */
73 (void)connection; /* Unused. Silent compiler warning. */ 74 (void) connection; /* Unused. Silent compiler warning. */
74 (void)toe; /* Unused. Silent compiler warning. */ 75 (void) toe; /* Unused. Silent compiler warning. */
75 76
76 if (NULL == tv) 77 if (NULL == tv)
77 return; 78 return;
@@ -80,10 +81,10 @@ completed_callback (void *cls,
80 delta = 0; 81 delta = 0;
81 if (tve.tv_usec >= tv->tv_usec) 82 if (tve.tv_usec >= tv->tv_usec)
82 delta += (tve.tv_sec - tv->tv_sec) * 1000000LL 83 delta += (tve.tv_sec - tv->tv_sec) * 1000000LL
83 + (tve.tv_usec - tv->tv_usec); 84 + (tve.tv_usec - tv->tv_usec);
84 else 85 else
85 delta += (tve.tv_sec - tv->tv_sec) * 1000000LL 86 delta += (tve.tv_sec - tv->tv_sec) * 1000000LL
86 - tv->tv_usec + tve.tv_usec; 87 - tv->tv_usec + tve.tv_usec;
87 if (delta < SMALL) 88 if (delta < SMALL)
88 small_deltas[delta]++; 89 small_deltas[delta]++;
89 else 90 else
@@ -94,11 +95,11 @@ completed_callback (void *cls,
94 95
95static void * 96static void *
96uri_logger_cb (void *cls, 97uri_logger_cb (void *cls,
97 const char *uri) 98 const char *uri)
98{ 99{
99 struct timeval *tv = malloc (sizeof (struct timeval)); 100 struct timeval *tv = malloc (sizeof (struct timeval));
100 (void)cls; /* Unused. Silent compiler warning. */ 101 (void) cls; /* Unused. Silent compiler warning. */
101 (void)uri; /* Unused. Silent compiler warning. */ 102 (void) uri; /* Unused. Silent compiler warning. */
102 103
103 if (NULL != tv) 104 if (NULL != tv)
104 gettimeofday (tv, NULL); 105 gettimeofday (tv, NULL);
@@ -114,12 +115,12 @@ ahc_echo (void *cls,
114 const char *version, 115 const char *version,
115 const char *upload_data, size_t *upload_data_size, void **ptr) 116 const char *upload_data, size_t *upload_data_size, void **ptr)
116{ 117{
117 (void)cls; /* Unused. Silent compiler warning. */ 118 (void) cls; /* Unused. Silent compiler warning. */
118 (void)url; /* Unused. Silent compiler warning. */ 119 (void) url; /* Unused. Silent compiler warning. */
119 (void)version; /* Unused. Silent compiler warning. */ 120 (void) version; /* Unused. Silent compiler warning. */
120 (void)upload_data; /* Unused. Silent compiler warning. */ 121 (void) upload_data; /* Unused. Silent compiler warning. */
121 (void)upload_data_size; /* Unused. Silent compiler warning. */ 122 (void) upload_data_size; /* Unused. Silent compiler warning. */
122 (void)ptr; /* Unused. Silent compiler warning. */ 123 (void) ptr; /* Unused. Silent compiler warning. */
123 124
124 if (0 != strcmp (method, "GET")) 125 if (0 != strcmp (method, "GET"))
125 return MHD_NO; /* unexpected method */ 126 return MHD_NO; /* unexpected method */
@@ -129,53 +130,53 @@ ahc_echo (void *cls,
129 130
130/* test server key */ 131/* test server key */
131const char srv_signed_key_pem[] = "-----BEGIN RSA PRIVATE KEY-----\n" 132const char srv_signed_key_pem[] = "-----BEGIN RSA PRIVATE KEY-----\n"
132 "MIIEowIBAAKCAQEAvfTdv+3fgvVTKRnP/HVNG81cr8TrUP/iiyuve/THMzvFXhCW\n" 133 "MIIEowIBAAKCAQEAvfTdv+3fgvVTKRnP/HVNG81cr8TrUP/iiyuve/THMzvFXhCW\n"
133 "+K03KwEku55QvnUndwBfU/ROzLlv+5hotgiDRNFT3HxurmhouySBrJNJv7qWp8IL\n" 134 "+K03KwEku55QvnUndwBfU/ROzLlv+5hotgiDRNFT3HxurmhouySBrJNJv7qWp8IL\n"
134 "q4sw32vo0fbMu5BZF49bUXK9L3kW2PdhTtSQPWHEzNrCxO+YgCilKHkY3vQNfdJ0\n" 135 "q4sw32vo0fbMu5BZF49bUXK9L3kW2PdhTtSQPWHEzNrCxO+YgCilKHkY3vQNfdJ0\n"
135 "20Q5EAAEseD1YtWCIpRvJzYlZMpjYB1ubTl24kwrgOKUJYKqM4jmF4DVQp4oOK/6\n" 136 "20Q5EAAEseD1YtWCIpRvJzYlZMpjYB1ubTl24kwrgOKUJYKqM4jmF4DVQp4oOK/6\n"
136 "QYGGh1QmHRPAy3CBII6sbb+sZT9cAqU6GYQVB35lm4XAgibXV6KgmpVxVQQ69U6x\n" 137 "QYGGh1QmHRPAy3CBII6sbb+sZT9cAqU6GYQVB35lm4XAgibXV6KgmpVxVQQ69U6x\n"
137 "yoOl204xuekZOaG9RUPId74Rtmwfi1TLbBzo2wIDAQABAoIBADu09WSICNq5cMe4\n" 138 "yoOl204xuekZOaG9RUPId74Rtmwfi1TLbBzo2wIDAQABAoIBADu09WSICNq5cMe4\n"
138 "+NKCLlgAT1NiQpLls1gKRbDhKiHU9j8QWNvWWkJWrCya4QdUfLCfeddCMeiQmv3K\n" 139 "+NKCLlgAT1NiQpLls1gKRbDhKiHU9j8QWNvWWkJWrCya4QdUfLCfeddCMeiQmv3K\n"
139 "lJMvDs+5OjJSHFoOsGiuW2Ias7IjnIojaJalfBml6frhJ84G27IXmdz6gzOiTIer\n" 140 "lJMvDs+5OjJSHFoOsGiuW2Ias7IjnIojaJalfBml6frhJ84G27IXmdz6gzOiTIer\n"
140 "DjeAgcwBaKH5WwIay2TxIaScl7AwHBauQkrLcyb4hTmZuQh6ArVIN6+pzoVuORXM\n" 141 "DjeAgcwBaKH5WwIay2TxIaScl7AwHBauQkrLcyb4hTmZuQh6ArVIN6+pzoVuORXM\n"
141 "bpeNWl2l/HSN3VtUN6aCAKbN/X3o0GavCCMn5Fa85uJFsab4ss/uP+2PusU71+zP\n" 142 "bpeNWl2l/HSN3VtUN6aCAKbN/X3o0GavCCMn5Fa85uJFsab4ss/uP+2PusU71+zP\n"
142 "sBm6p/2IbGvF5k3VPDA7X5YX61sukRjRBihY8xSnNYx1UcoOsX6AiPnbhifD8+xQ\n" 143 "sBm6p/2IbGvF5k3VPDA7X5YX61sukRjRBihY8xSnNYx1UcoOsX6AiPnbhifD8+xQ\n"
143 "Tlf8oJUCgYEA0BTfzqNpr9Wxw5/QXaSdw7S/0eP5a0C/nwURvmfSzuTD4equzbEN\n" 144 "Tlf8oJUCgYEA0BTfzqNpr9Wxw5/QXaSdw7S/0eP5a0C/nwURvmfSzuTD4equzbEN\n"
144 "d+dI/s2JMxrdj/I4uoAfUXRGaabevQIjFzC9uyE3LaOyR2zhuvAzX+vVcs6bSXeU\n" 145 "d+dI/s2JMxrdj/I4uoAfUXRGaabevQIjFzC9uyE3LaOyR2zhuvAzX+vVcs6bSXeU\n"
145 "pKpCAcN+3Z3evMaX2f+z/nfSUAl2i4J2R+/LQAWJW4KwRky/m+cxpfUCgYEA6bN1\n" 146 "pKpCAcN+3Z3evMaX2f+z/nfSUAl2i4J2R+/LQAWJW4KwRky/m+cxpfUCgYEA6bN1\n"
146 "b73bMgM8wpNt6+fcmS+5n0iZihygQ2U2DEud8nZJL4Nrm1dwTnfZfJBnkGj6+0Q0\n" 147 "b73bMgM8wpNt6+fcmS+5n0iZihygQ2U2DEud8nZJL4Nrm1dwTnfZfJBnkGj6+0Q0\n"
147 "cOwj2KS0/wcEdJBP0jucU4v60VMhp75AQeHqidIde0bTViSRo3HWKXHBIFGYoU3T\n" 148 "cOwj2KS0/wcEdJBP0jucU4v60VMhp75AQeHqidIde0bTViSRo3HWKXHBIFGYoU3T\n"
148 "LyPyKndbqsOObnsFXHn56Nwhr2HLf6nw4taGQY8CgYBoSW36FLCNbd6QGvLFXBGt\n" 149 "LyPyKndbqsOObnsFXHn56Nwhr2HLf6nw4taGQY8CgYBoSW36FLCNbd6QGvLFXBGt\n"
149 "2lMhEM8az/K58kJ4WXSwOLtr6MD/WjNT2tkcy0puEJLm6BFCd6A6pLn9jaKou/92\n" 150 "2lMhEM8az/K58kJ4WXSwOLtr6MD/WjNT2tkcy0puEJLm6BFCd6A6pLn9jaKou/92\n"
150 "SfltZjJPb3GUlp9zn5tAAeSSi7YMViBrfuFiHObij5LorefBXISLjuYbMwL03MgH\n" 151 "SfltZjJPb3GUlp9zn5tAAeSSi7YMViBrfuFiHObij5LorefBXISLjuYbMwL03MgH\n"
151 "Ocl2JtA2ywMp2KFXs8GQWQKBgFyIVv5ogQrbZ0pvj31xr9HjqK6d01VxIi+tOmpB\n" 152 "Ocl2JtA2ywMp2KFXs8GQWQKBgFyIVv5ogQrbZ0pvj31xr9HjqK6d01VxIi+tOmpB\n"
152 "4ocnOLEcaxX12BzprW55ytfOCVpF1jHD/imAhb3YrHXu0fwe6DXYXfZV4SSG2vB7\n" 153 "4ocnOLEcaxX12BzprW55ytfOCVpF1jHD/imAhb3YrHXu0fwe6DXYXfZV4SSG2vB7\n"
153 "IB9z14KBN5qLHjNGFpMQXHSMek+b/ftTU0ZnPh9uEM5D3YqRLVd7GcdUhHvG8P8Q\n" 154 "IB9z14KBN5qLHjNGFpMQXHSMek+b/ftTU0ZnPh9uEM5D3YqRLVd7GcdUhHvG8P8Q\n"
154 "C9aXAoGBAJtID6h8wOGMP0XYX5YYnhlC7dOLfk8UYrzlp3xhqVkzKthTQTj6wx9R\n" 155 "C9aXAoGBAJtID6h8wOGMP0XYX5YYnhlC7dOLfk8UYrzlp3xhqVkzKthTQTj6wx9R\n"
155 "GtC4k7U1ki8oJsfcIlBNXd768fqDVWjYju5rzShMpo8OCTS6ipAblKjCxPPVhIpv\n" 156 "GtC4k7U1ki8oJsfcIlBNXd768fqDVWjYju5rzShMpo8OCTS6ipAblKjCxPPVhIpv\n"
156 "tWPlbSn1qj6wylstJ5/3Z+ZW5H4wIKp5jmLiioDhcP0L/Ex3Zx8O\n" 157 "tWPlbSn1qj6wylstJ5/3Z+ZW5H4wIKp5jmLiioDhcP0L/Ex3Zx8O\n"
157 "-----END RSA PRIVATE KEY-----\n"; 158 "-----END RSA PRIVATE KEY-----\n";
158 159
159/* test server CA signed certificates */ 160/* test server CA signed certificates */
160const char srv_signed_cert_pem[] = "-----BEGIN CERTIFICATE-----\n" 161const char srv_signed_cert_pem[] = "-----BEGIN CERTIFICATE-----\n"
161 "MIIDGzCCAgWgAwIBAgIES0KCvTALBgkqhkiG9w0BAQUwFzEVMBMGA1UEAxMMdGVz\n" 162 "MIIDGzCCAgWgAwIBAgIES0KCvTALBgkqhkiG9w0BAQUwFzEVMBMGA1UEAxMMdGVz\n"
162 "dF9jYV9jZXJ0MB4XDTEwMDEwNTAwMDcyNVoXDTQ1MDMxMjAwMDcyNVowFzEVMBMG\n" 163 "dF9jYV9jZXJ0MB4XDTEwMDEwNTAwMDcyNVoXDTQ1MDMxMjAwMDcyNVowFzEVMBMG\n"
163 "A1UEAxMMdGVzdF9jYV9jZXJ0MIIBHzALBgkqhkiG9w0BAQEDggEOADCCAQkCggEA\n" 164 "A1UEAxMMdGVzdF9jYV9jZXJ0MIIBHzALBgkqhkiG9w0BAQEDggEOADCCAQkCggEA\n"
164 "vfTdv+3fgvVTKRnP/HVNG81cr8TrUP/iiyuve/THMzvFXhCW+K03KwEku55QvnUn\n" 165 "vfTdv+3fgvVTKRnP/HVNG81cr8TrUP/iiyuve/THMzvFXhCW+K03KwEku55QvnUn\n"
165 "dwBfU/ROzLlv+5hotgiDRNFT3HxurmhouySBrJNJv7qWp8ILq4sw32vo0fbMu5BZ\n" 166 "dwBfU/ROzLlv+5hotgiDRNFT3HxurmhouySBrJNJv7qWp8ILq4sw32vo0fbMu5BZ\n"
166 "F49bUXK9L3kW2PdhTtSQPWHEzNrCxO+YgCilKHkY3vQNfdJ020Q5EAAEseD1YtWC\n" 167 "F49bUXK9L3kW2PdhTtSQPWHEzNrCxO+YgCilKHkY3vQNfdJ020Q5EAAEseD1YtWC\n"
167 "IpRvJzYlZMpjYB1ubTl24kwrgOKUJYKqM4jmF4DVQp4oOK/6QYGGh1QmHRPAy3CB\n" 168 "IpRvJzYlZMpjYB1ubTl24kwrgOKUJYKqM4jmF4DVQp4oOK/6QYGGh1QmHRPAy3CB\n"
168 "II6sbb+sZT9cAqU6GYQVB35lm4XAgibXV6KgmpVxVQQ69U6xyoOl204xuekZOaG9\n" 169 "II6sbb+sZT9cAqU6GYQVB35lm4XAgibXV6KgmpVxVQQ69U6xyoOl204xuekZOaG9\n"
169 "RUPId74Rtmwfi1TLbBzo2wIDAQABo3YwdDAMBgNVHRMBAf8EAjAAMBMGA1UdJQQM\n" 170 "RUPId74Rtmwfi1TLbBzo2wIDAQABo3YwdDAMBgNVHRMBAf8EAjAAMBMGA1UdJQQM\n"
170 "MAoGCCsGAQUFBwMBMA8GA1UdDwEB/wQFAwMHIAAwHQYDVR0OBBYEFOFi4ilKOP1d\n" 171 "MAoGCCsGAQUFBwMBMA8GA1UdDwEB/wQFAwMHIAAwHQYDVR0OBBYEFOFi4ilKOP1d\n"
171 "XHlWCMwmVKr7mgy8MB8GA1UdIwQYMBaAFP2olB4s2T/xuoQ5pT2RKojFwZo2MAsG\n" 172 "XHlWCMwmVKr7mgy8MB8GA1UdIwQYMBaAFP2olB4s2T/xuoQ5pT2RKojFwZo2MAsG\n"
172 "CSqGSIb3DQEBBQOCAQEAHVWPxazupbOkG7Did+dY9z2z6RjTzYvurTtEKQgzM2Vz\n" 173 "CSqGSIb3DQEBBQOCAQEAHVWPxazupbOkG7Did+dY9z2z6RjTzYvurTtEKQgzM2Vz\n"
173 "GQBA+3pZ3c5mS97fPIs9hZXfnQeelMeZ2XP1a+9vp35bJjZBBhVH+pqxjCgiUflg\n" 174 "GQBA+3pZ3c5mS97fPIs9hZXfnQeelMeZ2XP1a+9vp35bJjZBBhVH+pqxjCgiUflg\n"
174 "A3Zqy0XwwVCgQLE2HyaU3DLUD/aeIFK5gJaOSdNTXZLv43K8kl4cqDbMeRpVTbkt\n" 175 "A3Zqy0XwwVCgQLE2HyaU3DLUD/aeIFK5gJaOSdNTXZLv43K8kl4cqDbMeRpVTbkt\n"
175 "YmG4AyEOYRNKGTqMEJXJoxD5E3rBUNrVI/XyTjYrulxbNPcMWEHKNeeqWpKDYTFo\n" 176 "YmG4AyEOYRNKGTqMEJXJoxD5E3rBUNrVI/XyTjYrulxbNPcMWEHKNeeqWpKDYTFo\n"
176 "Bb01PCthGXiq/4A2RLAFosadzRa8SBpoSjPPfZ0b2w4MJpReHqKbR5+T2t6hzml6\n" 177 "Bb01PCthGXiq/4A2RLAFosadzRa8SBpoSjPPfZ0b2w4MJpReHqKbR5+T2t6hzml6\n"
177 "4ToyOKPDmamiTuN5KzLN3cw7DQlvWMvqSOChPLnA3Q==\n" 178 "4ToyOKPDmamiTuN5KzLN3cw7DQlvWMvqSOChPLnA3Q==\n"
178 "-----END CERTIFICATE-----\n"; 179 "-----END CERTIFICATE-----\n";
179 180
180 181
181int 182int
@@ -185,36 +186,37 @@ main (int argc, char *const *argv)
185 unsigned int i; 186 unsigned int i;
186 187
187 if (argc != 2) 188 if (argc != 2)
188 { 189 {
189 printf ("%s PORT\n", argv[0]); 190 printf ("%s PORT\n", argv[0]);
190 return 1; 191 return 1;
191 } 192 }
192 response = MHD_create_response_from_buffer (strlen (PAGE), 193 response = MHD_create_response_from_buffer (strlen (PAGE),
193 (void *) PAGE, 194 (void *) PAGE,
194 MHD_RESPMEM_PERSISTENT); 195 MHD_RESPMEM_PERSISTENT);
195 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_TLS 196 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_TLS
196#ifdef EPOLL_SUPPORT 197#ifdef EPOLL_SUPPORT
197 | MHD_USE_EPOLL | MHD_USE_TURBO 198 | MHD_USE_EPOLL | MHD_USE_TURBO
198#endif 199#endif
199 , 200 ,
200 atoi (argv[1]), 201 atoi (argv[1]),
201 NULL, NULL, &ahc_echo, NULL, 202 NULL, NULL, &ahc_echo, NULL,
202 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, 203 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
203 MHD_OPTION_THREAD_POOL_SIZE, (unsigned int) NUMBER_OF_THREADS, 204 MHD_OPTION_THREAD_POOL_SIZE, (unsigned
204 MHD_OPTION_URI_LOG_CALLBACK, &uri_logger_cb, NULL, 205 int) NUMBER_OF_THREADS,
205 MHD_OPTION_NOTIFY_COMPLETED, &completed_callback, NULL, 206 MHD_OPTION_URI_LOG_CALLBACK, &uri_logger_cb, NULL,
206 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 1000, 207 MHD_OPTION_NOTIFY_COMPLETED, &completed_callback, NULL,
207 /* Optionally, the gnutls_load_file() can be used to 208 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 1000,
208 load the key and the certificate from file. */ 209 /* Optionally, the gnutls_load_file() can be used to
209 MHD_OPTION_HTTPS_MEM_KEY, srv_signed_key_pem, 210 load the key and the certificate from file. */
210 MHD_OPTION_HTTPS_MEM_CERT, srv_signed_cert_pem, 211 MHD_OPTION_HTTPS_MEM_KEY, srv_signed_key_pem,
211 MHD_OPTION_END); 212 MHD_OPTION_HTTPS_MEM_CERT, srv_signed_cert_pem,
213 MHD_OPTION_END);
212 if (d == NULL) 214 if (d == NULL)
213 return 1; 215 return 1;
214 (void) getc (stdin); 216 (void) getc (stdin);
215 MHD_stop_daemon (d); 217 MHD_stop_daemon (d);
216 MHD_destroy_response (response); 218 MHD_destroy_response (response);
217 for (i=0;i<SMALL;i++) 219 for (i = 0; i<SMALL; i++)
218 if (0 != small_deltas[i]) 220 if (0 != small_deltas[i])
219 fprintf (stdout, "D: %d %u\n", i, small_deltas[i]); 221 fprintf (stdout, "D: %d %u\n", i, small_deltas[i]);
220 return 0; 222 return 0;
diff --git a/src/examples/chunked_example.c b/src/examples/chunked_example.c
index 26dacd3b..cd6539ef 100644
--- a/src/examples/chunked_example.c
+++ b/src/examples/chunked_example.c
@@ -40,15 +40,15 @@ callback (void *cls,
40 size_t buf_size) 40 size_t buf_size)
41{ 41{
42 size_t size_to_copy; 42 size_t size_to_copy;
43 struct ResponseContentCallbackParam * const param = 43 struct ResponseContentCallbackParam *const param =
44 (struct ResponseContentCallbackParam *)cls; 44 (struct ResponseContentCallbackParam *) cls;
45 45
46 /* Note: 'pos' will never exceed size of transmitted data. */ 46 /* Note: 'pos' will never exceed size of transmitted data. */
47 /* You can use 'pos == param->response_size' in next check. */ 47 /* You can use 'pos == param->response_size' in next check. */
48 if (pos >= param->response_size) 48 if (pos >= param->response_size)
49 { /* Whole response was sent. Signal end of response. */ 49 { /* Whole response was sent. Signal end of response. */
50 return MHD_CONTENT_READER_END_OF_STREAM; 50 return MHD_CONTENT_READER_END_OF_STREAM;
51 } 51 }
52 52
53 /* Pseudo code. * 53 /* Pseudo code. *
54 if (data_not_ready) 54 if (data_not_ready)
@@ -82,12 +82,13 @@ callback (void *cls,
82static void 82static void
83free_callback_param (void *cls) 83free_callback_param (void *cls)
84{ 84{
85 free(cls); 85 free (cls);
86} 86}
87 87
88 88
89static const char simple_response_text[] = "<html><head><title>Simple response</title></head>" 89static const char simple_response_text[] =
90 "<body>Simple response text</body></html>"; 90 "<html><head><title>Simple response</title></head>"
91 "<body>Simple response text</body></html>";
91 92
92 93
93static int 94static int
@@ -104,27 +105,28 @@ ahc_echo (void *cls,
104 struct ResponseContentCallbackParam *callback_param; 105 struct ResponseContentCallbackParam *callback_param;
105 struct MHD_Response *response; 106 struct MHD_Response *response;
106 int ret; 107 int ret;
107 (void)cls; /* Unused. Silent compiler warning. */ 108 (void) cls; /* Unused. Silent compiler warning. */
108 (void)url; /* Unused. Silent compiler warning. */ 109 (void) url; /* Unused. Silent compiler warning. */
109 (void)version; /* Unused. Silent compiler warning. */ 110 (void) version; /* Unused. Silent compiler warning. */
110 (void)upload_data; /* Unused. Silent compiler warning. */ 111 (void) upload_data; /* Unused. Silent compiler warning. */
111 (void)upload_data_size; /* Unused. Silent compiler warning. */ 112 (void) upload_data_size; /* Unused. Silent compiler warning. */
112 113
113 if (0 != strcmp (method, "GET")) 114 if (0 != strcmp (method, "GET"))
114 return MHD_NO; /* unexpected method */ 115 return MHD_NO; /* unexpected method */
115 if (&aptr != *ptr) 116 if (&aptr != *ptr)
116 { 117 {
117 /* do never respond on first call */ 118 /* do never respond on first call */
118 *ptr = &aptr; 119 *ptr = &aptr;
119 return MHD_YES; 120 return MHD_YES;
120 } 121 }
121 122
122 callback_param = malloc (sizeof(struct ResponseContentCallbackParam)); 123 callback_param = malloc (sizeof(struct ResponseContentCallbackParam));
123 if (NULL == callback_param) 124 if (NULL == callback_param)
124 return MHD_NO; /* Not enough memory. */ 125 return MHD_NO; /* Not enough memory. */
125 126
126 callback_param->response_data = simple_response_text; 127 callback_param->response_data = simple_response_text;
127 callback_param->response_size = (sizeof(simple_response_text)/sizeof(char)) - 1; 128 callback_param->response_size = (sizeof(simple_response_text)
129 / sizeof(char)) - 1;
128 130
129 *ptr = NULL; /* reset when done */ 131 *ptr = NULL; /* reset when done */
130 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 132 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
@@ -150,28 +152,28 @@ main (int argc, char *const *argv)
150 int port; 152 int port;
151 153
152 if (argc != 2) 154 if (argc != 2)
153 { 155 {
154 printf ("%s PORT\n", argv[0]); 156 printf ("%s PORT\n", argv[0]);
155 return 1; 157 return 1;
156 } 158 }
157 port = atoi (argv[1]); 159 port = atoi (argv[1]);
158 if ( (1 > port) || 160 if ( (1 > port) ||
159 (port > UINT16_MAX) ) 161 (port > UINT16_MAX) )
160 { 162 {
161 fprintf (stderr, 163 fprintf (stderr,
162 "Port must be a number between 1 and 65535\n"); 164 "Port must be a number between 1 and 65535\n");
163 return 1; 165 return 1;
164 } 166 }
165 d = MHD_start_daemon (/* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */ 167 d = MHD_start_daemon (/* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */
166 MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 168 MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
167 /* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, */ 169 /* 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, */ 170 /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, */
169 /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */ 171 /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */
170 (uint16_t) port, 172 (uint16_t) port,
171 NULL, NULL, 173 NULL, NULL,
172 &ahc_echo, NULL, 174 &ahc_echo, NULL,
173 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, 175 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
174 MHD_OPTION_END); 176 MHD_OPTION_END);
175 if (NULL == d) 177 if (NULL == d)
176 return 1; 178 return 1;
177 (void) getc (stdin); 179 (void) getc (stdin);
diff --git a/src/examples/connection_close.c b/src/examples/connection_close.c
index 88222cbf..43ee379f 100644
--- a/src/examples/connection_close.c
+++ b/src/examples/connection_close.c
@@ -25,7 +25,8 @@
25#include "platform.h" 25#include "platform.h"
26#include <microhttpd.h> 26#include <microhttpd.h>
27 27
28#define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>" 28#define PAGE \
29 "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>"
29 30
30static int 31static int
31ahc_echo (void *cls, 32ahc_echo (void *cls,
@@ -39,23 +40,23 @@ ahc_echo (void *cls,
39 const char *me = cls; 40 const char *me = cls;
40 struct MHD_Response *response; 41 struct MHD_Response *response;
41 int ret; 42 int ret;
42 (void)url; /* Unused. Silent compiler warning. */ 43 (void) url; /* Unused. Silent compiler warning. */
43 (void)version; /* Unused. Silent compiler warning. */ 44 (void) version; /* Unused. Silent compiler warning. */
44 (void)upload_data; /* Unused. Silent compiler warning. */ 45 (void) upload_data; /* Unused. Silent compiler warning. */
45 (void)upload_data_size; /* Unused. Silent compiler warning. */ 46 (void) upload_data_size; /* Unused. Silent compiler warning. */
46 47
47 if (0 != strcmp (method, "GET")) 48 if (0 != strcmp (method, "GET"))
48 return MHD_NO; /* unexpected method */ 49 return MHD_NO; /* unexpected method */
49 if (&aptr != *ptr) 50 if (&aptr != *ptr)
50 { 51 {
51 /* do never respond on first call */ 52 /* do never respond on first call */
52 *ptr = &aptr; 53 *ptr = &aptr;
53 return MHD_YES; 54 return MHD_YES;
54 } 55 }
55 *ptr = NULL; /* reset when done */ 56 *ptr = NULL; /* reset when done */
56 response = MHD_create_response_from_buffer (strlen (me), 57 response = MHD_create_response_from_buffer (strlen (me),
57 (void *) me, 58 (void *) me,
58 MHD_RESPMEM_PERSISTENT); 59 MHD_RESPMEM_PERSISTENT);
59 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 60 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
60 MHD_destroy_response (response); 61 MHD_destroy_response (response);
61 return ret; 62 return ret;
@@ -65,27 +66,27 @@ ahc_echo (void *cls,
65 66
66static void 67static void
67request_completed (void *cls, 68request_completed (void *cls,
68 struct MHD_Connection *connection, 69 struct MHD_Connection *connection,
69 void **con_cls, 70 void **con_cls,
70 enum MHD_RequestTerminationCode toe) 71 enum MHD_RequestTerminationCode toe)
71{ 72{
72 fprintf (stderr, 73 fprintf (stderr,
73 "%llu - RC: %d\n", 74 "%llu - RC: %d\n",
74 (unsigned long long) __rdtsc(), 75 (unsigned long long) __rdtsc (),
75 toe); 76 toe);
76} 77}
77 78
78 79
79static void 80static void
80connection_completed (void *cls, 81connection_completed (void *cls,
81 struct MHD_Connection *connection, 82 struct MHD_Connection *connection,
82 void **socket_context, 83 void **socket_context,
83 enum MHD_ConnectionNotificationCode toe) 84 enum MHD_ConnectionNotificationCode toe)
84{ 85{
85 fprintf (stderr, 86 fprintf (stderr,
86 "%llu - CC: %d\n", 87 "%llu - CC: %d\n",
87 (unsigned long long) __rdtsc(), 88 (unsigned long long) __rdtsc (),
88 toe); 89 toe);
89} 90}
90 91
91 92
@@ -95,22 +96,23 @@ main (int argc, char *const *argv)
95 struct MHD_Daemon *d; 96 struct MHD_Daemon *d;
96 97
97 if (argc != 2) 98 if (argc != 2)
98 { 99 {
99 printf ("%s PORT\n", argv[0]); 100 printf ("%s PORT\n", argv[0]);
100 return 1; 101 return 1;
101 } 102 }
102 d = MHD_start_daemon (/* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */ 103 d = MHD_start_daemon (/* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */
103 /* MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */ 104 /* MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */
104 /* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, */ 105 /* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, */
105 MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, // | MHD_USE_ITC, 106 MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD
106 /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */ 107 | MHD_USE_ERROR_LOG | MHD_USE_POLL, // | MHD_USE_ITC,
107 atoi (argv[1]), 108 /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */
108 NULL, NULL, &ahc_echo, PAGE, 109 atoi (argv[1]),
109 MHD_OPTION_NOTIFY_COMPLETED, &request_completed, NULL, 110 NULL, NULL, &ahc_echo, PAGE,
110 MHD_OPTION_NOTIFY_CONNECTION, &connection_completed, NULL, 111 MHD_OPTION_NOTIFY_COMPLETED, &request_completed, NULL,
111 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, 112 MHD_OPTION_NOTIFY_CONNECTION, &connection_completed, NULL,
112 MHD_OPTION_STRICT_FOR_CLIENT, (int) 1, 113 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
113 MHD_OPTION_END); 114 MHD_OPTION_STRICT_FOR_CLIENT, (int) 1,
115 MHD_OPTION_END);
114 if (d == NULL) 116 if (d == NULL)
115 return 1; 117 return 1;
116 (void) getc (stdin); 118 (void) getc (stdin);
diff --git a/src/examples/demo.c b/src/examples/demo.c
index e93fb34b..375f2a63 100644
--- a/src/examples/demo.c
+++ b/src/examples/demo.c
@@ -41,10 +41,10 @@
41#include <limits.h> 41#include <limits.h>
42#include <ctype.h> 42#include <ctype.h>
43 43
44#if defined(CPU_COUNT) && (CPU_COUNT+0) < 2 44#if defined(CPU_COUNT) && (CPU_COUNT + 0) < 2
45#undef CPU_COUNT 45#undef CPU_COUNT
46#endif 46#endif
47#if !defined(CPU_COUNT) 47#if ! defined(CPU_COUNT)
48#define CPU_COUNT 2 48#define CPU_COUNT 2
49#endif 49#endif
50 50
@@ -73,46 +73,50 @@
73/** 73/**
74 * Page returned for file-not-found. 74 * Page returned for file-not-found.
75 */ 75 */
76#define FILE_NOT_FOUND_PAGE "<html><head><title>File not found</title></head><body>File not found</body></html>" 76#define FILE_NOT_FOUND_PAGE \
77 "<html><head><title>File not found</title></head><body>File not found</body></html>"
77 78
78 79
79/** 80/**
80 * Page returned for internal errors. 81 * Page returned for internal errors.
81 */ 82 */
82#define INTERNAL_ERROR_PAGE "<html><head><title>Internal error</title></head><body>Internal error</body></html>" 83#define INTERNAL_ERROR_PAGE \
84 "<html><head><title>Internal error</title></head><body>Internal error</body></html>"
83 85
84 86
85/** 87/**
86 * Page returned for refused requests. 88 * Page returned for refused requests.
87 */ 89 */
88#define REQUEST_REFUSED_PAGE "<html><head><title>Request refused</title></head><body>Request refused (file exists?)</body></html>" 90#define REQUEST_REFUSED_PAGE \
91 "<html><head><title>Request refused</title></head><body>Request refused (file exists?)</body></html>"
89 92
90 93
91/** 94/**
92 * Head of index page. 95 * Head of index page.
93 */ 96 */
94#define INDEX_PAGE_HEADER "<html>\n<head><title>Welcome</title></head>\n<body>\n"\ 97#define INDEX_PAGE_HEADER \
95 "<h1>Upload</h1>\n"\ 98 "<html>\n<head><title>Welcome</title></head>\n<body>\n" \
96 "<form method=\"POST\" enctype=\"multipart/form-data\" action=\"/\">\n"\ 99 "<h1>Upload</h1>\n" \
97 "<dl><dt>Content type:</dt><dd>"\ 100 "<form method=\"POST\" enctype=\"multipart/form-data\" action=\"/\">\n" \
98 "<input type=\"radio\" name=\"category\" value=\"books\">Book</input>"\ 101 "<dl><dt>Content type:</dt><dd>" \
99 "<input type=\"radio\" name=\"category\" value=\"images\">Image</input>"\ 102 "<input type=\"radio\" name=\"category\" value=\"books\">Book</input>" \
100 "<input type=\"radio\" name=\"category\" value=\"music\">Music</input>"\ 103 "<input type=\"radio\" name=\"category\" value=\"images\">Image</input>" \
101 "<input type=\"radio\" name=\"category\" value=\"software\">Software</input>"\ 104 "<input type=\"radio\" name=\"category\" value=\"music\">Music</input>" \
102 "<input type=\"radio\" name=\"category\" value=\"videos\">Videos</input>\n"\ 105 "<input type=\"radio\" name=\"category\" value=\"software\">Software</input>" \
103 "<input type=\"radio\" name=\"category\" value=\"other\" checked>Other</input></dd>"\ 106 "<input type=\"radio\" name=\"category\" value=\"videos\">Videos</input>\n" \
104 "<dt>Language:</dt><dd>"\ 107 "<input type=\"radio\" name=\"category\" value=\"other\" checked>Other</input></dd>" \
105 "<input type=\"radio\" name=\"language\" value=\"no-lang\" checked>none</input>"\ 108 "<dt>Language:</dt><dd>" \
106 "<input type=\"radio\" name=\"language\" value=\"en\">English</input>"\ 109 "<input type=\"radio\" name=\"language\" value=\"no-lang\" checked>none</input>" \
107 "<input type=\"radio\" name=\"language\" value=\"de\">German</input>"\ 110 "<input type=\"radio\" name=\"language\" value=\"en\">English</input>" \
108 "<input type=\"radio\" name=\"language\" value=\"fr\">French</input>"\ 111 "<input type=\"radio\" name=\"language\" value=\"de\">German</input>" \
109 "<input type=\"radio\" name=\"language\" value=\"es\">Spanish</input></dd>\n"\ 112 "<input type=\"radio\" name=\"language\" value=\"fr\">French</input>" \
110 "<dt>File:</dt><dd>"\ 113 "<input type=\"radio\" name=\"language\" value=\"es\">Spanish</input></dd>\n" \
111 "<input type=\"file\" name=\"upload\"/></dd></dl>"\ 114 "<dt>File:</dt><dd>" \
112 "<input type=\"submit\" value=\"Send!\"/>\n"\ 115 "<input type=\"file\" name=\"upload\"/></dd></dl>" \
113 "</form>\n"\ 116 "<input type=\"submit\" value=\"Send!\"/>\n" \
114 "<h1>Download</h1>\n"\ 117 "</form>\n" \
115 "<ol>\n" 118 "<h1>Download</h1>\n" \
119 "<ol>\n"
116 120
117/** 121/**
118 * Footer of index page. 122 * Footer of index page.
@@ -124,16 +128,15 @@
124 * NULL-terminated array of supported upload categories. Should match HTML 128 * NULL-terminated array of supported upload categories. Should match HTML
125 * in the form. 129 * in the form.
126 */ 130 */
127static const char * const categories[] = 131static const char *const categories[] = {
128 { 132 "books",
129 "books", 133 "images",
130 "images", 134 "music",
131 "music", 135 "software",
132 "software", 136 "videos",
133 "videos", 137 "other",
134 "other", 138 NULL,
135 NULL, 139};
136 };
137 140
138 141
139/** 142/**
@@ -157,15 +160,14 @@ struct Language
157 * NULL-terminated array of supported upload categories. Should match HTML 160 * NULL-terminated array of supported upload categories. Should match HTML
158 * in the form. 161 * in the form.
159 */ 162 */
160static const struct Language languages[] = 163static const struct Language languages[] = {
161 { 164 { "no-lang", "No language specified" },
162 { "no-lang", "No language specified" }, 165 { "en", "English" },
163 { "en", "English" }, 166 { "de", "German" },
164 { "de", "German" }, 167 { "fr", "French" },
165 { "fr", "French" }, 168 { "es", "Spanish" },
166 { "es", "Spanish" }, 169 { NULL, NULL },
167 { NULL, NULL }, 170};
168 };
169 171
170 172
171/** 173/**
@@ -210,8 +212,8 @@ static void
210mark_as_html (struct MHD_Response *response) 212mark_as_html (struct MHD_Response *response)
211{ 213{
212 (void) MHD_add_response_header (response, 214 (void) MHD_add_response_header (response,
213 MHD_HTTP_HEADER_CONTENT_TYPE, 215 MHD_HTTP_HEADER_CONTENT_TYPE,
214 "text/html"); 216 "text/html");
215} 217}
216 218
217 219
@@ -264,7 +266,7 @@ struct ResponseDataContext
264 */ 266 */
265static int 267static int
266list_directory (struct ResponseDataContext *rdc, 268list_directory (struct ResponseDataContext *rdc,
267 const char *dirname) 269 const char *dirname)
268{ 270{
269 char fullname[PATH_MAX]; 271 char fullname[PATH_MAX];
270 struct stat sbuf; 272 struct stat sbuf;
@@ -274,35 +276,35 @@ list_directory (struct ResponseDataContext *rdc,
274 if (NULL == (dir = opendir (dirname))) 276 if (NULL == (dir = opendir (dirname)))
275 return MHD_NO; 277 return MHD_NO;
276 while (NULL != (de = readdir (dir))) 278 while (NULL != (de = readdir (dir)))
279 {
280 if ('.' == de->d_name[0])
281 continue;
282 if (sizeof (fullname) <= (unsigned int)
283 snprintf (fullname, sizeof (fullname),
284 "%s/%s",
285 dirname, de->d_name))
286 continue; /* ugh, file too long? how can this be!? */
287 if (0 != stat (fullname, &sbuf))
288 continue; /* ugh, failed to 'stat' */
289 if (! S_ISREG (sbuf.st_mode))
290 continue; /* not a regular file, skip */
291 if (rdc->off + 1024 > rdc->buf_len)
277 { 292 {
278 if ('.' == de->d_name[0]) 293 void *r;
279 continue; 294
280 if (sizeof (fullname) <= (unsigned int) 295 if ( (2 * rdc->buf_len + 1024) < rdc->buf_len)
281 snprintf (fullname, sizeof (fullname), 296 break; /* more than SIZE_T _index_ size? Too big for us */
282 "%s/%s", 297 rdc->buf_len = 2 * rdc->buf_len + 1024;
283 dirname, de->d_name)) 298 if (NULL == (r = realloc (rdc->buf, rdc->buf_len)))
284 continue; /* ugh, file too long? how can this be!? */ 299 break; /* out of memory */
285 if (0 != stat (fullname, &sbuf)) 300 rdc->buf = r;
286 continue; /* ugh, failed to 'stat' */
287 if (! S_ISREG (sbuf.st_mode))
288 continue; /* not a regular file, skip */
289 if (rdc->off + 1024 > rdc->buf_len)
290 {
291 void *r;
292
293 if ( (2 * rdc->buf_len + 1024) < rdc->buf_len)
294 break; /* more than SIZE_T _index_ size? Too big for us */
295 rdc->buf_len = 2 * rdc->buf_len + 1024;
296 if (NULL == (r = realloc (rdc->buf, rdc->buf_len)))
297 break; /* out of memory */
298 rdc->buf = r;
299 }
300 rdc->off += snprintf (&rdc->buf[rdc->off],
301 rdc->buf_len - rdc->off,
302 "<li><a href=\"/%s\">%s</a></li>\n",
303 fullname,
304 de->d_name);
305 } 301 }
302 rdc->off += snprintf (&rdc->buf[rdc->off],
303 rdc->buf_len - rdc->off,
304 "<li><a href=\"/%s\">%s</a></li>\n",
305 fullname,
306 de->d_name);
307 }
306 (void) closedir (dir); 308 (void) closedir (dir);
307 return MHD_YES; 309 return MHD_YES;
308} 310}
@@ -326,65 +328,66 @@ update_directory ()
326 328
327 rdc.buf_len = initial_allocation; 329 rdc.buf_len = initial_allocation;
328 if (NULL == (rdc.buf = malloc (rdc.buf_len))) 330 if (NULL == (rdc.buf = malloc (rdc.buf_len)))
329 { 331 {
330 update_cached_response (NULL); 332 update_cached_response (NULL);
331 return; 333 return;
332 } 334 }
333 rdc.off = snprintf (rdc.buf, rdc.buf_len, 335 rdc.off = snprintf (rdc.buf, rdc.buf_len,
334 "%s", 336 "%s",
335 INDEX_PAGE_HEADER); 337 INDEX_PAGE_HEADER);
336 for (language_idx = 0; NULL != languages[language_idx].dirname; language_idx++) 338 for (language_idx = 0; NULL != languages[language_idx].dirname;
339 language_idx++)
340 {
341 language = &languages[language_idx];
342
343 if (0 != stat (language->dirname, &sbuf))
344 continue; /* empty */
345 /* we ensured always +1k room, filenames are ~256 bytes,
346 so there is always still enough space for the header
347 without need for an additional reallocation check. */
348 rdc.off += snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off,
349 "<h2>%s</h2>\n",
350 language->longname);
351 for (category_idx = 0; NULL != categories[category_idx]; category_idx++)
337 { 352 {
338 language = &languages[language_idx]; 353 category = categories[category_idx];
354 snprintf (dir_name, sizeof (dir_name),
355 "%s/%s",
356 language->dirname,
357 category);
358 if (0 != stat (dir_name, &sbuf))
359 continue; /* empty */
339 360
340 if (0 != stat (language->dirname, &sbuf))
341 continue; /* empty */
342 /* we ensured always +1k room, filenames are ~256 bytes, 361 /* we ensured always +1k room, filenames are ~256 bytes,
343 so there is always still enough space for the header 362 so there is always still enough space for the header
344 without need for an additional reallocation check. */ 363 without need for an additional reallocation check. */
345 rdc.off += snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off, 364 rdc.off += snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off,
346 "<h2>%s</h2>\n", 365 "<h3>%s</h3>\n",
347 language->longname); 366 category);
348 for (category_idx = 0; NULL != categories[category_idx]; category_idx++) 367
349 { 368 if (MHD_NO == list_directory (&rdc, dir_name))
350 category = categories[category_idx]; 369 {
351 snprintf (dir_name, sizeof (dir_name), 370 free (rdc.buf);
352 "%s/%s", 371 update_cached_response (NULL);
353 language->dirname, 372 return;
354 category); 373 }
355 if (0 != stat (dir_name, &sbuf))
356 continue; /* empty */
357
358 /* we ensured always +1k room, filenames are ~256 bytes,
359 so there is always still enough space for the header
360 without need for an additional reallocation check. */
361 rdc.off += snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off,
362 "<h3>%s</h3>\n",
363 category);
364
365 if (MHD_NO == list_directory (&rdc, dir_name))
366 {
367 free (rdc.buf);
368 update_cached_response (NULL);
369 return;
370 }
371 }
372 } 374 }
375 }
373 /* we ensured always +1k room, filenames are ~256 bytes, 376 /* we ensured always +1k room, filenames are ~256 bytes,
374 so there is always still enough space for the footer 377 so there is always still enough space for the footer
375 without need for a final reallocation check. */ 378 without need for a final reallocation check. */
376 rdc.off += snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off, 379 rdc.off += snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off,
377 "%s", 380 "%s",
378 INDEX_PAGE_FOOTER); 381 INDEX_PAGE_FOOTER);
379 initial_allocation = rdc.buf_len; /* remember for next time */ 382 initial_allocation = rdc.buf_len; /* remember for next time */
380 response = MHD_create_response_from_buffer (rdc.off, 383 response = MHD_create_response_from_buffer (rdc.off,
381 rdc.buf, 384 rdc.buf,
382 MHD_RESPMEM_MUST_FREE); 385 MHD_RESPMEM_MUST_FREE);
383 mark_as_html (response); 386 mark_as_html (response);
384#if FORCE_CLOSE 387#if FORCE_CLOSE
385 (void) MHD_add_response_header (response, 388 (void) MHD_add_response_header (response,
386 MHD_HTTP_HEADER_CONNECTION, 389 MHD_HTTP_HEADER_CONNECTION,
387 "close"); 390 "close");
388#endif 391#endif
389 update_cached_response (response); 392 update_cached_response (response);
390} 393}
@@ -443,8 +446,8 @@ struct UploadContext
443 */ 446 */
444static int 447static int
445do_append (char **ret, 448do_append (char **ret,
446 const char *data, 449 const char *data,
447 size_t size) 450 size_t size)
448{ 451{
449 char *buf; 452 char *buf;
450 size_t old_len; 453 size_t old_len;
@@ -456,15 +459,15 @@ do_append (char **ret,
456 if (NULL == (buf = malloc (old_len + size + 1))) 459 if (NULL == (buf = malloc (old_len + size + 1)))
457 return MHD_NO; 460 return MHD_NO;
458 if (NULL != *ret) 461 if (NULL != *ret)
459 { 462 {
460 memcpy (buf, 463 memcpy (buf,
461 *ret, 464 *ret,
462 old_len); 465 old_len);
463 free (*ret); 466 free (*ret);
464 } 467 }
465 memcpy (&buf[old_len], 468 memcpy (&buf[old_len],
466 data, 469 data,
467 size); 470 size);
468 buf[old_len + size] = '\0'; 471 buf[old_len + size] = '\0';
469 *ret = buf; 472 *ret = buf;
470 return MHD_YES; 473 return MHD_YES;
@@ -492,119 +495,119 @@ do_append (char **ret,
492 */ 495 */
493static int 496static int
494process_upload_data (void *cls, 497process_upload_data (void *cls,
495 enum MHD_ValueKind kind, 498 enum MHD_ValueKind kind,
496 const char *key, 499 const char *key,
497 const char *filename, 500 const char *filename,
498 const char *content_type, 501 const char *content_type,
499 const char *transfer_encoding, 502 const char *transfer_encoding,
500 const char *data, 503 const char *data,
501 uint64_t off, 504 uint64_t off,
502 size_t size) 505 size_t size)
503{ 506{
504 struct UploadContext *uc = cls; 507 struct UploadContext *uc = cls;
505 int i; 508 int i;
506 (void)kind; /* Unused. Silent compiler warning. */ 509 (void) kind; /* Unused. Silent compiler warning. */
507 (void)content_type; /* Unused. Silent compiler warning. */ 510 (void) content_type; /* Unused. Silent compiler warning. */
508 (void)transfer_encoding; /* Unused. Silent compiler warning. */ 511 (void) transfer_encoding; /* Unused. Silent compiler warning. */
509 (void)off; /* Unused. Silent compiler warning. */ 512 (void) off; /* Unused. Silent compiler warning. */
510 513
511 if (0 == strcmp (key, "category")) 514 if (0 == strcmp (key, "category"))
512 return do_append (&uc->category, data, size); 515 return do_append (&uc->category, data, size);
513 if (0 == strcmp (key, "language")) 516 if (0 == strcmp (key, "language"))
514 return do_append (&uc->language, data, size); 517 return do_append (&uc->language, data, size);
515 if (0 != strcmp (key, "upload")) 518 if (0 != strcmp (key, "upload"))
516 { 519 {
517 fprintf (stderr, 520 fprintf (stderr,
518 "Ignoring unexpected form value `%s'\n", 521 "Ignoring unexpected form value `%s'\n",
519 key); 522 key);
520 return MHD_YES; /* ignore */ 523 return MHD_YES; /* ignore */
521 } 524 }
522 if (NULL == filename) 525 if (NULL == filename)
523 { 526 {
524 fprintf (stderr, "No filename, aborting upload\n"); 527 fprintf (stderr, "No filename, aborting upload\n");
525 return MHD_NO; /* no filename, error */ 528 return MHD_NO; /* no filename, error */
526 } 529 }
527 if ( (NULL == uc->category) || 530 if ( (NULL == uc->category) ||
528 (NULL == uc->language) ) 531 (NULL == uc->language) )
532 {
533 fprintf (stderr,
534 "Missing form data for upload `%s'\n",
535 filename);
536 uc->response = request_refused_response;
537 return MHD_NO;
538 }
539 if (-1 == uc->fd)
540 {
541 char fn[PATH_MAX];
542
543 if ( (NULL != strstr (filename, "..")) ||
544 (NULL != strchr (filename, '/')) ||
545 (NULL != strchr (filename, '\\')) )
529 { 546 {
530 fprintf (stderr,
531 "Missing form data for upload `%s'\n",
532 filename);
533 uc->response = request_refused_response; 547 uc->response = request_refused_response;
534 return MHD_NO; 548 return MHD_NO;
535 } 549 }
536 if (-1 == uc->fd) 550 /* create directories -- if they don't exist already */
537 {
538 char fn[PATH_MAX];
539
540 if ( (NULL != strstr (filename, "..")) ||
541 (NULL != strchr (filename, '/')) ||
542 (NULL != strchr (filename, '\\')) )
543 {
544 uc->response = request_refused_response;
545 return MHD_NO;
546 }
547 /* create directories -- if they don't exist already */
548#ifdef WINDOWS 551#ifdef WINDOWS
549 (void) mkdir (uc->language); 552 (void) mkdir (uc->language);
550#else 553#else
551 (void) mkdir (uc->language, S_IRWXU); 554 (void) mkdir (uc->language, S_IRWXU);
552#endif 555#endif
553 snprintf (fn, sizeof (fn), 556 snprintf (fn, sizeof (fn),
554 "%s/%s", 557 "%s/%s",
555 uc->language, 558 uc->language,
556 uc->category); 559 uc->category);
557#ifdef WINDOWS 560#ifdef WINDOWS
558 (void) mkdir (fn); 561 (void) mkdir (fn);
559#else 562#else
560 (void) mkdir (fn, S_IRWXU); 563 (void) mkdir (fn, S_IRWXU);
561#endif 564#endif
562 /* open file */ 565 /* open file */
563 snprintf (fn, sizeof (fn), 566 snprintf (fn, sizeof (fn),
564 "%s/%s/%s", 567 "%s/%s/%s",
565 uc->language, 568 uc->language,
566 uc->category, 569 uc->category,
567 filename); 570 filename);
568 for (i=strlen (fn)-1;i>=0;i--) 571 for (i = strlen (fn) - 1; i>=0; i--)
569 if (! isprint ((unsigned char) fn[i])) 572 if (! isprint ((unsigned char) fn[i]))
570 fn[i] = '_'; 573 fn[i] = '_';
571 uc->fd = open (fn, 574 uc->fd = open (fn,
572 O_CREAT | O_EXCL 575 O_CREAT | O_EXCL
573#if O_LARGEFILE 576#if O_LARGEFILE
574 | O_LARGEFILE 577 | O_LARGEFILE
575#endif 578#endif
576 | O_WRONLY, 579 | O_WRONLY,
577 S_IRUSR | S_IWUSR); 580 S_IRUSR | S_IWUSR);
578 if (-1 == uc->fd) 581 if (-1 == uc->fd)
579 { 582 {
580 fprintf (stderr, 583 fprintf (stderr,
581 "Error opening file `%s' for upload: %s\n", 584 "Error opening file `%s' for upload: %s\n",
582 fn, 585 fn,
583 strerror (errno)); 586 strerror (errno));
584 uc->response = request_refused_response; 587 uc->response = request_refused_response;
585 return MHD_NO; 588 return MHD_NO;
586 }
587 uc->filename = strdup (fn);
588 } 589 }
590 uc->filename = strdup (fn);
591 }
589 if ( (0 != size) && 592 if ( (0 != size) &&
590 (size != (size_t) write (uc->fd, data, size)) ) 593 (size != (size_t) write (uc->fd, data, size)) )
594 {
595 /* write failed; likely: disk full */
596 fprintf (stderr,
597 "Error writing to file `%s': %s\n",
598 uc->filename,
599 strerror (errno));
600 uc->response = internal_error_response;
601 (void) close (uc->fd);
602 uc->fd = -1;
603 if (NULL != uc->filename)
591 { 604 {
592 /* write failed; likely: disk full */ 605 unlink (uc->filename);
593 fprintf (stderr, 606 free (uc->filename);
594 "Error writing to file `%s': %s\n", 607 uc->filename = NULL;
595 uc->filename,
596 strerror (errno));
597 uc->response = internal_error_response;
598 (void) close (uc->fd);
599 uc->fd = -1;
600 if (NULL != uc->filename)
601 {
602 unlink (uc->filename);
603 free (uc->filename);
604 uc->filename = NULL;
605 }
606 return MHD_NO;
607 } 608 }
609 return MHD_NO;
610 }
608 return MHD_YES; 611 return MHD_YES;
609} 612}
610 613
@@ -622,32 +625,32 @@ process_upload_data (void *cls,
622 */ 625 */
623static void 626static void
624response_completed_callback (void *cls, 627response_completed_callback (void *cls,
625 struct MHD_Connection *connection, 628 struct MHD_Connection *connection,
626 void **con_cls, 629 void **con_cls,
627 enum MHD_RequestTerminationCode toe) 630 enum MHD_RequestTerminationCode toe)
628{ 631{
629 struct UploadContext *uc = *con_cls; 632 struct UploadContext *uc = *con_cls;
630 (void)cls; /* Unused. Silent compiler warning. */ 633 (void) cls; /* Unused. Silent compiler warning. */
631 (void)connection; /* Unused. Silent compiler warning. */ 634 (void) connection; /* Unused. Silent compiler warning. */
632 (void)toe; /* Unused. Silent compiler warning. */ 635 (void) toe; /* Unused. Silent compiler warning. */
633 636
634 if (NULL == uc) 637 if (NULL == uc)
635 return; /* this request wasn't an upload request */ 638 return; /* this request wasn't an upload request */
636 if (NULL != uc->pp) 639 if (NULL != uc->pp)
637 { 640 {
638 MHD_destroy_post_processor (uc->pp); 641 MHD_destroy_post_processor (uc->pp);
639 uc->pp = NULL; 642 uc->pp = NULL;
640 } 643 }
641 if (-1 != uc->fd) 644 if (-1 != uc->fd)
642 { 645 {
643 (void) close (uc->fd); 646 (void) close (uc->fd);
644 if (NULL != uc->filename) 647 if (NULL != uc->filename)
645 { 648 {
646 fprintf (stderr, 649 fprintf (stderr,
647 "Upload of file `%s' failed (incomplete or aborted), removing file.\n", 650 "Upload of file `%s' failed (incomplete or aborted), removing file.\n",
648 uc->filename); 651 uc->filename);
649 (void) unlink (uc->filename); 652 (void) unlink (uc->filename);
650 } 653 }
651 } 654 }
652 if (NULL != uc->filename) 655 if (NULL != uc->filename)
653 free (uc->filename); 656 free (uc->filename);
@@ -669,12 +672,12 @@ return_directory_response (struct MHD_Connection *connection)
669 (void) pthread_mutex_lock (&mutex); 672 (void) pthread_mutex_lock (&mutex);
670 if (NULL == cached_directory_response) 673 if (NULL == cached_directory_response)
671 ret = MHD_queue_response (connection, 674 ret = MHD_queue_response (connection,
672 MHD_HTTP_INTERNAL_SERVER_ERROR, 675 MHD_HTTP_INTERNAL_SERVER_ERROR,
673 internal_error_response); 676 internal_error_response);
674 else 677 else
675 ret = MHD_queue_response (connection, 678 ret = MHD_queue_response (connection,
676 MHD_HTTP_OK, 679 MHD_HTTP_OK,
677 cached_directory_response); 680 cached_directory_response);
678 (void) pthread_mutex_unlock (&mutex); 681 (void) pthread_mutex_unlock (&mutex);
679 return ret; 682 return ret;
680} 683}
@@ -695,12 +698,12 @@ return_directory_response (struct MHD_Connection *connection)
695 */ 698 */
696static int 699static int
697generate_page (void *cls, 700generate_page (void *cls,
698 struct MHD_Connection *connection, 701 struct MHD_Connection *connection,
699 const char *url, 702 const char *url,
700 const char *method, 703 const char *method,
701 const char *version, 704 const char *version,
702 const char *upload_data, 705 const char *upload_data,
703 size_t *upload_data_size, void **ptr) 706 size_t *upload_data_size, void **ptr)
704{ 707{
705 struct MHD_Response *response; 708 struct MHD_Response *response;
706 int ret; 709 int ret;
@@ -710,117 +713,117 @@ generate_page (void *cls,
710 (void) version; /* Unused. Silent compiler warning. */ 713 (void) version; /* Unused. Silent compiler warning. */
711 714
712 if (0 != strcmp (url, "/")) 715 if (0 != strcmp (url, "/"))
713 { 716 {
714 /* should be file download */ 717 /* should be file download */
715#ifdef MHD_HAVE_LIBMAGIC 718#ifdef MHD_HAVE_LIBMAGIC
716 char file_data[MAGIC_HEADER_SIZE]; 719 char file_data[MAGIC_HEADER_SIZE];
717 ssize_t got; 720 ssize_t got;
718#endif /* MHD_HAVE_LIBMAGIC */ 721#endif /* MHD_HAVE_LIBMAGIC */
719 const char *mime; 722 const char *mime;
720 723
721 if ( (0 != strcmp (method, MHD_HTTP_METHOD_GET)) && 724 if ( (0 != strcmp (method, MHD_HTTP_METHOD_GET)) &&
722 (0 != strcmp (method, MHD_HTTP_METHOD_HEAD)) ) 725 (0 != strcmp (method, MHD_HTTP_METHOD_HEAD)) )
723 return MHD_NO; /* unexpected method (we're not polite...) */ 726 return MHD_NO; /* unexpected method (we're not polite...) */
724 fd = -1; 727 fd = -1;
725 if ( (NULL == strstr (&url[1], "..")) && 728 if ( (NULL == strstr (&url[1], "..")) &&
726 ('/' != url[1]) ) 729 ('/' != url[1]) )
727 { 730 {
728 fd = open (&url[1], O_RDONLY); 731 fd = open (&url[1], O_RDONLY);
729 if ( (-1 != fd) && 732 if ( (-1 != fd) &&
730 ( (0 != fstat (fd, &buf)) || 733 ( (0 != fstat (fd, &buf)) ||
731 (! S_ISREG (buf.st_mode)) ) ) 734 (! S_ISREG (buf.st_mode)) ) )
732 { 735 {
733 (void) close (fd); 736 (void) close (fd);
734 fd = -1; 737 fd = -1;
735 } 738 }
736 } 739 }
737 if (-1 == fd) 740 if (-1 == fd)
738 return MHD_queue_response (connection, 741 return MHD_queue_response (connection,
739 MHD_HTTP_NOT_FOUND, 742 MHD_HTTP_NOT_FOUND,
740 file_not_found_response); 743 file_not_found_response);
741#ifdef MHD_HAVE_LIBMAGIC 744#ifdef MHD_HAVE_LIBMAGIC
742 /* read beginning of the file to determine mime type */ 745 /* read beginning of the file to determine mime type */
743 got = read (fd, file_data, sizeof (file_data)); 746 got = read (fd, file_data, sizeof (file_data));
744 (void) lseek (fd, 0, SEEK_SET); 747 (void) lseek (fd, 0, SEEK_SET);
745 if (-1 != got) 748 if (-1 != got)
746 mime = magic_buffer (magic, file_data, got); 749 mime = magic_buffer (magic, file_data, got);
747 else 750 else
748#endif /* MHD_HAVE_LIBMAGIC */ 751#endif /* MHD_HAVE_LIBMAGIC */
749 mime = NULL; 752 mime = NULL;
750 753
751 if (NULL == (response = MHD_create_response_from_fd (buf.st_size, 754 if (NULL == (response = MHD_create_response_from_fd (buf.st_size,
752 fd))) 755 fd)))
753 { 756 {
754 /* internal error (i.e. out of memory) */ 757 /* internal error (i.e. out of memory) */
755 (void) close (fd); 758 (void) close (fd);
756 return MHD_NO; 759 return MHD_NO;
757 }
758
759 /* add mime type if we had one */
760 if (NULL != mime)
761 (void) MHD_add_response_header (response,
762 MHD_HTTP_HEADER_CONTENT_TYPE,
763 mime);
764 ret = MHD_queue_response (connection,
765 MHD_HTTP_OK,
766 response);
767 MHD_destroy_response (response);
768 return ret;
769 } 760 }
770 761
762 /* add mime type if we had one */
763 if (NULL != mime)
764 (void) MHD_add_response_header (response,
765 MHD_HTTP_HEADER_CONTENT_TYPE,
766 mime);
767 ret = MHD_queue_response (connection,
768 MHD_HTTP_OK,
769 response);
770 MHD_destroy_response (response);
771 return ret;
772 }
773
771 if (0 == strcmp (method, MHD_HTTP_METHOD_POST)) 774 if (0 == strcmp (method, MHD_HTTP_METHOD_POST))
775 {
776 /* upload! */
777 struct UploadContext *uc = *ptr;
778
779 if (NULL == uc)
780 {
781 if (NULL == (uc = malloc (sizeof (struct UploadContext))))
782 return MHD_NO; /* out of memory, close connection */
783 memset (uc, 0, sizeof (struct UploadContext));
784 uc->fd = -1;
785 uc->connection = connection;
786 uc->pp = MHD_create_post_processor (connection,
787 64 * 1024 /* buffer size */,
788 &process_upload_data, uc);
789 if (NULL == uc->pp)
790 {
791 /* out of memory, close connection */
792 free (uc);
793 return MHD_NO;
794 }
795 *ptr = uc;
796 return MHD_YES;
797 }
798 if (0 != *upload_data_size)
799 {
800 if (NULL == uc->response)
801 (void) MHD_post_process (uc->pp,
802 upload_data,
803 *upload_data_size);
804 *upload_data_size = 0;
805 return MHD_YES;
806 }
807 /* end of upload, finish it! */
808 MHD_destroy_post_processor (uc->pp);
809 uc->pp = NULL;
810 if (-1 != uc->fd)
811 {
812 close (uc->fd);
813 uc->fd = -1;
814 }
815 if (NULL != uc->response)
816 {
817 return MHD_queue_response (connection,
818 MHD_HTTP_FORBIDDEN,
819 uc->response);
820 }
821 else
772 { 822 {
773 /* upload! */ 823 update_directory ();
774 struct UploadContext *uc = *ptr; 824 return return_directory_response (connection);
775
776 if (NULL == uc)
777 {
778 if (NULL == (uc = malloc (sizeof (struct UploadContext))))
779 return MHD_NO; /* out of memory, close connection */
780 memset (uc, 0, sizeof (struct UploadContext));
781 uc->fd = -1;
782 uc->connection = connection;
783 uc->pp = MHD_create_post_processor (connection,
784 64 * 1024 /* buffer size */,
785 &process_upload_data, uc);
786 if (NULL == uc->pp)
787 {
788 /* out of memory, close connection */
789 free (uc);
790 return MHD_NO;
791 }
792 *ptr = uc;
793 return MHD_YES;
794 }
795 if (0 != *upload_data_size)
796 {
797 if (NULL == uc->response)
798 (void) MHD_post_process (uc->pp,
799 upload_data,
800 *upload_data_size);
801 *upload_data_size = 0;
802 return MHD_YES;
803 }
804 /* end of upload, finish it! */
805 MHD_destroy_post_processor (uc->pp);
806 uc->pp = NULL;
807 if (-1 != uc->fd)
808 {
809 close (uc->fd);
810 uc->fd = -1;
811 }
812 if (NULL != uc->response)
813 {
814 return MHD_queue_response (connection,
815 MHD_HTTP_FORBIDDEN,
816 uc->response);
817 }
818 else
819 {
820 update_directory ();
821 return return_directory_response (connection);
822 }
823 } 825 }
826 }
824 if ( (0 == strcmp (method, MHD_HTTP_METHOD_GET)) || 827 if ( (0 == strcmp (method, MHD_HTTP_METHOD_GET)) ||
825 (0 == strcmp (method, MHD_HTTP_METHOD_HEAD)) ) 828 (0 == strcmp (method, MHD_HTTP_METHOD_HEAD)) )
826 { 829 {
@@ -829,8 +832,8 @@ generate_page (void *cls,
829 832
830 /* unexpected request, refuse */ 833 /* unexpected request, refuse */
831 return MHD_queue_response (connection, 834 return MHD_queue_response (connection,
832 MHD_HTTP_FORBIDDEN, 835 MHD_HTTP_FORBIDDEN,
833 request_refused_response); 836 request_refused_response);
834} 837}
835 838
836 839
@@ -843,7 +846,7 @@ generate_page (void *cls,
843static void 846static void
844catcher (int sig) 847catcher (int sig)
845{ 848{
846 (void)sig; /* Unused. Silent compiler warning. */ 849 (void) sig; /* Unused. Silent compiler warning. */
847 /* do nothing */ 850 /* do nothing */
848} 851}
849 852
@@ -889,11 +892,11 @@ main (int argc, char *const *argv)
889 if ( (argc != 2) || 892 if ( (argc != 2) ||
890 (1 != sscanf (argv[1], "%u", &port)) || 893 (1 != sscanf (argv[1], "%u", &port)) ||
891 (UINT16_MAX < port) ) 894 (UINT16_MAX < port) )
892 { 895 {
893 fprintf (stderr, 896 fprintf (stderr,
894 "%s PORT\n", argv[0]); 897 "%s PORT\n", argv[0]);
895 return 1; 898 return 1;
896 } 899 }
897#ifndef MINGW 900#ifndef MINGW
898 ignore_sigpipe (); 901 ignore_sigpipe ();
899#endif 902#endif
@@ -903,31 +906,42 @@ main (int argc, char *const *argv)
903#endif /* MHD_HAVE_LIBMAGIC */ 906#endif /* MHD_HAVE_LIBMAGIC */
904 907
905 (void) pthread_mutex_init (&mutex, NULL); 908 (void) pthread_mutex_init (&mutex, NULL);
906 file_not_found_response = MHD_create_response_from_buffer (strlen (FILE_NOT_FOUND_PAGE), 909 file_not_found_response = MHD_create_response_from_buffer (strlen (
907 (void *) FILE_NOT_FOUND_PAGE, 910 FILE_NOT_FOUND_PAGE),
908 MHD_RESPMEM_PERSISTENT); 911 (void *)
912 FILE_NOT_FOUND_PAGE,
913 MHD_RESPMEM_PERSISTENT);
909 mark_as_html (file_not_found_response); 914 mark_as_html (file_not_found_response);
910 request_refused_response = MHD_create_response_from_buffer (strlen (REQUEST_REFUSED_PAGE), 915 request_refused_response = MHD_create_response_from_buffer (strlen (
911 (void *) REQUEST_REFUSED_PAGE, 916 REQUEST_REFUSED_PAGE),
912 MHD_RESPMEM_PERSISTENT); 917 (void *)
918 REQUEST_REFUSED_PAGE,
919 MHD_RESPMEM_PERSISTENT);
913 mark_as_html (request_refused_response); 920 mark_as_html (request_refused_response);
914 internal_error_response = MHD_create_response_from_buffer (strlen (INTERNAL_ERROR_PAGE), 921 internal_error_response = MHD_create_response_from_buffer (strlen (
915 (void *) INTERNAL_ERROR_PAGE, 922 INTERNAL_ERROR_PAGE),
916 MHD_RESPMEM_PERSISTENT); 923 (void *)
924 INTERNAL_ERROR_PAGE,
925 MHD_RESPMEM_PERSISTENT);
917 mark_as_html (internal_error_response); 926 mark_as_html (internal_error_response);
918 update_directory (); 927 update_directory ();
919 d = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 928 d = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD
929 | MHD_USE_ERROR_LOG,
920 port, 930 port,
921 NULL, NULL, 931 NULL, NULL,
922 &generate_page, NULL, 932 &generate_page, NULL,
923 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (256 * 1024), 933 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (256
934 * 1024),
924#if PRODUCTION 935#if PRODUCTION
925 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) (64), 936 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) (64),
926#endif 937#endif
927 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) (120 /* seconds */), 938 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned
928 MHD_OPTION_THREAD_POOL_SIZE, (unsigned int) NUMBER_OF_THREADS, 939 int) (120 /* seconds */),
929 MHD_OPTION_NOTIFY_COMPLETED, &response_completed_callback, NULL, 940 MHD_OPTION_THREAD_POOL_SIZE, (unsigned
930 MHD_OPTION_END); 941 int) NUMBER_OF_THREADS,
942 MHD_OPTION_NOTIFY_COMPLETED,
943 &response_completed_callback, NULL,
944 MHD_OPTION_END);
931 if (NULL == d) 945 if (NULL == d)
932 return 1; 946 return 1;
933 fprintf (stderr, "HTTP server running. Press ENTER to stop the server\n"); 947 fprintf (stderr, "HTTP server running. Press ENTER to stop the server\n");
diff --git a/src/examples/demo_https.c b/src/examples/demo_https.c
index f2230b16..7f7eb3a5 100644
--- a/src/examples/demo_https.c
+++ b/src/examples/demo_https.c
@@ -44,10 +44,10 @@
44#include <limits.h> 44#include <limits.h>
45#include <ctype.h> 45#include <ctype.h>
46 46
47#if defined(CPU_COUNT) && (CPU_COUNT+0) < 2 47#if defined(CPU_COUNT) && (CPU_COUNT + 0) < 2
48#undef CPU_COUNT 48#undef CPU_COUNT
49#endif 49#endif
50#if !defined(CPU_COUNT) 50#if ! defined(CPU_COUNT)
51#define CPU_COUNT 2 51#define CPU_COUNT 2
52#endif 52#endif
53 53
@@ -76,46 +76,50 @@
76/** 76/**
77 * Page returned for file-not-found. 77 * Page returned for file-not-found.
78 */ 78 */
79#define FILE_NOT_FOUND_PAGE "<html><head><title>File not found</title></head><body>File not found</body></html>" 79#define FILE_NOT_FOUND_PAGE \
80 "<html><head><title>File not found</title></head><body>File not found</body></html>"
80 81
81 82
82/** 83/**
83 * Page returned for internal errors. 84 * Page returned for internal errors.
84 */ 85 */
85#define INTERNAL_ERROR_PAGE "<html><head><title>Internal error</title></head><body>Internal error</body></html>" 86#define INTERNAL_ERROR_PAGE \
87 "<html><head><title>Internal error</title></head><body>Internal error</body></html>"
86 88
87 89
88/** 90/**
89 * Page returned for refused requests. 91 * Page returned for refused requests.
90 */ 92 */
91#define REQUEST_REFUSED_PAGE "<html><head><title>Request refused</title></head><body>Request refused (file exists?)</body></html>" 93#define REQUEST_REFUSED_PAGE \
94 "<html><head><title>Request refused</title></head><body>Request refused (file exists?)</body></html>"
92 95
93 96
94/** 97/**
95 * Head of index page. 98 * Head of index page.
96 */ 99 */
97#define INDEX_PAGE_HEADER "<html>\n<head><title>Welcome</title></head>\n<body>\n"\ 100#define INDEX_PAGE_HEADER \
98 "<h1>Upload</h1>\n"\ 101 "<html>\n<head><title>Welcome</title></head>\n<body>\n" \
99 "<form method=\"POST\" enctype=\"multipart/form-data\" action=\"/\">\n"\ 102 "<h1>Upload</h1>\n" \
100 "<dl><dt>Content type:</dt><dd>"\ 103 "<form method=\"POST\" enctype=\"multipart/form-data\" action=\"/\">\n" \
101 "<input type=\"radio\" name=\"category\" value=\"books\">Book</input>"\ 104 "<dl><dt>Content type:</dt><dd>" \
102 "<input type=\"radio\" name=\"category\" value=\"images\">Image</input>"\ 105 "<input type=\"radio\" name=\"category\" value=\"books\">Book</input>" \
103 "<input type=\"radio\" name=\"category\" value=\"music\">Music</input>"\ 106 "<input type=\"radio\" name=\"category\" value=\"images\">Image</input>" \
104 "<input type=\"radio\" name=\"category\" value=\"software\">Software</input>"\ 107 "<input type=\"radio\" name=\"category\" value=\"music\">Music</input>" \
105 "<input type=\"radio\" name=\"category\" value=\"videos\">Videos</input>\n"\ 108 "<input type=\"radio\" name=\"category\" value=\"software\">Software</input>" \
106 "<input type=\"radio\" name=\"category\" value=\"other\" checked>Other</input></dd>"\ 109 "<input type=\"radio\" name=\"category\" value=\"videos\">Videos</input>\n" \
107 "<dt>Language:</dt><dd>"\ 110 "<input type=\"radio\" name=\"category\" value=\"other\" checked>Other</input></dd>" \
108 "<input type=\"radio\" name=\"language\" value=\"no-lang\" checked>none</input>"\ 111 "<dt>Language:</dt><dd>" \
109 "<input type=\"radio\" name=\"language\" value=\"en\">English</input>"\ 112 "<input type=\"radio\" name=\"language\" value=\"no-lang\" checked>none</input>" \
110 "<input type=\"radio\" name=\"language\" value=\"de\">German</input>"\ 113 "<input type=\"radio\" name=\"language\" value=\"en\">English</input>" \
111 "<input type=\"radio\" name=\"language\" value=\"fr\">French</input>"\ 114 "<input type=\"radio\" name=\"language\" value=\"de\">German</input>" \
112 "<input type=\"radio\" name=\"language\" value=\"es\">Spanish</input></dd>\n"\ 115 "<input type=\"radio\" name=\"language\" value=\"fr\">French</input>" \
113 "<dt>File:</dt><dd>"\ 116 "<input type=\"radio\" name=\"language\" value=\"es\">Spanish</input></dd>\n" \
114 "<input type=\"file\" name=\"upload\"/></dd></dl>"\ 117 "<dt>File:</dt><dd>" \
115 "<input type=\"submit\" value=\"Send!\"/>\n"\ 118 "<input type=\"file\" name=\"upload\"/></dd></dl>" \
116 "</form>\n"\ 119 "<input type=\"submit\" value=\"Send!\"/>\n" \
117 "<h1>Download</h1>\n"\ 120 "</form>\n" \
118 "<ol>\n" 121 "<h1>Download</h1>\n" \
122 "<ol>\n"
119 123
120/** 124/**
121 * Footer of index page. 125 * Footer of index page.
@@ -127,16 +131,15 @@
127 * NULL-terminated array of supported upload categories. Should match HTML 131 * NULL-terminated array of supported upload categories. Should match HTML
128 * in the form. 132 * in the form.
129 */ 133 */
130static const char * const categories[] = 134static const char *const categories[] = {
131 { 135 "books",
132 "books", 136 "images",
133 "images", 137 "music",
134 "music", 138 "software",
135 "software", 139 "videos",
136 "videos", 140 "other",
137 "other", 141 NULL,
138 NULL, 142};
139 };
140 143
141 144
142/** 145/**
@@ -160,15 +163,14 @@ struct Language
160 * NULL-terminated array of supported upload categories. Should match HTML 163 * NULL-terminated array of supported upload categories. Should match HTML
161 * in the form. 164 * in the form.
162 */ 165 */
163static const struct Language languages[] = 166static const struct Language languages[] = {
164 { 167 { "no-lang", "No language specified" },
165 { "no-lang", "No language specified" }, 168 { "en", "English" },
166 { "en", "English" }, 169 { "de", "German" },
167 { "de", "German" }, 170 { "fr", "French" },
168 { "fr", "French" }, 171 { "es", "Spanish" },
169 { "es", "Spanish" }, 172 { NULL, NULL },
170 { NULL, NULL }, 173};
171 };
172 174
173 175
174/** 176/**
@@ -213,8 +215,8 @@ static void
213mark_as_html (struct MHD_Response *response) 215mark_as_html (struct MHD_Response *response)
214{ 216{
215 (void) MHD_add_response_header (response, 217 (void) MHD_add_response_header (response,
216 MHD_HTTP_HEADER_CONTENT_TYPE, 218 MHD_HTTP_HEADER_CONTENT_TYPE,
217 "text/html"); 219 "text/html");
218} 220}
219 221
220 222
@@ -267,7 +269,7 @@ struct ResponseDataContext
267 */ 269 */
268static int 270static int
269list_directory (struct ResponseDataContext *rdc, 271list_directory (struct ResponseDataContext *rdc,
270 const char *dirname) 272 const char *dirname)
271{ 273{
272 char fullname[PATH_MAX]; 274 char fullname[PATH_MAX];
273 struct stat sbuf; 275 struct stat sbuf;
@@ -277,35 +279,35 @@ list_directory (struct ResponseDataContext *rdc,
277 if (NULL == (dir = opendir (dirname))) 279 if (NULL == (dir = opendir (dirname)))
278 return MHD_NO; 280 return MHD_NO;
279 while (NULL != (de = readdir (dir))) 281 while (NULL != (de = readdir (dir)))
282 {
283 if ('.' == de->d_name[0])
284 continue;
285 if (sizeof (fullname) <= (size_t)
286 snprintf (fullname, sizeof (fullname),
287 "%s/%s",
288 dirname, de->d_name))
289 continue; /* ugh, file too long? how can this be!? */
290 if (0 != stat (fullname, &sbuf))
291 continue; /* ugh, failed to 'stat' */
292 if (! S_ISREG (sbuf.st_mode))
293 continue; /* not a regular file, skip */
294 if (rdc->off + 1024 > rdc->buf_len)
280 { 295 {
281 if ('.' == de->d_name[0]) 296 void *r;
282 continue; 297
283 if (sizeof (fullname) <= (size_t) 298 if ( (2 * rdc->buf_len + 1024) < rdc->buf_len)
284 snprintf (fullname, sizeof (fullname), 299 break; /* more than SIZE_T _index_ size? Too big for us */
285 "%s/%s", 300 rdc->buf_len = 2 * rdc->buf_len + 1024;
286 dirname, de->d_name)) 301 if (NULL == (r = realloc (rdc->buf, rdc->buf_len)))
287 continue; /* ugh, file too long? how can this be!? */ 302 break; /* out of memory */
288 if (0 != stat (fullname, &sbuf)) 303 rdc->buf = r;
289 continue; /* ugh, failed to 'stat' */
290 if (! S_ISREG (sbuf.st_mode))
291 continue; /* not a regular file, skip */
292 if (rdc->off + 1024 > rdc->buf_len)
293 {
294 void *r;
295
296 if ( (2 * rdc->buf_len + 1024) < rdc->buf_len)
297 break; /* more than SIZE_T _index_ size? Too big for us */
298 rdc->buf_len = 2 * rdc->buf_len + 1024;
299 if (NULL == (r = realloc (rdc->buf, rdc->buf_len)))
300 break; /* out of memory */
301 rdc->buf = r;
302 }
303 rdc->off += snprintf (&rdc->buf[rdc->off],
304 rdc->buf_len - rdc->off,
305 "<li><a href=\"/%s\">%s</a></li>\n",
306 fullname,
307 de->d_name);
308 } 304 }
305 rdc->off += snprintf (&rdc->buf[rdc->off],
306 rdc->buf_len - rdc->off,
307 "<li><a href=\"/%s\">%s</a></li>\n",
308 fullname,
309 de->d_name);
310 }
309 (void) closedir (dir); 311 (void) closedir (dir);
310 return MHD_YES; 312 return MHD_YES;
311} 313}
@@ -329,65 +331,66 @@ update_directory ()
329 331
330 rdc.buf_len = initial_allocation; 332 rdc.buf_len = initial_allocation;
331 if (NULL == (rdc.buf = malloc (rdc.buf_len))) 333 if (NULL == (rdc.buf = malloc (rdc.buf_len)))
332 { 334 {
333 update_cached_response (NULL); 335 update_cached_response (NULL);
334 return; 336 return;
335 } 337 }
336 rdc.off = snprintf (rdc.buf, rdc.buf_len, 338 rdc.off = snprintf (rdc.buf, rdc.buf_len,
337 "%s", 339 "%s",
338 INDEX_PAGE_HEADER); 340 INDEX_PAGE_HEADER);
339 for (language_idx = 0; NULL != languages[language_idx].dirname; language_idx++) 341 for (language_idx = 0; NULL != languages[language_idx].dirname;
342 language_idx++)
343 {
344 language = &languages[language_idx];
345
346 if (0 != stat (language->dirname, &sbuf))
347 continue; /* empty */
348 /* we ensured always +1k room, filenames are ~256 bytes,
349 so there is always still enough space for the header
350 without need for an additional reallocation check. */
351 rdc.off += snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off,
352 "<h2>%s</h2>\n",
353 language->longname);
354 for (category_idx = 0; NULL != categories[category_idx]; category_idx++)
340 { 355 {
341 language = &languages[language_idx]; 356 category = categories[category_idx];
357 snprintf (dir_name, sizeof (dir_name),
358 "%s/%s",
359 language->dirname,
360 category);
361 if (0 != stat (dir_name, &sbuf))
362 continue; /* empty */
342 363
343 if (0 != stat (language->dirname, &sbuf))
344 continue; /* empty */
345 /* we ensured always +1k room, filenames are ~256 bytes, 364 /* we ensured always +1k room, filenames are ~256 bytes,
346 so there is always still enough space for the header 365 so there is always still enough space for the header
347 without need for an additional reallocation check. */ 366 without need for an additional reallocation check. */
348 rdc.off += snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off, 367 rdc.off += snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off,
349 "<h2>%s</h2>\n", 368 "<h3>%s</h3>\n",
350 language->longname); 369 category);
351 for (category_idx = 0; NULL != categories[category_idx]; category_idx++) 370
352 { 371 if (MHD_NO == list_directory (&rdc, dir_name))
353 category = categories[category_idx]; 372 {
354 snprintf (dir_name, sizeof (dir_name), 373 free (rdc.buf);
355 "%s/%s", 374 update_cached_response (NULL);
356 language->dirname, 375 return;
357 category); 376 }
358 if (0 != stat (dir_name, &sbuf))
359 continue; /* empty */
360
361 /* we ensured always +1k room, filenames are ~256 bytes,
362 so there is always still enough space for the header
363 without need for an additional reallocation check. */
364 rdc.off += snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off,
365 "<h3>%s</h3>\n",
366 category);
367
368 if (MHD_NO == list_directory (&rdc, dir_name))
369 {
370 free (rdc.buf);
371 update_cached_response (NULL);
372 return;
373 }
374 }
375 } 377 }
378 }
376 /* we ensured always +1k room, filenames are ~256 bytes, 379 /* we ensured always +1k room, filenames are ~256 bytes,
377 so there is always still enough space for the footer 380 so there is always still enough space for the footer
378 without need for a final reallocation check. */ 381 without need for a final reallocation check. */
379 rdc.off += snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off, 382 rdc.off += snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off,
380 "%s", 383 "%s",
381 INDEX_PAGE_FOOTER); 384 INDEX_PAGE_FOOTER);
382 initial_allocation = rdc.buf_len; /* remember for next time */ 385 initial_allocation = rdc.buf_len; /* remember for next time */
383 response = MHD_create_response_from_buffer (rdc.off, 386 response = MHD_create_response_from_buffer (rdc.off,
384 rdc.buf, 387 rdc.buf,
385 MHD_RESPMEM_MUST_FREE); 388 MHD_RESPMEM_MUST_FREE);
386 mark_as_html (response); 389 mark_as_html (response);
387#if FORCE_CLOSE 390#if FORCE_CLOSE
388 (void) MHD_add_response_header (response, 391 (void) MHD_add_response_header (response,
389 MHD_HTTP_HEADER_CONNECTION, 392 MHD_HTTP_HEADER_CONNECTION,
390 "close"); 393 "close");
391#endif 394#endif
392 update_cached_response (response); 395 update_cached_response (response);
393} 396}
@@ -446,8 +449,8 @@ struct UploadContext
446 */ 449 */
447static int 450static int
448do_append (char **ret, 451do_append (char **ret,
449 const char *data, 452 const char *data,
450 size_t size) 453 size_t size)
451{ 454{
452 char *buf; 455 char *buf;
453 size_t old_len; 456 size_t old_len;
@@ -459,15 +462,15 @@ do_append (char **ret,
459 if (NULL == (buf = malloc (old_len + size + 1))) 462 if (NULL == (buf = malloc (old_len + size + 1)))
460 return MHD_NO; 463 return MHD_NO;
461 if (NULL != *ret) 464 if (NULL != *ret)
462 { 465 {
463 memcpy (buf, 466 memcpy (buf,
464 *ret, 467 *ret,
465 old_len); 468 old_len);
466 free (*ret); 469 free (*ret);
467 } 470 }
468 memcpy (&buf[old_len], 471 memcpy (&buf[old_len],
469 data, 472 data,
470 size); 473 size);
471 buf[old_len + size] = '\0'; 474 buf[old_len + size] = '\0';
472 *ret = buf; 475 *ret = buf;
473 return MHD_YES; 476 return MHD_YES;
@@ -495,119 +498,119 @@ do_append (char **ret,
495 */ 498 */
496static int 499static int
497process_upload_data (void *cls, 500process_upload_data (void *cls,
498 enum MHD_ValueKind kind, 501 enum MHD_ValueKind kind,
499 const char *key, 502 const char *key,
500 const char *filename, 503 const char *filename,
501 const char *content_type, 504 const char *content_type,
502 const char *transfer_encoding, 505 const char *transfer_encoding,
503 const char *data, 506 const char *data,
504 uint64_t off, 507 uint64_t off,
505 size_t size) 508 size_t size)
506{ 509{
507 struct UploadContext *uc = cls; 510 struct UploadContext *uc = cls;
508 int i; 511 int i;
509 (void)kind; /* Unused. Silent compiler warning. */ 512 (void) kind; /* Unused. Silent compiler warning. */
510 (void)content_type; /* Unused. Silent compiler warning. */ 513 (void) content_type; /* Unused. Silent compiler warning. */
511 (void)transfer_encoding; /* Unused. Silent compiler warning. */ 514 (void) transfer_encoding; /* Unused. Silent compiler warning. */
512 (void)off; /* Unused. Silent compiler warning. */ 515 (void) off; /* Unused. Silent compiler warning. */
513 516
514 if (0 == strcmp (key, "category")) 517 if (0 == strcmp (key, "category"))
515 return do_append (&uc->category, data, size); 518 return do_append (&uc->category, data, size);
516 if (0 == strcmp (key, "language")) 519 if (0 == strcmp (key, "language"))
517 return do_append (&uc->language, data, size); 520 return do_append (&uc->language, data, size);
518 if (0 != strcmp (key, "upload")) 521 if (0 != strcmp (key, "upload"))
519 { 522 {
520 fprintf (stderr, 523 fprintf (stderr,
521 "Ignoring unexpected form value `%s'\n", 524 "Ignoring unexpected form value `%s'\n",
522 key); 525 key);
523 return MHD_YES; /* ignore */ 526 return MHD_YES; /* ignore */
524 } 527 }
525 if (NULL == filename) 528 if (NULL == filename)
526 { 529 {
527 fprintf (stderr, "No filename, aborting upload\n"); 530 fprintf (stderr, "No filename, aborting upload\n");
528 return MHD_NO; /* no filename, error */ 531 return MHD_NO; /* no filename, error */
529 } 532 }
530 if ( (NULL == uc->category) || 533 if ( (NULL == uc->category) ||
531 (NULL == uc->language) ) 534 (NULL == uc->language) )
535 {
536 fprintf (stderr,
537 "Missing form data for upload `%s'\n",
538 filename);
539 uc->response = request_refused_response;
540 return MHD_NO;
541 }
542 if (-1 == uc->fd)
543 {
544 char fn[PATH_MAX];
545
546 if ( (NULL != strstr (filename, "..")) ||
547 (NULL != strchr (filename, '/')) ||
548 (NULL != strchr (filename, '\\')) )
532 { 549 {
533 fprintf (stderr,
534 "Missing form data for upload `%s'\n",
535 filename);
536 uc->response = request_refused_response; 550 uc->response = request_refused_response;
537 return MHD_NO; 551 return MHD_NO;
538 } 552 }
539 if (-1 == uc->fd) 553 /* create directories -- if they don't exist already */
540 {
541 char fn[PATH_MAX];
542
543 if ( (NULL != strstr (filename, "..")) ||
544 (NULL != strchr (filename, '/')) ||
545 (NULL != strchr (filename, '\\')) )
546 {
547 uc->response = request_refused_response;
548 return MHD_NO;
549 }
550 /* create directories -- if they don't exist already */
551#ifdef WINDOWS 554#ifdef WINDOWS
552 (void) mkdir (uc->language); 555 (void) mkdir (uc->language);
553#else 556#else
554 (void) mkdir (uc->language, S_IRWXU); 557 (void) mkdir (uc->language, S_IRWXU);
555#endif 558#endif
556 snprintf (fn, sizeof (fn), 559 snprintf (fn, sizeof (fn),
557 "%s/%s", 560 "%s/%s",
558 uc->language, 561 uc->language,
559 uc->category); 562 uc->category);
560#ifdef WINDOWS 563#ifdef WINDOWS
561 (void) mkdir (fn); 564 (void) mkdir (fn);
562#else 565#else
563 (void) mkdir (fn, S_IRWXU); 566 (void) mkdir (fn, S_IRWXU);
564#endif 567#endif
565 /* open file */ 568 /* open file */
566 snprintf (fn, sizeof (fn), 569 snprintf (fn, sizeof (fn),
567 "%s/%s/%s", 570 "%s/%s/%s",
568 uc->language, 571 uc->language,
569 uc->category, 572 uc->category,
570 filename); 573 filename);
571 for (i=strlen (fn)-1;i>=0;i--) 574 for (i = strlen (fn) - 1; i>=0; i--)
572 if (! isprint ((unsigned char) fn[i])) 575 if (! isprint ((unsigned char) fn[i]))
573 fn[i] = '_'; 576 fn[i] = '_';
574 uc->fd = open (fn, 577 uc->fd = open (fn,
575 O_CREAT | O_EXCL 578 O_CREAT | O_EXCL
576#if O_LARGEFILE 579#if O_LARGEFILE
577 | O_LARGEFILE 580 | O_LARGEFILE
578#endif 581#endif
579 | O_WRONLY, 582 | O_WRONLY,
580 S_IRUSR | S_IWUSR); 583 S_IRUSR | S_IWUSR);
581 if (-1 == uc->fd) 584 if (-1 == uc->fd)
582 { 585 {
583 fprintf (stderr, 586 fprintf (stderr,
584 "Error opening file `%s' for upload: %s\n", 587 "Error opening file `%s' for upload: %s\n",
585 fn, 588 fn,
586 strerror (errno)); 589 strerror (errno));
587 uc->response = request_refused_response; 590 uc->response = request_refused_response;
588 return MHD_NO; 591 return MHD_NO;
589 }
590 uc->filename = strdup (fn);
591 } 592 }
593 uc->filename = strdup (fn);
594 }
592 if ( (0 != size) && 595 if ( (0 != size) &&
593 (size != (size_t) write (uc->fd, data, size)) ) 596 (size != (size_t) write (uc->fd, data, size)) )
597 {
598 /* write failed; likely: disk full */
599 fprintf (stderr,
600 "Error writing to file `%s': %s\n",
601 uc->filename,
602 strerror (errno));
603 uc->response = internal_error_response;
604 close (uc->fd);
605 uc->fd = -1;
606 if (NULL != uc->filename)
594 { 607 {
595 /* write failed; likely: disk full */ 608 unlink (uc->filename);
596 fprintf (stderr, 609 free (uc->filename);
597 "Error writing to file `%s': %s\n", 610 uc->filename = NULL;
598 uc->filename,
599 strerror (errno));
600 uc->response = internal_error_response;
601 close (uc->fd);
602 uc->fd = -1;
603 if (NULL != uc->filename)
604 {
605 unlink (uc->filename);
606 free (uc->filename);
607 uc->filename = NULL;
608 }
609 return MHD_NO;
610 } 611 }
612 return MHD_NO;
613 }
611 return MHD_YES; 614 return MHD_YES;
612} 615}
613 616
@@ -625,32 +628,32 @@ process_upload_data (void *cls,
625 */ 628 */
626static void 629static void
627response_completed_callback (void *cls, 630response_completed_callback (void *cls,
628 struct MHD_Connection *connection, 631 struct MHD_Connection *connection,
629 void **con_cls, 632 void **con_cls,
630 enum MHD_RequestTerminationCode toe) 633 enum MHD_RequestTerminationCode toe)
631{ 634{
632 struct UploadContext *uc = *con_cls; 635 struct UploadContext *uc = *con_cls;
633 (void)cls; /* Unused. Silent compiler warning. */ 636 (void) cls; /* Unused. Silent compiler warning. */
634 (void)connection; /* Unused. Silent compiler warning. */ 637 (void) connection; /* Unused. Silent compiler warning. */
635 (void)toe; /* Unused. Silent compiler warning. */ 638 (void) toe; /* Unused. Silent compiler warning. */
636 639
637 if (NULL == uc) 640 if (NULL == uc)
638 return; /* this request wasn't an upload request */ 641 return; /* this request wasn't an upload request */
639 if (NULL != uc->pp) 642 if (NULL != uc->pp)
640 { 643 {
641 MHD_destroy_post_processor (uc->pp); 644 MHD_destroy_post_processor (uc->pp);
642 uc->pp = NULL; 645 uc->pp = NULL;
643 } 646 }
644 if (-1 != uc->fd) 647 if (-1 != uc->fd)
645 { 648 {
646 (void) close (uc->fd); 649 (void) close (uc->fd);
647 if (NULL != uc->filename) 650 if (NULL != uc->filename)
648 { 651 {
649 fprintf (stderr, 652 fprintf (stderr,
650 "Upload of file `%s' failed (incomplete or aborted), removing file.\n", 653 "Upload of file `%s' failed (incomplete or aborted), removing file.\n",
651 uc->filename); 654 uc->filename);
652 (void) unlink (uc->filename); 655 (void) unlink (uc->filename);
653 } 656 }
654 } 657 }
655 if (NULL != uc->filename) 658 if (NULL != uc->filename)
656 free (uc->filename); 659 free (uc->filename);
@@ -672,12 +675,12 @@ return_directory_response (struct MHD_Connection *connection)
672 (void) pthread_mutex_lock (&mutex); 675 (void) pthread_mutex_lock (&mutex);
673 if (NULL == cached_directory_response) 676 if (NULL == cached_directory_response)
674 ret = MHD_queue_response (connection, 677 ret = MHD_queue_response (connection,
675 MHD_HTTP_INTERNAL_SERVER_ERROR, 678 MHD_HTTP_INTERNAL_SERVER_ERROR,
676 internal_error_response); 679 internal_error_response);
677 else 680 else
678 ret = MHD_queue_response (connection, 681 ret = MHD_queue_response (connection,
679 MHD_HTTP_OK, 682 MHD_HTTP_OK,
680 cached_directory_response); 683 cached_directory_response);
681 (void) pthread_mutex_unlock (&mutex); 684 (void) pthread_mutex_unlock (&mutex);
682 return ret; 685 return ret;
683} 686}
@@ -698,131 +701,131 @@ return_directory_response (struct MHD_Connection *connection)
698 */ 701 */
699static int 702static int
700generate_page (void *cls, 703generate_page (void *cls,
701 struct MHD_Connection *connection, 704 struct MHD_Connection *connection,
702 const char *url, 705 const char *url,
703 const char *method, 706 const char *method,
704 const char *version, 707 const char *version,
705 const char *upload_data, 708 const char *upload_data,
706 size_t *upload_data_size, void **ptr) 709 size_t *upload_data_size, void **ptr)
707{ 710{
708 struct MHD_Response *response; 711 struct MHD_Response *response;
709 int ret; 712 int ret;
710 int fd; 713 int fd;
711 struct stat buf; 714 struct stat buf;
712 (void)cls; /* Unused. Silent compiler warning. */ 715 (void) cls; /* Unused. Silent compiler warning. */
713 (void)version; /* Unused. Silent compiler warning. */ 716 (void) version; /* Unused. Silent compiler warning. */
714 717
715 if (0 != strcmp (url, "/")) 718 if (0 != strcmp (url, "/"))
716 { 719 {
717 /* should be file download */ 720 /* should be file download */
718#ifdef MHD_HAVE_LIBMAGIC 721#ifdef MHD_HAVE_LIBMAGIC
719 char file_data[MAGIC_HEADER_SIZE]; 722 char file_data[MAGIC_HEADER_SIZE];
720 ssize_t got; 723 ssize_t got;
721#endif /* MHD_HAVE_LIBMAGIC */ 724#endif /* MHD_HAVE_LIBMAGIC */
722 const char *mime; 725 const char *mime;
723 726
724 if (0 != strcmp (method, MHD_HTTP_METHOD_GET)) 727 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
725 return MHD_NO; /* unexpected method (we're not polite...) */ 728 return MHD_NO; /* unexpected method (we're not polite...) */
726 fd = -1; 729 fd = -1;
727 if ( (NULL == strstr (&url[1], "..")) && 730 if ( (NULL == strstr (&url[1], "..")) &&
728 ('/' != url[1]) ) 731 ('/' != url[1]) )
729 { 732 {
730 fd = open (&url[1], O_RDONLY); 733 fd = open (&url[1], O_RDONLY);
731 if ( (-1 != fd) && 734 if ( (-1 != fd) &&
732 ( (0 != fstat (fd, &buf)) || 735 ( (0 != fstat (fd, &buf)) ||
733 (! S_ISREG (buf.st_mode)) ) ) 736 (! S_ISREG (buf.st_mode)) ) )
734 { 737 {
735 (void) close (fd); 738 (void) close (fd);
736 fd = -1; 739 fd = -1;
737 } 740 }
738 } 741 }
739 if (-1 == fd) 742 if (-1 == fd)
740 return MHD_queue_response (connection, 743 return MHD_queue_response (connection,
741 MHD_HTTP_NOT_FOUND, 744 MHD_HTTP_NOT_FOUND,
742 file_not_found_response); 745 file_not_found_response);
743#ifdef MHD_HAVE_LIBMAGIC 746#ifdef MHD_HAVE_LIBMAGIC
744 /* read beginning of the file to determine mime type */ 747 /* read beginning of the file to determine mime type */
745 got = read (fd, file_data, sizeof (file_data)); 748 got = read (fd, file_data, sizeof (file_data));
746 (void) lseek (fd, 0, SEEK_SET); 749 (void) lseek (fd, 0, SEEK_SET);
747 if (-1 != got) 750 if (-1 != got)
748 mime = magic_buffer (magic, file_data, got); 751 mime = magic_buffer (magic, file_data, got);
749 else 752 else
750#endif /* MHD_HAVE_LIBMAGIC */ 753#endif /* MHD_HAVE_LIBMAGIC */
751 mime = NULL; 754 mime = NULL;
752 755
753 if (NULL == (response = MHD_create_response_from_fd (buf.st_size, 756 if (NULL == (response = MHD_create_response_from_fd (buf.st_size,
754 fd))) 757 fd)))
755 { 758 {
756 /* internal error (i.e. out of memory) */ 759 /* internal error (i.e. out of memory) */
757 (void) close (fd); 760 (void) close (fd);
758 return MHD_NO; 761 return MHD_NO;
759 }
760
761 /* add mime type if we had one */
762 if (NULL != mime)
763 (void) MHD_add_response_header (response,
764 MHD_HTTP_HEADER_CONTENT_TYPE,
765 mime);
766 ret = MHD_queue_response (connection,
767 MHD_HTTP_OK,
768 response);
769 MHD_destroy_response (response);
770 return ret;
771 } 762 }
772 763
764 /* add mime type if we had one */
765 if (NULL != mime)
766 (void) MHD_add_response_header (response,
767 MHD_HTTP_HEADER_CONTENT_TYPE,
768 mime);
769 ret = MHD_queue_response (connection,
770 MHD_HTTP_OK,
771 response);
772 MHD_destroy_response (response);
773 return ret;
774 }
775
773 if (0 == strcmp (method, MHD_HTTP_METHOD_POST)) 776 if (0 == strcmp (method, MHD_HTTP_METHOD_POST))
777 {
778 /* upload! */
779 struct UploadContext *uc = *ptr;
780
781 if (NULL == uc)
782 {
783 if (NULL == (uc = malloc (sizeof (struct UploadContext))))
784 return MHD_NO; /* out of memory, close connection */
785 memset (uc, 0, sizeof (struct UploadContext));
786 uc->fd = -1;
787 uc->connection = connection;
788 uc->pp = MHD_create_post_processor (connection,
789 64 * 1024 /* buffer size */,
790 &process_upload_data, uc);
791 if (NULL == uc->pp)
792 {
793 /* out of memory, close connection */
794 free (uc);
795 return MHD_NO;
796 }
797 *ptr = uc;
798 return MHD_YES;
799 }
800 if (0 != *upload_data_size)
801 {
802 if (NULL == uc->response)
803 (void) MHD_post_process (uc->pp,
804 upload_data,
805 *upload_data_size);
806 *upload_data_size = 0;
807 return MHD_YES;
808 }
809 /* end of upload, finish it! */
810 MHD_destroy_post_processor (uc->pp);
811 uc->pp = NULL;
812 if (-1 != uc->fd)
813 {
814 close (uc->fd);
815 uc->fd = -1;
816 }
817 if (NULL != uc->response)
818 {
819 return MHD_queue_response (connection,
820 MHD_HTTP_FORBIDDEN,
821 uc->response);
822 }
823 else
774 { 824 {
775 /* upload! */ 825 update_directory ();
776 struct UploadContext *uc = *ptr; 826 return return_directory_response (connection);
777
778 if (NULL == uc)
779 {
780 if (NULL == (uc = malloc (sizeof (struct UploadContext))))
781 return MHD_NO; /* out of memory, close connection */
782 memset (uc, 0, sizeof (struct UploadContext));
783 uc->fd = -1;
784 uc->connection = connection;
785 uc->pp = MHD_create_post_processor (connection,
786 64 * 1024 /* buffer size */,
787 &process_upload_data, uc);
788 if (NULL == uc->pp)
789 {
790 /* out of memory, close connection */
791 free (uc);
792 return MHD_NO;
793 }
794 *ptr = uc;
795 return MHD_YES;
796 }
797 if (0 != *upload_data_size)
798 {
799 if (NULL == uc->response)
800 (void) MHD_post_process (uc->pp,
801 upload_data,
802 *upload_data_size);
803 *upload_data_size = 0;
804 return MHD_YES;
805 }
806 /* end of upload, finish it! */
807 MHD_destroy_post_processor (uc->pp);
808 uc->pp = NULL;
809 if (-1 != uc->fd)
810 {
811 close (uc->fd);
812 uc->fd = -1;
813 }
814 if (NULL != uc->response)
815 {
816 return MHD_queue_response (connection,
817 MHD_HTTP_FORBIDDEN,
818 uc->response);
819 }
820 else
821 {
822 update_directory ();
823 return return_directory_response (connection);
824 }
825 } 827 }
828 }
826 if (0 == strcmp (method, MHD_HTTP_METHOD_GET)) 829 if (0 == strcmp (method, MHD_HTTP_METHOD_GET))
827 { 830 {
828 return return_directory_response (connection); 831 return return_directory_response (connection);
@@ -830,8 +833,8 @@ generate_page (void *cls,
830 833
831 /* unexpected request, refuse */ 834 /* unexpected request, refuse */
832 return MHD_queue_response (connection, 835 return MHD_queue_response (connection,
833 MHD_HTTP_FORBIDDEN, 836 MHD_HTTP_FORBIDDEN,
834 request_refused_response); 837 request_refused_response);
835} 838}
836 839
837 840
@@ -844,7 +847,7 @@ generate_page (void *cls,
844static void 847static void
845catcher (int sig) 848catcher (int sig)
846{ 849{
847 (void)sig; /* Unused. Silent compiler warning. */ 850 (void) sig; /* Unused. Silent compiler warning. */
848 /* do nothing */ 851 /* do nothing */
849} 852}
850 853
@@ -873,53 +876,53 @@ ignore_sigpipe (void)
873 876
874/* test server key */ 877/* test server key */
875const char srv_signed_key_pem[] = "-----BEGIN RSA PRIVATE KEY-----\n" 878const char srv_signed_key_pem[] = "-----BEGIN RSA PRIVATE KEY-----\n"
876 "MIIEowIBAAKCAQEAvfTdv+3fgvVTKRnP/HVNG81cr8TrUP/iiyuve/THMzvFXhCW\n" 879 "MIIEowIBAAKCAQEAvfTdv+3fgvVTKRnP/HVNG81cr8TrUP/iiyuve/THMzvFXhCW\n"
877 "+K03KwEku55QvnUndwBfU/ROzLlv+5hotgiDRNFT3HxurmhouySBrJNJv7qWp8IL\n" 880 "+K03KwEku55QvnUndwBfU/ROzLlv+5hotgiDRNFT3HxurmhouySBrJNJv7qWp8IL\n"
878 "q4sw32vo0fbMu5BZF49bUXK9L3kW2PdhTtSQPWHEzNrCxO+YgCilKHkY3vQNfdJ0\n" 881 "q4sw32vo0fbMu5BZF49bUXK9L3kW2PdhTtSQPWHEzNrCxO+YgCilKHkY3vQNfdJ0\n"
879 "20Q5EAAEseD1YtWCIpRvJzYlZMpjYB1ubTl24kwrgOKUJYKqM4jmF4DVQp4oOK/6\n" 882 "20Q5EAAEseD1YtWCIpRvJzYlZMpjYB1ubTl24kwrgOKUJYKqM4jmF4DVQp4oOK/6\n"
880 "QYGGh1QmHRPAy3CBII6sbb+sZT9cAqU6GYQVB35lm4XAgibXV6KgmpVxVQQ69U6x\n" 883 "QYGGh1QmHRPAy3CBII6sbb+sZT9cAqU6GYQVB35lm4XAgibXV6KgmpVxVQQ69U6x\n"
881 "yoOl204xuekZOaG9RUPId74Rtmwfi1TLbBzo2wIDAQABAoIBADu09WSICNq5cMe4\n" 884 "yoOl204xuekZOaG9RUPId74Rtmwfi1TLbBzo2wIDAQABAoIBADu09WSICNq5cMe4\n"
882 "+NKCLlgAT1NiQpLls1gKRbDhKiHU9j8QWNvWWkJWrCya4QdUfLCfeddCMeiQmv3K\n" 885 "+NKCLlgAT1NiQpLls1gKRbDhKiHU9j8QWNvWWkJWrCya4QdUfLCfeddCMeiQmv3K\n"
883 "lJMvDs+5OjJSHFoOsGiuW2Ias7IjnIojaJalfBml6frhJ84G27IXmdz6gzOiTIer\n" 886 "lJMvDs+5OjJSHFoOsGiuW2Ias7IjnIojaJalfBml6frhJ84G27IXmdz6gzOiTIer\n"
884 "DjeAgcwBaKH5WwIay2TxIaScl7AwHBauQkrLcyb4hTmZuQh6ArVIN6+pzoVuORXM\n" 887 "DjeAgcwBaKH5WwIay2TxIaScl7AwHBauQkrLcyb4hTmZuQh6ArVIN6+pzoVuORXM\n"
885 "bpeNWl2l/HSN3VtUN6aCAKbN/X3o0GavCCMn5Fa85uJFsab4ss/uP+2PusU71+zP\n" 888 "bpeNWl2l/HSN3VtUN6aCAKbN/X3o0GavCCMn5Fa85uJFsab4ss/uP+2PusU71+zP\n"
886 "sBm6p/2IbGvF5k3VPDA7X5YX61sukRjRBihY8xSnNYx1UcoOsX6AiPnbhifD8+xQ\n" 889 "sBm6p/2IbGvF5k3VPDA7X5YX61sukRjRBihY8xSnNYx1UcoOsX6AiPnbhifD8+xQ\n"
887 "Tlf8oJUCgYEA0BTfzqNpr9Wxw5/QXaSdw7S/0eP5a0C/nwURvmfSzuTD4equzbEN\n" 890 "Tlf8oJUCgYEA0BTfzqNpr9Wxw5/QXaSdw7S/0eP5a0C/nwURvmfSzuTD4equzbEN\n"
888 "d+dI/s2JMxrdj/I4uoAfUXRGaabevQIjFzC9uyE3LaOyR2zhuvAzX+vVcs6bSXeU\n" 891 "d+dI/s2JMxrdj/I4uoAfUXRGaabevQIjFzC9uyE3LaOyR2zhuvAzX+vVcs6bSXeU\n"
889 "pKpCAcN+3Z3evMaX2f+z/nfSUAl2i4J2R+/LQAWJW4KwRky/m+cxpfUCgYEA6bN1\n" 892 "pKpCAcN+3Z3evMaX2f+z/nfSUAl2i4J2R+/LQAWJW4KwRky/m+cxpfUCgYEA6bN1\n"
890 "b73bMgM8wpNt6+fcmS+5n0iZihygQ2U2DEud8nZJL4Nrm1dwTnfZfJBnkGj6+0Q0\n" 893 "b73bMgM8wpNt6+fcmS+5n0iZihygQ2U2DEud8nZJL4Nrm1dwTnfZfJBnkGj6+0Q0\n"
891 "cOwj2KS0/wcEdJBP0jucU4v60VMhp75AQeHqidIde0bTViSRo3HWKXHBIFGYoU3T\n" 894 "cOwj2KS0/wcEdJBP0jucU4v60VMhp75AQeHqidIde0bTViSRo3HWKXHBIFGYoU3T\n"
892 "LyPyKndbqsOObnsFXHn56Nwhr2HLf6nw4taGQY8CgYBoSW36FLCNbd6QGvLFXBGt\n" 895 "LyPyKndbqsOObnsFXHn56Nwhr2HLf6nw4taGQY8CgYBoSW36FLCNbd6QGvLFXBGt\n"
893 "2lMhEM8az/K58kJ4WXSwOLtr6MD/WjNT2tkcy0puEJLm6BFCd6A6pLn9jaKou/92\n" 896 "2lMhEM8az/K58kJ4WXSwOLtr6MD/WjNT2tkcy0puEJLm6BFCd6A6pLn9jaKou/92\n"
894 "SfltZjJPb3GUlp9zn5tAAeSSi7YMViBrfuFiHObij5LorefBXISLjuYbMwL03MgH\n" 897 "SfltZjJPb3GUlp9zn5tAAeSSi7YMViBrfuFiHObij5LorefBXISLjuYbMwL03MgH\n"
895 "Ocl2JtA2ywMp2KFXs8GQWQKBgFyIVv5ogQrbZ0pvj31xr9HjqK6d01VxIi+tOmpB\n" 898 "Ocl2JtA2ywMp2KFXs8GQWQKBgFyIVv5ogQrbZ0pvj31xr9HjqK6d01VxIi+tOmpB\n"
896 "4ocnOLEcaxX12BzprW55ytfOCVpF1jHD/imAhb3YrHXu0fwe6DXYXfZV4SSG2vB7\n" 899 "4ocnOLEcaxX12BzprW55ytfOCVpF1jHD/imAhb3YrHXu0fwe6DXYXfZV4SSG2vB7\n"
897 "IB9z14KBN5qLHjNGFpMQXHSMek+b/ftTU0ZnPh9uEM5D3YqRLVd7GcdUhHvG8P8Q\n" 900 "IB9z14KBN5qLHjNGFpMQXHSMek+b/ftTU0ZnPh9uEM5D3YqRLVd7GcdUhHvG8P8Q\n"
898 "C9aXAoGBAJtID6h8wOGMP0XYX5YYnhlC7dOLfk8UYrzlp3xhqVkzKthTQTj6wx9R\n" 901 "C9aXAoGBAJtID6h8wOGMP0XYX5YYnhlC7dOLfk8UYrzlp3xhqVkzKthTQTj6wx9R\n"
899 "GtC4k7U1ki8oJsfcIlBNXd768fqDVWjYju5rzShMpo8OCTS6ipAblKjCxPPVhIpv\n" 902 "GtC4k7U1ki8oJsfcIlBNXd768fqDVWjYju5rzShMpo8OCTS6ipAblKjCxPPVhIpv\n"
900 "tWPlbSn1qj6wylstJ5/3Z+ZW5H4wIKp5jmLiioDhcP0L/Ex3Zx8O\n" 903 "tWPlbSn1qj6wylstJ5/3Z+ZW5H4wIKp5jmLiioDhcP0L/Ex3Zx8O\n"
901 "-----END RSA PRIVATE KEY-----\n"; 904 "-----END RSA PRIVATE KEY-----\n";
902 905
903/* test server CA signed certificates */ 906/* test server CA signed certificates */
904const char srv_signed_cert_pem[] = "-----BEGIN CERTIFICATE-----\n" 907const char srv_signed_cert_pem[] = "-----BEGIN CERTIFICATE-----\n"
905 "MIIDGzCCAgWgAwIBAgIES0KCvTALBgkqhkiG9w0BAQUwFzEVMBMGA1UEAxMMdGVz\n" 908 "MIIDGzCCAgWgAwIBAgIES0KCvTALBgkqhkiG9w0BAQUwFzEVMBMGA1UEAxMMdGVz\n"
906 "dF9jYV9jZXJ0MB4XDTEwMDEwNTAwMDcyNVoXDTQ1MDMxMjAwMDcyNVowFzEVMBMG\n" 909 "dF9jYV9jZXJ0MB4XDTEwMDEwNTAwMDcyNVoXDTQ1MDMxMjAwMDcyNVowFzEVMBMG\n"
907 "A1UEAxMMdGVzdF9jYV9jZXJ0MIIBHzALBgkqhkiG9w0BAQEDggEOADCCAQkCggEA\n" 910 "A1UEAxMMdGVzdF9jYV9jZXJ0MIIBHzALBgkqhkiG9w0BAQEDggEOADCCAQkCggEA\n"
908 "vfTdv+3fgvVTKRnP/HVNG81cr8TrUP/iiyuve/THMzvFXhCW+K03KwEku55QvnUn\n" 911 "vfTdv+3fgvVTKRnP/HVNG81cr8TrUP/iiyuve/THMzvFXhCW+K03KwEku55QvnUn\n"
909 "dwBfU/ROzLlv+5hotgiDRNFT3HxurmhouySBrJNJv7qWp8ILq4sw32vo0fbMu5BZ\n" 912 "dwBfU/ROzLlv+5hotgiDRNFT3HxurmhouySBrJNJv7qWp8ILq4sw32vo0fbMu5BZ\n"
910 "F49bUXK9L3kW2PdhTtSQPWHEzNrCxO+YgCilKHkY3vQNfdJ020Q5EAAEseD1YtWC\n" 913 "F49bUXK9L3kW2PdhTtSQPWHEzNrCxO+YgCilKHkY3vQNfdJ020Q5EAAEseD1YtWC\n"
911 "IpRvJzYlZMpjYB1ubTl24kwrgOKUJYKqM4jmF4DVQp4oOK/6QYGGh1QmHRPAy3CB\n" 914 "IpRvJzYlZMpjYB1ubTl24kwrgOKUJYKqM4jmF4DVQp4oOK/6QYGGh1QmHRPAy3CB\n"
912 "II6sbb+sZT9cAqU6GYQVB35lm4XAgibXV6KgmpVxVQQ69U6xyoOl204xuekZOaG9\n" 915 "II6sbb+sZT9cAqU6GYQVB35lm4XAgibXV6KgmpVxVQQ69U6xyoOl204xuekZOaG9\n"
913 "RUPId74Rtmwfi1TLbBzo2wIDAQABo3YwdDAMBgNVHRMBAf8EAjAAMBMGA1UdJQQM\n" 916 "RUPId74Rtmwfi1TLbBzo2wIDAQABo3YwdDAMBgNVHRMBAf8EAjAAMBMGA1UdJQQM\n"
914 "MAoGCCsGAQUFBwMBMA8GA1UdDwEB/wQFAwMHIAAwHQYDVR0OBBYEFOFi4ilKOP1d\n" 917 "MAoGCCsGAQUFBwMBMA8GA1UdDwEB/wQFAwMHIAAwHQYDVR0OBBYEFOFi4ilKOP1d\n"
915 "XHlWCMwmVKr7mgy8MB8GA1UdIwQYMBaAFP2olB4s2T/xuoQ5pT2RKojFwZo2MAsG\n" 918 "XHlWCMwmVKr7mgy8MB8GA1UdIwQYMBaAFP2olB4s2T/xuoQ5pT2RKojFwZo2MAsG\n"
916 "CSqGSIb3DQEBBQOCAQEAHVWPxazupbOkG7Did+dY9z2z6RjTzYvurTtEKQgzM2Vz\n" 919 "CSqGSIb3DQEBBQOCAQEAHVWPxazupbOkG7Did+dY9z2z6RjTzYvurTtEKQgzM2Vz\n"
917 "GQBA+3pZ3c5mS97fPIs9hZXfnQeelMeZ2XP1a+9vp35bJjZBBhVH+pqxjCgiUflg\n" 920 "GQBA+3pZ3c5mS97fPIs9hZXfnQeelMeZ2XP1a+9vp35bJjZBBhVH+pqxjCgiUflg\n"
918 "A3Zqy0XwwVCgQLE2HyaU3DLUD/aeIFK5gJaOSdNTXZLv43K8kl4cqDbMeRpVTbkt\n" 921 "A3Zqy0XwwVCgQLE2HyaU3DLUD/aeIFK5gJaOSdNTXZLv43K8kl4cqDbMeRpVTbkt\n"
919 "YmG4AyEOYRNKGTqMEJXJoxD5E3rBUNrVI/XyTjYrulxbNPcMWEHKNeeqWpKDYTFo\n" 922 "YmG4AyEOYRNKGTqMEJXJoxD5E3rBUNrVI/XyTjYrulxbNPcMWEHKNeeqWpKDYTFo\n"
920 "Bb01PCthGXiq/4A2RLAFosadzRa8SBpoSjPPfZ0b2w4MJpReHqKbR5+T2t6hzml6\n" 923 "Bb01PCthGXiq/4A2RLAFosadzRa8SBpoSjPPfZ0b2w4MJpReHqKbR5+T2t6hzml6\n"
921 "4ToyOKPDmamiTuN5KzLN3cw7DQlvWMvqSOChPLnA3Q==\n" 924 "4ToyOKPDmamiTuN5KzLN3cw7DQlvWMvqSOChPLnA3Q==\n"
922 "-----END CERTIFICATE-----\n"; 925 "-----END CERTIFICATE-----\n";
923 926
924 927
925/** 928/**
@@ -940,11 +943,11 @@ main (int argc, char *const *argv)
940 if ( (argc != 2) || 943 if ( (argc != 2) ||
941 (1 != sscanf (argv[1], "%u", &port)) || 944 (1 != sscanf (argv[1], "%u", &port)) ||
942 (UINT16_MAX < port) ) 945 (UINT16_MAX < port) )
943 { 946 {
944 fprintf (stderr, 947 fprintf (stderr,
945 "%s PORT\n", argv[0]); 948 "%s PORT\n", argv[0]);
946 return 1; 949 return 1;
947 } 950 }
948 #ifndef MINGW 951 #ifndef MINGW
949 ignore_sigpipe (); 952 ignore_sigpipe ();
950 #endif 953 #endif
@@ -954,35 +957,46 @@ main (int argc, char *const *argv)
954#endif /* MHD_HAVE_LIBMAGIC */ 957#endif /* MHD_HAVE_LIBMAGIC */
955 958
956 (void) pthread_mutex_init (&mutex, NULL); 959 (void) pthread_mutex_init (&mutex, NULL);
957 file_not_found_response = MHD_create_response_from_buffer (strlen (FILE_NOT_FOUND_PAGE), 960 file_not_found_response = MHD_create_response_from_buffer (strlen (
958 (void *) FILE_NOT_FOUND_PAGE, 961 FILE_NOT_FOUND_PAGE),
959 MHD_RESPMEM_PERSISTENT); 962 (void *)
963 FILE_NOT_FOUND_PAGE,
964 MHD_RESPMEM_PERSISTENT);
960 mark_as_html (file_not_found_response); 965 mark_as_html (file_not_found_response);
961 request_refused_response = MHD_create_response_from_buffer (strlen (REQUEST_REFUSED_PAGE), 966 request_refused_response = MHD_create_response_from_buffer (strlen (
962 (void *) REQUEST_REFUSED_PAGE, 967 REQUEST_REFUSED_PAGE),
963 MHD_RESPMEM_PERSISTENT); 968 (void *)
969 REQUEST_REFUSED_PAGE,
970 MHD_RESPMEM_PERSISTENT);
964 mark_as_html (request_refused_response); 971 mark_as_html (request_refused_response);
965 internal_error_response = MHD_create_response_from_buffer (strlen (INTERNAL_ERROR_PAGE), 972 internal_error_response = MHD_create_response_from_buffer (strlen (
966 (void *) INTERNAL_ERROR_PAGE, 973 INTERNAL_ERROR_PAGE),
967 MHD_RESPMEM_PERSISTENT); 974 (void *)
975 INTERNAL_ERROR_PAGE,
976 MHD_RESPMEM_PERSISTENT);
968 mark_as_html (internal_error_response); 977 mark_as_html (internal_error_response);
969 update_directory (); 978 update_directory ();
970 d = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_TLS, 979 d = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD
980 | MHD_USE_ERROR_LOG | MHD_USE_TLS,
971 port, 981 port,
972 NULL, NULL, 982 NULL, NULL,
973 &generate_page, NULL, 983 &generate_page, NULL,
974 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (256 * 1024), 984 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (256
985 * 1024),
975#if PRODUCTION 986#if PRODUCTION
976 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) (64), 987 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) (64),
977#endif 988#endif
978 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) (120 /* seconds */), 989 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned
979 MHD_OPTION_THREAD_POOL_SIZE, (unsigned int) NUMBER_OF_THREADS, 990 int) (120 /* seconds */),
980 MHD_OPTION_NOTIFY_COMPLETED, &response_completed_callback, NULL, 991 MHD_OPTION_THREAD_POOL_SIZE, (unsigned
981 /* Optionally, the gnutls_load_file() can be used to 992 int) NUMBER_OF_THREADS,
982 load the key and the certificate from file. */ 993 MHD_OPTION_NOTIFY_COMPLETED,
983 MHD_OPTION_HTTPS_MEM_KEY, srv_signed_key_pem, 994 &response_completed_callback, NULL,
984 MHD_OPTION_HTTPS_MEM_CERT, srv_signed_cert_pem, 995 /* Optionally, the gnutls_load_file() can be used to
985 MHD_OPTION_END); 996 load the key and the certificate from file. */
997 MHD_OPTION_HTTPS_MEM_KEY, srv_signed_key_pem,
998 MHD_OPTION_HTTPS_MEM_CERT, srv_signed_cert_pem,
999 MHD_OPTION_END);
986 if (NULL == d) 1000 if (NULL == d)
987 return 1; 1001 return 1;
988 fprintf (stderr, "HTTP server running. Press ENTER to stop the server\n"); 1002 fprintf (stderr, "HTTP server running. Press ENTER to stop the server\n");
diff --git a/src/examples/digest_auth_example.c b/src/examples/digest_auth_example.c
index 889967fb..720576cd 100644
--- a/src/examples/digest_auth_example.c
+++ b/src/examples/digest_auth_example.c
@@ -26,9 +26,11 @@
26#include <microhttpd.h> 26#include <microhttpd.h>
27#include <stdlib.h> 27#include <stdlib.h>
28 28
29#define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>Access granted</body></html>" 29#define PAGE \
30 "<html><head><title>libmicrohttpd demo</title></head><body>Access granted</body></html>"
30 31
31#define DENIED "<html><head><title>libmicrohttpd demo</title></head><body>Access denied</body></html>" 32#define DENIED \
33 "<html><head><title>libmicrohttpd demo</title></head><body>Access denied</body></html>"
32 34
33#define MY_OPAQUE_STR "11733b200778ce33060f31c9af70a870ba96ddd4" 35#define MY_OPAQUE_STR "11733b200778ce33060f31c9af70a870ba96ddd4"
34 36
@@ -45,51 +47,52 @@ ahc_echo (void *cls,
45 const char *password = "testpass"; 47 const char *password = "testpass";
46 const char *realm = "test@example.com"; 48 const char *realm = "test@example.com";
47 int ret; 49 int ret;
48 (void)cls; /* Unused. Silent compiler warning. */ 50 (void) cls; /* Unused. Silent compiler warning. */
49 (void)url; /* Unused. Silent compiler warning. */ 51 (void) url; /* Unused. Silent compiler warning. */
50 (void)method; /* Unused. Silent compiler warning. */ 52 (void) method; /* Unused. Silent compiler warning. */
51 (void)version; /* Unused. Silent compiler warning. */ 53 (void) version; /* Unused. Silent compiler warning. */
52 (void)upload_data; /* Unused. Silent compiler warning. */ 54 (void) upload_data; /* Unused. Silent compiler warning. */
53 (void)upload_data_size; /* Unused. Silent compiler warning. */ 55 (void) upload_data_size; /* Unused. Silent compiler warning. */
54 (void)ptr; /* Unused. Silent compiler warning. */ 56 (void) ptr; /* Unused. Silent compiler warning. */
55 57
56 username = MHD_digest_auth_get_username(connection); 58 username = MHD_digest_auth_get_username (connection);
57 if (NULL == username) 59 if (NULL == username)
58 { 60 {
59 response = MHD_create_response_from_buffer(strlen (DENIED), 61 response = MHD_create_response_from_buffer (strlen (DENIED),
60 DENIED, 62 DENIED,
61 MHD_RESPMEM_PERSISTENT); 63 MHD_RESPMEM_PERSISTENT);
62 ret = MHD_queue_auth_fail_response(connection, realm, 64 ret = MHD_queue_auth_fail_response (connection, realm,
63 MY_OPAQUE_STR, 65 MY_OPAQUE_STR,
64 response, 66 response,
65 MHD_NO); 67 MHD_NO);
66 MHD_destroy_response(response); 68 MHD_destroy_response (response);
67 return ret; 69 return ret;
68 } 70 }
69 ret = MHD_digest_auth_check(connection, realm, 71 ret = MHD_digest_auth_check (connection, realm,
70 username, 72 username,
71 password, 73 password,
72 300); 74 300);
73 MHD_free (username); 75 MHD_free (username);
74 if ( (ret == MHD_INVALID_NONCE) || 76 if ( (ret == MHD_INVALID_NONCE) ||
75 (ret == MHD_NO) ) 77 (ret == MHD_NO) )
76 { 78 {
77 response = MHD_create_response_from_buffer(strlen (DENIED), 79 response = MHD_create_response_from_buffer (strlen (DENIED),
78 DENIED, 80 DENIED,
79 MHD_RESPMEM_PERSISTENT); 81 MHD_RESPMEM_PERSISTENT);
80 if (NULL == response) 82 if (NULL == response)
81 return MHD_NO; 83 return MHD_NO;
82 ret = MHD_queue_auth_fail_response(connection, realm, 84 ret = MHD_queue_auth_fail_response (connection, realm,
83 MY_OPAQUE_STR, 85 MY_OPAQUE_STR,
84 response, 86 response,
85 (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO); 87 (ret == MHD_INVALID_NONCE) ? MHD_YES :
86 MHD_destroy_response(response); 88 MHD_NO);
87 return ret; 89 MHD_destroy_response (response);
88 } 90 return ret;
89 response = MHD_create_response_from_buffer(strlen(PAGE), PAGE, 91 }
90 MHD_RESPMEM_PERSISTENT); 92 response = MHD_create_response_from_buffer (strlen (PAGE), PAGE,
91 ret = MHD_queue_response(connection, MHD_HTTP_OK, response); 93 MHD_RESPMEM_PERSISTENT);
92 MHD_destroy_response(response); 94 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
95 MHD_destroy_response (response);
93 return ret; 96 return ret;
94} 97}
95 98
@@ -104,40 +107,41 @@ main (int argc, char *const *argv)
104 struct MHD_Daemon *d; 107 struct MHD_Daemon *d;
105 108
106 if (argc != 2) 109 if (argc != 2)
107 { 110 {
108 printf ("%s PORT\n", argv[0]); 111 printf ("%s PORT\n", argv[0]);
109 return 1; 112 return 1;
110 } 113 }
111 fd = open("/dev/urandom", O_RDONLY); 114 fd = open ("/dev/urandom", O_RDONLY);
112 if (-1 == fd) 115 if (-1 == fd)
113 { 116 {
114 fprintf (stderr, "Failed to open `%s': %s\n", 117 fprintf (stderr, "Failed to open `%s': %s\n",
115 "/dev/urandom", 118 "/dev/urandom",
116 strerror (errno)); 119 strerror (errno));
117 return 1; 120 return 1;
118 } 121 }
119 off = 0; 122 off = 0;
120 while (off < 8) 123 while (off < 8)
124 {
125 len = read (fd, rnd, 8);
126 if (len == -1)
121 { 127 {
122 len = read(fd, rnd, 8); 128 fprintf (stderr, "Failed to read `%s': %s\n",
123 if (len == -1) 129 "/dev/urandom",
124 { 130 strerror (errno));
125 fprintf (stderr, "Failed to read `%s': %s\n", 131 (void) close (fd);
126 "/dev/urandom", 132 return 1;
127 strerror (errno));
128 (void) close (fd);
129 return 1;
130 }
131 off += len;
132 } 133 }
133 (void) close(fd); 134 off += len;
134 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 135 }
136 (void) close (fd);
137 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
138 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
135 atoi (argv[1]), 139 atoi (argv[1]),
136 NULL, NULL, &ahc_echo, PAGE, 140 NULL, NULL, &ahc_echo, PAGE,
137 MHD_OPTION_DIGEST_AUTH_RANDOM, sizeof(rnd), rnd, 141 MHD_OPTION_DIGEST_AUTH_RANDOM, sizeof(rnd), rnd,
138 MHD_OPTION_NONCE_NC_SIZE, 300, 142 MHD_OPTION_NONCE_NC_SIZE, 300,
139 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, 143 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
140 MHD_OPTION_END); 144 MHD_OPTION_END);
141 if (d == NULL) 145 if (d == NULL)
142 return 1; 146 return 1;
143 (void) getc (stdin); 147 (void) getc (stdin);
diff --git a/src/examples/dual_stack_example.c b/src/examples/dual_stack_example.c
index 31b25438..ffacec4a 100644
--- a/src/examples/dual_stack_example.c
+++ b/src/examples/dual_stack_example.c
@@ -25,7 +25,8 @@
25#include "platform.h" 25#include "platform.h"
26#include <microhttpd.h> 26#include <microhttpd.h>
27 27
28#define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>" 28#define PAGE \
29 "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>"
29 30
30static int 31static int
31ahc_echo (void *cls, 32ahc_echo (void *cls,
@@ -39,23 +40,23 @@ ahc_echo (void *cls,
39 const char *me = cls; 40 const char *me = cls;
40 struct MHD_Response *response; 41 struct MHD_Response *response;
41 int ret; 42 int ret;
42 (void)url; /* Unused. Silent compiler warning. */ 43 (void) url; /* Unused. Silent compiler warning. */
43 (void)version; /* Unused. Silent compiler warning. */ 44 (void) version; /* Unused. Silent compiler warning. */
44 (void)upload_data; /* Unused. Silent compiler warning. */ 45 (void) upload_data; /* Unused. Silent compiler warning. */
45 (void)upload_data_size; /* Unused. Silent compiler warning. */ 46 (void) upload_data_size; /* Unused. Silent compiler warning. */
46 47
47 if (0 != strcmp (method, "GET")) 48 if (0 != strcmp (method, "GET"))
48 return MHD_NO; /* unexpected method */ 49 return MHD_NO; /* unexpected method */
49 if (&aptr != *ptr) 50 if (&aptr != *ptr)
50 { 51 {
51 /* do never respond on first call */ 52 /* do never respond on first call */
52 *ptr = &aptr; 53 *ptr = &aptr;
53 return MHD_YES; 54 return MHD_YES;
54 } 55 }
55 *ptr = NULL; /* reset when done */ 56 *ptr = NULL; /* reset when done */
56 response = MHD_create_response_from_buffer (strlen (me), 57 response = MHD_create_response_from_buffer (strlen (me),
57 (void *) me, 58 (void *) me,
58 MHD_RESPMEM_PERSISTENT); 59 MHD_RESPMEM_PERSISTENT);
59 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 60 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
60 MHD_destroy_response (response); 61 MHD_destroy_response (response);
61 return ret; 62 return ret;
@@ -68,15 +69,16 @@ main (int argc, char *const *argv)
68 struct MHD_Daemon *d; 69 struct MHD_Daemon *d;
69 70
70 if (argc != 2) 71 if (argc != 2)
71 { 72 {
72 printf ("%s PORT\n", argv[0]); 73 printf ("%s PORT\n", argv[0]);
73 return 1; 74 return 1;
74 } 75 }
75 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_DUAL_STACK, 76 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG
76 atoi (argv[1]), 77 | MHD_USE_DUAL_STACK,
77 NULL, NULL, &ahc_echo, PAGE, 78 atoi (argv[1]),
78 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, 79 NULL, NULL, &ahc_echo, PAGE,
79 MHD_OPTION_END); 80 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
81 MHD_OPTION_END);
80 (void) getc (stdin); 82 (void) getc (stdin);
81 MHD_stop_daemon (d); 83 MHD_stop_daemon (d);
82 return 0; 84 return 0;
diff --git a/src/examples/fileserver_example_dirs.c b/src/examples/fileserver_example_dirs.c
index 8c37f219..2b544b69 100644
--- a/src/examples/fileserver_example_dirs.c
+++ b/src/examples/fileserver_example_dirs.c
@@ -28,7 +28,8 @@
28#include <microhttpd.h> 28#include <microhttpd.h>
29#include <unistd.h> 29#include <unistd.h>
30 30
31#define PAGE "<html><head><title>File not found</title></head><body>File not found</body></html>" 31#define PAGE \
32 "<html><head><title>File not found</title></head><body>File not found</body></html>"
32 33
33 34
34static ssize_t 35static ssize_t
@@ -66,17 +67,17 @@ dir_reader (void *cls, uint64_t pos, char *buf, size_t max)
66 67
67 if (max < 512) 68 if (max < 512)
68 return 0; 69 return 0;
69 (void)pos; /* 'pos' is ignored as function return next one single entry per call. */ 70 (void) pos; /* 'pos' is ignored as function return next one single entry per call. */
70 do 71 do
71 { 72 {
72 e = readdir (dir); 73 e = readdir (dir);
73 if (e == NULL) 74 if (e == NULL)
74 return MHD_CONTENT_READER_END_OF_STREAM; 75 return MHD_CONTENT_READER_END_OF_STREAM;
75 } while (e->d_name[0] == '.'); 76 } while (e->d_name[0] == '.');
76 return snprintf (buf, max, 77 return snprintf (buf, max,
77 "<a href=\"/%s\">%s</a><br>", 78 "<a href=\"/%s\">%s</a><br>",
78 e->d_name, 79 e->d_name,
79 e->d_name); 80 e->d_name);
80} 81}
81 82
82 83
@@ -87,7 +88,7 @@ ahc_echo (void *cls,
87 const char *method, 88 const char *method,
88 const char *version, 89 const char *version,
89 const char *upload_data, 90 const char *upload_data,
90 size_t *upload_data_size, void **ptr) 91 size_t *upload_data_size, void **ptr)
91{ 92{
92 static int aptr; 93 static int aptr;
93 struct MHD_Response *response; 94 struct MHD_Response *response;
@@ -97,90 +98,90 @@ ahc_echo (void *cls,
97 DIR *dir; 98 DIR *dir;
98 struct stat buf; 99 struct stat buf;
99 char emsg[1024]; 100 char emsg[1024];
100 (void)cls; /* Unused. Silent compiler warning. */ 101 (void) cls; /* Unused. Silent compiler warning. */
101 (void)version; /* Unused. Silent compiler warning. */ 102 (void) version; /* Unused. Silent compiler warning. */
102 (void)upload_data; /* Unused. Silent compiler warning. */ 103 (void) upload_data; /* Unused. Silent compiler warning. */
103 (void)upload_data_size; /* Unused. Silent compiler warning. */ 104 (void) upload_data_size; /* Unused. Silent compiler warning. */
104 105
105 if (0 != strcmp (method, MHD_HTTP_METHOD_GET)) 106 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
106 return MHD_NO; /* unexpected method */ 107 return MHD_NO; /* unexpected method */
107 if (&aptr != *ptr) 108 if (&aptr != *ptr)
108 { 109 {
109 /* do never respond on first call */ 110 /* do never respond on first call */
110 *ptr = &aptr; 111 *ptr = &aptr;
111 return MHD_YES; 112 return MHD_YES;
112 } 113 }
113 *ptr = NULL; /* reset when done */ 114 *ptr = NULL; /* reset when done */
114 115
115 file = fopen (&url[1], "rb"); 116 file = fopen (&url[1], "rb");
116 if (NULL != file) 117 if (NULL != file)
118 {
119 fd = fileno (file);
120 if (-1 == fd)
117 { 121 {
118 fd = fileno (file); 122 (void) fclose (file);
119 if (-1 == fd) 123 return MHD_NO; /* internal error */
120 {
121 (void) fclose (file);
122 return MHD_NO; /* internal error */
123 }
124 if ( (0 != fstat (fd, &buf)) ||
125 (! S_ISREG (buf.st_mode)) )
126 {
127 /* not a regular file, refuse to serve */
128 fclose (file);
129 file = NULL;
130 }
131 } 124 }
125 if ( (0 != fstat (fd, &buf)) ||
126 (! S_ISREG (buf.st_mode)) )
127 {
128 /* not a regular file, refuse to serve */
129 fclose (file);
130 file = NULL;
131 }
132 }
132 133
133 if (NULL == file) 134 if (NULL == file)
135 {
136 dir = opendir (".");
137 if (NULL == dir)
134 { 138 {
135 dir = opendir ("."); 139 /* most likely cause: more concurrent requests than
136 if (NULL == dir) 140 available file descriptors / 2 */
137 { 141 snprintf (emsg,
138 /* most likely cause: more concurrent requests than 142 sizeof (emsg),
139 available file descriptors / 2 */ 143 "Failed to open directory `.': %s\n",
140 snprintf (emsg, 144 strerror (errno));
141 sizeof (emsg), 145 response = MHD_create_response_from_buffer (strlen (emsg),
142 "Failed to open directory `.': %s\n", 146 emsg,
143 strerror (errno)); 147 MHD_RESPMEM_MUST_COPY);
144 response = MHD_create_response_from_buffer (strlen (emsg), 148 if (NULL == response)
145 emsg, 149 return MHD_NO;
146 MHD_RESPMEM_MUST_COPY); 150 ret = MHD_queue_response (connection,
147 if (NULL == response) 151 MHD_HTTP_SERVICE_UNAVAILABLE,
148 return MHD_NO; 152 response);
149 ret = MHD_queue_response (connection, 153 MHD_destroy_response (response);
150 MHD_HTTP_SERVICE_UNAVAILABLE,
151 response);
152 MHD_destroy_response (response);
153 }
154 else
155 {
156 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
157 32 * 1024,
158 &dir_reader,
159 dir,
160 &dir_free_callback);
161 if (NULL == response)
162 {
163 closedir (dir);
164 return MHD_NO;
165 }
166 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
167 MHD_destroy_response (response);
168 }
169 } 154 }
170 else 155 else
171 { 156 {
172 response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k page size */ 157 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
173 &file_reader, 158 32 * 1024,
174 file, 159 &dir_reader,
175 &file_free_callback); 160 dir,
161 &dir_free_callback);
176 if (NULL == response) 162 if (NULL == response)
177 { 163 {
178 fclose (file); 164 closedir (dir);
179 return MHD_NO; 165 return MHD_NO;
180 } 166 }
181 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 167 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
182 MHD_destroy_response (response); 168 MHD_destroy_response (response);
183 } 169 }
170 }
171 else
172 {
173 response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k page size */
174 &file_reader,
175 file,
176 &file_free_callback);
177 if (NULL == response)
178 {
179 fclose (file);
180 return MHD_NO;
181 }
182 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
183 MHD_destroy_response (response);
184 }
184 return ret; 185 return ret;
185} 186}
186 187
@@ -191,11 +192,12 @@ main (int argc, char *const *argv)
191 struct MHD_Daemon *d; 192 struct MHD_Daemon *d;
192 193
193 if (argc != 2) 194 if (argc != 2)
194 { 195 {
195 printf ("%s PORT\n", argv[0]); 196 printf ("%s PORT\n", argv[0]);
196 return 1; 197 return 1;
197 } 198 }
198 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 199 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
200 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
199 atoi (argv[1]), 201 atoi (argv[1]),
200 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); 202 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END);
201 if (NULL == d) 203 if (NULL == d)
diff --git a/src/examples/fileserver_example_external_select.c b/src/examples/fileserver_example_external_select.c
index 6aea6dbf..187cdf7e 100644
--- a/src/examples/fileserver_example_external_select.c
+++ b/src/examples/fileserver_example_external_select.c
@@ -27,7 +27,8 @@
27#include <sys/stat.h> 27#include <sys/stat.h>
28#include <unistd.h> 28#include <unistd.h>
29 29
30#define PAGE "<html><head><title>File not found</title></head><body>File not found</body></html>" 30#define PAGE \
31 "<html><head><title>File not found</title></head><body>File not found</body></html>"
31 32
32static ssize_t 33static ssize_t
33file_reader (void *cls, uint64_t pos, char *buf, size_t max) 34file_reader (void *cls, uint64_t pos, char *buf, size_t max)
@@ -54,7 +55,7 @@ ahc_echo (void *cls,
54 const char *method, 55 const char *method,
55 const char *version, 56 const char *version,
56 const char *upload_data, 57 const char *upload_data,
57 size_t *upload_data_size, void **ptr) 58 size_t *upload_data_size, void **ptr)
58{ 59{
59 static int aptr; 60 static int aptr;
60 struct MHD_Response *response; 61 struct MHD_Response *response;
@@ -62,61 +63,61 @@ ahc_echo (void *cls,
62 FILE *file; 63 FILE *file;
63 int fd; 64 int fd;
64 struct stat buf; 65 struct stat buf;
65 (void)cls; /* Unused. Silent compiler warning. */ 66 (void) cls; /* Unused. Silent compiler warning. */
66 (void)version; /* Unused. Silent compiler warning. */ 67 (void) version; /* Unused. Silent compiler warning. */
67 (void)upload_data; /* Unused. Silent compiler warning. */ 68 (void) upload_data; /* Unused. Silent compiler warning. */
68 (void)upload_data_size; /* Unused. Silent compiler warning. */ 69 (void) upload_data_size; /* Unused. Silent compiler warning. */
69 70
70 if (0 != strcmp (method, MHD_HTTP_METHOD_GET)) 71 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
71 return MHD_NO; /* unexpected method */ 72 return MHD_NO; /* unexpected method */
72 if (&aptr != *ptr) 73 if (&aptr != *ptr)
73 { 74 {
74 /* do never respond on first call */ 75 /* do never respond on first call */
75 *ptr = &aptr; 76 *ptr = &aptr;
76 return MHD_YES; 77 return MHD_YES;
77 } 78 }
78 *ptr = NULL; /* reset when done */ 79 *ptr = NULL; /* reset when done */
79 80
80 file = fopen (&url[1], "rb"); 81 file = fopen (&url[1], "rb");
81 if (NULL != file) 82 if (NULL != file)
83 {
84 fd = fileno (file);
85 if (-1 == fd)
82 { 86 {
83 fd = fileno (file); 87 (void) fclose (file);
84 if (-1 == fd) 88 return MHD_NO; /* internal error */
85 {
86 (void) fclose (file);
87 return MHD_NO; /* internal error */
88 }
89 if ( (0 != fstat (fd, &buf)) ||
90 (! S_ISREG (buf.st_mode)) )
91 {
92 /* not a regular file, refuse to serve */
93 fclose (file);
94 file = NULL;
95 }
96 } 89 }
97 90 if ( (0 != fstat (fd, &buf)) ||
98 if (NULL == file) 91 (! S_ISREG (buf.st_mode)) )
99 { 92 {
100 response = MHD_create_response_from_buffer (strlen (PAGE), 93 /* not a regular file, refuse to serve */
101 (void *) PAGE, 94 fclose (file);
102 MHD_RESPMEM_PERSISTENT); 95 file = NULL;
103 ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
104 MHD_destroy_response (response);
105 } 96 }
97 }
98
99 if (NULL == file)
100 {
101 response = MHD_create_response_from_buffer (strlen (PAGE),
102 (void *) PAGE,
103 MHD_RESPMEM_PERSISTENT);
104 ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
105 MHD_destroy_response (response);
106 }
106 else 107 else
108 {
109 response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k page size */
110 &file_reader,
111 file,
112 &free_callback);
113 if (NULL == response)
107 { 114 {
108 response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k page size */ 115 fclose (file);
109 &file_reader, 116 return MHD_NO;
110 file,
111 &free_callback);
112 if (NULL == response)
113 {
114 fclose (file);
115 return MHD_NO;
116 }
117 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
118 MHD_destroy_response (response);
119 } 117 }
118 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
119 MHD_destroy_response (response);
120 }
120 return ret; 121 return ret;
121} 122}
122 123
@@ -135,10 +136,10 @@ main (int argc, char *const *argv)
135 MHD_UNSIGNED_LONG_LONG mhd_timeout; 136 MHD_UNSIGNED_LONG_LONG mhd_timeout;
136 137
137 if (argc != 3) 138 if (argc != 3)
138 { 139 {
139 printf ("%s PORT SECONDS-TO-RUN\n", argv[0]); 140 printf ("%s PORT SECONDS-TO-RUN\n", argv[0]);
140 return 1; 141 return 1;
141 } 142 }
142 d = MHD_start_daemon (MHD_USE_ERROR_LOG, 143 d = MHD_start_daemon (MHD_USE_ERROR_LOG,
143 atoi (argv[1]), 144 atoi (argv[1]),
144 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); 145 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END);
@@ -146,30 +147,30 @@ main (int argc, char *const *argv)
146 return 1; 147 return 1;
147 end = time (NULL) + atoi (argv[2]); 148 end = time (NULL) + atoi (argv[2]);
148 while ((t = time (NULL)) < end) 149 while ((t = time (NULL)) < end)
150 {
151 tv.tv_sec = end - t;
152 tv.tv_usec = 0;
153 max = 0;
154 FD_ZERO (&rs);
155 FD_ZERO (&ws);
156 FD_ZERO (&es);
157 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
158 break; /* fatal internal error */
159 if (MHD_get_timeout (d, &mhd_timeout) == MHD_YES)
160 {
161 if (((MHD_UNSIGNED_LONG_LONG) tv.tv_sec) < mhd_timeout / 1000LL)
162 {
163 tv.tv_sec = mhd_timeout / 1000LL;
164 tv.tv_usec = (mhd_timeout - (tv.tv_sec * 1000LL)) * 1000LL;
165 }
166 }
167 if (-1 == select (max + 1, &rs, &ws, &es, &tv))
149 { 168 {
150 tv.tv_sec = end - t; 169 if (EINTR != errno)
151 tv.tv_usec = 0; 170 abort ();
152 max = 0;
153 FD_ZERO (&rs);
154 FD_ZERO (&ws);
155 FD_ZERO (&es);
156 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
157 break; /* fatal internal error */
158 if (MHD_get_timeout (d, &mhd_timeout) == MHD_YES)
159 {
160 if (((MHD_UNSIGNED_LONG_LONG)tv.tv_sec) < mhd_timeout / 1000LL)
161 {
162 tv.tv_sec = mhd_timeout / 1000LL;
163 tv.tv_usec = (mhd_timeout - (tv.tv_sec * 1000LL)) * 1000LL;
164 }
165 }
166 if (-1 == select (max + 1, &rs, &ws, &es, &tv))
167 {
168 if (EINTR != er