diff options
author | Christian Grothoff <christian@grothoff.org> | 2015-12-11 22:29:46 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2015-12-11 22:29:46 +0000 |
commit | 26a19e6787689b1ee88c9896ac8e8cb2342355d4 (patch) | |
tree | 88d252a32add95d39999357357059bf666bfcc11 | |
parent | 5ac9c9835594471154e54252fbc364fd8f50e6a5 (diff) | |
download | libmicrohttpd-26a19e6787689b1ee88c9896ac8e8cb2342355d4.tar.gz libmicrohttpd-26a19e6787689b1ee88c9896ac8e8cb2342355d4.zip |
cleaning up largepost
-rw-r--r-- | doc/examples/largepost.c | 101 |
1 files changed, 68 insertions, 33 deletions
diff --git a/doc/examples/largepost.c b/doc/examples/largepost.c index 0b9de2d5..b8762a45 100644 --- a/doc/examples/largepost.c +++ b/doc/examples/largepost.c | |||
@@ -17,14 +17,17 @@ | |||
17 | #define POSTBUFFERSIZE 512 | 17 | #define POSTBUFFERSIZE 512 |
18 | #define MAXCLIENTS 2 | 18 | #define MAXCLIENTS 2 |
19 | 19 | ||
20 | #define GET 0 | 20 | enum ConnectionType |
21 | #define POST 1 | 21 | { |
22 | GET = 0, | ||
23 | POST = 1 | ||
24 | }; | ||
22 | 25 | ||
23 | static unsigned int nr_of_uploading_clients = 0; | 26 | static unsigned int nr_of_uploading_clients = 0; |
24 | 27 | ||
25 | struct connection_info_struct | 28 | struct connection_info_struct |
26 | { | 29 | { |
27 | int connectiontype; | 30 | enum ConnectionType connectiontype; |
28 | struct MHD_PostProcessor *postprocessor; | 31 | struct MHD_PostProcessor *postprocessor; |
29 | FILE *fp; | 32 | FILE *fp; |
30 | const char *answerstring; | 33 | const char *answerstring; |
@@ -54,19 +57,25 @@ const char *fileexistspage = | |||
54 | 57 | ||
55 | 58 | ||
56 | static int | 59 | static int |
57 | send_page (struct MHD_Connection *connection, const char *page, | 60 | send_page (struct MHD_Connection *connection, |
61 | const char *page, | ||
58 | int status_code) | 62 | int status_code) |
59 | { | 63 | { |
60 | int ret; | 64 | int ret; |
61 | struct MHD_Response *response; | 65 | struct MHD_Response *response; |
62 | 66 | ||
63 | response = | 67 | response = |
64 | MHD_create_response_from_buffer (strlen (page), (void *) page, | 68 | MHD_create_response_from_buffer (strlen (page), |
69 | (void *) page, | ||
65 | MHD_RESPMEM_MUST_COPY); | 70 | MHD_RESPMEM_MUST_COPY); |
66 | if (!response) | 71 | if (!response) |
67 | return MHD_NO; | 72 | return MHD_NO; |
68 | MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE, "text/html"); | 73 | MHD_add_response_header (response, |
69 | ret = MHD_queue_response (connection, status_code, response); | 74 | MHD_HTTP_HEADER_CONTENT_TYPE, |
75 | "text/html"); | ||
76 | ret = MHD_queue_response (connection, | ||
77 | status_code, | ||
78 | response); | ||
70 | MHD_destroy_response (response); | 79 | MHD_destroy_response (response); |
71 | 80 | ||
72 | return ret; | 81 | return ret; |
@@ -74,9 +83,14 @@ send_page (struct MHD_Connection *connection, const char *page, | |||
74 | 83 | ||
75 | 84 | ||
76 | static int | 85 | static int |
77 | iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key, | 86 | iterate_post (void *coninfo_cls, |
78 | const char *filename, const char *content_type, | 87 | enum MHD_ValueKind kind, |
79 | const char *transfer_encoding, const char *data, uint64_t off, | 88 | const char *key, |
89 | const char *filename, | ||
90 | const char *content_type, | ||
91 | const char *transfer_encoding, | ||
92 | const char *data, | ||
93 | uint64_t off, | ||
80 | size_t size) | 94 | size_t size) |
81 | { | 95 | { |
82 | struct connection_info_struct *con_info = coninfo_cls; | 96 | struct connection_info_struct *con_info = coninfo_cls; |
@@ -88,7 +102,7 @@ iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key, | |||
88 | if (0 != strcmp (key, "file")) | 102 | if (0 != strcmp (key, "file")) |
89 | return MHD_NO; | 103 | return MHD_NO; |
90 | 104 | ||
91 | if (!con_info->fp) | 105 | if (! con_info->fp) |
92 | { | 106 | { |
93 | if (NULL != (fp = fopen (filename, "rb"))) | 107 | if (NULL != (fp = fopen (filename, "rb"))) |
94 | { | 108 | { |
@@ -105,7 +119,7 @@ iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key, | |||
105 | 119 | ||
106 | if (size > 0) | 120 | if (size > 0) |
107 | { | 121 | { |
108 | if (!fwrite (data, size, sizeof (char), con_info->fp)) | 122 | if (! fwrite (data, sizeof (char), size, con_info->fp)) |
109 | return MHD_NO; | 123 | return MHD_NO; |
110 | } | 124 | } |
111 | 125 | ||
@@ -117,8 +131,10 @@ iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key, | |||
117 | 131 | ||
118 | 132 | ||
119 | static void | 133 | static void |
120 | request_completed (void *cls, struct MHD_Connection *connection, | 134 | request_completed (void *cls, |
121 | void **con_cls, enum MHD_RequestTerminationCode toe) | 135 | struct MHD_Connection *connection, |
136 | void **con_cls, | ||
137 | enum MHD_RequestTerminationCode toe) | ||
122 | { | 138 | { |
123 | struct connection_info_struct *con_info = *con_cls; | 139 | struct connection_info_struct *con_info = *con_cls; |
124 | 140 | ||
@@ -143,17 +159,23 @@ request_completed (void *cls, struct MHD_Connection *connection, | |||
143 | 159 | ||
144 | 160 | ||
145 | static int | 161 | static int |
146 | answer_to_connection (void *cls, struct MHD_Connection *connection, | 162 | answer_to_connection (void *cls, |
147 | const char *url, const char *method, | 163 | struct MHD_Connection *connection, |
148 | const char *version, const char *upload_data, | 164 | const char *url, |
149 | size_t *upload_data_size, void **con_cls) | 165 | const char *method, |
166 | const char *version, | ||
167 | const char *upload_data, | ||
168 | size_t *upload_data_size, | ||
169 | void **con_cls) | ||
150 | { | 170 | { |
151 | if (NULL == *con_cls) | 171 | if (NULL == *con_cls) |
152 | { | 172 | { |
153 | struct connection_info_struct *con_info; | 173 | struct connection_info_struct *con_info; |
154 | 174 | ||
155 | if (nr_of_uploading_clients >= MAXCLIENTS) | 175 | if (nr_of_uploading_clients >= MAXCLIENTS) |
156 | return send_page (connection, busypage, MHD_HTTP_SERVICE_UNAVAILABLE); | 176 | return send_page (connection, |
177 | busypage, | ||
178 | MHD_HTTP_SERVICE_UNAVAILABLE); | ||
157 | 179 | ||
158 | con_info = malloc (sizeof (struct connection_info_struct)); | 180 | con_info = malloc (sizeof (struct connection_info_struct)); |
159 | if (NULL == con_info) | 181 | if (NULL == con_info) |
@@ -161,11 +183,13 @@ answer_to_connection (void *cls, struct MHD_Connection *connection, | |||
161 | 183 | ||
162 | con_info->fp = NULL; | 184 | con_info->fp = NULL; |
163 | 185 | ||
164 | if (0 == strcmp (method, "POST")) | 186 | if (0 == strcasecmp (method, MHD_HTTP_METHOD_POST)) |
165 | { | 187 | { |
166 | con_info->postprocessor = | 188 | con_info->postprocessor = |
167 | MHD_create_post_processor (connection, POSTBUFFERSIZE, | 189 | MHD_create_post_processor (connection, |
168 | iterate_post, (void *) con_info); | 190 | POSTBUFFERSIZE, |
191 | &iterate_post, | ||
192 | (void *) con_info); | ||
169 | 193 | ||
170 | if (NULL == con_info->postprocessor) | 194 | if (NULL == con_info->postprocessor) |
171 | { | 195 | { |
@@ -187,21 +211,27 @@ answer_to_connection (void *cls, struct MHD_Connection *connection, | |||
187 | return MHD_YES; | 211 | return MHD_YES; |
188 | } | 212 | } |
189 | 213 | ||
190 | if (0 == strcmp (method, "GET")) | 214 | if (0 == strcasecmp (method, MHD_HTTP_METHOD_GET)) |
191 | { | 215 | { |
192 | char buffer[1024]; | 216 | char buffer[1024]; |
193 | 217 | ||
194 | snprintf (buffer, sizeof (buffer), askpage, nr_of_uploading_clients); | 218 | snprintf (buffer, |
195 | return send_page (connection, buffer, MHD_HTTP_OK); | 219 | sizeof (buffer), |
220 | askpage, | ||
221 | nr_of_uploading_clients); | ||
222 | return send_page (connection, | ||
223 | buffer, | ||
224 | MHD_HTTP_OK); | ||
196 | } | 225 | } |
197 | 226 | ||
198 | if (0 == strcmp (method, "POST")) | 227 | if (0 == strcasecmp (method, MHD_HTTP_METHOD_POST)) |
199 | { | 228 | { |
200 | struct connection_info_struct *con_info = *con_cls; | 229 | struct connection_info_struct *con_info = *con_cls; |
201 | 230 | ||
202 | if (0 != *upload_data_size) | 231 | if (0 != *upload_data_size) |
203 | { | 232 | { |
204 | MHD_post_process (con_info->postprocessor, upload_data, | 233 | MHD_post_process (con_info->postprocessor, |
234 | upload_data, | ||
205 | *upload_data_size); | 235 | *upload_data_size); |
206 | *upload_data_size = 0; | 236 | *upload_data_size = 0; |
207 | 237 | ||
@@ -214,14 +244,18 @@ answer_to_connection (void *cls, struct MHD_Connection *connection, | |||
214 | fclose (con_info->fp); | 244 | fclose (con_info->fp); |
215 | con_info->fp = NULL; | 245 | con_info->fp = NULL; |
216 | } | 246 | } |
217 | /* Now it is safe to open and inspect the file before calling send_page with a response */ | 247 | /* Now it is safe to open and inspect the file before |
218 | return send_page (connection, con_info->answerstring, | 248 | calling send_page with a response */ |
249 | return send_page (connection, | ||
250 | con_info->answerstring, | ||
219 | con_info->answercode); | 251 | con_info->answercode); |
220 | } | 252 | } |
221 | 253 | ||
222 | } | 254 | } |
223 | 255 | ||
224 | return send_page (connection, errorpage, MHD_HTTP_BAD_REQUEST); | 256 | return send_page (connection, |
257 | errorpage, | ||
258 | MHD_HTTP_BAD_REQUEST); | ||
225 | } | 259 | } |
226 | 260 | ||
227 | 261 | ||
@@ -230,10 +264,11 @@ main () | |||
230 | { | 264 | { |
231 | struct MHD_Daemon *daemon; | 265 | struct MHD_Daemon *daemon; |
232 | 266 | ||
233 | daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL, | 267 | daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, |
268 | PORT, NULL, NULL, | ||
234 | &answer_to_connection, NULL, | 269 | &answer_to_connection, NULL, |
235 | MHD_OPTION_NOTIFY_COMPLETED, request_completed, | 270 | MHD_OPTION_NOTIFY_COMPLETED, &request_completed, NULL, |
236 | NULL, MHD_OPTION_END); | 271 | MHD_OPTION_END); |
237 | if (NULL == daemon) | 272 | if (NULL == daemon) |
238 | return 1; | 273 | return 1; |
239 | (void) getchar (); | 274 | (void) getchar (); |