From f7887066807b7bbf4c965850b05b232f2e0f531a Mon Sep 17 00:00:00 2001 From: josch Date: Fri, 19 Dec 2014 07:57:01 +0100 Subject: [PATCH] mfconn_create_signed_get: add more verbosity to error conditions --- mfapi/apicalls/device_get_changes.c | 4 +++ mfapi/apicalls/device_get_patch.c | 4 +++ mfapi/apicalls/device_get_status.c | 4 +++ mfapi/apicalls/device_get_updates.c | 5 +++ mfapi/apicalls/file_delete.c | 4 +++ mfapi/apicalls/file_get_info.c | 4 +++ mfapi/apicalls/file_get_links.c | 4 +++ mfapi/apicalls/folder_create.c | 4 +++ mfapi/apicalls/folder_delete.c | 4 +++ mfapi/apicalls/folder_get_content.c | 4 +++ mfapi/apicalls/folder_get_info.c | 4 +++ mfapi/apicalls/upload_patch.c | 4 +++ mfapi/apicalls/upload_poll_upload.c | 4 +++ mfapi/apicalls/upload_simple.c | 5 ++- mfapi/apicalls/user_get_info.c | 4 +++ mfapi/mfconn.c | 52 ++++++++++++++++++++--------- 16 files changed, 97 insertions(+), 17 deletions(-) diff --git a/mfapi/apicalls/device_get_changes.c b/mfapi/apicalls/device_get_changes.c index f59918d..6cfe242 100644 --- a/mfapi/apicalls/device_get_changes.c +++ b/mfapi/apicalls/device_get_changes.c @@ -57,6 +57,10 @@ int mfconn_api_device_get_changes(mfconn * conn, uint64_t revision, api_call = mfconn_create_signed_get(conn, 0, "device/get_changes.php", "?revision=%" PRIu64 "&response_format=json", revision); + if (api_call == NULL) { + fprintf(stderr, "mfconn_create_signed_get failed\n"); + return -1; + } http = http_create(); retval = diff --git a/mfapi/apicalls/device_get_patch.c b/mfapi/apicalls/device_get_patch.c index 681fdf6..14e3f18 100644 --- a/mfapi/apicalls/device_get_patch.c +++ b/mfapi/apicalls/device_get_patch.c @@ -61,6 +61,10 @@ int mfconn_api_device_get_patch(mfconn * conn, mfpatch * patch, "&target_revision=%" PRIu64 "&response_format=json", quickkey, source_revision, target_revision); + if (api_call == NULL) { + fprintf(stderr, "mfconn_create_signed_get failed\n"); + return -1; + } http = http_create(); retval = http_get_buf(http, api_call, _decode_device_get_patch, diff --git a/mfapi/apicalls/device_get_status.c b/mfapi/apicalls/device_get_status.c index 931e6a6..9f0dec0 100644 --- a/mfapi/apicalls/device_get_status.c +++ b/mfapi/apicalls/device_get_status.c @@ -41,6 +41,10 @@ int mfconn_api_device_get_status(mfconn * conn, uint64_t * revision) for (i = 0; i < mfconn_get_max_num_retries(conn); i++) { api_call = mfconn_create_signed_get(conn, 0, "device/get_status.php", "?response_format=json"); + if (api_call == NULL) { + fprintf(stderr, "mfconn_create_signed_get failed\n"); + return -1; + } http = http_create(); retval = diff --git a/mfapi/apicalls/device_get_updates.c b/mfapi/apicalls/device_get_updates.c index 664cadf..8f5333c 100644 --- a/mfapi/apicalls/device_get_updates.c +++ b/mfapi/apicalls/device_get_updates.c @@ -82,6 +82,11 @@ int mfconn_api_device_get_updates(mfconn * conn, const char *quickkey, quickkey, revision, target_revision); } + if (api_call == NULL) { + fprintf(stderr, "mfconn_create_signed_get failed\n"); + return -1; + } + http = http_create(); retval = http_get_buf(http, api_call, _decode_device_get_updates, (void *)patches); diff --git a/mfapi/apicalls/file_delete.c b/mfapi/apicalls/file_delete.c index f413c19..cab3bb5 100644 --- a/mfapi/apicalls/file_delete.c +++ b/mfapi/apicalls/file_delete.c @@ -45,6 +45,10 @@ int mfconn_api_file_delete(mfconn * conn, const char *quickkey) api_call = mfconn_create_signed_get(conn, 0, "file/delete.php", "?quick_key=%s" "&response_format=json", quickkey); + if (api_call == NULL) { + fprintf(stderr, "mfconn_create_signed_get failed\n"); + return -1; + } http = http_create(); retval = diff --git a/mfapi/apicalls/file_get_info.c b/mfapi/apicalls/file_get_info.c index 03868fe..5f7f110 100644 --- a/mfapi/apicalls/file_get_info.c +++ b/mfapi/apicalls/file_get_info.c @@ -58,6 +58,10 @@ int mfconn_api_file_get_info(mfconn * conn, mffile * file, api_call = mfconn_create_signed_get(conn, 0, "file/get_info.php", "?quick_key=%s" "&response_format=json", quickkey); + if (api_call == NULL) { + fprintf(stderr, "mfconn_create_signed_get failed\n"); + return -1; + } http = http_create(); retval = http_get_buf(http, api_call, _decode_file_get_info, file); diff --git a/mfapi/apicalls/file_get_links.c b/mfapi/apicalls/file_get_links.c index 1f03851..698afa4 100644 --- a/mfapi/apicalls/file_get_links.c +++ b/mfapi/apicalls/file_get_links.c @@ -56,6 +56,10 @@ int mfconn_api_file_get_links(mfconn * conn, mffile * file, api_call = mfconn_create_signed_get(conn, 0, "file/get_links.php", "?quick_key=%s" "&response_format=json", quickkey); + if (api_call == NULL) { + fprintf(stderr, "mfconn_create_signed_get failed\n"); + return -1; + } http = http_create(); retval = http_get_buf(http, api_call, _decode_file_get_links, file); diff --git a/mfapi/apicalls/folder_create.c b/mfapi/apicalls/folder_create.c index 48ccf0d..70526eb 100644 --- a/mfapi/apicalls/folder_create.c +++ b/mfapi/apicalls/folder_create.c @@ -59,6 +59,10 @@ int mfconn_api_folder_create(mfconn * conn, const char *parent, "?foldername=%s&response_format=json", name); } + if (api_call == NULL) { + fprintf(stderr, "mfconn_create_signed_get failed\n"); + return -1; + } http = http_create(); retval = http_get_buf(http, api_call, mfapi_decode_common, diff --git a/mfapi/apicalls/folder_delete.c b/mfapi/apicalls/folder_delete.c index f5b63d5..7bc41d7 100644 --- a/mfapi/apicalls/folder_delete.c +++ b/mfapi/apicalls/folder_delete.c @@ -46,6 +46,10 @@ int mfconn_api_folder_delete(mfconn * conn, const char *folderkey) "?folder_key=%s" "&response_format=json", folderkey); + if (api_call == NULL) { + fprintf(stderr, "mfconn_create_signed_get failed\n"); + return -1; + } http = http_create(); retval = http_get_buf(http, api_call, mfapi_decode_common, diff --git a/mfapi/apicalls/folder_get_content.c b/mfapi/apicalls/folder_get_content.c index de007be..2bed1f7 100644 --- a/mfapi/apicalls/folder_get_content.c +++ b/mfapi/apicalls/folder_get_content.c @@ -99,6 +99,10 @@ mfconn_api_folder_get_content(mfconn * conn, const int mode, "&response_format=json", folderkey, content_type); } + if (api_call == NULL) { + fprintf(stderr, "mfconn_create_signed_get failed\n"); + return -1; + } http = http_create(); if (mode == 0) diff --git a/mfapi/apicalls/folder_get_info.c b/mfapi/apicalls/folder_get_info.c index c674015..894868e 100644 --- a/mfapi/apicalls/folder_get_info.c +++ b/mfapi/apicalls/folder_get_info.c @@ -61,6 +61,10 @@ mfconn_api_folder_get_info(mfconn * conn, mffolder * folder, "&response_format=json", folderkey); } + if (api_call == NULL) { + fprintf(stderr, "mfconn_create_signed_get failed\n"); + return -1; + } http = http_create(); retval = http_get_buf(http, api_call, _decode_folder_get_info, folder); diff --git a/mfapi/apicalls/upload_patch.c b/mfapi/apicalls/upload_patch.c index 77d8fc2..ce17cfa 100644 --- a/mfapi/apicalls/upload_patch.c +++ b/mfapi/apicalls/upload_patch.c @@ -89,6 +89,10 @@ mfconn_api_upload_patch(mfconn * conn, const char *quickkey, "&quickkey=%s", source_hash, target_hash, target_size, quickkey); + if (api_call == NULL) { + fprintf(stderr, "mfconn_create_signed_get failed\n"); + return -1; + } custom_headers = curl_slist_append(custom_headers, "x-filename: dummy.patch"); diff --git a/mfapi/apicalls/upload_poll_upload.c b/mfapi/apicalls/upload_poll_upload.c index bc0d1c4..a90f714 100644 --- a/mfapi/apicalls/upload_poll_upload.c +++ b/mfapi/apicalls/upload_poll_upload.c @@ -56,6 +56,10 @@ mfconn_api_upload_poll_upload(mfconn * conn, const char *upload_key, "upload/poll_upload.php", "?response_format=json" "&key=%s", upload_key); + if (api_call == NULL) { + fprintf(stderr, "mfconn_create_unsigned_get failed\n"); + return -1; + } http = http_create(); retval = http_get_buf(http, api_call, _decode_upload_poll_upload, diff --git a/mfapi/apicalls/upload_simple.c b/mfapi/apicalls/upload_simple.c index 4689ae4..feb4404 100644 --- a/mfapi/apicalls/upload_simple.c +++ b/mfapi/apicalls/upload_simple.c @@ -90,7 +90,10 @@ mfconn_api_upload_simple(mfconn * conn, const char *folderkey, "?response_format=json" "&folder_key=%s", folderkey); } - + if (api_call == NULL) { + fprintf(stderr, "mfconn_create_signed_get failed\n"); + return -1; + } // make sure that we are at the beginning of the file rewind(fh); diff --git a/mfapi/apicalls/user_get_info.c b/mfapi/apicalls/user_get_info.c index 7513f1b..290511f 100644 --- a/mfapi/apicalls/user_get_info.c +++ b/mfapi/apicalls/user_get_info.c @@ -40,6 +40,10 @@ int mfconn_api_user_get_info(mfconn * conn) api_call = mfconn_create_signed_get(conn, 0, "user/get_info.php", "?response_format=json"); + if (api_call == NULL) { + fprintf(stderr, "mfconn_create_signed_get failed\n"); + return -1; + } http = http_create(); retval = http_get_buf(http, api_call, _decode_user_get_info, NULL); diff --git a/mfapi/mfconn.c b/mfapi/mfconn.c index b81818b..a26e5e6 100644 --- a/mfapi/mfconn.c +++ b/mfapi/mfconn.c @@ -225,18 +225,24 @@ const char *mfconn_create_unsigned_get(mfconn * conn, int ssl, int api_len; va_list ap; - if (conn == NULL) + if (conn == NULL) { + fprintf(stderr, "conn cannot be NULL\n"); return NULL; - if (conn->server == NULL) + } + if (conn->server == NULL) { + fprintf(stderr, "server cannot be NULL\n"); return NULL; - + } // make sure the api (ex: user/get_info.php) is sane - if (api == NULL) + if (api == NULL) { + fprintf(stderr, "api call cannot be NULL\n"); return NULL; + } api_len = strlen(api); - if (api_len < 3) + if (api_len < 3) { + fprintf(stderr, "api call length cannot be less than 3\n"); return NULL; - + } // calculate how big of a buffer we need va_start(ap, fmt); api_args_len = (vsnprintf(NULL, 0, fmt, ap) + 1); // + 1 for NULL @@ -252,8 +258,10 @@ const char *mfconn_create_unsigned_get(mfconn * conn, int ssl, va_end(ap); // correct user error of trailing slash - if (api[api_len - 1] == '/') + if (api[api_len - 1] == '/') { + fprintf(stderr, "api call must not end with slash\n"); return NULL; + } api_request = strdup_printf("%s//%s/api/%s", (ssl ? "https:" : "http:"), conn->server, api); @@ -290,22 +298,32 @@ const char *mfconn_create_signed_get(mfconn * conn, int ssl, int api_len; va_list ap; - if (conn == NULL) + if (conn == NULL) { + fprintf(stderr, "conn cannot be NULL\n"); return NULL; - if (conn->server == NULL) + } + if (conn->server == NULL) { + fprintf(stderr, "server cannot be NULL\n"); return NULL; - if (conn->secret_time == NULL) + } + if (conn->secret_time == NULL) { + fprintf(stderr, "secret_time cannot be NULL\n"); return NULL; - if (conn->session_token == NULL) + } + if (conn->session_token == NULL) { + fprintf(stderr, "session_token cannot be NULL\n"); return NULL; - + } // make sure the api (ex: user/get_info.php) is sane - if (api == NULL) + if (api == NULL) { + fprintf(stderr, "api name cannot be NULL\n"); return NULL; + } api_len = strlen(api); - if (api_len < 3) + if (api_len < 3) { + fprintf(stderr, "api name length cannot be less than 3\n"); return NULL; - + } // calculate how big of a buffer we need va_start(ap, fmt); api_args_len = (vsnprintf(NULL, 0, fmt, ap) + 1); // + 1 for NULL @@ -328,8 +346,10 @@ const char *mfconn_create_signed_get(mfconn * conn, int ssl, free(session_token); // correct user error of trailing slash - if (api[api_len - 1] == '/') + if (api[api_len - 1] == '/') { + fprintf(stderr, "api name cannot end with slash\n"); return NULL; + } api_request = strdup_printf("%s//%s/api/%s", (ssl ? "https:" : "http:"), conn->server, api);