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;
40 static struct hreq_data *get_data(struct afb_hreq *hreq, const char *key, int create)
42 struct hreq_data *data = hreq->data;
45 while (data != NULL) {
46 if (!strcasecmp(data->key, key))
51 data = calloc(1, sizeof *data);
53 data->key = strdup(key);
54 if (data->key == NULL) {
58 data->next = hreq->data;
66 /* a valid subpath is a relative path not looking deeper than root using .. */
67 static int validsubpath(const char *subpath)
72 switch (subpath[i++]) {
76 if (subpath[i] == '/') {
80 if (subpath[i++] == '.') {
86 if (subpath[i++] == '/') {
93 while (subpath[i] && subpath[i] != '/')
104 * Removes the 'prefix' of 'length' frome the tail of 'hreq'
105 * if and only if the prefix exists and is terminated by a leading
108 int afb_hreq_unprefix(struct afb_hreq *hreq, const char *prefix, size_t length)
110 /* check the prefix ? */
111 if (length > hreq->lentail || (hreq->tail[length] && hreq->tail[length] != '/')
112 || strncasecmp(prefix, hreq->tail, length))
115 /* removes successives / */
116 while (length < hreq->lentail && hreq->tail[length + 1] == '/')
119 /* update the tail */
120 hreq->lentail -= length;
121 hreq->tail += length;
125 int afb_hreq_valid_tail(struct afb_hreq *hreq)
127 return validsubpath(hreq->tail);
130 void afb_hreq_reply_error(struct afb_hreq *hreq, unsigned int status)
134 struct MHD_Response *response;
136 length = asprintf(&buffer, "<html><body>error %u</body></html>", status);
138 response = MHD_create_response_from_buffer((unsigned)length, buffer, MHD_RESPMEM_MUST_FREE);
140 buffer = "<html><body>error</body></html>";
141 response = MHD_create_response_from_buffer(strlen(buffer), buffer, MHD_RESPMEM_PERSISTENT);
143 if (!MHD_queue_response(hreq->connection, status, response))
144 fprintf(stderr, "Failed to reply error code %u", status);
145 MHD_destroy_response(response);
148 int afb_hreq_reply_file_if_exist(struct afb_hreq *hreq, int dirfd, const char *filename)
154 char etag[1 + 2 * sizeof(int)];
156 struct MHD_Response *response;
158 /* Opens the file or directory */
159 fd = openat(dirfd, filename, O_RDONLY);
163 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
167 /* Retrieves file's status */
168 if (fstat(fd, &st) != 0) {
170 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
174 /* serve directory */
175 if (S_ISDIR(st.st_mode)) {
176 if (hreq->url[hreq->lenurl - 1] != '/') {
177 /* the redirect is needed for reliability of relative path */
178 char *tourl = alloca(hreq->lenurl + 2);
179 memcpy(tourl, hreq->url, hreq->lenurl);
180 tourl[hreq->lenurl] = '/';
181 tourl[hreq->lenurl + 1] = 0;
182 rc = afb_hreq_redirect_to(hreq, tourl);
184 rc = afb_hreq_reply_file_if_exist(hreq, fd, "index.html");
190 /* Don't serve special files */
191 if (!S_ISREG(st.st_mode)) {
193 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
197 /* Check the method */
198 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
200 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
204 /* computes the etag */
205 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
207 /* checks the etag */
208 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
209 if (inm && 0 == strcmp(inm, etag)) {
210 /* etag ok, return NOT MODIFIED */
213 fprintf(stderr, "Not Modified: [%s]\n", filename);
214 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
215 status = MHD_HTTP_NOT_MODIFIED;
218 if (st.st_size != (off_t) (size_t) st.st_size) {
220 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
224 /* create the response */
225 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
226 status = MHD_HTTP_OK;
228 #if defined(USE_MAGIC_MIME_TYPE)
230 if (hreq->session->magic) {
231 const char *mimetype = magic_descriptor(hreq->session->magic, fd);
232 if (mimetype != NULL)
233 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
238 /* fills the value and send */
239 MHD_add_response_header(response, MHD_HTTP_HEADER_CACHE_CONTROL, hreq->session->cacheTimeout);
240 MHD_add_response_header(response, MHD_HTTP_HEADER_ETAG, etag);
241 MHD_queue_response(hreq->connection, status, response);
242 MHD_destroy_response(response);
246 int afb_hreq_reply_file(struct afb_hreq *hreq, int dirfd, const char *filename)
248 int rc = afb_hreq_reply_file_if_exist(hreq, dirfd, filename);
250 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
254 int afb_hreq_redirect_to(struct afb_hreq *hreq, const char *url)
256 struct MHD_Response *response;
258 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
259 MHD_add_response_header(response, MHD_HTTP_HEADER_LOCATION, url);
260 MHD_queue_response(hreq->connection, MHD_HTTP_MOVED_PERMANENTLY, response);
261 MHD_destroy_response(response);
263 fprintf(stderr, "redirect from [%s] to [%s]\n", hreq->url, url);
267 const char *afb_hreq_get_cookie(struct afb_hreq *hreq, const char *name)
269 return MHD_lookup_connection_value(hreq->connection, MHD_COOKIE_KIND, name);
272 const char *afb_hreq_get_argument(struct afb_hreq *hreq, const char *name)
274 struct hreq_data *data = get_data(hreq, name, 0);
275 return data ? data->value : MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
278 const char *afb_hreq_get_header(struct afb_hreq *hreq, const char *name)
280 return MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, name);
283 const struct afb_req_itf afb_hreq_itf = {
284 .get_cookie = (void*)afb_hreq_get_cookie,
285 .get_argument = (void*)afb_hreq_get_argument
289 void afb_hreq_post_end(struct afb_hreq *hreq)
291 struct hreq_data *data = hreq->data;
293 if (data->file > 0) {
301 int afb_hreq_post_add(struct afb_hreq *hreq, const char *key, const char *data, size_t size)
304 struct hreq_data *hdat = get_data(hreq, key, 1);
308 p = realloc(hdat->value, hdat->length + size + 1);
313 memcpy(&hdat->value[hdat->length], data, size);
314 hdat->length += size;
315 hdat->value[hdat->length] = 0;
319 int afb_hreq_post_add_file(struct afb_hreq *hreq, const char *key, const char *file, const char *data, size_t size)
321 struct hreq_data *hdat = get_data(hreq, key, 1);
323 /* continuation with reopening */
324 if (hdat->file < 0) {
325 hdat->file = open(hdat->value, O_WRONLY|O_APPEND);
326 if (hdat->file == 0) {
333 if (hdat->file > 0) {
334 write(hdat->file, data, size);