diff options
Diffstat (limited to 'doc/examples/largepost.c')
-rw-r--r-- | doc/examples/largepost.c | 166 |
1 files changed, 96 insertions, 70 deletions
diff --git a/doc/examples/largepost.c b/doc/examples/largepost.c index 3dff5e90..d5dc591f 100644 --- a/doc/examples/largepost.c +++ b/doc/examples/largepost.c | |||
@@ -11,18 +11,18 @@ | |||
11 | #define GET 0 | 11 | #define GET 0 |
12 | #define POST 1 | 12 | #define POST 1 |
13 | 13 | ||
14 | static unsigned char nr_of_uploading_clients = 0; | 14 | static unsigned char nr_of_uploading_clients = 0; |
15 | 15 | ||
16 | struct connection_info_struct | 16 | struct connection_info_struct |
17 | { | 17 | { |
18 | int connectiontype; | 18 | int connectiontype; |
19 | struct MHD_PostProcessor *postprocessor; | 19 | struct MHD_PostProcessor *postprocessor; |
20 | FILE *fp; | 20 | FILE *fp; |
21 | const char *answerstring; | 21 | const char *answerstring; |
22 | int answercode; | 22 | int answercode; |
23 | }; | 23 | }; |
24 | 24 | ||
25 | const char* askpage = "<html><body>\n\ | 25 | const char *askpage = "<html><body>\n\ |
26 | Upload a file, please!<br>\n\ | 26 | Upload a file, please!<br>\n\ |
27 | There are %d clients uploading at the moment.<br>\n\ | 27 | There are %d clients uploading at the moment.<br>\n\ |
28 | <form action=\"/filepost\" method=\"post\" enctype=\"multipart/form-data\">\n\ | 28 | <form action=\"/filepost\" method=\"post\" enctype=\"multipart/form-data\">\n\ |
@@ -30,25 +30,34 @@ const char* askpage = "<html><body>\n\ | |||
30 | <input type=\"submit\" value=\" Send \"></form>\n\ | 30 | <input type=\"submit\" value=\" Send \"></form>\n\ |
31 | </body></html>"; | 31 | </body></html>"; |
32 | 32 | ||
33 | const char* busypage = "<html><body>This server is busy, please try again later.</body></html>"; | 33 | const char *busypage = |
34 | "<html><body>This server is busy, please try again later.</body></html>"; | ||
34 | 35 | ||
35 | const char* completepage = "<html><body>The upload has been completed.</body></html>"; | 36 | const char *completepage = |
37 | "<html><body>The upload has been completed.</body></html>"; | ||
36 | 38 | ||
37 | const char* errorpage = "<html><body>This doesn't seem to be right.</body></html>"; | 39 | const char *errorpage = |
38 | const char* servererrorpage = "<html><body>An internal server error has occured.</body></html>"; | 40 | "<html><body>This doesn't seem to be right.</body></html>"; |
39 | const char* fileexistspage = "<html><body>This file already exists.</body></html>"; | 41 | const char *servererrorpage = |
42 | "<html><body>An internal server error has occured.</body></html>"; | ||
43 | const char *fileexistspage = | ||
44 | "<html><body>This file already exists.</body></html>"; | ||
40 | 45 | ||
41 | 46 | ||
42 | int | 47 | int |
43 | send_page (struct MHD_Connection *connection, const char* page, int status_code) | 48 | send_page (struct MHD_Connection *connection, const char *page, |
49 | int status_code) | ||
44 | { | 50 | { |
45 | int ret; | 51 | int ret; |
46 | struct MHD_Response *response; | 52 | struct MHD_Response *response; |
47 | |||
48 | 53 | ||
49 | response = MHD_create_response_from_data (strlen (page), (void*) page, MHD_NO, MHD_YES); | 54 | |
50 | if (!response) return MHD_NO; | 55 | response = |
51 | 56 | MHD_create_response_from_data (strlen (page), (void *) page, MHD_NO, | |
57 | MHD_YES); | ||
58 | if (!response) | ||
59 | return MHD_NO; | ||
60 | |||
52 | ret = MHD_queue_response (connection, status_code, response); | 61 | ret = MHD_queue_response (connection, status_code, response); |
53 | MHD_destroy_response (response); | 62 | MHD_destroy_response (response); |
54 | 63 | ||
@@ -56,36 +65,41 @@ send_page (struct MHD_Connection *connection, const char* page, int status_code) | |||
56 | } | 65 | } |
57 | 66 | ||
58 | 67 | ||
59 | int | 68 | int |
60 | iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key, | 69 | iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key, |
61 | const char *filename, const char *content_type, | 70 | const char *filename, const char *content_type, |
62 | const char *transfer_encoding, const char *data, size_t off, size_t size) | 71 | const char *transfer_encoding, const char *data, size_t off, |
72 | size_t size) | ||
63 | { | 73 | { |
64 | FILE *fp; | 74 | FILE *fp; |
65 | struct connection_info_struct *con_info = (struct connection_info_struct*) coninfo_cls; | 75 | struct connection_info_struct *con_info = |
76 | (struct connection_info_struct *) coninfo_cls; | ||
66 | 77 | ||
67 | con_info->answerstring = servererrorpage; | 78 | con_info->answerstring = servererrorpage; |
68 | con_info->answercode = MHD_HTTP_INTERNAL_SERVER_ERROR; | 79 | con_info->answercode = MHD_HTTP_INTERNAL_SERVER_ERROR; |
69 | 80 | ||
70 | if (0 != strcmp (key, "file")) return MHD_NO; | 81 | if (0 != strcmp (key, "file")) |
82 | return MHD_NO; | ||
71 | 83 | ||
72 | if (!con_info->fp) | 84 | if (!con_info->fp) |
73 | { | 85 | { |
74 | if (NULL != (fp = fopen (filename, "r")) ) | 86 | if (NULL != (fp = fopen (filename, "r"))) |
75 | { | 87 | { |
76 | fclose (fp); | 88 | fclose (fp); |
77 | con_info->answerstring = fileexistspage; | 89 | con_info->answerstring = fileexistspage; |
78 | con_info->answercode = MHD_HTTP_FORBIDDEN; | 90 | con_info->answercode = MHD_HTTP_FORBIDDEN; |
79 | return MHD_NO; | 91 | return MHD_NO; |
80 | } | 92 | } |
81 | 93 | ||
82 | con_info->fp = fopen (filename, "ab"); | 94 | con_info->fp = fopen (filename, "ab"); |
83 | if (!con_info->fp) return MHD_NO; | 95 | if (!con_info->fp) |
96 | return MHD_NO; | ||
84 | } | 97 | } |
85 | 98 | ||
86 | if (size > 0) | 99 | if (size > 0) |
87 | { | 100 | { |
88 | if (!fwrite (data, size, sizeof(char), con_info->fp)) return MHD_NO; | 101 | if (!fwrite (data, size, sizeof (char), con_info->fp)) |
102 | return MHD_NO; | ||
89 | } | 103 | } |
90 | 104 | ||
91 | con_info->answerstring = completepage; | 105 | con_info->answerstring = completepage; |
@@ -94,95 +108,105 @@ iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key, | |||
94 | return MHD_YES; | 108 | return MHD_YES; |
95 | } | 109 | } |
96 | 110 | ||
97 | void | 111 | void |
98 | request_completed (void *cls, struct MHD_Connection *connection, | 112 | request_completed (void *cls, struct MHD_Connection *connection, |
99 | void **con_cls, enum MHD_RequestTerminationCode toe) | 113 | void **con_cls, enum MHD_RequestTerminationCode toe) |
100 | { | 114 | { |
101 | struct connection_info_struct *con_info = (struct connection_info_struct*) *con_cls; | 115 | struct connection_info_struct *con_info = |
116 | (struct connection_info_struct *) *con_cls; | ||
102 | 117 | ||
103 | if (NULL == con_info) return; | 118 | if (NULL == con_info) |
119 | return; | ||
104 | 120 | ||
105 | if (con_info->connectiontype == POST) | 121 | if (con_info->connectiontype == POST) |
106 | { | 122 | { |
107 | if (NULL != con_info->postprocessor) | 123 | if (NULL != con_info->postprocessor) |
108 | { | 124 | { |
109 | MHD_destroy_post_processor (con_info->postprocessor); | 125 | MHD_destroy_post_processor (con_info->postprocessor); |
110 | nr_of_uploading_clients--; | 126 | nr_of_uploading_clients--; |
111 | } | 127 | } |
112 | 128 | ||
113 | if (con_info->fp) fclose (con_info->fp); | 129 | if (con_info->fp) |
130 | fclose (con_info->fp); | ||
114 | } | 131 | } |
115 | 132 | ||
116 | free (con_info); | 133 | free (con_info); |
117 | *con_cls = NULL; | 134 | *con_cls = NULL; |
118 | } | 135 | } |
119 | 136 | ||
120 | 137 | ||
121 | int | 138 | int |
122 | answer_to_connection (void *cls, struct MHD_Connection *connection, const char *url, | 139 | answer_to_connection (void *cls, struct MHD_Connection *connection, |
123 | const char *method, const char *version, const char *upload_data, | 140 | const char *url, const char *method, |
141 | const char *version, const char *upload_data, | ||
124 | unsigned int *upload_data_size, void **con_cls) | 142 | unsigned int *upload_data_size, void **con_cls) |
125 | { | 143 | { |
126 | if (NULL == *con_cls) | 144 | if (NULL == *con_cls) |
127 | { | 145 | { |
128 | struct connection_info_struct *con_info; | 146 | struct connection_info_struct *con_info; |
129 | 147 | ||
130 | if (nr_of_uploading_clients >= MAXCLIENTS) | 148 | if (nr_of_uploading_clients >= MAXCLIENTS) |
131 | return send_page(connection, busypage, MHD_HTTP_SERVICE_UNAVAILABLE); | 149 | return send_page (connection, busypage, MHD_HTTP_SERVICE_UNAVAILABLE); |
132 | 150 | ||
133 | con_info = malloc (sizeof (struct connection_info_struct)); | 151 | con_info = malloc (sizeof (struct connection_info_struct)); |
134 | if (NULL == con_info) return MHD_NO; | 152 | if (NULL == con_info) |
153 | return MHD_NO; | ||
135 | 154 | ||
136 | con_info->fp = NULL; | 155 | con_info->fp = NULL; |
137 | 156 | ||
138 | if (0 == strcmp (method, "POST")) | 157 | if (0 == strcmp (method, "POST")) |
139 | { | 158 | { |
140 | con_info->postprocessor = MHD_create_post_processor (connection, POSTBUFFERSIZE, | 159 | con_info->postprocessor = |
141 | iterate_post, (void*) con_info); | 160 | MHD_create_post_processor (connection, POSTBUFFERSIZE, |
161 | iterate_post, (void *) con_info); | ||
142 | 162 | ||
143 | if (NULL == con_info->postprocessor) | 163 | if (NULL == con_info->postprocessor) |
144 | { | 164 | { |
145 | free (con_info); | 165 | free (con_info); |
146 | return MHD_NO; | 166 | return MHD_NO; |
147 | } | 167 | } |
148 | 168 | ||
149 | nr_of_uploading_clients++; | 169 | nr_of_uploading_clients++; |
150 | 170 | ||
151 | con_info->connectiontype = POST; | 171 | con_info->connectiontype = POST; |
152 | con_info->answercode = MHD_HTTP_OK; | 172 | con_info->answercode = MHD_HTTP_OK; |
153 | con_info->answerstring = completepage; | 173 | con_info->answerstring = completepage; |
154 | } | 174 | } |
155 | else con_info->connectiontype = GET; | 175 | else |
176 | con_info->connectiontype = GET; | ||
177 | |||
178 | *con_cls = (void *) con_info; | ||
156 | 179 | ||
157 | *con_cls = (void*) con_info; | ||
158 | |||
159 | return MHD_YES; | 180 | return MHD_YES; |
160 | } | 181 | } |
161 | 182 | ||
162 | if (0 == strcmp (method, "GET")) | 183 | if (0 == strcmp (method, "GET")) |
163 | { | 184 | { |
164 | int ret; | 185 | int ret; |
165 | char buffer[1024] = {0}; | 186 | char buffer[1024] = { 0 }; |
166 | 187 | ||
167 | sprintf (buffer, askpage, nr_of_uploading_clients); | 188 | sprintf (buffer, askpage, nr_of_uploading_clients); |
168 | return send_page (connection, buffer, MHD_HTTP_OK); | 189 | return send_page (connection, buffer, MHD_HTTP_OK); |
169 | } | 190 | } |
170 | 191 | ||
171 | if (0 == strcmp (method, "POST")) | 192 | if (0 == strcmp (method, "POST")) |
172 | { | 193 | { |
173 | struct connection_info_struct *con_info = *con_cls; | 194 | struct connection_info_struct *con_info = *con_cls; |
174 | 195 | ||
175 | if (0 != *upload_data_size) | 196 | if (0 != *upload_data_size) |
176 | { | 197 | { |
177 | MHD_post_process(con_info->postprocessor, upload_data, *upload_data_size); | 198 | MHD_post_process (con_info->postprocessor, upload_data, |
199 | *upload_data_size); | ||
178 | *upload_data_size = 0; | 200 | *upload_data_size = 0; |
179 | 201 | ||
180 | return MHD_YES; | 202 | return MHD_YES; |
181 | } | 203 | } |
182 | else return send_page (connection, con_info->answerstring, con_info->answercode); | 204 | else |
183 | } | 205 | return send_page (connection, con_info->answerstring, |
206 | con_info->answercode); | ||
207 | } | ||
184 | 208 | ||
185 | return send_page(connection, errorpage, MHD_HTTP_BAD_REQUEST); | 209 | return send_page (connection, errorpage, MHD_HTTP_BAD_REQUEST); |
186 | } | 210 | } |
187 | 211 | ||
188 | int | 212 | int |
@@ -192,11 +216,13 @@ main () | |||
192 | 216 | ||
193 | 217 | ||
194 | daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL, | 218 | daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL, |
195 | &answer_to_connection, NULL, MHD_OPTION_NOTIFY_COMPLETED, | 219 | &answer_to_connection, NULL, |
196 | request_completed, NULL, MHD_OPTION_END); | 220 | MHD_OPTION_NOTIFY_COMPLETED, request_completed, |
197 | if (NULL == daemon) return 1; | 221 | NULL, MHD_OPTION_END); |
222 | if (NULL == daemon) | ||
223 | return 1; | ||
198 | 224 | ||
199 | getchar (); | 225 | getchar (); |
200 | 226 | ||
201 | MHD_stop_daemon (daemon); | 227 | MHD_stop_daemon (daemon); |
202 | 228 | ||