upload of files
[src/app-framework-binder.git] / src / afb-hreq.c
index ece9b39..7c481e9 100644 (file)
@@ -184,7 +184,34 @@ static const char *magic_mimetype_fd(int fd)
 
 #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)
 {
@@ -257,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]) {
@@ -336,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 */
-               {
-                       const char *mimetype = magic_mimetype_fd(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 */
@@ -423,9 +447,29 @@ int afb_hreq_post_add(struct afb_hreq *hreq, const char *key, const char *data,
 
 int afb_hreq_post_add_file(struct afb_hreq *hreq, const char *key, const char *file, const char *data, size_t size)
 {
+       ssize_t sz;
        struct hreq_data *hdat = get_data(hreq, key, 1);
 
-       /* continuation with reopening */
+       if (hdat->value == NULL) {
+               hdat->file = open(file, O_WRONLY|O_TRUNC|O_CREAT, 0600);
+               if (hdat->file == 0) {
+                       hdat->file = dup(0);
+                       close(0);
+               }
+               if (hdat->file <= 0) {
+                       hdat->file = 0;
+                       return 0;
+               }
+               hdat->value = strdup(file);
+               if (hdat->value == NULL) {
+                       close(hdat->file);
+                       hdat->file = 0;
+                       return 0;
+               }
+       } else {
+               if (strcmp(hdat->value, file))
+                       return 0;
+       }
        if (hdat->file < 0) {
                hdat->file = open(hdat->value, O_WRONLY|O_APPEND);
                if (hdat->file == 0) {
@@ -435,15 +479,16 @@ int afb_hreq_post_add_file(struct afb_hreq *hreq, const char *key, const char *f
                if (hdat->file <= 0)
                        return 0;
        }
-       if (hdat->file > 0) {
-               write(hdat->file, data, size);
-               return 1;
+       while (size) {
+               sz = write(hdat->file, data, size);
+               if (sz >= 0) {
+                       hdat->length += (size_t)sz;
+                       size -= (size_t)sz;
+                       data += sz;
+               } else if (errno != EINTR)
+                       return 0;
        }
-
-       /* creation */
-       /* TODO */
-       return 0;
-       
+       return 1;
 }
 
 int afb_hreq_is_argument_a_file(struct afb_hreq *hreq, const char *key)