2 * Copyright (C) 2015 "IoT.bzh"
3 * Author "Fulup Ar Foll"
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <sys/types.h>
20 #include <sys/select.h>
21 #include <sys/socket.h>
22 #include <microhttpd.h>
25 #define POSTBUFFERSIZE 512
31 static unsigned int nr_of_uploading_clients = 0;
33 struct connection_info_struct
36 struct MHD_PostProcessor *postprocessor;
38 const char *answerstring;
42 const char *askpage = "<html><body>\n\
43 Upload a file, please!<br>\n\
44 There are %u clients uploading at the moment.<br>\n\
45 <form action=\"/filepost\" method=\"post\" enctype=\"multipart/form-data\">\n\
46 <input name=\"file\" type=\"file\">\n\
47 <input type=\"submit\" value=\" Send \"></form>\n\
50 const char *busypage =
51 "<html><body>This server is busy, please try again later.</body></html>";
53 const char *completepage =
54 "<html><body>The upload has been completed.</body></html>";
56 const char *errorpage =
57 "<html><body>This doesn't seem to be right.</body></html>";
58 const char *servererrorpage =
59 "<html><body>An internal server error has occured.</body></html>";
60 const char *fileexistspage =
61 "<html><body>This file already exists.</body></html>";
65 send_page (struct MHD_Connection *connection, const char *page,
69 struct MHD_Response *response;
72 MHD_create_response_from_buffer (strlen (page), (void *) page,
73 MHD_RESPMEM_PERSISTENT);
77 ret = MHD_queue_response (connection, status_code, response);
78 MHD_destroy_response (response);
85 iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key,
86 const char *filename, const char *content_type,
87 const char *transfer_encoding, const char *data, uint64_t off,
90 struct connection_info_struct *con_info = coninfo_cls;
93 con_info->answerstring = servererrorpage;
94 con_info->answercode = MHD_HTTP_INTERNAL_SERVER_ERROR;
96 if (0 != strcmp (key, "file"))
101 if (NULL != (fp = fopen (filename, "rb")))
104 con_info->answerstring = fileexistspage;
105 con_info->answercode = MHD_HTTP_FORBIDDEN;
109 con_info->fp = fopen (filename, "ab");
116 if (!fwrite (data, size, sizeof (char), con_info->fp))
120 con_info->answerstring = completepage;
121 con_info->answercode = MHD_HTTP_OK;
127 request_completed (void *cls, struct MHD_Connection *connection,
128 void **con_cls, enum MHD_RequestTerminationCode toe)
130 struct connection_info_struct *con_info = *con_cls;
132 if (NULL == con_info)
135 if (con_info->connectiontype == POST)
137 if (NULL != con_info->postprocessor)
139 MHD_destroy_post_processor (con_info->postprocessor);
140 nr_of_uploading_clients--;
144 fclose (con_info->fp);
153 answer_to_connection (void *cls, struct MHD_Connection *connection,
154 const char *url, const char *method,
155 const char *version, const char *upload_data,
156 size_t *upload_data_size, void **con_cls)
158 if (NULL == *con_cls)
160 struct connection_info_struct *con_info;
162 if (nr_of_uploading_clients >= MAXCLIENTS)
163 return send_page (connection, busypage, MHD_HTTP_SERVICE_UNAVAILABLE);
165 con_info = malloc (sizeof (struct connection_info_struct));
166 if (NULL == con_info)
171 if (0 == strcmp (method, "POST"))
173 con_info->postprocessor =
174 MHD_create_post_processor (connection, POSTBUFFERSIZE,
175 iterate_post, (void *) con_info);
177 if (NULL == con_info->postprocessor)
183 nr_of_uploading_clients++;
185 con_info->connectiontype = POST;
186 con_info->answercode = MHD_HTTP_OK;
187 con_info->answerstring = completepage;
190 con_info->connectiontype = GET;
192 *con_cls = (void *) con_info;
197 if (0 == strcmp (method, "GET"))
202 sprintf (buffer, askpage, nr_of_uploading_clients);
203 return send_page (connection, buffer, MHD_HTTP_OK);
206 if (0 == strcmp (method, "POST"))
208 struct connection_info_struct *con_info = *con_cls;
210 if (0 != *upload_data_size)
212 MHD_post_process (con_info->postprocessor, upload_data,
214 *upload_data_size = 0;
219 return send_page (connection, con_info->answerstring,
220 con_info->answercode);
223 return send_page (connection, errorpage, MHD_HTTP_BAD_REQUEST);
229 struct MHD_Daemon *daemon;
232 daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL,
233 &answer_to_connection, NULL,
234 MHD_OPTION_NOTIFY_COMPLETED, request_completed,
235 NULL, MHD_OPTION_END);
241 MHD_stop_daemon (daemon);