diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/daemon/daemon.c | 8 | ||||
-rw-r--r-- | src/examples/fileserver_example.c | 27 | ||||
-rw-r--r-- | src/examples/fileserver_example_dirs.c | 65 | ||||
-rw-r--r-- | src/examples/fileserver_example_external_select.c | 17 | ||||
-rw-r--r-- | src/examples/https_fileserver_example.c | 6 | ||||
-rw-r--r-- | src/examples/minimal_example.c | 6 | ||||
-rw-r--r-- | src/examples/minimal_example_comet.c | 6 | ||||
-rw-r--r-- | src/examples/querystring_example.c | 11 | ||||
-rw-r--r-- | src/examples/refuse_post_example.c | 6 | ||||
-rw-r--r-- | src/include/microhttpd.h | 6 |
10 files changed, 121 insertions, 37 deletions
diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c index e8eed2ab..74d20233 100644 --- a/src/daemon/daemon.c +++ b/src/daemon/daemon.c | |||
@@ -1254,6 +1254,14 @@ parse_options_va (struct MHD_Daemon *daemon, | |||
1254 | break; | 1254 | break; |
1255 | case MHD_OPTION_THREAD_POOL_SIZE: | 1255 | case MHD_OPTION_THREAD_POOL_SIZE: |
1256 | daemon->worker_pool_size = va_arg (ap, unsigned int); | 1256 | daemon->worker_pool_size = va_arg (ap, unsigned int); |
1257 | if (daemon->worker_pool_size >= SIZE_MAX / sizeof (struct MHD_Daemon)) | ||
1258 | { | ||
1259 | #if HAVE_MESSAGES | ||
1260 | FPRINTF (stderr, | ||
1261 | "Specified thread pool size too big\n"); | ||
1262 | #endif | ||
1263 | return MHD_NO; | ||
1264 | } | ||
1257 | break; | 1265 | break; |
1258 | #if HTTPS_SUPPORT | 1266 | #if HTTPS_SUPPORT |
1259 | case MHD_OPTION_PROTOCOL_VERSION: | 1267 | case MHD_OPTION_PROTOCOL_VERSION: |
diff --git a/src/examples/fileserver_example.c b/src/examples/fileserver_example.c index 76a10f2f..93604d70 100644 --- a/src/examples/fileserver_example.c +++ b/src/examples/fileserver_example.c | |||
@@ -38,6 +38,13 @@ file_reader (void *cls, uint64_t pos, char *buf, int max) | |||
38 | return fread (buf, 1, max, file); | 38 | return fread (buf, 1, max, file); |
39 | } | 39 | } |
40 | 40 | ||
41 | static void | ||
42 | free_callback (void *cls) | ||
43 | { | ||
44 | FILE *file = cls; | ||
45 | fclose (file); | ||
46 | } | ||
47 | |||
41 | static int | 48 | static int |
42 | ahc_echo (void *cls, | 49 | ahc_echo (void *cls, |
43 | struct MHD_Connection *connection, | 50 | struct MHD_Connection *connection, |
@@ -62,7 +69,10 @@ ahc_echo (void *cls, | |||
62 | return MHD_YES; | 69 | return MHD_YES; |
63 | } | 70 | } |
64 | *ptr = NULL; /* reset when done */ | 71 | *ptr = NULL; /* reset when done */ |
65 | file = fopen (&url[1], "rb"); | 72 | if (0 == stat (&url[1], &buf)) |
73 | file = fopen (&url[1], "rb"); | ||
74 | else | ||
75 | file = NULL; | ||
66 | if (file == NULL) | 76 | if (file == NULL) |
67 | { | 77 | { |
68 | response = MHD_create_response_from_data (strlen (PAGE), | 78 | response = MHD_create_response_from_data (strlen (PAGE), |
@@ -73,12 +83,15 @@ ahc_echo (void *cls, | |||
73 | } | 83 | } |
74 | else | 84 | else |
75 | { | 85 | { |
76 | stat (&url[1], &buf); | ||
77 | response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k page size */ | 86 | response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k page size */ |
78 | &file_reader, | 87 | &file_reader, |
79 | file, | 88 | file, |
80 | (MHD_ContentReaderFreeCallback) | 89 | &free_callback); |
81 | & fclose); | 90 | if (response == NULL) |
91 | { | ||
92 | fclose (file); | ||
93 | return MHD_NO; | ||
94 | } | ||
82 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | 95 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); |
83 | MHD_destroy_response (response); | 96 | MHD_destroy_response (response); |
84 | } | 97 | } |
@@ -90,9 +103,9 @@ main (int argc, char *const *argv) | |||
90 | { | 103 | { |
91 | struct MHD_Daemon *d; | 104 | struct MHD_Daemon *d; |
92 | 105 | ||
93 | if (argc != 3) | 106 | if (argc != 2) |
94 | { | 107 | { |
95 | printf ("%s PORT SECONDS-TO-RUN\n", argv[0]); | 108 | printf ("%s PORT\n", argv[0]); |
96 | return 1; | 109 | return 1; |
97 | } | 110 | } |
98 | d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, | 111 | d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, |
@@ -100,7 +113,7 @@ main (int argc, char *const *argv) | |||
100 | NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); | 113 | NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); |
101 | if (d == NULL) | 114 | if (d == NULL) |
102 | return 1; | 115 | return 1; |
103 | sleep (atoi (argv[2])); | 116 | getc (stdin); |
104 | MHD_stop_daemon (d); | 117 | MHD_stop_daemon (d); |
105 | return 0; | 118 | return 0; |
106 | } | 119 | } |
diff --git a/src/examples/fileserver_example_dirs.c b/src/examples/fileserver_example_dirs.c index 04519c63..30749fee 100644 --- a/src/examples/fileserver_example_dirs.c +++ b/src/examples/fileserver_example_dirs.c | |||
@@ -39,16 +39,32 @@ file_reader (void *cls, uint64_t pos, char *buf, int max) | |||
39 | return fread (buf, 1, max, file); | 39 | return fread (buf, 1, max, file); |
40 | } | 40 | } |
41 | 41 | ||
42 | static void | ||
43 | file_free_callback (void *cls) | ||
44 | { | ||
45 | FILE *file = cls; | ||
46 | fclose (file); | ||
47 | } | ||
48 | |||
49 | static void | ||
50 | dir_free_callback (void *cls) | ||
51 | { | ||
52 | DIR *dir = cls; | ||
53 | if (dir != NULL) | ||
54 | closedir (dir); | ||
55 | } | ||
42 | 56 | ||
43 | static int | 57 | static int |
44 | dir_reader (void *cls, uint64_t pos, char *buf, int max) | 58 | dir_reader (void *cls, uint64_t pos, char *buf, int max) |
45 | { | 59 | { |
60 | DIR *dir = cls; | ||
46 | struct dirent *e; | 61 | struct dirent *e; |
62 | |||
47 | if (max < 512) | 63 | if (max < 512) |
48 | return 0; | 64 | return 0; |
49 | do | 65 | do |
50 | { | 66 | { |
51 | e = readdir (cls); | 67 | e = readdir (dir); |
52 | if (e == NULL) | 68 | if (e == NULL) |
53 | return -1; | 69 | return -1; |
54 | } while (e->d_name[0] == '.'); | 70 | } while (e->d_name[0] == '.'); |
@@ -72,7 +88,9 @@ ahc_echo (void *cls, | |||
72 | struct MHD_Response *response; | 88 | struct MHD_Response *response; |
73 | int ret; | 89 | int ret; |
74 | FILE *file; | 90 | FILE *file; |
91 | DIR *dir; | ||
75 | struct stat buf; | 92 | struct stat buf; |
93 | char emsg[1024]; | ||
76 | 94 | ||
77 | if (0 != strcmp (method, MHD_HTTP_METHOD_GET)) | 95 | if (0 != strcmp (method, MHD_HTTP_METHOD_GET)) |
78 | return MHD_NO; /* unexpected method */ | 96 | return MHD_NO; /* unexpected method */ |
@@ -86,13 +104,39 @@ ahc_echo (void *cls, | |||
86 | file = fopen (&url[1], "rb"); | 104 | file = fopen (&url[1], "rb"); |
87 | if (file == NULL) | 105 | if (file == NULL) |
88 | { | 106 | { |
89 | response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, | 107 | dir = opendir ("."); |
90 | 32 * 1024, | 108 | if (dir == NULL) |
91 | &dir_reader, | 109 | { |
92 | opendir ("."), | 110 | /* most likely cause: more concurrent requests than |
93 | (MHD_ContentReaderFreeCallback) &closedir); | 111 | available file descriptors / 2 */ |
94 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | 112 | snprintf (emsg, |
95 | MHD_destroy_response (response); | 113 | sizeof (emsg), |
114 | "Failed to open directory `.': %s\n", | ||
115 | strerror (errno)); | ||
116 | response = MHD_create_response_from_data (strlen (emsg), | ||
117 | emsg, | ||
118 | MHD_NO, | ||
119 | MHD_YES); | ||
120 | if (response == NULL) | ||
121 | return MHD_NO; | ||
122 | ret = MHD_queue_response (connection, MHD_HTTP_SERVICE_UNAVAILABLE, response); | ||
123 | MHD_destroy_response (response); | ||
124 | } | ||
125 | else | ||
126 | { | ||
127 | response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, | ||
128 | 32 * 1024, | ||
129 | &dir_reader, | ||
130 | dir, | ||
131 | &dir_free_callback); | ||
132 | if (response == NULL) | ||
133 | { | ||
134 | closedir (dir); | ||
135 | return MHD_NO; | ||
136 | } | ||
137 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | ||
138 | MHD_destroy_response (response); | ||
139 | } | ||
96 | } | 140 | } |
97 | else | 141 | else |
98 | { | 142 | { |
@@ -100,8 +144,7 @@ ahc_echo (void *cls, | |||
100 | response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k page size */ | 144 | response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k page size */ |
101 | &file_reader, | 145 | &file_reader, |
102 | file, | 146 | file, |
103 | (MHD_ContentReaderFreeCallback) | 147 | &file_free_callback); |
104 | & fclose); | ||
105 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | 148 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); |
106 | MHD_destroy_response (response); | 149 | MHD_destroy_response (response); |
107 | } | 150 | } |
@@ -123,7 +166,7 @@ main (int argc, char *const *argv) | |||
123 | NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); | 166 | NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); |
124 | if (d == NULL) | 167 | if (d == NULL) |
125 | return 1; | 168 | return 1; |
126 | while (1) sleep (1); | 169 | getc (stdin); |
127 | MHD_stop_daemon (d); | 170 | MHD_stop_daemon (d); |
128 | return 0; | 171 | return 0; |
129 | } | 172 | } |
diff --git a/src/examples/fileserver_example_external_select.c b/src/examples/fileserver_example_external_select.c index 6867335b..e34da291 100644 --- a/src/examples/fileserver_example_external_select.c +++ b/src/examples/fileserver_example_external_select.c | |||
@@ -34,10 +34,17 @@ file_reader (void *cls, uint64_t pos, char *buf, int max) | |||
34 | { | 34 | { |
35 | FILE *file = cls; | 35 | FILE *file = cls; |
36 | 36 | ||
37 | fseek (file, pos, SEEK_SET); | 37 | (void) fseek (file, pos, SEEK_SET); |
38 | return fread (buf, 1, max, file); | 38 | return fread (buf, 1, max, file); |
39 | } | 39 | } |
40 | 40 | ||
41 | static void | ||
42 | free_callback (void *cls) | ||
43 | { | ||
44 | FILE *file = cls; | ||
45 | fclose (file); | ||
46 | } | ||
47 | |||
41 | static int | 48 | static int |
42 | ahc_echo (void *cls, | 49 | ahc_echo (void *cls, |
43 | struct MHD_Connection *connection, | 50 | struct MHD_Connection *connection, |
@@ -77,8 +84,12 @@ ahc_echo (void *cls, | |||
77 | response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k page size */ | 84 | response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k page size */ |
78 | &file_reader, | 85 | &file_reader, |
79 | file, | 86 | file, |
80 | (MHD_ContentReaderFreeCallback) | 87 | &free_callback); |
81 | & fclose); | 88 | if (response == NULL) |
89 | { | ||
90 | fclose (file); | ||
91 | return MHD_NO; | ||
92 | } | ||
82 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | 93 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); |
83 | MHD_destroy_response (response); | 94 | MHD_destroy_response (response); |
84 | } | 95 | } |
diff --git a/src/examples/https_fileserver_example.c b/src/examples/https_fileserver_example.c index 9f9728dd..a675d310 100644 --- a/src/examples/https_fileserver_example.c +++ b/src/examples/https_fileserver_example.c | |||
@@ -158,7 +158,7 @@ main (int argc, char *const *argv) | |||
158 | { | 158 | { |
159 | struct MHD_Daemon *TLS_daemon; | 159 | struct MHD_Daemon *TLS_daemon; |
160 | 160 | ||
161 | if (argc == 3) | 161 | if (argc == 2) |
162 | { | 162 | { |
163 | /* TODO check if this is truly necessary - disallow usage of the blocking /dev/random */ | 163 | /* TODO check if this is truly necessary - disallow usage of the blocking /dev/random */ |
164 | /* gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM, 0); */ | 164 | /* gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM, 0); */ |
@@ -172,7 +172,7 @@ main (int argc, char *const *argv) | |||
172 | } | 172 | } |
173 | else | 173 | else |
174 | { | 174 | { |
175 | printf ("Usage: %s HTTP-PORT SECONDS-TO-RUN\n", argv[0]); | 175 | printf ("Usage: %s HTTP-PORT\n", argv[0]); |
176 | return 1; | 176 | return 1; |
177 | } | 177 | } |
178 | 178 | ||
@@ -186,7 +186,7 @@ main (int argc, char *const *argv) | |||
186 | printf ("MHD daemon listening on port %d\n", atoi (argv[1])); | 186 | printf ("MHD daemon listening on port %d\n", atoi (argv[1])); |
187 | } | 187 | } |
188 | 188 | ||
189 | sleep (atoi (argv[2])); | 189 | getc (stdin); |
190 | 190 | ||
191 | MHD_stop_daemon (TLS_daemon); | 191 | MHD_stop_daemon (TLS_daemon); |
192 | 192 | ||
diff --git a/src/examples/minimal_example.c b/src/examples/minimal_example.c index 4c73d6f2..abc12772 100644 --- a/src/examples/minimal_example.c +++ b/src/examples/minimal_example.c | |||
@@ -61,9 +61,9 @@ main (int argc, char *const *argv) | |||
61 | { | 61 | { |
62 | struct MHD_Daemon *d; | 62 | struct MHD_Daemon *d; |
63 | 63 | ||
64 | if (argc != 3) | 64 | if (argc != 2) |
65 | { | 65 | { |
66 | printf ("%s PORT SECONDS-TO-RUN\n", argv[0]); | 66 | printf ("%s PORT\n", argv[0]); |
67 | return 1; | 67 | return 1; |
68 | } | 68 | } |
69 | d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, | 69 | d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, |
@@ -71,7 +71,7 @@ main (int argc, char *const *argv) | |||
71 | NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); | 71 | NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); |
72 | if (d == NULL) | 72 | if (d == NULL) |
73 | return 1; | 73 | return 1; |
74 | sleep (atoi (argv[2])); | 74 | getc (stdin); |
75 | MHD_stop_daemon (d); | 75 | MHD_stop_daemon (d); |
76 | return 0; | 76 | return 0; |
77 | } | 77 | } |
diff --git a/src/examples/minimal_example_comet.c b/src/examples/minimal_example_comet.c index 03607f32..28f3e3c2 100644 --- a/src/examples/minimal_example_comet.c +++ b/src/examples/minimal_example_comet.c | |||
@@ -69,9 +69,9 @@ main (int argc, char *const *argv) | |||
69 | { | 69 | { |
70 | struct MHD_Daemon *d; | 70 | struct MHD_Daemon *d; |
71 | 71 | ||
72 | if (argc != 3) | 72 | if (argc != 2) |
73 | { | 73 | { |
74 | printf ("%s PORT SECONDS-TO-RUN\n", argv[0]); | 74 | printf ("%s PORT\n", argv[0]); |
75 | return 1; | 75 | return 1; |
76 | } | 76 | } |
77 | d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, | 77 | d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, |
@@ -79,7 +79,7 @@ main (int argc, char *const *argv) | |||
79 | NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END); | 79 | NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END); |
80 | if (d == NULL) | 80 | if (d == NULL) |
81 | return 1; | 81 | return 1; |
82 | sleep (atoi (argv[2])); | 82 | getc (stdin); |
83 | MHD_stop_daemon (d); | 83 | MHD_stop_daemon (d); |
84 | return 0; | 84 | return 0; |
85 | } | 85 | } |
diff --git a/src/examples/querystring_example.c b/src/examples/querystring_example.c index 210d9149..3bcc0be8 100644 --- a/src/examples/querystring_example.c +++ b/src/examples/querystring_example.c | |||
@@ -58,6 +58,11 @@ ahc_echo (void *cls, | |||
58 | return MHD_NO; | 58 | return MHD_NO; |
59 | sprintf (me, fmt, "q", val); | 59 | sprintf (me, fmt, "q", val); |
60 | response = MHD_create_response_from_data (strlen (me), me, MHD_YES, MHD_NO); | 60 | response = MHD_create_response_from_data (strlen (me), me, MHD_YES, MHD_NO); |
61 | if (response == NULL) | ||
62 | { | ||
63 | free (me); | ||
64 | return MHD_NO; | ||
65 | } | ||
61 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | 66 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); |
62 | MHD_destroy_response (response); | 67 | MHD_destroy_response (response); |
63 | return ret; | 68 | return ret; |
@@ -68,9 +73,9 @@ main (int argc, char *const *argv) | |||
68 | { | 73 | { |
69 | struct MHD_Daemon *d; | 74 | struct MHD_Daemon *d; |
70 | 75 | ||
71 | if (argc != 3) | 76 | if (argc != 2) |
72 | { | 77 | { |
73 | printf ("%s PORT SECONDS-TO-RUN\n", argv[0]); | 78 | printf ("%s PORT\n", argv[0]); |
74 | return 1; | 79 | return 1; |
75 | } | 80 | } |
76 | d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, | 81 | d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, |
@@ -78,7 +83,7 @@ main (int argc, char *const *argv) | |||
78 | NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); | 83 | NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); |
79 | if (d == NULL) | 84 | if (d == NULL) |
80 | return 1; | 85 | return 1; |
81 | sleep (atoi (argv[2])); | 86 | getc (stdin); |
82 | MHD_stop_daemon (d); | 87 | MHD_stop_daemon (d); |
83 | return 0; | 88 | return 0; |
84 | } | 89 | } |
diff --git a/src/examples/refuse_post_example.c b/src/examples/refuse_post_example.c index db6a13e9..616eb3da 100644 --- a/src/examples/refuse_post_example.c +++ b/src/examples/refuse_post_example.c | |||
@@ -80,9 +80,9 @@ main (int argc, char *const *argv) | |||
80 | { | 80 | { |
81 | struct MHD_Daemon *d; | 81 | struct MHD_Daemon *d; |
82 | 82 | ||
83 | if (argc != 3) | 83 | if (argc != 2) |
84 | { | 84 | { |
85 | printf ("%s PORT SECONDS-TO-RUN\n", argv[0]); | 85 | printf ("%s PORT\n", argv[0]); |
86 | return 1; | 86 | return 1; |
87 | } | 87 | } |
88 | d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, | 88 | d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, |
@@ -91,7 +91,7 @@ main (int argc, char *const *argv) | |||
91 | MHD_OPTION_END); | 91 | MHD_OPTION_END); |
92 | if (d == NULL) | 92 | if (d == NULL) |
93 | return 1; | 93 | return 1; |
94 | sleep (atoi (argv[2])); | 94 | getc (stdin); |
95 | MHD_stop_daemon (d); | 95 | MHD_stop_daemon (d); |
96 | return 0; | 96 | return 0; |
97 | } | 97 | } |
diff --git a/src/include/microhttpd.h b/src/include/microhttpd.h index 89289e6e..4952240b 100644 --- a/src/include/microhttpd.h +++ b/src/include/microhttpd.h | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of libmicrohttpd | 2 | This file is part of libmicrohttpd |
3 | (C) 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors) | 3 | (C) 2006, 2007, 2008, 2009, 2010 Christian Grothoff (and other contributing authors) |
4 | 4 | ||
5 | This library is free software; you can redistribute it and/or | 5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public | 6 | modify it under the terms of the GNU Lesser General Public |
@@ -97,7 +97,11 @@ extern "C" | |||
97 | * Constant used to indicate unknown size (use when | 97 | * Constant used to indicate unknown size (use when |
98 | * creating a response). | 98 | * creating a response). |
99 | */ | 99 | */ |
100 | #ifdef UINT64_MAX | ||
101 | #define MHD_SIZE_UNKNOWN UINT64_MAX | ||
102 | #else | ||
100 | #define MHD_SIZE_UNKNOWN ((uint64_t) -1LL) | 103 | #define MHD_SIZE_UNKNOWN ((uint64_t) -1LL) |
104 | #endif | ||
101 | 105 | ||
102 | /** | 106 | /** |
103 | * HTTP response codes. | 107 | * HTTP response codes. |