X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fhttp-svc.c;h=034753f461eeb962b34b76fbd206a4594029f310;hb=8e61a22b3d7219c59f3925ac8e173b9b74fbcf30;hp=cd488961db1c665f77e30368d8fe164d2310b316;hpb=b55efc33fb8df8b0518570b2584b6da9abb3221b;p=src%2Fapp-framework-binder.git diff --git a/src/http-svc.c b/src/http-svc.c index cd488961..034753f4 100644 --- a/src/http-svc.c +++ b/src/http-svc.c @@ -38,6 +38,7 @@ // let's compute fixed URL length only once static apiUrlLen=0; static baseUrlLen=0; +static rootUrlLen=0; // proto missing from GCC char *strcasestr(const char *haystack, const char *needle); @@ -99,15 +100,9 @@ STATIC int servFile (struct MHD_Connection *connection, AFB_session *session, co goto abortRequest; } - if (! S_ISREG (sbuf.st_mode)) { // only standard file any other one including symbolic links are refused. - close (staticfile->fd); // nothing useful to do with this file - fprintf (stderr, "Fail file: [%s] is not a regular file\n", staticfile->path); - const char *errorstr = "Alsa-Json-Gateway Invalid file type"; - response = MHD_create_response_from_buffer (strlen (errorstr), - (void *) errorstr, MHD_RESPMEM_PERSISTENT); - MHD_queue_response (connection, MHD_HTTP_INTERNAL_SERVER_ERROR, response); - goto sendRequest; - } + + + // if url is a directory let's add index.html and redirect client if (S_ISDIR (sbuf.st_mode)) { @@ -128,7 +123,15 @@ STATIC int servFile (struct MHD_Connection *connection, AFB_session *session, co fprintf(stderr, "No Index.html in direcory [%s]\n", staticfile->path); goto abortRequest; } - } + } else if (! S_ISREG (sbuf.st_mode)) { // only standard file any other one including symbolic links are refused. + close (staticfile->fd); // nothing useful to do with this file + fprintf (stderr, "Fail file: [%s] is not a regular file\n", staticfile->path); + const char *errorstr = "Application Framework Binder Invalid file type"; + response = MHD_create_response_from_buffer (strlen (errorstr), + (void *) errorstr, MHD_RESPMEM_PERSISTENT); + MHD_queue_response (connection, MHD_HTTP_INTERNAL_SERVER_ERROR, response); + goto sendRequest; + } // https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching?hl=fr // ftp://ftp.heanet.ie/disk1/www.gnu.org/software/libmicrohttpd/doxygen/dc/d0c/microhttpd_8h.html @@ -190,15 +193,26 @@ STATIC int redirectHTML5(struct MHD_Connection *connection, AFB_session *session // minimal httpd file server for static HTML,JS,CSS,etc... STATIC int requestFile(struct MHD_Connection *connection, AFB_session *session, const char* url) { - int fd; - int ret; + int fd, ret, idx; AFB_staticfile staticfile; - + char *requestdir, *requesturl; + + // default search for file is rootdir base + requestdir= session->config->rootdir; + requesturl=(char*)url; + + // Check for optional aliases + for (idx=0; session->config->aliasdir[idx].url != NULL; idx++) { + if (0 == strncmp(url, session->config->aliasdir[idx].url, session->config->aliasdir[idx].len)) { + requestdir = session->config->aliasdir[idx].path; + requesturl=(char*)&url[session->config->aliasdir[idx].len]; + break; + } + } + // build full path from rootdir + url - - - strncpy(staticfile.path, session->config->rootdir, sizeof (staticfile.path)); - strncat(staticfile.path, url, sizeof (staticfile.path)); + strncpy(staticfile.path, requestdir, sizeof (staticfile.path)); + strncat(staticfile.path, requesturl, sizeof (staticfile.path)); // try to open file and get its size if (-1 == (staticfile.fd = open(staticfile.path, O_RDONLY))) { @@ -259,8 +273,9 @@ PUBLIC AFB_error httpdStart(AFB_session *session) { // compute fixed URL length at startup time apiUrlLen = strlen (session->config->rootapi); baseUrlLen= strlen (session->config->rootbase); - - // open libmagic cache + rootUrlLen= strlen (session->config->rootdir); + + // TBD open libmagic cache [fail to pass EFENCE check] // initLibMagic (session);