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 hreq_data *get_data(struct afb_hreq *hreq, const char *key, int create)
41 struct hreq_data *data = hreq->data;
44 while (data != NULL) {
45 if (!strcasecmp(data->key, key))
50 data = calloc(1, sizeof *data);
52 data->key = strdup(key);
53 if (data->key == NULL) {
57 data->next = hreq->data;
65 /* a valid subpath is a relative path not looking deeper than root using .. */
66 static int validsubpath(const char *subpath)
71 switch (subpath[i++]) {
75 if (subpath[i] == '/') {
79 if (subpath[i++] == '.') {
85 if (subpath[i++] == '/') {
92 while (subpath[i] && subpath[i] != '/')
103 * Removes the 'prefix' of 'length' frome the tail of 'hreq'
104 * if and only if the prefix exists and is terminated by a leading
107 int afb_hreq_unprefix(struct afb_hreq *hreq, const char *prefix, size_t length)
109 /* check the prefix ? */
110 if (length > hreq->lentail || (hreq->tail[length] && hreq->tail[length] != '/')
111 || strncasecmp(prefix, hreq->tail, length))
114 /* removes successives / */
115 while (length < hreq->lentail && hreq->tail[length + 1] == '/')
118 /* update the tail */
119 hreq->lentail -= length;
120 hreq->tail += length;
124 int afb_hreq_valid_tail(struct afb_hreq *hreq)
126 return validsubpath(hreq->tail);
129 void afb_hreq_reply_error(struct afb_hreq *hreq, unsigned int status)
133 struct MHD_Response *response;
135 length = asprintf(&buffer, "<html><body>error %u</body></html>", status);
137 response = MHD_create_response_from_buffer((unsigned)length, buffer, MHD_RESPMEM_MUST_FREE);
139 buffer = "<html><body>error</body></html>";
140 response = MHD_create_response_from_buffer(strlen(buffer), buffer, MHD_RESPMEM_PERSISTENT);
142 if (!MHD_queue_response(hreq->connection, status, response))
143 fprintf(stderr, "Failed to reply error code %u", status);
144 MHD_destroy_response(response);
147 int afb_hreq_reply_file_if_exist(struct afb_hreq *hreq, int dirfd, const char *filename)
153 char etag[1 + 2 * sizeof(int)];
155 struct MHD_Response *response;
157 /* Opens the file or directory */
158 fd = openat(dirfd, filename, O_RDONLY);
162 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
166 /* Retrieves file's status */
167 if (fstat(fd, &st) != 0) {
169 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
173 /* serve directory */
174 if (S_ISDIR(st.st_mode)) {
175 if (hreq->url[hreq->lenurl - 1] != '/') {
176 /* the redirect is needed for reliability of relative path */
177 char *tourl = alloca(hreq->lenurl + 2);
178 memcpy(tourl, hreq->url, hreq->lenurl);
179 tourl[hreq->lenurl] = '/';
180 tourl[hreq->lenurl + 1] = 0;
181 rc = afb_hreq_redirect_to(hreq, tourl);
183 rc = afb_hreq_reply_file_if_exist(hreq, fd, "index.html");
189 /* Don't serve special files */
190 if (!S_ISREG(st.st_mode)) {
192 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
196 /* Check the method */
197 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
199 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
203 /* computes the etag */
204 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
206 /* checks the etag */
207 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
208 if (inm && 0 == strcmp(inm, etag)) {
209 /* etag ok, return NOT MODIFIED */
212 fprintf(stderr, "Not Modified: [%s]\n", filename);
213 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
214 status = MHD_HTTP_NOT_MODIFIED;
217 if (st.st_size != (off_t) (size_t) st.st_size) {
219 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
223 /* create the response */
224 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
225 status = MHD_HTTP_OK;
227 #if defined(USE_MAGIC_MIME_TYPE)
229 if (hreq->session->magic) {
230 const char *mimetype = magic_descriptor(hreq->session->magic, fd);
231 if (mimetype != NULL)
232 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
237 /* fills the value and send */
238 MHD_add_response_header(response, MHD_HTTP_HEADER_CACHE_CONTROL, hreq->session->cacheTimeout);
239 MHD_add_response_header(response, MHD_HTTP_HEADER_ETAG, etag);
240 MHD_queue_response(hreq->connection, status, response);
241 MHD_destroy_response(response);
245 int afb_hreq_reply_file(struct afb_hreq *hreq, int dirfd, const char *filename)
247 int rc = afb_hreq_reply_file_if_exist(hreq, dirfd, filename);
249 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
253 int afb_hreq_redirect_to(struct afb_hreq *hreq, const char *url)
255 struct MHD_Response *response;
257 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
258 MHD_add_response_header(response, MHD_HTTP_HEADER_LOCATION, url);
259 MHD_queue_response(hreq->connection, MHD_HTTP_MOVED_PERMANENTLY, response);
260 MHD_destroy_response(response);
262 fprintf(stderr, "redirect from [%s] to [%s]\n", hreq->url, url);
266 const char *afb_hreq_get_cookie(struct afb_hreq *hreq, const char *name)
268 return MHD_lookup_connection_value(hreq->connection, MHD_COOKIE_KIND, name);
271 const char *afb_hreq_get_argument(struct afb_hreq *hreq, const char *name)
273 struct hreq_data *data = get_data(hreq, name, 0);
274 return data ? data->value : MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
277 const char *afb_hreq_get_header(struct afb_hreq *hreq, const char *name)
279 return MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, name);
282 const struct afb_req_itf afb_hreq_itf = {
283 .get_cookie = (void*)afb_hreq_get_cookie,
284 .get_argument = (void*)afb_hreq_get_argument
288 void afb_hreq_post_end(struct afb_hreq *hreq)
290 struct hreq_data *data = hreq->data;
292 if (data->file > 0) {
300 int afb_hreq_post_add(struct afb_hreq *hreq, const char *key, const char *data, size_t size)
303 struct hreq_data *hdat = get_data(hreq, key, 1);
307 p = realloc(hdat->value, hdat->length + size + 1);
312 memcpy(&hdat->value[hdat->length], data, size);
313 hdat->length += size;
314 hdat->value[hdat->length] = 0;
318 int afb_hreq_post_add_file(struct afb_hreq *hreq, const char *key, const char *file, const char *data, size_t size)
320 struct hreq_data *hdat = get_data(hreq, key, 1);
322 /* continuation with reopening */
323 if (hdat->file < 0) {
324 hdat->file = open(hdat->value, O_WRONLY|O_APPEND);
325 if (hdat->file == 0) {
332 if (hdat->file > 0) {
333 write(hdat->file, data, size);