diff --git a/mfapi/apicalls/upload_simple.c b/mfapi/apicalls/upload_simple.c index 7da69cf..5070538 100644 --- a/mfapi/apicalls/upload_simple.c +++ b/mfapi/apicalls/upload_simple.c @@ -25,10 +25,13 @@ #include #include #include +#include #include +#include #include "../../utils/http.h" #include "../../utils/hash.h" +#include "../../utils/strings.h" #include "../mfconn.h" #include "../apicalls.h" // IWYU pragma: keep @@ -45,6 +48,8 @@ mfconn_api_upload_simple(mfconn * conn, const char *folderkey, char *file_hash; uint64_t file_size; int i; + struct curl_slist *custom_headers = NULL; + char *tmpheader; if (conn == NULL) return -1; @@ -70,6 +75,10 @@ mfconn_api_upload_simple(mfconn * conn, const char *folderkey, free(*upload_key); *upload_key = NULL; } + if (custom_headers != NULL) { + curl_slist_free_all(custom_headers); + custom_headers = NULL; + } if (folderkey == NULL) { api_call = mfconn_create_signed_get(conn, 0, @@ -85,13 +94,28 @@ mfconn_api_upload_simple(mfconn * conn, const char *folderkey, // make sure that we are at the beginning of the file rewind(fh); + // the following three pseudo headers are interpreted by the mediafire + // server + tmpheader = strdup_printf("x-filename: %s", file_name); + custom_headers = curl_slist_append(custom_headers, tmpheader); + free(tmpheader); + tmpheader = strdup_printf("x-filesize: %" PRIu64, file_size); + custom_headers = curl_slist_append(custom_headers, tmpheader); + free(tmpheader); + tmpheader = strdup_printf("x-filehash: %s", file_hash); + custom_headers = curl_slist_append(custom_headers, tmpheader); + free(tmpheader); + http = http_create(); - retval = http_post_file(http, api_call, fh, file_name, - file_size, file_hash, + retval = http_post_file(http, api_call, fh, &custom_headers, file_size, _decode_upload_simple, upload_key); http_destroy(http); mfconn_update_secret_key(conn); + if (custom_headers != NULL) { + curl_slist_free_all(custom_headers); + custom_headers = NULL; + } free((void *)api_call); if (retval != 127 && retval != 28) diff --git a/utils/http.c b/utils/http.c index 45e12b9..77ea749 100644 --- a/utils/http.c +++ b/utils/http.c @@ -21,11 +21,9 @@ #include #include #include -#include #include #include "http.h" -#include "strings.h" static int http_progress_cb(void *user_ptr, double dltotal, double dlnow, double ultotal, double ulnow); @@ -280,37 +278,25 @@ http_read_file_cb(char *data, size_t size, size_t nmemb, void *user_ptr) int http_post_file(mfhttp * conn, const char *url, FILE * fh, - const char *filename, uint64_t filesize, const char *fhash, + struct curl_slist **custom_headers, uint64_t filesize, int (*data_handler) (mfhttp * conn, void *data), void *data) { - struct curl_slist *custom_headers = NULL; - char *tmpheader; int retval; http_curl_reset(conn); conn->write_buf_len = 0; - // the following three pseudo headers are interpreted by the mediafire - // server - tmpheader = strdup_printf("x-filename: %s", filename); - custom_headers = curl_slist_append(custom_headers, tmpheader); - free(tmpheader); - tmpheader = strdup_printf("x-filesize: %" PRIu64, filesize); - custom_headers = curl_slist_append(custom_headers, tmpheader); - free(tmpheader); - tmpheader = strdup_printf("x-filehash: %s", fhash); - custom_headers = curl_slist_append(custom_headers, tmpheader); - free(tmpheader); // when using POST, curl implicitly sets // Content-Type: application/x-www-form-urlencoded // make sure it is set to application/octet-stream instead - custom_headers = curl_slist_append(custom_headers, - "Content-Type: application/octet-stream"); + *custom_headers = + curl_slist_append(*custom_headers, + "Content-Type: application/octet-stream"); // when using POST, curl implicitly sets Expect: 100-continue // make sure it is not set - custom_headers = curl_slist_append(custom_headers, "Expect:"); + *custom_headers = curl_slist_append(*custom_headers, "Expect:"); curl_easy_setopt(conn->curl_handle, CURLOPT_POST, 1); - curl_easy_setopt(conn->curl_handle, CURLOPT_HTTPHEADER, custom_headers); + curl_easy_setopt(conn->curl_handle, CURLOPT_HTTPHEADER, *custom_headers); curl_easy_setopt(conn->curl_handle, CURLOPT_URL, url); curl_easy_setopt(conn->curl_handle, CURLOPT_READFUNCTION, http_read_file_cb); @@ -323,7 +309,8 @@ http_post_file(mfhttp * conn, const char *url, FILE * fh, conn->stream = fh; fprintf(stderr, "POST: %s\n", url); retval = curl_easy_perform(conn->curl_handle); - curl_slist_free_all(custom_headers); + curl_slist_free_all(*custom_headers); + *custom_headers = NULL; if (retval != CURLE_OK) { fprintf(stderr, "error curl_easy_perform %s\n\r", conn->error_buf); return retval; diff --git a/utils/http.h b/utils/http.h index 3981a73..a345098 100644 --- a/utils/http.h +++ b/utils/http.h @@ -22,6 +22,8 @@ #include #include #include +#include +#include typedef struct mfhttp mfhttp; @@ -38,9 +40,9 @@ int http_get_file(mfhttp * conn, const char *url, const char *path); json_t *http_parse_buf_json(mfhttp * conn, size_t flags, json_error_t * error); -int http_post_file(mfhttp * conn, const char *url, - FILE * fh, const char *filename, - uint64_t filesize, const char *fhash, +int http_post_file(mfhttp * conn, const char *url, FILE * fh, + struct curl_slist **custom_headers, + uint64_t filesize, int (*data_handler) (mfhttp * conn, void *data), void *data);