simplifications
[src/app-framework-binder.git] / src / afb-hreq.c
index 16c235a..3f95c53 100644 (file)
@@ -15,6 +15,7 @@
  * limitations under the License.
  */
 
+#define USE_MAGIC_MIME_TYPE
 #define _GNU_SOURCE
 
 #include <stdlib.h>
 
 #include <microhttpd.h>
 
+#if defined(USE_MAGIC_MIME_TYPE)
+#include <magic.h>
+#endif
+
 #include "local-def.h"
 #include "afb-method.h"
 #include "afb-req-itf.h"
 #include "afb-hreq.h"
 #include "session.h"
+#include "verbose.h"
 
 #define SIZE_RESPONSE_BUFFER   8000
 
@@ -49,9 +55,9 @@ static const char token_cookie[] = "token";
 struct hreq_data {
        struct hreq_data *next;
        char *key;
-       int file;
        size_t length;
        char *value;
+       char *path;
 };
 
 static struct afb_arg req_get(struct afb_hreq *hreq, const char *name);
@@ -135,6 +141,78 @@ static int validsubpath(const char *subpath)
        return 1;
 }
 
+#if defined(USE_MAGIC_MIME_TYPE)
+
+#if !defined(MAGIC_DB)
+#define MAGIC_DB "/usr/share/misc/magic.mgc"
+#endif
+
+static magic_t lazy_libmagic()
+{
+       static int done = 0;
+       static magic_t result = NULL;
+
+       if (!done) {
+               done = 1;
+               /* MAGIC_MIME tells magic to return a mime of the file,
+                        but you can specify different things */
+               if (verbosity)
+                       printf("Loading mimetype default magic database\n");
+
+               result = magic_open(MAGIC_MIME_TYPE);
+               if (result == NULL) {
+                       fprintf(stderr,"ERROR: unable to initialize magic library\n");
+               }
+               /* Warning: should not use NULL for DB
+                               [libmagic bug wont pass efence check] */
+               else if (magic_load(result, MAGIC_DB) != 0) {
+                       fprintf(stderr,"cannot load magic database - %s\n",
+                                       magic_error(result));
+                       magic_close(result);
+                       result = NULL;
+               }
+       }
+
+       return result;
+}
+
+static const char *magic_mimetype_fd(int fd)
+{
+       magic_t lib = lazy_libmagic();
+       return lib ? magic_descriptor(lib, fd) : NULL;
+}
+
+#endif
+
+static const char *mimetype_fd_name(int fd, const char *filename)
+{
+       const char *result = NULL;
+
+#if defined(INFER_EXTENSION)
+       const char *extension = strrchr(filename, '.');
+       if (extension) {
+               static const char *const known[][2] = {
+                       { ".js", "text/javascript" },
+                       { ".html", "text/html" },
+                       { NULL, NULL }
+               };
+               int i = 0;
+               while (known[i][0]) {
+                       if (!strcasecmp(extension, known[i][0])) {
+                               result = known[i][1];
+                               break;
+                       }
+                       i++;
+               }
+       }
+#endif
+#if defined(USE_MAGIC_MIME_TYPE)
+       if (result == NULL)
+               result = magic_mimetype_fd(fd);
+#endif
+       return result;
+}
+
 void afb_hreq_free(struct afb_hreq *hreq)
 {
        struct hreq_data *data;
@@ -206,6 +284,7 @@ int afb_hreq_reply_file_if_exist(struct afb_hreq *hreq, int dirfd, const char *f
        char etag[1 + 2 * sizeof(int)];
        const char *inm;
        struct MHD_Response *response;
+       const char *mimetype;
 
        /* Opens the file or directory */
        if (filename[0]) {
@@ -233,15 +312,24 @@ int afb_hreq_reply_file_if_exist(struct afb_hreq *hreq, int dirfd, const char *f
 
        /* serve directory */
        if (S_ISDIR(st.st_mode)) {
-               if (hreq->url[hreq->lenurl - 1] != '/') {
-                       /* the redirect is needed for reliability of relative path */
-                       char *tourl = alloca(hreq->lenurl + 2);
-                       memcpy(tourl, hreq->url, hreq->lenurl);
-                       tourl[hreq->lenurl] = '/';
-                       tourl[hreq->lenurl + 1] = 0;
-                       rc = afb_hreq_redirect_to(hreq, tourl);
-               } else {
-                       rc = afb_hreq_reply_file_if_exist(hreq, fd, "index.html");
+               static const char *indexes[] = { "index.html", NULL };
+               int i = 0;
+               rc = 0;
+               while (indexes[i] != NULL) {
+                       if (faccessat(fd, indexes[i], R_OK, 0) == 0) {
+                               if (hreq->url[hreq->lenurl - 1] != '/') {
+                                       /* the redirect is needed for reliability of relative path */
+                                       char *tourl = alloca(hreq->lenurl + 2);
+                                       memcpy(tourl, hreq->url, hreq->lenurl);
+                                       tourl[hreq->lenurl] = '/';
+                                       tourl[hreq->lenurl + 1] = 0;
+                                       rc = afb_hreq_redirect_to(hreq, tourl);
+                               } else {
+                                       rc = afb_hreq_reply_file_if_exist(hreq, fd, indexes[i]);
+                               }
+                               break;
+                       }
+                       i++;
                }
                close(fd);
                return rc;
@@ -269,7 +357,7 @@ int afb_hreq_reply_file_if_exist(struct afb_hreq *hreq, int dirfd, const char *f
        if (inm && 0 == strcmp(inm, etag)) {
                /* etag ok, return NOT MODIFIED */
                close(fd);
-               if (verbose)
+               if (verbosity)
                        fprintf(stderr, "Not Modified: [%s]\n", filename);
                response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
                status = MHD_HTTP_NOT_MODIFIED;
@@ -285,18 +373,14 @@ int afb_hreq_reply_file_if_exist(struct afb_hreq *hreq, int dirfd, const char *f
                response = MHD_create_response_from_fd((size_t) st.st_size, fd);
                status = MHD_HTTP_OK;
 
-#if defined(USE_MAGIC_MIME_TYPE)
                /* set the type */
-               if (hreq->session->magic) {
-                       const char *mimetype = magic_descriptor(hreq->session->magic, fd);
-                       if (mimetype != NULL)
-                               MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
-               }
-#endif
+               mimetype = mimetype_fd_name(fd, filename);
+               if (mimetype != NULL)
+                       MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
        }
 
        /* fills the value and send */
-       MHD_add_response_header(response, MHD_HTTP_HEADER_CACHE_CONTROL, hreq->session->cacheTimeout);
+       MHD_add_response_header(response, MHD_HTTP_HEADER_CACHE_CONTROL, hreq->cacheTimeout);
        MHD_add_response_header(response, MHD_HTTP_HEADER_ETAG, etag);
        MHD_queue_response(hreq->connection, status, response);
        MHD_destroy_response(response);
@@ -319,7 +403,7 @@ int afb_hreq_redirect_to(struct afb_hreq *hreq, const char *url)
        MHD_add_response_header(response, MHD_HTTP_HEADER_LOCATION, url);
        MHD_queue_response(hreq->connection, MHD_HTTP_MOVED_PERMANENTLY, response);
        MHD_destroy_response(response);
-       if (verbose)
+       if (verbosity)
                fprintf(stderr, "redirect from [%s] to [%s]\n", hreq->url, url);
        return 1;
 }
@@ -340,23 +424,11 @@ const char *afb_hreq_get_header(struct afb_hreq *hreq, const char *name)
        return MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, name);
 }
 
-void afb_hreq_post_end(struct afb_hreq *hreq)
-{
-       struct hreq_data *data = hreq->data;
-       while(data) {
-               if (data->file > 0) {
-                       close(data->file);
-                       data->file = -1;
-               }
-               data = data->next;
-       }
-}
-
 int afb_hreq_post_add(struct afb_hreq *hreq, const char *key, const char *data, size_t size)
 {
        void *p;
        struct hreq_data *hdat = get_data(hreq, key, 1);
-       if (hdat->file) {
+       if (hdat->path != NULL) {
                return 0;
        }
        p = realloc(hdat->value, hdat->length + size + 1);
@@ -370,38 +442,55 @@ int afb_hreq_post_add(struct afb_hreq *hreq, const char *key, const char *data,
        return 1;
 }
 
+static int opentempfile(char **path)
+{
+       int fd;
+       char *fname;
+
+       fname = strdup("XXXXXX"); /* TODO improve the path */
+       if (fname == NULL)
+               return -1;
+
+       fd = mkostemp(fname, O_CLOEXEC|O_WRONLY);
+       if (fd < 0)
+               free(fname);
+       else
+               *path = fname;
+       return fd;
+}
+
 int afb_hreq_post_add_file(struct afb_hreq *hreq, const char *key, const char *file, const char *data, size_t size)
 {
+       int fd;
+       ssize_t sz;
        struct hreq_data *hdat = get_data(hreq, key, 1);
 
-       /* continuation with reopening */
-       if (hdat->file < 0) {
-               hdat->file = open(hdat->value, O_WRONLY|O_APPEND);
-               if (hdat->file == 0) {
-                       hdat->file = dup(0);
-                       close(0);
-               }
-               if (hdat->file <= 0)
+fprintf(stderr, "%s=%s %s=%s %s\n",key,hdat->key,file,hdat->value,hdat->path);
+       if (hdat->value == NULL) {
+               hdat->value = strdup(file);
+               if (hdat->value == NULL)
                        return 0;
+               fd = opentempfile(&hdat->path);
+       } else if (strcmp(hdat->value, file) || hdat->path == NULL) {
+               return 0;
+       } else {
+               fd = open(hdat->path, O_WRONLY|O_APPEND);
        }
-       if (hdat->file > 0) {
-               write(hdat->file, data, size);
-               return 1;
+       if (fd < 0)
+               return 0;
+       while (size) {
+               sz = write(fd, data, size);
+               if (sz >= 0) {
+                       hdat->length += (size_t)sz;
+                       size -= (size_t)sz;
+                       data += sz;
+               } else if (errno != EINTR)
+                       break;
        }
-
-       /* creation */
-       /* TODO */
-       return 0;
-       
+       close(fd);
+       return !size;
 }
 
-int afb_hreq_is_argument_a_file(struct afb_hreq *hreq, const char *key)
-{
-       struct hreq_data *hdat = get_data(hreq, key, 0);
-       return hdat != NULL && hdat->file != 0;
-}
-
-
 struct afb_req afb_hreq_to_req(struct afb_hreq *hreq)
 {
        return (struct afb_req){ .itf = &afb_hreq_itf, .data = hreq };
@@ -415,14 +504,14 @@ static struct afb_arg req_get(struct afb_hreq *hreq, const char *name)
                        .name = hdat->key,
                        .value = hdat->value,
                        .size = hdat->length,
-                       .is_file = (hdat->file != 0)
+                       .path = hdat->path
                };
                
        return (struct afb_arg){
                .name = name,
                .value = MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name),
                .size = 0,
-               .is_file = 0
+               .path = NULL
        };
 }
 
@@ -439,9 +528,9 @@ static int _iterargs_(struct iterdata *id, enum MHD_ValueKind kind, const char *
                return 1;
        return id->iterator(id->closure, (struct afb_arg){
                .name = key,
-               .value = value,
-               .size = 0,
-               .is_file = 0
+               .value = value ? : "",
+               .size = value ? strlen(value) : 0,
+               .path = NULL
        });
 }
 
@@ -454,7 +543,7 @@ static void req_iterate(struct afb_hreq *hreq, int (*iterator)(void *closure, st
                        .name = hdat->key,
                        .value = hdat->value,
                        .size = hdat->length,
-                       .is_file = (hdat->file != 0)}))
+                       .path = hdat->path}))
                        return;
                hdat = hdat->next;
        }
@@ -511,7 +600,7 @@ struct AFB_clientCtx *afb_hreq_context(struct afb_hreq *hreq)
                        uuid = afb_hreq_get_argument(hreq, uuid_arg);
                if (uuid == NULL)
                        uuid = afb_hreq_get_cookie(hreq, uuid_cookie);
-               hreq->context = ctxClientGet(uuid);
+               hreq->context = ctxClientGetForUuid(uuid);
        }
        return hreq->context;
 }