6d5c44be0e97e46f3df12d8b39cd9355e7161a1d
[src/app-framework-binder.git] / src / SamplePost.c
1 /*
2  * Copyright (C) 2015 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include <sys/types.h>
20 #include <sys/select.h>
21 #include <sys/socket.h>
22 #include <microhttpd.h>
23
24 #define PORT            8888
25 #define POSTBUFFERSIZE  512
26 #define MAXCLIENTS      2
27
28 #define GET             0
29 #define POST            1
30
31 static unsigned int nr_of_uploading_clients = 0;
32
33 struct connection_info_struct
34 {
35   int connectiontype;
36   struct MHD_PostProcessor *postprocessor;
37   FILE *fp;
38   const char *answerstring;
39   int answercode;
40 };
41
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\
48                        </body></html>";
49
50 const char *busypage =
51   "<html><body>This server is busy, please try again later.</body></html>";
52
53 const char *completepage =
54   "<html><body>The upload has been completed.</body></html>";
55
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>";
62
63
64 static int
65 send_page (struct MHD_Connection *connection, const char *page,
66            int status_code)
67 {
68   int ret;
69   struct MHD_Response *response;
70
71   response =
72     MHD_create_response_from_buffer (strlen (page), (void *) page,
73                                      MHD_RESPMEM_PERSISTENT);
74   if (!response)
75     return MHD_NO;
76
77   ret = MHD_queue_response (connection, status_code, response);
78   MHD_destroy_response (response);
79
80   return ret;
81 }
82
83
84 static int
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,
88               size_t size)
89 {
90   struct connection_info_struct *con_info = coninfo_cls;
91   FILE *fp;
92
93   con_info->answerstring = servererrorpage;
94   con_info->answercode = MHD_HTTP_INTERNAL_SERVER_ERROR;
95
96   if (0 != strcmp (key, "file"))
97     return MHD_NO;
98
99   if (!con_info->fp)
100     {
101       if (NULL != (fp = fopen (filename, "rb")))
102         {
103           fclose (fp);
104           con_info->answerstring = fileexistspage;
105           con_info->answercode = MHD_HTTP_FORBIDDEN;
106           return MHD_NO;
107         }
108
109       con_info->fp = fopen (filename, "ab");
110       if (!con_info->fp)
111         return MHD_NO;
112     }
113
114   if (size > 0)
115     {
116       if (!fwrite (data, size, sizeof (char), con_info->fp))
117         return MHD_NO;
118     }
119
120   con_info->answerstring = completepage;
121   con_info->answercode = MHD_HTTP_OK;
122
123   return MHD_YES;
124 }
125
126 static void
127 request_completed (void *cls, struct MHD_Connection *connection,
128                    void **con_cls, enum MHD_RequestTerminationCode toe)
129 {
130   struct connection_info_struct *con_info = *con_cls;
131
132   if (NULL == con_info)
133     return;
134
135   if (con_info->connectiontype == POST)
136     {
137       if (NULL != con_info->postprocessor)
138         {
139           MHD_destroy_post_processor (con_info->postprocessor);
140           nr_of_uploading_clients--;
141         }
142
143       if (con_info->fp)
144         fclose (con_info->fp);
145     }
146
147   free (con_info);
148   *con_cls = NULL;
149 }
150
151
152 static int
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)
157 {
158   if (NULL == *con_cls)
159     {
160       struct connection_info_struct *con_info;
161
162       if (nr_of_uploading_clients >= MAXCLIENTS)
163         return send_page (connection, busypage, MHD_HTTP_SERVICE_UNAVAILABLE);
164
165       con_info = malloc (sizeof (struct connection_info_struct));
166       if (NULL == con_info)
167         return MHD_NO;
168
169       con_info->fp = NULL;
170
171       if (0 == strcmp (method, "POST"))
172         {
173           con_info->postprocessor =
174             MHD_create_post_processor (connection, POSTBUFFERSIZE,
175                                        iterate_post, (void *) con_info);
176
177           if (NULL == con_info->postprocessor)
178             {
179               free (con_info);
180               return MHD_NO;
181             }
182
183           nr_of_uploading_clients++;
184
185           con_info->connectiontype = POST;
186           con_info->answercode = MHD_HTTP_OK;
187           con_info->answerstring = completepage;
188         }
189       else
190         con_info->connectiontype = GET;
191
192       *con_cls = (void *) con_info;
193
194       return MHD_YES;
195     }
196
197   if (0 == strcmp (method, "GET"))
198     {
199       int ret;
200       char buffer[1024];
201
202       sprintf (buffer, askpage, nr_of_uploading_clients);
203       return send_page (connection, buffer, MHD_HTTP_OK);
204     }
205
206   if (0 == strcmp (method, "POST"))
207     {
208       struct connection_info_struct *con_info = *con_cls;
209
210       if (0 != *upload_data_size)
211         {
212           MHD_post_process (con_info->postprocessor, upload_data,
213                             *upload_data_size);
214           *upload_data_size = 0;
215
216           return MHD_YES;
217         }
218       else
219         return send_page (connection, con_info->answerstring,
220                           con_info->answercode);
221     }
222
223   return send_page (connection, errorpage, MHD_HTTP_BAD_REQUEST);
224 }
225
226 int
227 main ()
228 {
229   struct MHD_Daemon *daemon;
230
231
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);
236   if (NULL == daemon)
237     return 1;
238
239   getchar ();
240
241   MHD_stop_daemon (daemon);
242
243   return 0;
244 }