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[1] = "";
32 /* a valid subpath is a relative path not looking deeper than root using .. */
33 static int validsubpath(const char *subpath)
38 switch (subpath[i++]) {
42 if (subpath[i] == '/') {
46 if (subpath[i++] == '.') {
52 if (subpath[i++] == '/') {
59 while (subpath[i] && subpath[i] != '/')
70 * Removes the 'prefix' of 'length' frome the tail of 'hreq'
71 * if and only if the prefix exists and is terminated by a leading
74 int afb_hreq_unprefix(struct afb_hreq *hreq, const char *prefix, size_t length)
76 /* check the prefix ? */
77 if (length > hreq->lentail || (hreq->tail[length] && hreq->tail[length] != '/')
78 || strncasecmp(prefix, hreq->tail, length))
81 /* removes successives / */
82 while (length < hreq->lentail && hreq->tail[length + 1] == '/')
86 hreq->lentail -= length;
91 int afb_hreq_valid_tail(struct afb_hreq *hreq)
93 return validsubpath(hreq->tail);
96 void afb_hreq_reply_error(struct afb_hreq *hreq, unsigned int status)
100 struct MHD_Response *response;
102 length = asprintf(&buffer, "<html><body>error %u</body></html>", status);
104 response = MHD_create_response_from_buffer((unsigned)length, buffer, MHD_RESPMEM_MUST_FREE);
106 buffer = "<html><body>error</body></html>";
107 response = MHD_create_response_from_buffer(strlen(buffer), buffer, MHD_RESPMEM_PERSISTENT);
109 if (!MHD_queue_response(hreq->connection, status, response))
110 fprintf(stderr, "Failed to reply error code %u", status);
111 MHD_destroy_response(response);
114 int afb_hreq_reply_file_if_exist(struct afb_hreq *hreq, int dirfd, const char *filename)
120 char etag[1 + 2 * sizeof(int)];
122 struct MHD_Response *response;
124 /* Opens the file or directory */
125 fd = openat(dirfd, filename, O_RDONLY);
129 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
133 /* Retrieves file's status */
134 if (fstat(fd, &st) != 0) {
136 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
140 /* serve directory */
141 if (S_ISDIR(st.st_mode)) {
142 if (hreq->url[hreq->lenurl - 1] != '/') {
143 /* the redirect is needed for reliability of relative path */
144 char *tourl = alloca(hreq->lenurl + 2);
145 memcpy(tourl, hreq->url, hreq->lenurl);
146 tourl[hreq->lenurl] = '/';
147 tourl[hreq->lenurl + 1] = 0;
148 rc = afb_hreq_redirect_to(hreq, tourl);
150 rc = afb_hreq_reply_file_if_exist(hreq, fd, "index.html");
156 /* Don't serve special files */
157 if (!S_ISREG(st.st_mode)) {
159 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
163 /* Check the method */
164 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
166 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
170 /* computes the etag */
171 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
173 /* checks the etag */
174 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
175 if (inm && 0 == strcmp(inm, etag)) {
176 /* etag ok, return NOT MODIFIED */
179 fprintf(stderr, "Not Modified: [%s]\n", filename);
180 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
181 status = MHD_HTTP_NOT_MODIFIED;
184 if (st.st_size != (off_t) (size_t) st.st_size) {
186 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
190 /* create the response */
191 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
192 status = MHD_HTTP_OK;
194 #if defined(USE_MAGIC_MIME_TYPE)
196 if (hreq->session->magic) {
197 const char *mimetype = magic_descriptor(hreq->session->magic, fd);
198 if (mimetype != NULL)
199 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
204 /* fills the value and send */
205 MHD_add_response_header(response, MHD_HTTP_HEADER_CACHE_CONTROL, hreq->session->cacheTimeout);
206 MHD_add_response_header(response, MHD_HTTP_HEADER_ETAG, etag);
207 MHD_queue_response(hreq->connection, status, response);
208 MHD_destroy_response(response);
212 int afb_hreq_reply_file(struct afb_hreq *hreq, int dirfd, const char *filename)
214 int rc = afb_hreq_reply_file_if_exist(hreq, dirfd, filename);
216 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
220 int afb_hreq_redirect_to(struct afb_hreq *hreq, const char *url)
222 struct MHD_Response *response;
224 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
225 MHD_add_response_header(response, MHD_HTTP_HEADER_LOCATION, url);
226 MHD_queue_response(hreq->connection, MHD_HTTP_MOVED_PERMANENTLY, response);
227 MHD_destroy_response(response);
229 fprintf(stderr, "redirect from [%s] to [%s]\n", hreq->url, url);
233 const char *afb_hreq_get_cookie(struct afb_hreq *hreq, const char *name)
235 return MHD_lookup_connection_value(hreq->connection, MHD_COOKIE_KIND, name);
238 const char *afb_hreq_get_argument(struct afb_hreq *hreq, const char *name)
240 return MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
243 const char *afb_hreq_get_header(struct afb_hreq *hreq, const char *name)
245 return MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, name);
248 struct afb_req_itf afb_hreq_itf = {
249 .get_cookie = (void*)afb_hreq_get_cookie,
250 .get_argument = (void*)afb_hreq_get_argument