/*! * restd_rest_handler.c * * Copyright (c) 2015-2019, NADAL Jean-Baptiste. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301 USA * * @Author: NADAL Jean-Baptiste * @Date: 23/12/2019 * */ // This is an independent project of an individual developer. Dear PVS-Studio, please check it. // PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com /*------------------------------- INCLUDES ----------------------------------*/ #include #include #include #include #include #include #include "restd_server.h" #include "restd_http_handler.h" #include "restd_rest_handler.h" #include "macro.h" /*--------------------------------------------------------------------------*/ struct mimetype_s { const char *extn; const char *mime; }; /*--------------------------------------------------------------------------*/ static const struct mimetype_s g_mime_types[] = { {"txt", "text/plain"}, {"log", "text/plain"}, {"js", "text/javascript"}, {"css", "text/css"}, {"htm", "text/html"}, {"html", "text/html"}, {"diff", "text/x-patch"}, {"patch", "text/x-patch"}, {"c", "text/x-csrc"}, {"h", "text/x-chdr"}, {"o", "text/x-object"}, {"ko", "text/x-object"}, {"bmp", "image/bmp"}, {"gif", "image/gif"}, {"png", "image/png"}, {"jpg", "image/jpeg"}, {"jpeg", "image/jpeg"}, {"svg", "image/svg+xml"}, {"json", "application/json"}, {"jsonp", "application/javascript"}, {"zip", "application/zip"}, {"pdf", "application/pdf"}, {"xml", "application/xml"}, {"xsl", "application/xml"}, {"doc", "application/msword"}, {"ppt", "application/vnd.ms-powerpoint"}, {"xls", "application/vnd.ms-excel"}, {"odt", "application/vnd.oasis.opendocument.text"}, {"odp", "application/vnd.oasis.opendocument.presentation"}, {"pl", "application/x-perl"}, {"sh", "application/x-shellscript"}, {"php", "application/x-php"}, {"deb", "application/x-deb"}, {"iso", "application/x-cd-image"}, {"tar.gz", "application/x-compressed-tar"}, {"tgz", "application/x-compressed-tar"}, {"gz", "application/x-gzip"}, {"tar.bz2", "application/x-bzip-compressed-tar"}, {"tbz", "application/x-bzip-compressed-tar"}, {"bz2", "application/x-bzip"}, {"tar", "application/x-tar"}, {"rar", "application/x-rar-compressed"}, {"mp3", "audio/mpeg"}, {"ogg", "audio/x-vorbis+ogg"}, {"wav", "audio/x-wav"}, {"mpg", "video/mpeg"}, {"mpeg", "video/mpeg"}, {"avi", "video/x-msvideo"}, {"README", "text/plain"}, {"log", "text/plain"}, {"cfg", "text/plain"}, {"conf", "text/plain"}, {"pac", "application/x-ns-proxy-autoconfig"}, {"wpad.dat", "application/x-ns-proxy-autoconfig"}, {"appcache", "text/cache-manifest"}, {"manifest", "text/cache-manifest"}, {NULL, NULL}}; /*--------------------------------------------------------------------------*/ static const char *file_mime_lookup(const char *path) { const struct mimetype_s *m = &g_mime_types[0]; const char *e; while (m->extn) { e = &path[strlen(path) - 1]; while (e >= path) { if ((*e == '.' || *e == '/') && !strcasecmp(&e[1], m->extn)) return m->mime; e--; } m++; } return "application/octet-stream"; } /*--------------------------------------------------------------------------*/ static bool contain(const char *src, const char *dest, int len) { int len_src, len_dest; int i = 0; len_src = strlen(src); if (len_src < len) return false; len_dest = strlen(dest); if (len_dest < len) return false; while (i < len) { if (src[i] != dest[i]) return false; i++; } return true; } /*! ---------------------------------------------------------------------------- * @fn restd_rest_handler * * @brief Main function to manage http request. */ int restd_rest_handler(short event, restd_conn_t *conn, void *userdata) { int reason = RESTD_ERROR_PATH_NOT_FOUND; DEBUG("********restd_rest_handler: event 0x%x", event); restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn); char *root_path; qlist_t *hooks = conn->server->hooks; qlist_obj_t obj; bzero((void *)&obj, sizeof(qlist_obj_t)); while (hooks->getnext(hooks, &obj, false) == true) { restd_hook_t *hook = (restd_hook_t *)obj.data; if (hook->cb) { // DEBUG("call_hooks: method: %s - %s \n", hook->method, conn->method); // DEBUG("call_hooks: path: %s - %s\n", hook->path, http->request.path); // DEBUG("HOOK FOUND !!!!\n"); if ((hook->method == NULL) || (conn->method == NULL) || (strcmp(hook->method, conn->method) != 0)) { // DEBUG("Hook found but method failed.\n"); reason = RESTD_ERROR_METHOD_NOT_ALLOWED; break; } if ((hook->path != NULL) && (http->request.path != NULL)) { int i = 0; int pos = -1; while (hook->path[i]) { if (hook->path[i] == ':') pos = i; i++; } if (pos != -1 && contain(hook->path, http->request.path, pos)) { const char *buffer = &http->request.path[pos]; // printf("buffer: <%s>\n", buffer); conn->id = atoi(buffer); return hook->cb(event, conn, hook->userdata); } else if (strcmp(hook->path, http->request.path) == 0) return hook->cb(event, conn, hook->userdata); } } } // No Hook Found check if it's a real file into document root. root_path = restd_server_get_option(conn->server, "server.root_path"); if ((root_path != NULL) && (strlen(root_path) != 0)) { FILE *file; char buf[1024] = ""; qstrcatf(buf, "%s%s", root_path, http->request.path); if ((file = fopen(buf, "r"))) { long fsize; char *file_buf; fseek(file, 0, SEEK_END); fsize = ftell(file); fseek(file, 0, SEEK_SET); file_buf = malloc(fsize + 1); fread(file_buf, 1, fsize, file); fclose(file); file_buf[fsize] = 0; printf("mime type:%s\n", file_mime_lookup(buf)); restd_http_response(conn, 200, file_mime_lookup(buf), file_buf, fsize); return RESTD_CLOSE; } } return conn->server->error_handler(reason, conn, NULL); }