]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
Added support for Content-Type and other optional headers when doing an HTTP PUT
authordschreiber <d@d-man.org>
Wed, 20 Jun 2012 04:25:00 +0000 (21:25 -0700)
committerdschreiber <d@d-man.org>
Wed, 20 Jun 2012 04:25:00 +0000 (21:25 -0700)
src/mod/applications/mod_http_cache/mod_http_cache.c

index 381b30bddd45f3e242330f9058327cf83c92b304..4e9e89297681a726722e852c79a145aca4ea2ef3 100644 (file)
@@ -24,6 +24,7 @@
  * Contributor(s):
  *
  * Christopher M. Rienzo <chris@rienzo.com>
+ * Darren Schreiber <d@d-man.org>
  *
  * Maintainer: Christopher M. Rienzo <chris@rienzo.com>
  *
@@ -190,12 +191,29 @@ static void url_cache_clear(url_cache_t *cache, switch_core_session_t *session);
 static switch_status_t http_put(url_cache_t *cache, switch_core_session_t *session, const char *url, const char *filename)
 {
        switch_status_t status = SWITCH_STATUS_SUCCESS;
+\r
+       switch_curl_slist_t *headers = NULL;  /* optional linked-list of HTTP headers */\r
+       char *ext;  /* file extension, used for MIME type identification */\r
+       const char *mime_type = "application/octet-stream";\r
+       char *buf;\r
+\r
        CURL *curl_handle = NULL;
        long httpRes = 0;
        struct stat file_info = {0};
        FILE *file_to_put = NULL;
        int fd;
-       
+\r
+       /* guess what type of mime content this is going to be */\r
+       if ((ext = strrchr(filename, '.'))) {\r
+               ext++;\r
+               if (!(mime_type = switch_core_mime_ext2type(ext))) {\r
+                       mime_type = "application/octet-stream";\r
+               }\r
+       }\r
+\r
+       buf = switch_mprintf("Content-Type: %s", mime_type);\r
+       headers = switch_curl_slist_append(headers, buf);\r
+\r
        /* open file and get the file size */
        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "opening %s for upload to %s\n", filename, url);
        fd = open(filename, O_RDONLY);
@@ -226,6 +244,7 @@ static switch_status_t http_put(url_cache_t *cache, switch_core_session_t *sessi
        switch_curl_easy_setopt(curl_handle, CURLOPT_UPLOAD, 1);
        switch_curl_easy_setopt(curl_handle, CURLOPT_PUT, 1);
        switch_curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
+       switch_curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers);\r
        switch_curl_easy_setopt(curl_handle, CURLOPT_URL, url);
        switch_curl_easy_setopt(curl_handle, CURLOPT_READDATA, file_to_put);
        switch_curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
@@ -260,6 +279,12 @@ done:
                fclose(file_to_put);
        }
 
+       if (headers) {\r
+               switch_curl_slist_free_all(headers);\r
+       }\r
+\r
+       switch_safe_free(buf);\r
+\r
        return status;
 }