websocket first version works
[src/app-framework-binder.git] / src / afb-hreq.c
index 16c235a..474b90e 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
 
@@ -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]) {
@@ -269,7 +348,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,14 +364,10 @@ 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 */
@@ -319,7 +394,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;
 }
@@ -511,7 +586,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;
 }