2 * Copyright 2016 IoT.bzh
3 * Author: José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include <microhttpd.h>
24 #include "../include/local-def.h"
25 #include "afb-method.h"
26 #include "afb-req-itf.h"
29 static char empty_string[] = "";
32 struct hreq_data *next;
39 static struct afb_arg getarg(struct afb_hreq *hreq, const char *name);
40 static void iterargs(struct afb_hreq *hreq, int (*iterator)(void *closure, struct afb_arg arg), void *closure);
42 static const struct afb_req_itf afb_hreq_itf = {
44 .iterate = (void*)iterargs
47 static struct hreq_data *get_data(struct afb_hreq *hreq, const char *key, int create)
49 struct hreq_data *data = hreq->data;
52 while (data != NULL) {
53 if (!strcasecmp(data->key, key))
58 data = calloc(1, sizeof *data);
60 data->key = strdup(key);
61 if (data->key == NULL) {
65 data->next = hreq->data;
73 /* a valid subpath is a relative path not looking deeper than root using .. */
74 static int validsubpath(const char *subpath)
79 switch (subpath[i++]) {
83 if (subpath[i] == '/') {
87 if (subpath[i++] == '.') {
93 if (subpath[i++] == '/') {
100 while (subpath[i] && subpath[i] != '/')
111 * Removes the 'prefix' of 'length' frome the tail of 'hreq'
112 * if and only if the prefix exists and is terminated by a leading
115 int afb_hreq_unprefix(struct afb_hreq *hreq, const char *prefix, size_t length)
117 /* check the prefix ? */
118 if (length > hreq->lentail || (hreq->tail[length] && hreq->tail[length] != '/')
119 || strncasecmp(prefix, hreq->tail, length))
122 /* removes successives / */
123 while (length < hreq->lentail && hreq->tail[length + 1] == '/')
126 /* update the tail */
127 hreq->lentail -= length;
128 hreq->tail += length;
132 int afb_hreq_valid_tail(struct afb_hreq *hreq)
134 return validsubpath(hreq->tail);
137 void afb_hreq_reply_error(struct afb_hreq *hreq, unsigned int status)
141 struct MHD_Response *response;
143 length = asprintf(&buffer, "<html><body>error %u</body></html>", status);
145 response = MHD_create_response_from_buffer((unsigned)length, buffer, MHD_RESPMEM_MUST_FREE);
147 buffer = "<html><body>error</body></html>";
148 response = MHD_create_response_from_buffer(strlen(buffer), buffer, MHD_RESPMEM_PERSISTENT);
150 if (!MHD_queue_response(hreq->connection, status, response))
151 fprintf(stderr, "Failed to reply error code %u", status);
152 MHD_destroy_response(response);
155 int afb_hreq_reply_file_if_exist(struct afb_hreq *hreq, int dirfd, const char *filename)
161 char etag[1 + 2 * sizeof(int)];
163 struct MHD_Response *response;
165 /* Opens the file or directory */
166 fd = openat(dirfd, filename, O_RDONLY);
170 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
174 /* Retrieves file's status */
175 if (fstat(fd, &st) != 0) {
177 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
181 /* serve directory */
182 if (S_ISDIR(st.st_mode)) {
183 if (hreq->url[hreq->lenurl - 1] != '/') {
184 /* the redirect is needed for reliability of relative path */
185 char *tourl = alloca(hreq->lenurl + 2);
186 memcpy(tourl, hreq->url, hreq->lenurl);
187 tourl[hreq->lenurl] = '/';
188 tourl[hreq->lenurl + 1] = 0;
189 rc = afb_hreq_redirect_to(hreq, tourl);
191 rc = afb_hreq_reply_file_if_exist(hreq, fd, "index.html");
197 /* Don't serve special files */
198 if (!S_ISREG(st.st_mode)) {
200 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
204 /* Check the method */
205 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
207 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
211 /* computes the etag */
212 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
214 /* checks the etag */
215 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
216 if (inm && 0 == strcmp(inm, etag)) {
217 /* etag ok, return NOT MODIFIED */
220 fprintf(stderr, "Not Modified: [%s]\n", filename);
221 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
222 status = MHD_HTTP_NOT_MODIFIED;
225 if (st.st_size != (off_t) (size_t) st.st_size) {
227 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
231 /* create the response */
232 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
233 status = MHD_HTTP_OK;
235 #if defined(USE_MAGIC_MIME_TYPE)
237 if (hreq->session->magic) {
238 const char *mimetype = magic_descriptor(hreq->session->magic, fd);
239 if (mimetype != NULL)
240 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
245 /* fills the value and send */
246 MHD_add_response_header(response, MHD_HTTP_HEADER_CACHE_CONTROL, hreq->session->cacheTimeout);
247 MHD_add_response_header(response, MHD_HTTP_HEADER_ETAG, etag);
248 MHD_queue_response(hreq->connection, status, response);
249 MHD_destroy_response(response);
253 int afb_hreq_reply_file(struct afb_hreq *hreq, int dirfd, const char *filename)
255 int rc = afb_hreq_reply_file_if_exist(hreq, dirfd, filename);
257 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
261 int afb_hreq_redirect_to(struct afb_hreq *hreq, const char *url)
263 struct MHD_Response *response;
265 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
266 MHD_add_response_header(response, MHD_HTTP_HEADER_LOCATION, url);
267 MHD_queue_response(hreq->connection, MHD_HTTP_MOVED_PERMANENTLY, response);
268 MHD_destroy_response(response);
270 fprintf(stderr, "redirect from [%s] to [%s]\n", hreq->url, url);
274 const char *afb_hreq_get_cookie(struct afb_hreq *hreq, const char *name)
276 return MHD_lookup_connection_value(hreq->connection, MHD_COOKIE_KIND, name);
279 const char *afb_hreq_get_argument(struct afb_hreq *hreq, const char *name)
281 struct hreq_data *data = get_data(hreq, name, 0);
282 return data ? data->value : MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
285 const char *afb_hreq_get_header(struct afb_hreq *hreq, const char *name)
287 return MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, name);
290 void afb_hreq_post_end(struct afb_hreq *hreq)
292 struct hreq_data *data = hreq->data;
294 if (data->file > 0) {
302 int afb_hreq_post_add(struct afb_hreq *hreq, const char *key, const char *data, size_t size)
305 struct hreq_data *hdat = get_data(hreq, key, 1);
309 p = realloc(hdat->value, hdat->length + size + 1);
314 memcpy(&hdat->value[hdat->length], data, size);
315 hdat->length += size;
316 hdat->value[hdat->length] = 0;
320 int afb_hreq_post_add_file(struct afb_hreq *hreq, const char *key, const char *file, const char *data, size_t size)
322 struct hreq_data *hdat = get_data(hreq, key, 1);
324 /* continuation with reopening */
325 if (hdat->file < 0) {
326 hdat->file = open(hdat->value, O_WRONLY|O_APPEND);
327 if (hdat->file == 0) {
334 if (hdat->file > 0) {
335 write(hdat->file, data, size);
345 int afb_hreq_is_argument_a_file(struct afb_hreq *hreq, const char *key)
347 struct hreq_data *hdat = get_data(hreq, key, 0);
348 return hdat != NULL && hdat->file != 0;
352 struct afb_req afb_hreq_to_req(struct afb_hreq *hreq)
354 return (struct afb_req){ .itf = &afb_hreq_itf, .data = hreq };
359 struct afb_hreq *hreq;
360 int (*iterator)(void *closure, const char *key, const char *value, int isfile);
364 static int itargs(struct iterator_data *id, enum MHD_ValueKind kind, const char *key, const char *value)
366 if (get_data(id->hreq, key, 0))
368 return id->iterator(id->closure, key, value, 0);
371 void afb_hreq_iterate_arguments(struct afb_hreq *hreq, int (*iterator)(void *closure, const char *key, const char *value, int isfile), void *closure)
373 struct iterator_data id = { .hreq = hreq, .iterator = iterator, .closure = closure };
374 struct hreq_data *data = hreq->data;
376 if (!iterator(closure, data->key, data->value, !!data->file))
380 MHD_get_connection_values (hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)itargs, &id);
383 static struct afb_arg getarg(struct afb_hreq *hreq, const char *name)
385 struct hreq_data *hdat = get_data(hreq, name, 0);
387 return (struct afb_arg){
389 .value = hdat->value,
390 .size = hdat->length,
391 .is_file = (hdat->file != 0)
394 return (struct afb_arg){
396 .value = MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name),
404 struct afb_hreq *hreq;
405 int (*iterator)(void *closure, struct afb_arg arg);
409 static int _iterargs_(struct iterdata *id, enum MHD_ValueKind kind, const char *key, const char *value)
411 if (get_data(id->hreq, key, 0))
413 return id->iterator(id->closure, (struct afb_arg){
421 static void iterargs(struct afb_hreq *hreq, int (*iterator)(void *closure, struct afb_arg arg), void *closure)
423 struct iterdata id = { .hreq = hreq, .iterator = iterator, .closure = closure };
424 struct hreq_data *hdat = hreq->data;
426 if (!iterator(closure, (struct afb_arg){
428 .value = hdat->value,
429 .size = hdat->length,
430 .is_file = (hdat->file != 0)}))
434 MHD_get_connection_values (hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_iterargs_, &id);
437 void afb_hreq_drop_data(struct afb_hreq *hreq)
439 struct hreq_data *data = hreq->data;
441 hreq->data = data->next;