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 'request'
70 * if and only if the prefix exists and is terminated by a leading
73 int afb_hreq_unprefix(struct afb_hreq *request, const char *prefix, size_t length)
75 /* check the prefix ? */
76 if (length > request->lentail || (request->tail[length] && request->tail[length] != '/')
77 || memcmp(prefix, request->tail, length))
80 /* removes successives / */
81 while (length < request->lentail && request->tail[length + 1] == '/')
85 request->lentail -= length;
86 request->tail += length;
90 int afb_hreq_valid_tail(struct afb_hreq *request)
92 return validsubpath(request->tail);
95 void afb_hreq_reply_error(struct afb_hreq *request, 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(request->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 *request, 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(request, MHD_HTTP_FORBIDDEN);
132 /* Retrieves file's status */
133 if (fstat(fd, &st) != 0) {
135 afb_hreq_reply_error(request, MHD_HTTP_INTERNAL_SERVER_ERROR);
139 /* Don't serve directory */
140 if (S_ISDIR(st.st_mode)) {
141 rc = afb_hreq_reply_file_if_exist(request, fd, "index.html");
146 /* Don't serve special files */
147 if (!S_ISREG(st.st_mode)) {
149 afb_hreq_reply_error(request, MHD_HTTP_FORBIDDEN);
153 /* Check the method */
154 if ((request->method & (afb_method_get | afb_method_head)) == 0) {
156 afb_hreq_reply_error(request, MHD_HTTP_METHOD_NOT_ALLOWED);
160 /* computes the etag */
161 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
163 /* checks the etag */
164 inm = MHD_lookup_connection_value(request->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
165 if (inm && 0 == strcmp(inm, etag)) {
166 /* etag ok, return NOT MODIFIED */
169 fprintf(stderr, "Not Modified: [%s]\n", filename);
170 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
171 status = MHD_HTTP_NOT_MODIFIED;
174 if (st.st_size != (off_t) (size_t) st.st_size) {
176 afb_hreq_reply_error(request, MHD_HTTP_INTERNAL_SERVER_ERROR);
180 /* create the response */
181 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
182 status = MHD_HTTP_OK;
184 #if defined(USE_MAGIC_MIME_TYPE)
186 if (request->session->magic) {
187 const char *mimetype = magic_descriptor(request->session->magic, fd);
188 if (mimetype != NULL)
189 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
194 /* fills the value and send */
195 MHD_add_response_header(response, MHD_HTTP_HEADER_CACHE_CONTROL, request->session->cacheTimeout);
196 MHD_add_response_header(response, MHD_HTTP_HEADER_ETAG, etag);
197 MHD_queue_response(request->connection, status, response);
198 MHD_destroy_response(response);
202 int afb_hreq_reply_file(struct afb_hreq *request, int dirfd, const char *filename)
204 int rc = afb_hreq_reply_file_if_exist(request, dirfd, filename);
206 afb_hreq_reply_error(request, MHD_HTTP_NOT_FOUND);
210 int afb_hreq_redirect_to(struct afb_hreq *request, const char *url)
212 struct MHD_Response *response;
214 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
215 MHD_add_response_header(response, MHD_HTTP_HEADER_LOCATION, url);
216 MHD_queue_response(request->connection, MHD_HTTP_MOVED_PERMANENTLY, response);
217 MHD_destroy_response(response);
219 fprintf(stderr, "redirect from [%s] to [%s]\n", request->url, url);