aboutsummaryrefslogtreecommitdiff
path: root/src/examples
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 /src/examples
parentcbbfd0591fc639ddd21a5850f54f403fb7394174 (diff)
downloadlibmicrohttpd-972103dc288e2a2396e060018e7c3733f29a940d.tar.gz
libmicrohttpd-972103dc288e2a2396e060018e7c3733f29a940d.zip
applying uncrustify to ensure uniform indentation
Diffstat (limited to 'src/examples')
-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
22 files changed, 1927 insertions, 1847 deletions
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 != errno)
169 abort ();
170 }
171 MHD_run (d);
172 } 171 }
172 MHD_run (d);
173 }
173 MHD_stop_daemon (d); 174 MHD_stop_daemon (d);
174 return 0; 175 return 0;
175} 176}
diff --git a/src/examples/http_chunked_compression.c b/src/examples/http_chunked_compression.c
index 9c3f629f..461f4d3a 100644
--- a/src/examples/http_chunked_compression.c
+++ b/src/examples/http_chunked_compression.c
@@ -38,20 +38,22 @@
38#elif defined(INTPTR_MAX) 38#elif defined(INTPTR_MAX)
39#define SSIZE_MAX INTPTR_MAX 39#define SSIZE_MAX INTPTR_MAX
40#else 40#else
41#define SSIZE_MAX ((ssize_t)(((size_t)-1)>>1)) 41#define SSIZE_MAX ((ssize_t) (((size_t) -1) >> 1))
42#endif 42#endif
43#endif /* ! SSIZE_MAX */ 43#endif /* ! SSIZE_MAX */
44 44
45#define CHUNK 16384 45#define CHUNK 16384
46 46
47struct Holder { 47struct Holder
48 FILE *file; 48{
49 z_stream stream; 49 FILE *file;
50 void *buf; 50 z_stream stream;
51 void *buf;
51}; 52};
52 53
53static int 54static int
54compress_buf (z_stream *strm, const void *src, size_t src_size, size_t *offset, void **dest, size_t *dest_size, 55compress_buf (z_stream *strm, const void *src, size_t src_size, size_t *offset,
56 void **dest, size_t *dest_size,
55 void *tmp) 57 void *tmp)
56{ 58{
57 unsigned int have; 59 unsigned int have;
@@ -61,39 +63,39 @@ compress_buf (z_stream *strm, const void *src, size_t src_size, size_t *offset,
61 *dest = NULL; 63 *dest = NULL;
62 *dest_size = 0; 64 *dest_size = 0;
63 do 65 do
66 {
67 if (src_size > CHUNK)
64 { 68 {
65 if (src_size > CHUNK) 69 strm->avail_in = CHUNK;
66 { 70 src_size -= CHUNK;
67 strm->avail_in = CHUNK; 71 flush = Z_NO_FLUSH;
68 src_size -= CHUNK;
69 flush = Z_NO_FLUSH;
70 }
71 else
72 {
73 strm->avail_in = (uInt) src_size;
74 flush = Z_SYNC_FLUSH;
75 }
76 *offset += strm->avail_in;
77 strm->next_in = (Bytef *) src;
78 do
79 {
80 strm->avail_out = CHUNK;
81 strm->next_out = tmp;
82 ret = deflate (strm, flush);
83 have = CHUNK - strm->avail_out;
84 *dest_size += have;
85 tmp_dest = realloc (*dest, *dest_size);
86 if (NULL == tmp_dest)
87 {
88 free (*dest);
89 *dest = NULL;
90 return MHD_NO;
91 }
92 *dest = tmp_dest;
93 memcpy ((*dest) + ((*dest_size) - have), tmp, have);
94 }
95 while (0 == strm->avail_out);
96 } 72 }
73 else
74 {
75 strm->avail_in = (uInt) src_size;
76 flush = Z_SYNC_FLUSH;
77 }
78 *offset += strm->avail_in;
79 strm->next_in = (Bytef *) src;
80 do
81 {
82 strm->avail_out = CHUNK;
83 strm->next_out = tmp;
84 ret = deflate (strm, flush);
85 have = CHUNK - strm->avail_out;
86 *dest_size += have;
87 tmp_dest = realloc (*dest, *dest_size);
88 if (NULL == tmp_dest)
89 {
90 free (*dest);
91 *dest = NULL;
92 return MHD_NO;
93 }
94 *dest = tmp_dest;
95 memcpy ((*dest) + ((*dest_size) - have), tmp, have);
96 }
97 while (0 == strm->avail_out);
98 }
97 while (flush != Z_SYNC_FLUSH); 99 while (flush != Z_SYNC_FLUSH);
98 return (Z_OK == ret) ? MHD_YES : MHD_NO; 100 return (Z_OK == ret) ? MHD_YES : MHD_NO;
99} 101}
@@ -114,22 +116,23 @@ read_cb (void *cls, uint64_t pos, char *mem, size_t size)
114 return MHD_CONTENT_READER_END_WITH_ERROR; 116 return MHD_CONTENT_READER_END_WITH_ERROR;
115 ret = fread (src, 1, size, holder->file); 117 ret = fread (src, 1, size, holder->file);
116 if (ret < 0) 118 if (ret < 0)
117 { 119 {
118 ret = MHD_CONTENT_READER_END_WITH_ERROR; 120 ret = MHD_CONTENT_READER_END_WITH_ERROR;
119 goto done; 121 goto done;
120 } 122 }
121 if (0 == size) 123 if (0 == size)
122 { 124 {
123 ret = MHD_CONTENT_READER_END_OF_STREAM; 125 ret = MHD_CONTENT_READER_END_OF_STREAM;
124 goto done; 126 goto done;
125 } 127 }
126 if (MHD_YES != compress_buf (&holder->stream, src, ret, &offset, &buf, &size, holder->buf)) 128 if (MHD_YES != compress_buf (&holder->stream, src, ret, &offset, &buf, &size,
129 holder->buf))
127 ret = MHD_CONTENT_READER_END_WITH_ERROR; 130 ret = MHD_CONTENT_READER_END_WITH_ERROR;
128 else 131 else
129 { 132 {
130 memcpy (mem, buf, size); 133 memcpy (mem, buf, size);
131 ret = size; 134 ret = size;
132 } 135 }
133 free (buf); /* Buf may be set even on error return. */ 136 free (buf); /* Buf may be set even on error return. */
134done: 137done:
135 free (src); 138 free (src);
@@ -147,7 +150,8 @@ free_cb (void *cls)
147} 150}
148 151
149static int 152static int
150ahc_echo (void *cls, struct MHD_Connection *con, const char *url, const char *method, const char *version, 153ahc_echo (void *cls, struct MHD_Connection *con, const char *url, const
154 char *method, const char *version,
151 const char *upload_data, size_t *upload_size, void **ptr) 155 const char *upload_data, size_t *upload_size, void **ptr)
152{ 156{
153 struct Holder *holder; 157 struct Holder *holder;
@@ -160,27 +164,29 @@ ahc_echo (void *cls, struct MHD_Connection *con, const char *url, const char *me
160 (void) upload_data; 164 (void) upload_data;
161 (void) upload_size; 165 (void) upload_size;
162 if (NULL == *ptr) 166 if (NULL == *ptr)
163 { 167 {
164 *ptr = (void *) 1; 168 *ptr = (void *) 1;
165 return MHD_YES; 169 return MHD_YES;
166 } 170 }
167 *ptr = NULL; 171 *ptr = NULL;
168 holder = calloc (1, sizeof (struct Holder)); 172 holder = calloc (1, sizeof (struct Holder));
169 if (!holder) 173 if (! holder)
170 return MHD_NO; 174 return MHD_NO;
171 holder->file = fopen (__FILE__, "rb"); 175 holder->file = fopen (__FILE__, "rb");
172 if (NULL == holder->file) 176 if (NULL == holder->file)
173 goto file_error; 177 goto file_error;
174 ret = deflateInit(&holder->stream, Z_BEST_COMPRESSION); 178 ret = deflateInit (&holder->stream, Z_BEST_COMPRESSION);
175 if (ret != Z_OK) 179 if (ret != Z_OK)
176 goto stream_error; 180 goto stream_error;
177 holder->buf = malloc (CHUNK); 181 holder->buf = malloc (CHUNK);
178 if (NULL == holder->buf) 182 if (NULL == holder->buf)
179 goto buf_error; 183 goto buf_error;
180 res = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 1024, &read_cb, holder, &free_cb); 184 res = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 1024, &read_cb,
185 holder, &free_cb);
181 if (NULL == res) 186 if (NULL == res)
182 goto error; 187 goto error;
183 ret = MHD_add_response_header (res, MHD_HTTP_HEADER_CONTENT_ENCODING, "deflate"); 188 ret = MHD_add_response_header (res, MHD_HTTP_HEADER_CONTENT_ENCODING,
189 "deflate");
184 if (MHD_YES != ret) 190 if (MHD_YES != ret)
185 goto res_error; 191 goto res_error;
186 ret = MHD_add_response_header (res, MHD_HTTP_HEADER_CONTENT_TYPE, "text/x-c"); 192 ret = MHD_add_response_header (res, MHD_HTTP_HEADER_CONTENT_TYPE, "text/x-c");
@@ -209,18 +215,21 @@ main (int argc, char *const *argv)
209 if ((argc != 2) || 215 if ((argc != 2) ||
210 (1 != sscanf (argv[1], "%u", &port)) || 216 (1 != sscanf (argv[1], "%u", &port)) ||
211 (UINT16_MAX < port)) 217 (UINT16_MAX < port))
212 { 218 {
213 fprintf (stderr, "%s PORT\n", argv[0]); 219 fprintf (stderr, "%s PORT\n", argv[0]);
214 return 1; 220 return 1;
215 } 221 }
216 d = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD, (uint16_t) port, NULL, NULL, 222 d = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD,
223 (uint16_t) port, NULL, NULL,
217 &ahc_echo, NULL, 224 &ahc_echo, NULL,
218 MHD_OPTION_END); 225 MHD_OPTION_END);
219 if (NULL == d) 226 if (NULL == d)
220 return 1; 227 return 1;
221 if (0 == port) 228 if (0 == port)
222 MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT, &port); 229 MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT, &port);
223 fprintf (stdout, "HTTP server running at http://localhost:%u\n\nPress ENTER to stop the server ...\n", port); 230 fprintf (stdout,
231 "HTTP server running at http://localhost:%u\n\nPress ENTER to stop the server ...\n",
232 port);
224 (void) getc (stdin); 233 (void) getc (stdin);
225 MHD_stop_daemon (d); 234 MHD_stop_daemon (d);
226 return 0; 235 return 0;
diff --git a/src/examples/http_compression.c b/src/examples/http_compression.c
index 1324fa4d..af153f6e 100644
--- a/src/examples/http_compression.c
+++ b/src/examples/http_compression.c
@@ -77,11 +77,11 @@ body_compress (void **buf,
77 *buf_size); 77 *buf_size);
78 if ((Z_OK != ret) || 78 if ((Z_OK != ret) ||
79 (cbuf_size >= *buf_size)) 79 (cbuf_size >= *buf_size))
80 { 80 {
81 /* compression failed */ 81 /* compression failed */
82 free (cbuf); 82 free (cbuf);
83 return MHD_NO; 83 return MHD_NO;
84 } 84 }
85 free (*buf); 85 free (*buf);
86 *buf = (void *) cbuf; 86 *buf = (void *) cbuf;
87 *buf_size = (size_t) cbuf_size; 87 *buf_size = (size_t) cbuf_size;
@@ -109,18 +109,18 @@ ahc_echo (void *cls,
109 109
110 if (0 != strcmp (method, "GET")) 110 if (0 != strcmp (method, "GET"))
111 return MHD_NO; /* unexpected method */ 111 return MHD_NO; /* unexpected method */
112 if (!*ptr) 112 if (! *ptr)
113 { 113 {
114 *ptr = (void *) 1; 114 *ptr = (void *) 1;
115 return MHD_YES; 115 return MHD_YES;
116 } 116 }
117 *ptr = NULL; 117 *ptr = NULL;
118 118
119 body_str = strdup (PAGE); 119 body_str = strdup (PAGE);
120 if (NULL == body_str) 120 if (NULL == body_str)
121 { 121 {
122 return MHD_NO; 122 return MHD_NO;
123 } 123 }
124 body_len = strlen (body_str); 124 body_len = strlen (body_str);
125 /* try to compress the body */ 125 /* try to compress the body */
126 comp = MHD_NO; 126 comp = MHD_NO;
@@ -132,23 +132,23 @@ ahc_echo (void *cls,
132 body_str, 132 body_str,
133 MHD_RESPMEM_MUST_FREE); 133 MHD_RESPMEM_MUST_FREE);
134 if (NULL == response) 134 if (NULL == response)
135 { 135 {
136 free (body_str); 136 free (body_str);
137 return MHD_NO; 137 return MHD_NO;
138 } 138 }
139 139
140 if (MHD_YES == comp) 140 if (MHD_YES == comp)
141 {
142 /* Need to indicate to client that body is compressed */
143 if (MHD_NO ==
144 MHD_add_response_header (response,
145 MHD_HTTP_HEADER_CONTENT_ENCODING,
146 "deflate"))
141 { 147 {
142 /* Need to indicate to client that body is compressed */ 148 MHD_destroy_response (response);
143 if (MHD_NO == 149 return MHD_NO;
144 MHD_add_response_header (response,
145 MHD_HTTP_HEADER_CONTENT_ENCODING,
146 "deflate"))
147 {
148 MHD_destroy_response (response);
149 return MHD_NO;
150 }
151 } 150 }
151 }
152 ret = MHD_queue_response (connection, 152 ret = MHD_queue_response (connection,
153 200, 153 200,
154 response); 154 response);
@@ -162,11 +162,12 @@ main (int argc, char *const *argv)
162 struct MHD_Daemon *d; 162 struct MHD_Daemon *d;
163 163
164 if (argc != 2) 164 if (argc != 2)
165 { 165 {
166 printf ("%s PORT\n", argv[0]); 166 printf ("%s PORT\n", argv[0]);
167 return 1; 167 return 1;
168 } 168 }
169 d = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 169 d = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD
170 | MHD_USE_ERROR_LOG,
170 atoi (argv[1]), NULL, NULL, 171 atoi (argv[1]), NULL, NULL,
171 &ahc_echo, NULL, 172 &ahc_echo, NULL,
172 MHD_OPTION_END); 173 MHD_OPTION_END);
diff --git a/src/examples/https_fileserver_example.c b/src/examples/https_fileserver_example.c
index 7e3e6d43..cd7aa355 100644
--- a/src/examples/https_fileserver_example.c
+++ b/src/examples/https_fileserver_example.c
@@ -43,7 +43,8 @@
43#define CAFILE "ca.pem" 43#define CAFILE "ca.pem"
44#define CRLFILE "crl.pem" 44#define CRLFILE "crl.pem"
45 45
46#define EMPTY_PAGE "<html><head><title>File not found</title></head><body>File not found</body></html>" 46#define EMPTY_PAGE \
47 "<html><head><title>File not found</title></head><body>File not found</body></html>"
47 48
48/* Test Certificate */ 49/* Test Certificate */
49const char cert_pem[] = 50const char cert_pem[] =
@@ -117,7 +118,7 @@ http_ahc (void *cls,
117 const char *method, 118 const char *method,
118 const char *version, 119 const char *version,
119 const char *upload_data, 120 const char *upload_data,
120 size_t *upload_data_size, void **ptr) 121 size_t *upload_data_size, void **ptr)
121{ 122{
122 static int aptr; 123 static int aptr;
123 struct MHD_Response *response; 124 struct MHD_Response *response;
@@ -125,60 +126,60 @@ http_ahc (void *cls,
125 FILE *file; 126 FILE *file;
126 int fd; 127 int fd;
127 struct stat buf; 128 struct stat buf;
128 (void)cls; /* Unused. Silent compiler warning. */ 129 (void) cls; /* Unused. Silent compiler warning. */
129 (void)version; /* Unused. Silent compiler warning. */ 130 (void) version; /* Unused. Silent compiler warning. */
130 (void)upload_data; /* Unused. Silent compiler warning. */ 131 (void) upload_data; /* Unused. Silent compiler warning. */
131 (void)upload_data_size; /* Unused. Silent compiler warning. */ 132 (void) upload_data_size; /* Unused. Silent compiler warning. */
132 133
133 if (0 != strcmp (method, MHD_HTTP_METHOD_GET)) 134 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
134 return MHD_NO; /* unexpected method */ 135 return MHD_NO; /* unexpected method */
135 if (&aptr != *ptr) 136 if (&aptr != *ptr)
136 { 137 {
137 /* do never respond on first call */ 138 /* do never respond on first call */
138 *ptr = &aptr; 139 *ptr = &aptr;
139 return MHD_YES; 140 return MHD_YES;
140 } 141 }
141 *ptr = NULL; /* reset when done */ 142 *ptr = NULL; /* reset when done */
142 143
143 file = fopen (&url[1], "rb"); 144 file = fopen (&url[1], "rb");
144 if (NULL != file) 145 if (NULL != file)
146 {
147 fd = fileno (file);
148 if (-1 == fd)
145 { 149 {
146 fd = fileno (file); 150 (void) fclose (file);
147 if (-1 == fd) 151 return MHD_NO; /* internal error */
148 {
149 (void) fclose (file);
150 return MHD_NO; /* internal error */
151 }
152 if ( (0 != fstat (fd, &buf)) ||
153 (! S_ISREG (buf.st_mode)) )
154 {
155 /* not a regular file, refuse to serve */
156 fclose (file);
157 file = NULL;
158 }
159 } 152 }
160 153 if ( (0 != fstat (fd, &buf)) ||
161 if (NULL == file) 154 (! S_ISREG (buf.st_mode)) )
162 { 155 {
163 response = MHD_create_response_from_buffer (strlen (EMPTY_PAGE), 156 /* not a regular file, refuse to serve */
164 (void *) EMPTY_PAGE, 157 fclose (file);
165 MHD_RESPMEM_PERSISTENT); 158 file = NULL;
166 ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
167 MHD_destroy_response (response);
168 } 159 }
160 }
161
162 if (NULL == file)
163 {
164 response = MHD_create_response_from_buffer (strlen (EMPTY_PAGE),
165 (void *) EMPTY_PAGE,
166 MHD_RESPMEM_PERSISTENT);
167 ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
168 MHD_destroy_response (response);
169 }
169 else 170 else
171 {
172 response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k PAGE_NOT_FOUND size */
173 &file_reader, file,
174 &file_free_callback);
175 if (NULL == response)
170 { 176 {
171 response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k PAGE_NOT_FOUND size */ 177 fclose (file);
172 &file_reader, file, 178 return MHD_NO;
173 &file_free_callback);
174 if (NULL == response)
175 {
176 fclose (file);
177 return MHD_NO;
178 }
179 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
180 MHD_destroy_response (response);
181 } 179 }
180 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
181 MHD_destroy_response (response);
182 }
182 return ret; 183 return ret;
183} 184}
184 185
@@ -190,22 +191,23 @@ main (int argc, char *const *argv)
190 int port; 191 int port;
191 192
192 if (argc != 2) 193 if (argc != 2)
193 { 194 {
194 printf ("%s PORT\n", argv[0]); 195 printf ("%s PORT\n", argv[0]);
195 return 1; 196 return 1;
196 } 197 }
197 port = atoi (argv[1]); 198 port = atoi (argv[1]);
198 if ( (1 > port) || 199 if ( (1 > port) ||
199 (port > UINT16_MAX) ) 200 (port > UINT16_MAX) )
200 { 201 {
201 fprintf (stderr, 202 fprintf (stderr,
202 "Port must be a number between 1 and 65535\n"); 203 "Port must be a number between 1 and 65535\n");
203 return 1; 204 return 1;
204 } 205 }
205 206
206 TLS_daemon = 207 TLS_daemon =
207 MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | 208 MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
208 MHD_USE_TLS, 209 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG
210 | MHD_USE_TLS,
209 (uint16_t) port, 211 (uint16_t) port,
210 NULL, NULL, 212 NULL, NULL,
211 &http_ahc, NULL, 213 &http_ahc, NULL,
@@ -214,10 +216,10 @@ main (int argc, char *const *argv)
214 MHD_OPTION_HTTPS_MEM_CERT, cert_pem, 216 MHD_OPTION_HTTPS_MEM_CERT, cert_pem,
215 MHD_OPTION_END); 217 MHD_OPTION_END);
216 if (NULL == TLS_daemon) 218 if (NULL == TLS_daemon)
217 { 219 {
218 fprintf (stderr, "Error: failed to start TLS_daemon\n"); 220 fprintf (stderr, "Error: failed to start TLS_daemon\n");
219 return 1; 221 return 1;
220 } 222 }
221 printf ("MHD daemon listening on port %u\n", 223 printf ("MHD daemon listening on port %u\n",
222 (unsigned int) port); 224 (unsigned int) port);
223 225
diff --git a/src/examples/minimal_example_comet.c b/src/examples/minimal_example_comet.c
index c4a3395d..53f8f666 100644
--- a/src/examples/minimal_example_comet.c
+++ b/src/examples/minimal_example_comet.c
@@ -28,8 +28,8 @@
28static ssize_t 28static ssize_t
29data_generator (void *cls, uint64_t pos, char *buf, size_t max) 29data_generator (void *cls, uint64_t pos, char *buf, size_t max)
30{ 30{
31 (void)cls; /* Unused. Silent compiler warning. */ 31 (void) cls; /* Unused. Silent compiler warning. */
32 (void)pos; /* Unused. Silent compiler warning. */ 32 (void) pos; /* Unused. Silent compiler warning. */
33 if (max < 80) 33 if (max < 80)
34 return 0; 34 return 0;
35 memset (buf, 'A', max - 1); 35 memset (buf, 'A', max - 1);
@@ -48,20 +48,20 @@ ahc_echo (void *cls,
48 static int aptr; 48 static int aptr;
49 struct MHD_Response *response; 49 struct MHD_Response *response;
50 int ret; 50 int ret;
51 (void)cls; /* Unused. Silent compiler warning. */ 51 (void) cls; /* Unused. Silent compiler warning. */
52 (void)url; /* Unused. Silent compiler warning. */ 52 (void) url; /* Unused. Silent compiler warning. */
53 (void)version; /* Unused. Silent compiler warning. */ 53 (void) version; /* Unused. Silent compiler warning. */
54 (void)upload_data; /* Unused. Silent compiler warning. */ 54 (void) upload_data; /* Unused. Silent compiler warning. */
55 (void)upload_data_size; /* Unused. Silent compiler warning. */ 55 (void) upload_data_size; /* Unused. Silent compiler warning. */
56 56
57 if (0 != strcmp (method, "GET")) 57 if (0 != strcmp (method, "GET"))
58 return MHD_NO; /* unexpected method */ 58 return MHD_NO; /* unexpected method */
59 if (&aptr != *ptr) 59 if (&aptr != *ptr)
60 { 60 {
61 /* do never respond on first call */ 61 /* do never respond on first call */
62 *ptr = &aptr; 62 *ptr = &aptr;
63 return MHD_YES; 63 return MHD_YES;
64 } 64 }
65 *ptr = NULL; /* reset when done */ 65 *ptr = NULL; /* reset when done */
66 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 66 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
67 80, 67 80,
@@ -77,11 +77,12 @@ main (int argc, char *const *argv)
77 struct MHD_Daemon *d; 77 struct MHD_Daemon *d;
78 78
79 if (argc != 2) 79 if (argc != 2)
80 { 80 {
81 printf ("%s PORT\n", argv[0]); 81 printf ("%s PORT\n", argv[0]);
82 return 1; 82 return 1;
83 } 83 }
84 d = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 84 d = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_THREAD_PER_CONNECTION
85 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
85 atoi (argv[1]), 86 atoi (argv[1]),
86 NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END); 87 NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
87 if (d == NULL) 88 if (d == NULL)
diff --git a/src/examples/msgs_i18n.c b/src/examples/msgs_i18n.c
index 2d8eb566..e5f92bb5 100644
--- a/src/examples/msgs_i18n.c
+++ b/src/examples/msgs_i18n.c
@@ -45,29 +45,29 @@
45 45
46static int 46static int
47ahc_echo (void *cls, 47ahc_echo (void *cls,
48 struct MHD_Connection *cnc, 48 struct MHD_Connection *cnc,
49 const char *url, 49 const char *url,
50 const char *mt, 50 const char *mt,
51 const char *ver, 51 const char *ver,
52 const char *upd, 52 const char *upd,
53 size_t *upsz, 53 size_t *upsz,
54 void **ptr) 54 void **ptr)
55{ 55{
56 return MHD_NO; 56 return MHD_NO;
57} 57}
58 58
59 59
60static void 60static void
61error_handler (void *cls, 61error_handler (void *cls,
62 const char *fm, 62 const char *fm,
63 va_list ap) 63 va_list ap)
64{ 64{
65 /* Here we do the translation using GNU gettext. 65 /* Here we do the translation using GNU gettext.
66 As the error message is from libmicrohttpd, we specify 66 As the error message is from libmicrohttpd, we specify
67 "libmicrohttpd" as the translation domain here. */ 67 "libmicrohttpd" as the translation domain here. */
68 vprintf (dgettext ("libmicrohttpd", 68 vprintf (dgettext ("libmicrohttpd",
69 fm), 69 fm),
70 ap); 70 ap);
71} 71}
72 72
73 73
@@ -75,21 +75,22 @@ int
75main (int argc, 75main (int argc,
76 char **argv) 76 char **argv)
77{ 77{
78 setlocale(LC_ALL, ""); 78 setlocale (LC_ALL, "");
79 79
80 /* The example uses PO files in the directory 80 /* The example uses PO files in the directory
81 "libmicrohttpd/src/examples/locale". This 81 "libmicrohttpd/src/examples/locale". This
82 needs to be adapted to match 82 needs to be adapted to match
83 where the MHD PO files are installed. */ 83 where the MHD PO files are installed. */
84 bindtextdomain ("libmicrohttpd", 84 bindtextdomain ("libmicrohttpd",
85 "locale"); 85 "locale");
86 MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_FEATURE_MESSAGES | MHD_USE_ERROR_LOG, 86 MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_FEATURE_MESSAGES
87 8080, 87 | MHD_USE_ERROR_LOG,
88 NULL, NULL, 88 8080,
89 &ahc_echo, NULL, 89 NULL, NULL,
90 MHD_OPTION_EXTERNAL_LOGGER, &error_handler, NULL, 90 &ahc_echo, NULL,
91 99999 /* invalid option, to raise the error 91 MHD_OPTION_EXTERNAL_LOGGER, &error_handler, NULL,
92 "Invalid option ..." which we are going 92 99999 /* invalid option, to raise the error
93 to translate */); 93 "Invalid option ..." which we are going
94 to translate */);
94 return 1; /* This program won't "succeed"... */ 95 return 1; /* This program won't "succeed"... */
95} 96}
diff --git a/src/examples/post_example.c b/src/examples/post_example.c
index 6c9a3f4b..311bf6c2 100644
--- a/src/examples/post_example.c
+++ b/src/examples/post_example.c
@@ -32,32 +32,38 @@
32/** 32/**
33 * Invalid method page. 33 * Invalid method page.
34 */ 34 */
35#define METHOD_ERROR "<html><head><title>Illegal request</title></head><body>Go away.</body></html>" 35#define METHOD_ERROR \
36 "<html><head><title>Illegal request</title></head><body>Go away.</body></html>"
36 37
37/** 38/**
38 * Invalid URL page. 39 * Invalid URL page.
39 */ 40 */
40#define NOT_FOUND_ERROR "<html><head><title>Not found</title></head><body>Go away.</body></html>" 41#define NOT_FOUND_ERROR \
42 "<html><head><title>Not found</title></head><body>Go away.</body></html>"
41 43
42/** 44/**
43 * Front page. (/) 45 * Front page. (/)
44 */ 46 */
45#define MAIN_PAGE "<html><head><title>Welcome</title></head><body><form action=\"/2\" method=\"post\">What is your name? <input type=\"text\" name=\"v1\" value=\"%s\" /><input type=\"submit\" value=\"Next\" /></body></html>" 47#define MAIN_PAGE \
48 "<html><head><title>Welcome</title></head><body><form action=\"/2\" method=\"post\">What is your name? <input type=\"text\" name=\"v1\" value=\"%s\" /><input type=\"submit\" value=\"Next\" /></body></html>"
46 49
47/** 50/**
48 * Second page. (/2) 51 * Second page. (/2)
49 */ 52 */
50#define SECOND_PAGE "<html><head><title>Tell me more</title></head><body><a href=\"/\">previous</a> <form action=\"/S\" method=\"post\">%s, what is your job? <input type=\"text\" name=\"v2\" value=\"%s\" /><input type=\"submit\" value=\"Next\" /></body></html>" 53#define SECOND_PAGE \
54 "<html><head><title>Tell me more</title></head><body><a href=\"/\">previous</a> <form action=\"/S\" method=\"post\">%s, what is your job? <input type=\"text\" name=\"v2\" value=\"%s\" /><input type=\"submit\" value=\"Next\" /></body></html>"
51 55
52/** 56/**
53 * Second page (/S) 57 * Second page (/S)
54 */ 58 */
55#define SUBMIT_PAGE "<html><head><title>Ready to submit?</title></head><body><form action=\"/F\" method=\"post\"><a href=\"/2\">previous </a> <input type=\"hidden\" name=\"DONE\" value=\"yes\" /><input type=\"submit\" value=\"Submit\" /></body></html>" 59#define SUBMIT_PAGE \
60 "<html><head><title>Ready to submit?</title></head><body><form action=\"/F\" method=\"post\"><a href=\"/2\">previous </a> <input type=\"hidden\" name=\"DONE\" value=\"yes\" /><input type=\"submit\" value=\"Submit\" /></body></html>"
56 61
57/** 62/**
58 * Last page. 63 * Last page.
59 */ 64 */
60#define LAST_PAGE "<html><head><title>Thank you</title></head><body>Thank you.</body></html>" 65#define LAST_PAGE \
66 "<html><head><title>Thank you</title></head><body>Thank you.</body></html>"
61 67
62/** 68/**
63 * Name of our cookie. 69 * Name of our cookie.
@@ -150,40 +156,40 @@ get_session (struct MHD_Connection *connection)
150 const char *cookie; 156 const char *cookie;
151 157
152 cookie = MHD_lookup_connection_value (connection, 158 cookie = MHD_lookup_connection_value (connection,
153 MHD_COOKIE_KIND, 159 MHD_COOKIE_KIND,
154 COOKIE_NAME); 160 COOKIE_NAME);
155 if (cookie != NULL) 161 if (cookie != NULL)
162 {
163 /* find existing session */
164 ret = sessions;
165 while (NULL != ret)
166 {
167 if (0 == strcmp (cookie, ret->sid))
168 break;
169 ret = ret->next;
170 }
171 if (NULL != ret)
156 { 172 {
157 /* find existing session */ 173 ret->rc++;
158 ret = sessions; 174 return ret;
159 while (NULL != ret)
160 {
161 if (0 == strcmp (cookie, ret->sid))
162 break;
163 ret = ret->next;
164 }
165 if (NULL != ret)
166 {
167 ret->rc++;
168 return ret;
169 }
170 } 175 }
176 }
171 /* create fresh session */ 177 /* create fresh session */
172 ret = calloc (1, sizeof (struct Session)); 178 ret = calloc (1, sizeof (struct Session));
173 if (NULL == ret) 179 if (NULL == ret)
174 { 180 {
175 fprintf (stderr, "calloc error: %s\n", strerror (errno)); 181 fprintf (stderr, "calloc error: %s\n", strerror (errno));
176 return NULL; 182 return NULL;
177 } 183 }
178 /* not a super-secure way to generate a random session ID, 184 /* not a super-secure way to generate a random session ID,
179 but should do for a simple example... */ 185 but should do for a simple example... */
180 snprintf (ret->sid, 186 snprintf (ret->sid,
181 sizeof (ret->sid), 187 sizeof (ret->sid),
182 "%X%X%X%X", 188 "%X%X%X%X",
183 (unsigned int) rand (), 189 (unsigned int) rand (),
184 (unsigned int) rand (), 190 (unsigned int) rand (),
185 (unsigned int) rand (), 191 (unsigned int) rand (),
186 (unsigned int) rand ()); 192 (unsigned int) rand ());
187 ret->rc++; 193 ret->rc++;
188 ret->start = time (NULL); 194 ret->start = time (NULL);
189 ret->next = sessions; 195 ret->next = sessions;
@@ -202,9 +208,9 @@ get_session (struct MHD_Connection *connection)
202 * @param MHD_YES on success, MHD_NO on failure 208 * @param MHD_YES on success, MHD_NO on failure
203 */ 209 */
204typedef int (*PageHandler)(const void *cls, 210typedef int (*PageHandler)(const void *cls,
205 const char *mime, 211 const char *mime,
206 struct Session *session, 212 struct Session *session,
207 struct MHD_Connection *connection); 213 struct MHD_Connection *connection);
208 214
209 215
210/** 216/**
@@ -242,22 +248,22 @@ struct Page
242 */ 248 */
243static void 249static void
244add_session_cookie (struct Session *session, 250add_session_cookie (struct Session *session,
245 struct MHD_Response *response) 251 struct MHD_Response *response)
246{ 252{
247 char cstr[256]; 253 char cstr[256];
248 snprintf (cstr, 254 snprintf (cstr,
249 sizeof (cstr), 255 sizeof (cstr),
250 "%s=%s", 256 "%s=%s",
251 COOKIE_NAME, 257 COOKIE_NAME,
252 session->sid); 258 session->sid);
253 if (MHD_NO == 259 if (MHD_NO ==
254 MHD_add_response_header (response, 260 MHD_add_response_header (response,
255 MHD_HTTP_HEADER_SET_COOKIE, 261 MHD_HTTP_HEADER_SET_COOKIE,
256 cstr)) 262 cstr))
257 { 263 {
258 fprintf (stderr, 264 fprintf (stderr,
259 "Failed to set session cookie header!\n"); 265 "Failed to set session cookie header!\n");
260 } 266 }
261} 267}
262 268
263 269
@@ -272,9 +278,9 @@ add_session_cookie (struct Session *session,
272 */ 278 */
273static int 279static int
274serve_simple_form (const void *cls, 280serve_simple_form (const void *cls,
275 const char *mime, 281 const char *mime,
276 struct Session *session, 282 struct Session *session,
277 struct MHD_Connection *connection) 283 struct MHD_Connection *connection)
278{ 284{
279 int ret; 285 int ret;
280 const char *form = cls; 286 const char *form = cls;
@@ -282,17 +288,17 @@ serve_simple_form (const void *cls,
282 288
283 /* return static form */ 289 /* return static form */
284 response = MHD_create_response_from_buffer (strlen (form), 290 response = MHD_create_response_from_buffer (strlen (form),
285 (void *) form, 291 (void *) form,
286 MHD_RESPMEM_PERSISTENT); 292 MHD_RESPMEM_PERSISTENT);
287 if (NULL == response) 293 if (NULL == response)
288 return MHD_NO; 294 return MHD_NO;
289 add_session_cookie (session, response); 295 add_session_cookie (session, response);
290 MHD_add_response_header (response, 296 MHD_add_response_header (response,
291 MHD_HTTP_HEADER_CONTENT_ENCODING, 297 MHD_HTTP_HEADER_CONTENT_ENCODING,
292 mime); 298 mime);
293 ret = MHD_queue_response (connection, 299 ret = MHD_queue_response (connection,
294 MHD_HTTP_OK, 300 MHD_HTTP_OK,
295 response); 301 response);
296 MHD_destroy_response (response); 302 MHD_destroy_response (response);
297 return ret; 303 return ret;
298} 304}
@@ -308,28 +314,28 @@ serve_simple_form (const void *cls,
308 */ 314 */
309static int 315static int
310fill_v1_form (const void *cls, 316fill_v1_form (const void *cls,
311 const char *mime, 317 const char *mime,
312 struct Session *session, 318 struct Session *session,
313 struct MHD_Connection *connection) 319 struct MHD_Connection *connection)
314{ 320{
315 int ret; 321 int ret;
316 size_t slen; 322 size_t slen;
317 char *reply; 323 char *reply;
318 struct MHD_Response *response; 324 struct MHD_Response *response;
319 (void)cls; /* Unused. Silent compiler warning. */ 325 (void) cls; /* Unused. Silent compiler warning. */
320 326
321 slen = strlen (MAIN_PAGE) + strlen (session->value_1); 327 slen = strlen (MAIN_PAGE) + strlen (session->value_1);
322 reply = malloc (slen + 1); 328 reply = malloc (slen + 1);
323 if (NULL == reply) 329 if (NULL == reply)
324 return MHD_NO; 330 return MHD_NO;
325 snprintf (reply, 331 snprintf (reply,
326 slen + 1, 332 slen + 1,
327 MAIN_PAGE, 333 MAIN_PAGE,
328 session->value_1); 334 session->value_1);
329 /* return static form */ 335 /* return static form */
330 response = MHD_create_response_from_buffer (slen, 336 response = MHD_create_response_from_buffer (slen,
331 (void *) reply, 337 (void *) reply,
332 MHD_RESPMEM_MUST_FREE); 338 MHD_RESPMEM_MUST_FREE);
333 if (NULL == response) 339 if (NULL == response)
334 { 340 {
335 free (reply); 341 free (reply);
@@ -337,11 +343,11 @@ fill_v1_form (const void *cls,
337 } 343 }
338 add_session_cookie (session, response); 344 add_session_cookie (session, response);
339 MHD_add_response_header (response, 345 MHD_add_response_header (response,
340 MHD_HTTP_HEADER_CONTENT_ENCODING, 346 MHD_HTTP_HEADER_CONTENT_ENCODING,
341 mime); 347 mime);
342 ret = MHD_queue_response (connection, 348 ret = MHD_queue_response (connection,
343 MHD_HTTP_OK, 349 MHD_HTTP_OK,
344 response); 350 response);
345 MHD_destroy_response (response); 351 MHD_destroy_response (response);
346 return ret; 352 return ret;
347} 353}
@@ -357,29 +363,30 @@ fill_v1_form (const void *cls,
357 */ 363 */
358static int 364static int
359fill_v1_v2_form (const void *cls, 365fill_v1_v2_form (const void *cls,
360 const char *mime, 366 const char *mime,
361 struct Session *session, 367 struct Session *session,
362 struct MHD_Connection *connection) 368 struct MHD_Connection *connection)
363{ 369{
364 int ret; 370 int ret;
365 char *reply; 371 char *reply;
366 struct MHD_Response *response; 372 struct MHD_Response *response;
367 size_t slen; 373 size_t slen;
368 (void)cls; /* Unused. Silent compiler warning. */ 374 (void) cls; /* Unused. Silent compiler warning. */
369 375
370 slen = strlen (SECOND_PAGE) + strlen (session->value_1) + strlen (session->value_2); 376 slen = strlen (SECOND_PAGE) + strlen (session->value_1) + strlen (
377 session->value_2);
371 reply = malloc (slen + 1); 378 reply = malloc (slen + 1);
372 if (NULL == reply) 379 if (NULL == reply)
373 return MHD_NO; 380 return MHD_NO;
374 snprintf (reply, 381 snprintf (reply,
375 slen + 1, 382 slen + 1,
376 SECOND_PAGE, 383 SECOND_PAGE,
377 session->value_1, 384 session->value_1,
378 session->value_2); 385 session->value_2);
379 /* return static form */ 386 /* return static form */
380 response = MHD_create_response_from_buffer (slen, 387 response = MHD_create_response_from_buffer (slen,
381 (void *) reply, 388 (void *) reply,
382 MHD_RESPMEM_MUST_FREE); 389 MHD_RESPMEM_MUST_FREE);
383 if (NULL == response) 390 if (NULL == response)
384 { 391 {
385 free (reply); 392 free (reply);
@@ -387,11 +394,11 @@ fill_v1_v2_form (const void *cls,
387 } 394 }
388 add_session_cookie (session, response); 395 add_session_cookie (session, response);
389 MHD_add_response_header (response, 396 MHD_add_response_header (response,
390 MHD_HTTP_HEADER_CONTENT_ENCODING, 397 MHD_HTTP_HEADER_CONTENT_ENCODING,
391 mime); 398 mime);
392 ret = MHD_queue_response (connection, 399 ret = MHD_queue_response (connection,
393 MHD_HTTP_OK, 400 MHD_HTTP_OK,
394 response); 401 response);
395 MHD_destroy_response (response); 402 MHD_destroy_response (response);
396 return ret; 403 return ret;
397} 404}
@@ -407,27 +414,27 @@ fill_v1_v2_form (const void *cls,
407 */ 414 */
408static int 415static int
409not_found_page (const void *cls, 416not_found_page (const void *cls,
410 const char *mime, 417 const char *mime,
411 struct Session *session, 418 struct Session *session,
412 struct MHD_Connection *connection) 419 struct MHD_Connection *connection)
413{ 420{
414 int ret; 421 int ret;
415 struct MHD_Response *response; 422 struct MHD_Response *response;
416 (void)cls; /* Unused. Silent compiler warning. */ 423 (void) cls; /* Unused. Silent compiler warning. */
417 (void)session; /* Unused. Silent compiler warning. */ 424 (void) session; /* Unused. Silent compiler warning. */
418 425
419 /* unsupported HTTP method */ 426 /* unsupported HTTP method */
420 response = MHD_create_response_from_buffer (strlen (NOT_FOUND_ERROR), 427 response = MHD_create_response_from_buffer (strlen (NOT_FOUND_ERROR),
421 (void *) NOT_FOUND_ERROR, 428 (void *) NOT_FOUND_ERROR,
422 MHD_RESPMEM_PERSISTENT); 429 MHD_RESPMEM_PERSISTENT);
423 if (NULL == response) 430 if (NULL == response)
424 return MHD_NO; 431 return MHD_NO;
425 ret = MHD_queue_response (connection, 432 ret = MHD_queue_response (connection,
426 MHD_HTTP_NOT_FOUND, 433 MHD_HTTP_NOT_FOUND,
427 response); 434 response);
428 MHD_add_response_header (response, 435 MHD_add_response_header (response,
429 MHD_HTTP_HEADER_CONTENT_ENCODING, 436 MHD_HTTP_HEADER_CONTENT_ENCODING,
430 mime); 437 mime);
431 MHD_destroy_response (response); 438 MHD_destroy_response (response);
432 return ret; 439 return ret;
433} 440}
@@ -436,14 +443,13 @@ not_found_page (const void *cls,
436/** 443/**
437 * List of all pages served by this HTTP server. 444 * List of all pages served by this HTTP server.
438 */ 445 */
439static struct Page pages[] = 446static struct Page pages[] = {
440 { 447 { "/", "text/html", &fill_v1_form, NULL },
441 { "/", "text/html", &fill_v1_form, NULL }, 448 { "/2", "text/html", &fill_v1_v2_form, NULL },
442 { "/2", "text/html", &fill_v1_v2_form, NULL }, 449 { "/S", "text/html", &serve_simple_form, SUBMIT_PAGE },
443 { "/S", "text/html", &serve_simple_form, SUBMIT_PAGE }, 450 { "/F", "text/html", &serve_simple_form, LAST_PAGE },
444 { "/F", "text/html", &serve_simple_form, LAST_PAGE }, 451 { NULL, NULL, &not_found_page, NULL } /* 404 */
445 { NULL, NULL, &not_found_page, NULL } /* 404 */ 452};
446 };
447 453
448 454
449 455
@@ -468,49 +474,49 @@ static struct Page pages[] =
468 */ 474 */
469static int 475static int
470post_iterator (void *cls, 476post_iterator (void *cls,
471 enum MHD_ValueKind kind, 477 enum MHD_ValueKind kind,
472 const char *key, 478 const char *key,
473 const char *filename, 479 const char *filename,
474 const char *content_type, 480 const char *content_type,
475 const char *transfer_encoding, 481 const char *transfer_encoding,
476 const char *data, uint64_t off, size_t size) 482 const char *data, uint64_t off, size_t size)
477{ 483{
478 struct Request *request = cls; 484 struct Request *request = cls;
479 struct Session *session = request->session; 485 struct Session *session = request->session;
480 (void)kind; /* Unused. Silent compiler warning. */ 486 (void) kind; /* Unused. Silent compiler warning. */
481 (void)filename; /* Unused. Silent compiler warning. */ 487 (void) filename; /* Unused. Silent compiler warning. */
482 (void)content_type; /* Unused. Silent compiler warning. */ 488 (void) content_type; /* Unused. Silent compiler warning. */
483 (void)transfer_encoding; /* Unused. Silent compiler warning. */ 489 (void) transfer_encoding; /* Unused. Silent compiler warning. */
484 490
485 if (0 == strcmp ("DONE", key)) 491 if (0 == strcmp ("DONE", key))
486 { 492 {
487 fprintf (stdout, 493 fprintf (stdout,
488 "Session `%s' submitted `%s', `%s'\n", 494 "Session `%s' submitted `%s', `%s'\n",
489 session->sid, 495 session->sid,
490 session->value_1, 496 session->value_1,
491 session->value_2); 497 session->value_2);
492 return MHD_YES; 498 return MHD_YES;
493 } 499 }
494 if (0 == strcmp ("v1", key)) 500 if (0 == strcmp ("v1", key))
495 { 501 {
496 if (size + off >= sizeof(session->value_1)) 502 if (size + off >= sizeof(session->value_1))
497 size = sizeof (session->value_1) - off - 1; 503 size = sizeof (session->value_1) - off - 1;
498 memcpy (&session->value_1[off], 504 memcpy (&session->value_1[off],
499 data, 505 data,
500 size); 506 size);
501 session->value_1[size+off] = '\0'; 507 session->value_1[size + off] = '\0';
502 return MHD_YES; 508 return MHD_YES;
503 } 509 }
504 if (0 == strcmp ("v2", key)) 510 if (0 == strcmp ("v2", key))
505 { 511 {
506 if (size + off >= sizeof(session->value_2)) 512 if (size + off >= sizeof(session->value_2))
507 size = sizeof (session->value_2) - off - 1; 513 size = sizeof (session->value_2) - off - 1;
508 memcpy (&session->value_2[off], 514 memcpy (&session->value_2[off],
509 data, 515 data,
510 size); 516 size);
511 session->value_2[size+off] = '\0'; 517 session->value_2[size + off] = '\0';
512 return MHD_YES; 518 return MHD_YES;
513 } 519 }
514 fprintf (stderr, 520 fprintf (stderr,
515 "Unsupported form value `%s'\n", 521 "Unsupported form value `%s'\n",
516 key); 522 key);
@@ -553,99 +559,99 @@ post_iterator (void *cls,
553 */ 559 */
554static int 560static int
555create_response (void *cls, 561create_response (void *cls,
556 struct MHD_Connection *connection, 562 struct MHD_Connection *connection,
557 const char *url, 563 const char *url,
558 const char *method, 564 const char *method,
559 const char *version, 565 const char *version,
560 const char *upload_data, 566 const char *upload_data,
561 size_t *upload_data_size, 567 size_t *upload_data_size,
562 void **ptr) 568 void **ptr)
563{ 569{
564 struct MHD_Response *response; 570 struct MHD_Response *response;
565 struct Request *request; 571 struct Request *request;
566 struct Session *session; 572 struct Session *session;
567 int ret; 573 int ret;
568 unsigned int i; 574 unsigned int i;
569 (void)cls; /* Unused. Silent compiler warning. */ 575 (void) cls; /* Unused. Silent compiler warning. */
570 (void)version; /* Unused. Silent compiler warning. */ 576 (void) version; /* Unused. Silent compiler warning. */
571 577
572 request = *ptr; 578 request = *ptr;
573 if (NULL == request) 579 if (NULL == request)
580 {
581 request = calloc (1, sizeof (struct Request));
582 if (NULL == request)
574 { 583 {
575 request = calloc (1, sizeof (struct Request)); 584 fprintf (stderr, "calloc error: %s\n", strerror (errno));
576 if (NULL == request) 585 return MHD_NO;
577 {
578 fprintf (stderr, "calloc error: %s\n", strerror (errno));
579 return MHD_NO;
580 }
581 *ptr = request;
582 if (0 == strcmp (method, MHD_HTTP_METHOD_POST))
583 {
584 request->pp = MHD_create_post_processor (connection, 1024,
585 &post_iterator, request);
586 if (NULL == request->pp)
587 {
588 fprintf (stderr, "Failed to setup post processor for `%s'\n",
589 url);
590 return MHD_NO; /* internal error */
591 }
592 }
593 return MHD_YES;
594 } 586 }
587 *ptr = request;
588 if (0 == strcmp (method, MHD_HTTP_METHOD_POST))
589 {
590 request->pp = MHD_create_post_processor (connection, 1024,
591 &post_iterator, request);
592 if (NULL == request->pp)
593 {
594 fprintf (stderr, "Failed to setup post processor for `%s'\n",
595 url);
596 return MHD_NO; /* internal error */
597 }
598 }
599 return MHD_YES;
600 }
595 if (NULL == request->session) 601 if (NULL == request->session)
602 {
603 request->session = get_session (connection);
604 if (NULL == request->session)
596 { 605 {
597 request->session = get_session (connection); 606 fprintf (stderr, "Failed to setup session for `%s'\n",
598 if (NULL == request->session) 607 url);
599 { 608 return MHD_NO; /* internal error */
600 fprintf (stderr, "Failed to setup session for `%s'\n",
601 url);
602 return MHD_NO; /* internal error */
603 }
604 } 609 }
610 }
605 session = request->session; 611 session = request->session;
606 session->start = time (NULL); 612 session->start = time (NULL);
607 if (0 == strcmp (method, MHD_HTTP_METHOD_POST)) 613 if (0 == strcmp (method, MHD_HTTP_METHOD_POST))
614 {
615 /* evaluate POST data */
616 MHD_post_process (request->pp,
617 upload_data,
618 *upload_data_size);
619 if (0 != *upload_data_size)
608 { 620 {
609 /* evaluate POST data */ 621 *upload_data_size = 0;
610 MHD_post_process (request->pp, 622 return MHD_YES;
611 upload_data,
612 *upload_data_size);
613 if (0 != *upload_data_size)
614 {
615 *upload_data_size = 0;
616 return MHD_YES;
617 }
618 /* done with POST data, serve response */
619 MHD_destroy_post_processor (request->pp);
620 request->pp = NULL;
621 method = MHD_HTTP_METHOD_GET; /* fake 'GET' */
622 if (NULL != request->post_url)
623 url = request->post_url;
624 } 623 }
624 /* done with POST data, serve response */
625 MHD_destroy_post_processor (request->pp);
626 request->pp = NULL;
627 method = MHD_HTTP_METHOD_GET; /* fake 'GET' */
628 if (NULL != request->post_url)
629 url = request->post_url;
630 }
625 631
626 if ( (0 == strcmp (method, MHD_HTTP_METHOD_GET)) || 632 if ( (0 == strcmp (method, MHD_HTTP_METHOD_GET)) ||
627 (0 == strcmp (method, MHD_HTTP_METHOD_HEAD)) ) 633 (0 == strcmp (method, MHD_HTTP_METHOD_HEAD)) )
628 { 634 {
629 /* find out which page to serve */ 635 /* find out which page to serve */
630 i=0; 636 i = 0;
631 while ( (pages[i].url != NULL) && 637 while ( (pages[i].url != NULL) &&
632 (0 != strcmp (pages[i].url, url)) ) 638 (0 != strcmp (pages[i].url, url)) )
633 i++; 639 i++;
634 ret = pages[i].handler (pages[i].handler_cls, 640 ret = pages[i].handler (pages[i].handler_cls,
635 pages[i].mime, 641 pages[i].mime,
636 session, connection); 642 session, connection);
637 if (ret != MHD_YES) 643 if (ret != MHD_YES)
638 fprintf (stderr, "Failed to create page for `%s'\n", 644 fprintf (stderr, "Failed to create page for `%s'\n",
639 url); 645 url);
640 return ret; 646 return ret;
641 } 647 }
642 /* unsupported HTTP method */ 648 /* unsupported HTTP method */
643 response = MHD_create_response_from_buffer (strlen (METHOD_ERROR), 649 response = MHD_create_response_from_buffer (strlen (METHOD_ERROR),
644 (void *) METHOD_ERROR, 650 (void *) METHOD_ERROR,
645 MHD_RESPMEM_PERSISTENT); 651 MHD_RESPMEM_PERSISTENT);
646 ret = MHD_queue_response (connection, 652 ret = MHD_queue_response (connection,
647 MHD_HTTP_NOT_ACCEPTABLE, 653 MHD_HTTP_NOT_ACCEPTABLE,
648 response); 654 response);
649 MHD_destroy_response (response); 655 MHD_destroy_response (response);
650 return ret; 656 return ret;
651} 657}
@@ -662,14 +668,14 @@ create_response (void *cls,
662 */ 668 */
663static void 669static void
664request_completed_callback (void *cls, 670request_completed_callback (void *cls,
665 struct MHD_Connection *connection, 671 struct MHD_Connection *connection,
666 void **con_cls, 672 void **con_cls,
667 enum MHD_RequestTerminationCode toe) 673 enum MHD_RequestTerminationCode toe)
668{ 674{
669 struct Request *request = *con_cls; 675 struct Request *request = *con_cls;
670 (void)cls; /* Unused. Silent compiler warning. */ 676 (void) cls; /* Unused. Silent compiler warning. */
671 (void)connection; /* Unused. Silent compiler warning. */ 677 (void) connection; /* Unused. Silent compiler warning. */
672 (void)toe; /* Unused. Silent compiler warning. */ 678 (void) toe; /* Unused. Silent compiler warning. */
673 679
674 if (NULL == request) 680 if (NULL == request)
675 return; 681 return;
@@ -697,21 +703,21 @@ expire_sessions ()
697 prev = NULL; 703 prev = NULL;
698 pos = sessions; 704 pos = sessions;
699 while (NULL != pos) 705 while (NULL != pos)
706 {
707 next = pos->next;
708 if (now - pos->start > 60 * 60)
700 { 709 {
701 next = pos->next; 710 /* expire sessions after 1h */
702 if (now - pos->start > 60 * 60) 711 if (NULL == prev)
703 { 712 sessions = pos->next;
704 /* expire sessions after 1h */
705 if (NULL == prev)
706 sessions = pos->next;
707 else
708 prev->next = next;
709 free (pos);
710 }
711 else 713 else
712 prev = pos; 714 prev->next = next;
713 pos = next; 715 free (pos);
714 } 716 }
717 else
718 prev = pos;
719 pos = next;
720 }
715} 721}
716 722
717 723
@@ -732,45 +738,46 @@ main (int argc, char *const *argv)
732 MHD_UNSIGNED_LONG_LONG mhd_timeout; 738 MHD_UNSIGNED_LONG_LONG mhd_timeout;
733 739
734 if (argc != 2) 740 if (argc != 2)
735 { 741 {
736 printf ("%s PORT\n", argv[0]); 742 printf ("%s PORT\n", argv[0]);
737 return 1; 743 return 1;
738 } 744 }
739 /* initialize PRNG */ 745 /* initialize PRNG */
740 srand ((unsigned int) time (NULL)); 746 srand ((unsigned int) time (NULL));
741 d = MHD_start_daemon (MHD_USE_ERROR_LOG, 747 d = MHD_start_daemon (MHD_USE_ERROR_LOG,
742 atoi (argv[1]), 748 atoi (argv[1]),
743 NULL, NULL, 749 NULL, NULL,
744 &create_response, NULL, 750 &create_response, NULL,
745 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 15, 751 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 15,
746 MHD_OPTION_NOTIFY_COMPLETED, &request_completed_callback, NULL, 752 MHD_OPTION_NOTIFY_COMPLETED,
747 MHD_OPTION_END); 753 &request_completed_callback, NULL,
754 MHD_OPTION_END);
748 if (NULL == d) 755 if (NULL == d)
749 return 1; 756 return 1;
750 while (1) 757 while (1)
758 {
759 expire_sessions ();
760 max = 0;
761 FD_ZERO (&rs);
762 FD_ZERO (&ws);
763 FD_ZERO (&es);
764 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
765 break; /* fatal internal error */
766 if (MHD_get_timeout (d, &mhd_timeout) == MHD_YES)
751 { 767 {
752 expire_sessions (); 768 tv.tv_sec = mhd_timeout / 1000;
753 max = 0; 769 tv.tv_usec = (mhd_timeout - (tv.tv_sec * 1000)) * 1000;
754 FD_ZERO (&rs); 770 tvp = &tv;
755 FD_ZERO (&ws);
756 FD_ZERO (&es);
757 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
758 break; /* fatal internal error */
759 if (MHD_get_timeout (d, &mhd_timeout) == MHD_YES)
760 {
761 tv.tv_sec = mhd_timeout / 1000;
762 tv.tv_usec = (mhd_timeout - (tv.tv_sec * 1000)) * 1000;
763 tvp = &tv;
764 }
765 else
766 tvp = NULL;
767 if (-1 == select (max + 1, &rs, &ws, &es, tvp))
768 {
769 if (EINTR != errno)
770 abort ();
771 }
772 MHD_run (d);
773 } 771 }
772 else
773 tvp = NULL;
774 if (-1 == select (max + 1, &rs, &ws, &es, tvp))
775 {
776 if (EINTR != errno)
777 abort ();
778 }
779 MHD_run (d);
780 }
774 MHD_stop_daemon (d); 781 MHD_stop_daemon (d);
775 return 0; 782 return 0;
776} 783}
diff --git a/src/examples/querystring_example.c b/src/examples/querystring_example.c
index db0d6f1a..a8f0f572 100644
--- a/src/examples/querystring_example.c
+++ b/src/examples/querystring_example.c
@@ -26,7 +26,8 @@
26#include "platform.h" 26#include "platform.h"
27#include <microhttpd.h> 27#include <microhttpd.h>
28 28
29#define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>Query string for &quot;%s&quot; was &quot;%s&quot;</body></html>" 29#define PAGE \
30 "<html><head><title>libmicrohttpd demo</title></head><body>Query string for &quot;%s&quot; was &quot;%s&quot;</body></html>"
30 31
31static int 32static int
32ahc_echo (void *cls, 33ahc_echo (void *cls,
@@ -42,19 +43,19 @@ ahc_echo (void *cls,
42 char *me; 43 char *me;
43 struct MHD_Response *response; 44 struct MHD_Response *response;
44 int ret; 45 int ret;
45 (void)url; /* Unused. Silent compiler warning. */ 46 (void) url; /* Unused. Silent compiler warning. */
46 (void)version; /* Unused. Silent compiler warning. */ 47 (void) version; /* Unused. Silent compiler warning. */
47 (void)upload_data; /* Unused. Silent compiler warning. */ 48 (void) upload_data; /* Unused. Silent compiler warning. */
48 (void)upload_data_size; /* Unused. Silent compiler warning. */ 49 (void) upload_data_size; /* Unused. Silent compiler warning. */
49 50
50 if (0 != strcmp (method, "GET")) 51 if (0 != strcmp (method, "GET"))
51 return MHD_NO; /* unexpected method */ 52 return MHD_NO; /* unexpected method */
52 if (&aptr != *ptr) 53 if (&aptr != *ptr)
53 { 54 {
54 /* do never respond on first call */ 55 /* do never respond on first call */
55 *ptr = &aptr; 56 *ptr = &aptr;
56 return MHD_YES; 57 return MHD_YES;
57 } 58 }
58 *ptr = NULL; /* reset when done */ 59 *ptr = NULL; /* reset when done */
59 val = MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, "q"); 60 val = MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, "q");
60 me = malloc (snprintf (NULL, 0, fmt, "q", val) + 1); 61 me = malloc (snprintf (NULL, 0, fmt, "q", val) + 1);
@@ -62,12 +63,12 @@ ahc_echo (void *cls,
62 return MHD_NO; 63 return MHD_NO;
63 sprintf (me, fmt, "q", val); 64 sprintf (me, fmt, "q", val);
64 response = MHD_create_response_from_buffer (strlen (me), me, 65 response = MHD_create_response_from_buffer (strlen (me), me,
65 MHD_RESPMEM_MUST_FREE); 66 MHD_RESPMEM_MUST_FREE);
66 if (response == NULL) 67 if (response == NULL)
67 { 68 {
68 free (me); 69 free (me);
69 return MHD_NO; 70 return MHD_NO;
70 } 71 }
71 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 72 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
72 MHD_destroy_response (response); 73 MHD_destroy_response (response);
73 return ret; 74 return ret;
@@ -80,18 +81,19 @@ main (int argc, char *const *argv)
80 int port; 81 int port;
81 82
82 if (argc != 2) 83 if (argc != 2)
83 { 84 {
84 printf ("%s PORT\n", argv[0]); 85 printf ("%s PORT\n", argv[0]);
85 return 1; 86 return 1;
86 } 87 }
87 port = atoi (argv[1]); 88 port = atoi (argv[1]);
88 if ( (port < 0) || 89 if ( (port < 0) ||
89 (port > UINT16_MAX) ) 90 (port > UINT16_MAX) )
90 { 91 {
91 printf ("%s PORT\n", argv[0]); 92 printf ("%s PORT\n", argv[0]);
92 return 1; 93 return 1;
93 } 94 }
94 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 95 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
96 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
95 (uint16_t) port, 97 (uint16_t) port,
96 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); 98 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END);
97 if (NULL == d) 99 if (NULL == d)
diff --git a/src/examples/refuse_post_example.c b/src/examples/refuse_post_example.c
index dad3beb1..9241ce43 100644
--- a/src/examples/refuse_post_example.c
+++ b/src/examples/refuse_post_example.c
@@ -24,14 +24,16 @@
24#include "platform.h" 24#include "platform.h"
25#include <microhttpd.h> 25#include <microhttpd.h>
26 26
27const char *askpage = "<html><body>\n\ 27const char *askpage =
28 "<html><body>\n\
28 Upload a file, please!<br>\n\ 29 Upload a file, please!<br>\n\
29 <form action=\"/filepost\" method=\"post\" enctype=\"multipart/form-data\">\n\ 30 <form action=\"/filepost\" method=\"post\" enctype=\"multipart/form-data\">\n\
30 <input name=\"file\" type=\"file\">\n\ 31 <input name=\"file\" type=\"file\">\n\
31 <input type=\"submit\" value=\" Send \"></form>\n\ 32 <input type=\"submit\" value=\" Send \"></form>\n\
32 </body></html>"; 33 </body></html>";
33 34
34#define BUSYPAGE "<html><head><title>Webserver busy</title></head><body>We are too busy to process POSTs right now.</body></html>" 35#define BUSYPAGE \
36 "<html><head><title>Webserver busy</title></head><body>We are too busy to process POSTs right now.</body></html>"
35 37
36static int 38static int
37ahc_echo (void *cls, 39ahc_echo (void *cls,
@@ -45,37 +47,37 @@ ahc_echo (void *cls,
45 const char *me = cls; 47 const char *me = cls;
46 struct MHD_Response *response; 48 struct MHD_Response *response;
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)version; /* Unused. Silent compiler warning. */ 52 (void) version; /* Unused. Silent compiler warning. */
51 (void)upload_data; /* Unused. Silent compiler warning. */ 53 (void) upload_data; /* Unused. Silent compiler warning. */
52 (void)upload_data_size; /* Unused. Silent compiler warning. */ 54 (void) upload_data_size; /* Unused. Silent compiler warning. */
53 55
54 if ((0 != strcmp (method, "GET")) && (0 != strcmp (method, "POST"))) 56 if ((0 != strcmp (method, "GET")) && (0 != strcmp (method, "POST")))
55 return MHD_NO; /* unexpected method */ 57 return MHD_NO; /* unexpected method */
56 58
57 if (&aptr != *ptr) 59 if (&aptr != *ptr)
58 { 60 {
59 *ptr = &aptr; 61 *ptr = &aptr;
60 62
61 /* always to busy for POST requests */ 63 /* always to busy for POST requests */
62 if (0 == strcmp (method, "POST")) 64 if (0 == strcmp (method, "POST"))
63 { 65 {
64 response = MHD_create_response_from_buffer (strlen (BUSYPAGE), 66 response = MHD_create_response_from_buffer (strlen (BUSYPAGE),
65 (void *) BUSYPAGE, 67 (void *) BUSYPAGE,
66 MHD_RESPMEM_PERSISTENT); 68 MHD_RESPMEM_PERSISTENT);
67 ret = 69 ret =
68 MHD_queue_response (connection, MHD_HTTP_SERVICE_UNAVAILABLE, 70 MHD_queue_response (connection, MHD_HTTP_SERVICE_UNAVAILABLE,
69 response); 71 response);
70 MHD_destroy_response (response); 72 MHD_destroy_response (response);
71 return ret; 73 return ret;
72 }
73 } 74 }
75 }
74 76
75 *ptr = NULL; /* reset when done */ 77 *ptr = NULL; /* reset when done */
76 response = MHD_create_response_from_buffer (strlen (me), 78 response = MHD_create_response_from_buffer (strlen (me),
77 (void *) me, 79 (void *) me,
78 MHD_RESPMEM_PERSISTENT); 80 MHD_RESPMEM_PERSISTENT);
79 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 81 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
80 MHD_destroy_response (response); 82 MHD_destroy_response (response);
81 return ret; 83 return ret;
@@ -87,11 +89,12 @@ main (int argc, char *const *argv)
87 struct MHD_Daemon *d; 89 struct MHD_Daemon *d;
88 90
89 if (argc != 2) 91 if (argc != 2)
90 { 92 {
91 printf ("%s PORT\n", argv[0]); 93 printf ("%s PORT\n", argv[0]);
92 return 1; 94 return 1;
93 } 95 }
94 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 96 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
97 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
95 atoi (argv[1]), 98 atoi (argv[1]),
96 NULL, NULL, &ahc_echo, (void *) askpage, 99 NULL, NULL, &ahc_echo, (void *) askpage,
97 MHD_OPTION_END); 100 MHD_OPTION_END);
diff --git a/src/examples/suspend_resume_epoll.c b/src/examples/suspend_resume_epoll.c
index 3e0be24c..a69fdc4a 100644
--- a/src/examples/suspend_resume_epoll.c
+++ b/src/examples/suspend_resume_epoll.c
@@ -31,7 +31,8 @@
31 31
32#define TIMEOUT_INFINITE -1 32#define TIMEOUT_INFINITE -1
33 33
34struct Request { 34struct Request
35{
35 struct MHD_Connection *connection; 36 struct MHD_Connection *connection;
36 int timerfd; 37 int timerfd;
37}; 38};
@@ -52,7 +53,7 @@ ahc_echo (void *cls,
52{ 53{
53 struct MHD_Response *response; 54 struct MHD_Response *response;
54 int ret; 55 int ret;
55 struct Request* req; 56 struct Request*req;
56 struct itimerspec ts; 57 struct itimerspec ts;
57 58
58 (void) cls; 59 (void) cls;
@@ -89,29 +90,29 @@ ahc_echo (void *cls,
89 return ret; 90 return ret;
90 } 91 }
91 /* create timer and suspend connection */ 92 /* create timer and suspend connection */
92 req->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK); 93 req->timerfd = timerfd_create (CLOCK_MONOTONIC, TFD_NONBLOCK);
93 if (-1 == req->timerfd) 94 if (-1 == req->timerfd)
94 { 95 {
95 printf("timerfd_create: %s", strerror(errno)); 96 printf ("timerfd_create: %s", strerror (errno));
96 return MHD_NO; 97 return MHD_NO;
97 } 98 }
98 evt.events = EPOLLIN; 99 evt.events = EPOLLIN;
99 evt.data.ptr = req; 100 evt.data.ptr = req;
100 if (-1 == epoll_ctl(epfd, EPOLL_CTL_ADD, req->timerfd, &evt)) 101 if (-1 == epoll_ctl (epfd, EPOLL_CTL_ADD, req->timerfd, &evt))
101 { 102 {
102 printf("epoll_ctl: %s", strerror(errno)); 103 printf ("epoll_ctl: %s", strerror (errno));
103 return MHD_NO; 104 return MHD_NO;
104 } 105 }
105 ts.it_value.tv_sec = 1; 106 ts.it_value.tv_sec = 1;
106 ts.it_value.tv_nsec = 0; 107 ts.it_value.tv_nsec = 0;
107 ts.it_interval.tv_sec = 0; 108 ts.it_interval.tv_sec = 0;
108 ts.it_interval.tv_nsec = 0; 109 ts.it_interval.tv_nsec = 0;
109 if (-1 == timerfd_settime(req->timerfd, 0, &ts, NULL)) 110 if (-1 == timerfd_settime (req->timerfd, 0, &ts, NULL))
110 { 111 {
111 printf("timerfd_settime: %s", strerror(errno)); 112 printf ("timerfd_settime: %s", strerror (errno));
112 return MHD_NO; 113 return MHD_NO;
113 } 114 }
114 MHD_suspend_connection(connection); 115 MHD_suspend_connection (connection);
115 return MHD_YES; 116 return MHD_YES;
116} 117}
117 118
@@ -130,7 +131,7 @@ connection_done (void *cls,
130 if (-1 != req->timerfd) 131 if (-1 != req->timerfd)
131 if (0 != close (req->timerfd)) 132 if (0 != close (req->timerfd))
132 abort (); 133 abort ();
133 free(req); 134 free (req);
134} 135}
135 136
136 137
@@ -139,17 +140,17 @@ main (int argc,
139 char *const *argv) 140 char *const *argv)
140{ 141{
141 struct MHD_Daemon *d; 142 struct MHD_Daemon *d;
142 const union MHD_DaemonInfo * info; 143 const union MHD_DaemonInfo *info;
143 int current_event_count; 144 int current_event_count;
144 struct epoll_event events_list[1]; 145 struct epoll_event events_list[1];
145 struct Request *req; 146 struct Request *req;
146 uint64_t timer_expirations; 147 uint64_t timer_expirations;
147 148
148 if (argc != 2) 149 if (argc != 2)
149 { 150 {
150 printf ("%s PORT\n", argv[0]); 151 printf ("%s PORT\n", argv[0]);
151 return 1; 152 return 1;
152 } 153 }
153 d = MHD_start_daemon (MHD_USE_EPOLL | MHD_ALLOW_SUSPEND_RESUME, 154 d = MHD_start_daemon (MHD_USE_EPOLL | MHD_ALLOW_SUSPEND_RESUME,
154 atoi (argv[1]), 155 atoi (argv[1]),
155 NULL, NULL, &ahc_echo, NULL, 156 NULL, NULL, &ahc_echo, NULL,
@@ -158,17 +159,17 @@ main (int argc,
158 if (d == NULL) 159 if (d == NULL)
159 return 1; 160 return 1;
160 161
161 info = MHD_get_daemon_info(d, MHD_DAEMON_INFO_EPOLL_FD); 162 info = MHD_get_daemon_info (d, MHD_DAEMON_INFO_EPOLL_FD);
162 if (info == NULL) 163 if (info == NULL)
163 return 1; 164 return 1;
164 165
165 epfd = epoll_create1(EPOLL_CLOEXEC); 166 epfd = epoll_create1 (EPOLL_CLOEXEC);
166 if (-1 == epfd) 167 if (-1 == epfd)
167 return 1; 168 return 1;
168 169
169 evt.events = EPOLLIN; 170 evt.events = EPOLLIN;
170 evt.data.ptr = NULL; 171 evt.data.ptr = NULL;
171 if (-1 == epoll_ctl(epfd, EPOLL_CTL_ADD, info->epoll_fd, &evt)) 172 if (-1 == epoll_ctl (epfd, EPOLL_CTL_ADD, info->epoll_fd, &evt))
172 return 1; 173 return 1;
173 174
174 while (1) 175 while (1)
@@ -182,7 +183,7 @@ main (int argc,
182 timeout = TIMEOUT_INFINITE; 183 timeout = TIMEOUT_INFINITE;
183 else 184 else
184 timeout = (to < INT_MAX - 1) ? (int) to : (INT_MAX - 1); 185 timeout = (to < INT_MAX - 1) ? (int) to : (INT_MAX - 1);
185 current_event_count = epoll_wait(epfd, events_list, 1, timeout); 186 current_event_count = epoll_wait (epfd, events_list, 1, timeout);
186 187
187 if (1 == current_event_count) 188 if (1 == current_event_count)
188 { 189 {
@@ -191,12 +192,13 @@ main (int argc,
191 /* A timer has timed out */ 192 /* A timer has timed out */
192 req = events_list[0].data.ptr; 193 req = events_list[0].data.ptr;
193 /* read from the fd so the system knows we heard the notice */ 194 /* read from the fd so the system knows we heard the notice */
194 if (-1 == read(req->timerfd, &timer_expirations, sizeof(timer_expirations))) 195 if (-1 == read (req->timerfd, &timer_expirations,
196 sizeof(timer_expirations)))
195 { 197 {
196 return 1; 198 return 1;
197 } 199 }
198 /* Now resume the connection */ 200 /* Now resume the connection */
199 MHD_resume_connection(req->connection); 201 MHD_resume_connection (req->connection);
200 } 202 }
201 } 203 }
202 else if (0 == current_event_count) 204 else if (0 == current_event_count)
@@ -208,7 +210,7 @@ main (int argc,
208 /* error */ 210 /* error */
209 return 1; 211 return 1;
210 } 212 }
211 if (! MHD_run(d)) 213 if (! MHD_run (d))
212 return 1; 214 return 1;
213 } 215 }
214 216
diff --git a/src/examples/timeout.c b/src/examples/timeout.c
index 91de1599..cfb66950 100644
--- a/src/examples/timeout.c
+++ b/src/examples/timeout.c
@@ -30,27 +30,27 @@
30#define PORT 8080 30#define PORT 8080
31 31
32static int 32static int
33answer_to_connection(void *cls, 33answer_to_connection (void *cls,
34 struct MHD_Connection *connection, 34 struct MHD_Connection *connection,
35 const char *url, 35 const char *url,
36 const char *method, 36 const char *method,
37 const char *version, 37 const char *version,
38 const char *upload_data, 38 const char *upload_data,
39 size_t *upload_data_size, 39 size_t *upload_data_size,
40 void **con_cls) 40 void **con_cls)
41{ 41{
42 const char *page = "<html><body>Hello timeout!</body></html>"; 42 const char *page = "<html><body>Hello timeout!</body></html>";
43 struct MHD_Response *response; 43 struct MHD_Response *response;
44 int ret; 44 int ret;
45 (void)cls; /* Unused. Silent compiler warning. */ 45 (void) cls; /* Unused. Silent compiler warning. */
46 (void)url; /* Unused. Silent compiler warning. */ 46 (void) url; /* Unused. Silent compiler warning. */
47 (void)version; /* Unused. Silent compiler warning. */ 47 (void) version; /* Unused. Silent compiler warning. */
48 (void)method; /* Unused. Silent compiler warning. */ 48 (void) method; /* Unused. Silent compiler warning. */
49 (void)upload_data; /* Unused. Silent compiler warning. */ 49 (void) upload_data; /* Unused. Silent compiler warning. */
50 (void)upload_data_size; /* Unused. Silent compiler warning. */ 50 (void) upload_data_size; /* Unused. Silent compiler warning. */
51 (void)con_cls; /* Unused. Silent compiler warning. */ 51 (void) con_cls; /* Unused. Silent compiler warning. */
52 52
53 response = MHD_create_response_from_buffer (strlen(page), 53 response = MHD_create_response_from_buffer (strlen (page),
54 (void *) page, 54 (void *) page,
55 MHD_RESPMEM_PERSISTENT); 55 MHD_RESPMEM_PERSISTENT);
56 MHD_add_response_header (response, 56 MHD_add_response_header (response,
@@ -59,7 +59,7 @@ answer_to_connection(void *cls,
59 ret = MHD_queue_response (connection, 59 ret = MHD_queue_response (connection,
60 MHD_HTTP_OK, 60 MHD_HTTP_OK,
61 response); 61 response);
62 MHD_destroy_response(response); 62 MHD_destroy_response (response);
63 return ret; 63 return ret;
64} 64}
65 65
@@ -69,7 +69,8 @@ main (void)
69{ 69{
70 struct MHD_Daemon *daemon; 70 struct MHD_Daemon *daemon;
71 71
72 daemon = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD, 72 daemon = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
73 | MHD_USE_INTERNAL_POLLING_THREAD,
73 PORT, 74 PORT,
74 NULL, NULL, 75 NULL, NULL,
75 &answer_to_connection, NULL, 76 &answer_to_connection, NULL,
@@ -78,7 +79,7 @@ main (void)
78 MHD_OPTION_END); 79 MHD_OPTION_END);
79 if (NULL == daemon) 80 if (NULL == daemon)
80 return 1; 81 return 1;
81 (void) getchar(); 82 (void) getchar ();
82 MHD_stop_daemon (daemon); 83 MHD_stop_daemon (daemon);
83 return 0; 84 return 0;
84} 85}
diff --git a/src/examples/upgrade_example.c b/src/examples/upgrade_example.c
index 35761c97..98675e09 100644
--- a/src/examples/upgrade_example.c
+++ b/src/examples/upgrade_example.c
@@ -32,7 +32,8 @@
32#include <microhttpd.h> 32#include <microhttpd.h>
33#include <pthread.h> 33#include <pthread.h>
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/** 39/**
@@ -72,23 +73,23 @@ send_all (MHD_socket sock,
72 73
73 make_blocking (sock); 74 make_blocking (sock);
74 for (off = 0; off < len; off += ret) 75 for (off = 0; off < len; off += ret)
76 {
77 ret = send (sock,
78 &buf[off],
79 len - off,
80 0);
81 if (0 > ret)
75 { 82 {
76 ret = send (sock, 83 if (EAGAIN == errno)
77 &buf[off], 84 {
78 len - off, 85 ret = 0;
79 0); 86 continue;
80 if (0 > ret) 87 }
81 { 88 break;
82 if (EAGAIN == errno)
83 {
84 ret = 0;
85 continue;
86 }
87 break;
88 }
89 if (0 == ret)
90 break;
91 } 89 }
90 if (0 == ret)
91 break;
92 }
92} 93}
93 94
94 95
@@ -118,25 +119,25 @@ run_usock (void *cls)
118 make_blocking (md->sock); 119 make_blocking (md->sock);
119 /* start by sending extra data MHD may have already read, if any */ 120 /* start by sending extra data MHD may have already read, if any */
120 if (0 != md->extra_in_size) 121 if (0 != md->extra_in_size)
121 { 122 {
122 send_all (md->sock, 123 send_all (md->sock,
123 md->extra_in, 124 md->extra_in,
124 md->extra_in_size); 125 md->extra_in_size);
125 free (md->extra_in); 126 free (md->extra_in);
126 } 127 }
127 /* now echo in a loop */ 128 /* now echo in a loop */
128 while (1) 129 while (1)
129 { 130 {
130 got = recv (md->sock, 131 got = recv (md->sock,
131 buf,
132 sizeof (buf),
133 0);
134 if (0 >= got)
135 break;
136 send_all (md->sock,
137 buf, 132 buf,
138 got); 133 sizeof (buf),
139 } 134 0);
135 if (0 >= got)
136 break;
137 send_all (md->sock,
138 buf,
139 got);
140 }
140 free (md); 141 free (md);
141 MHD_upgrade_action (urh, 142 MHD_upgrade_action (urh,
142 MHD_UPGRADE_ACTION_CLOSE); 143 MHD_UPGRADE_ACTION_CLOSE);
@@ -202,23 +203,23 @@ uh_cb (void *cls,
202{ 203{
203 struct MyData *md; 204 struct MyData *md;
204 pthread_t pt; 205 pthread_t pt;
205 (void)cls; /* Unused. Silent compiler warning. */ 206 (void) cls; /* Unused. Silent compiler warning. */
206 (void)connection; /* Unused. Silent compiler warning. */ 207 (void) connection; /* Unused. Silent compiler warning. */
207 (void)con_cls; /* Unused. Silent compiler warning. */ 208 (void) con_cls; /* Unused. Silent compiler warning. */
208 209
209 md = malloc (sizeof (struct MyData)); 210 md = malloc (sizeof (struct MyData));
210 if (NULL == md) 211 if (NULL == md)
211 abort (); 212 abort ();
212 memset (md, 0, sizeof (struct MyData)); 213 memset (md, 0, sizeof (struct MyData));
213 if (0 != extra_in_size) 214 if (0 != extra_in_size)
214 { 215 {
215 md->extra_in = malloc (extra_in_size); 216 md->extra_in = malloc (extra_in_size);
216 if (NULL == md->extra_in) 217 if (NULL == md->extra_in)
217 abort (); 218 abort ();
218 memcpy (md->extra_in, 219 memcpy (md->extra_in,
219 extra_in, 220 extra_in,
220 extra_in_size); 221 extra_in_size);
221 } 222 }
222 md->extra_in_size = extra_in_size; 223 md->extra_in_size = extra_in_size;
223 md->sock = sock; 224 md->sock = sock;
224 md->urh = urh; 225 md->urh = urh;
@@ -252,20 +253,20 @@ ahc_echo (void *cls,
252 static int aptr; 253 static int aptr;
253 struct MHD_Response *response; 254 struct MHD_Response *response;
254 int ret; 255 int ret;
255 (void)cls; /* Unused. Silent compiler warning. */ 256 (void) cls; /* Unused. Silent compiler warning. */
256 (void)url; /* Unused. Silent compiler warning. */ 257 (void) url; /* Unused. Silent compiler warning. */
257 (void)version; /* Unused. Silent compiler warning. */ 258 (void) version; /* Unused. Silent compiler warning. */
258 (void)upload_data; /* Unused. Silent compiler warning. */ 259 (void) upload_data; /* Unused. Silent compiler warning. */
259 (void)upload_data_size; /* Unused. Silent compiler warning. */ 260 (void) upload_data_size; /* Unused. Silent compiler warning. */
260 261
261 if (0 != strcmp (method, "GET")) 262 if (0 != strcmp (method, "GET"))
262 return MHD_NO; /* unexpected method */ 263 return MHD_NO; /* unexpected method */
263 if (&aptr != *ptr) 264 if (&aptr != *ptr)
264 { 265 {
265 /* do never respond on first call */ 266 /* do never respond on first call */
266 *ptr = &aptr; 267 *ptr = &aptr;
267 return MHD_YES; 268 return MHD_YES;
268 } 269 }
269 *ptr = NULL; /* reset when done */ 270 *ptr = NULL; /* reset when done */
270 response = MHD_create_response_for_upgrade (&uh_cb, 271 response = MHD_create_response_for_upgrade (&uh_cb,
271 NULL); 272 NULL);
@@ -288,11 +289,12 @@ main (int argc,
288 struct MHD_Daemon *d; 289 struct MHD_Daemon *d;
289 290
290 if (argc != 2) 291 if (argc != 2)
291 { 292 {
292 printf ("%s PORT\n", argv[0]); 293 printf ("%s PORT\n", argv[0]);
293 return 1; 294 return 1;
294 } 295 }
295 d = MHD_start_daemon (MHD_ALLOW_UPGRADE | MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 296 d = MHD_start_daemon (MHD_ALLOW_UPGRADE | MHD_USE_AUTO
297 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
296 atoi (argv[1]), 298 atoi (argv[1]),
297 NULL, NULL, 299 NULL, NULL,
298 &ahc_echo, NULL, 300 &ahc_echo, NULL,