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"
29 static char empty_string[1] = "";
31 /* a valid subpath is a relative path not looking deeper than root using .. */
32 static int validsubpath(const char *subpath)
37 switch (subpath[i++]) {
41 if (subpath[i] == '/') {
45 if (subpath[i++] == '.') {
51 if (subpath[i++] == '/') {
58 while (subpath[i] && subpath[i] != '/')
69 * Removes the 'prefix' of 'length' frome the tail of 'hreq'
70 * if and only if the prefix exists and is terminated by a leading
73 int afb_hreq_unprefix(struct afb_hreq *hreq, const char *prefix, size_t length)
75 /* check the prefix ? */
76 if (length > hreq->lentail || (hreq->tail[length] && hreq->tail[length] != '/')
77 || memcmp(prefix, hreq->tail, length))
80 /* removes successives / */
81 while (length < hreq->lentail && hreq->tail[length + 1] == '/')
85 hreq->lentail -= length;
90 int afb_hreq_valid_tail(struct afb_hreq *hreq)
92 return validsubpath(hreq->tail);
95 void afb_hreq_reply_error(struct afb_hreq *hreq, unsigned int status)
99 struct MHD_Response *response;
101 length = asprintf(&buffer, "<html><body>error %u</body></html>", status);
103 response = MHD_create_response_from_buffer((unsigned)length, buffer, MHD_RESPMEM_MUST_FREE);
105 buffer = "<html><body>error</body></html>";
106 response = MHD_create_response_from_buffer(strlen(buffer), buffer, MHD_RESPMEM_PERSISTENT);
108 if (!MHD_queue_response(hreq->connection, status, response))
109 fprintf(stderr, "Failed to reply error code %u", status);
110 MHD_destroy_response(response);
113 int afb_hreq_reply_file_if_exist(struct afb_hreq *hreq, int dirfd, const char *filename)
119 char etag[1 + 2 * sizeof(int)];
121 struct MHD_Response *response;
123 /* Opens the file or directory */
124 fd = openat(dirfd, filename, O_RDONLY);
128 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
132 /* Retrieves file's status */
133 if (fstat(fd, &st) != 0) {
135 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
139 /* serve directory */
140 if (S_ISDIR(st.st_mode)) {
141 if (hreq->url[hreq->lenurl - 1] != '/') {
142 /* the redirect is needed for reliability of relative path */
143 char *tourl = alloca(hreq->lenurl + 2);
144 memcpy(tourl, hreq->url, hreq->lenurl);
145 tourl[hreq->lenurl] = '/';
146 tourl[hreq->lenurl + 1] = 0;
147 rc = afb_hreq_redirect_to(hreq, tourl);
149 rc = afb_hreq_reply_file_if_exist(hreq, fd, "index.html");
155 /* Don't serve special files */
156 if (!S_ISREG(st.st_mode)) {
158 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
162 /* Check the method */
163 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
165 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
169 /* computes the etag */
170 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
172 /* checks the etag */
173 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
174 if (inm && 0 == strcmp(inm, etag)) {
175 /* etag ok, return NOT MODIFIED */
178 fprintf(stderr, "Not Modified: [%s]\n", filename);
179 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
180 status = MHD_HTTP_NOT_MODIFIED;
183 if (st.st_size != (off_t) (size_t) st.st_size) {
185 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
189 /* create the response */
190 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
191 status = MHD_HTTP_OK;
193 #if defined(USE_MAGIC_MIME_TYPE)
195 if (hreq->session->magic) {
196 const char *mimetype = magic_descriptor(hreq->session->magic, fd);
197 if (mimetype != NULL)
198 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
203 /* fills the value and send */
204 MHD_add_response_header(response, MHD_HTTP_HEADER_CACHE_CONTROL, hreq->session->cacheTimeout);
205 MHD_add_response_header(response, MHD_HTTP_HEADER_ETAG, etag);
206 MHD_queue_response(hreq->connection, status, response);
207 MHD_destroy_response(response);
211 int afb_hreq_reply_file(struct afb_hreq *hreq, int dirfd, const char *filename)
213 int rc = afb_hreq_reply_file_if_exist(hreq, dirfd, filename);
215 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
219 int afb_hreq_redirect_to(struct afb_hreq *hreq, const char *url)
221 struct MHD_Response *response;
223 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
224 MHD_add_response_header(response, MHD_HTTP_HEADER_LOCATION, url);
225 MHD_queue_response(hreq->connection, MHD_HTTP_MOVED_PERMANENTLY, response);
226 MHD_destroy_response(response);
228 fprintf(stderr, "redirect from [%s] to [%s]\n", hreq->url, url);