From 633726e37f081f36e9e095c833567c39fbdc995f Mon Sep 17 00:00:00 2001 From: Seonah Moon Date: Fri, 4 Nov 2016 14:11:56 +0900 Subject: [PATCH] Add new APIs - Sets the upload file - Cancels the transaction Change-Id: I66991035e12bf4e8bd5c5512dc5431661f86f903 Signed-off-by: Seonah Moon --- include/http.h | 31 +++++++++++++++++++++++ include/http_internal.h | 14 ----------- include/http_private.h | 1 + packaging/capi-network-http.spec | 2 +- src/http_session.c | 4 +++ src/http_transaction.c | 16 +++++++++++- test/http_test.c | 54 ++++++++++++++++++++++++++++++++++++++-- 7 files changed, 104 insertions(+), 18 deletions(-) diff --git a/include/http.h b/include/http.h index 274b13b..e7ea496 100644 --- a/include/http.h +++ b/include/http.h @@ -125,6 +125,7 @@ typedef enum { HTTP_ERROR_COULDNT_CONNECT = TIZEN_ERROR_HTTP|0x07, /**< Couldn't Connect to host */ HTTP_ERROR_OPERATION_TIMEDOUT = TIZEN_ERROR_HTTP|0x28, /**< Timeout */ HTTP_ERROR_SSL_CONNECT_ERROR = TIZEN_ERROR_HTTP|0x35, /**< SSL Error */ + HTTP_ERROR_CANCELED = TIZEN_ERROR_CANCELED, /**< Operation Canceled */ HTTP_ERROR_NOT_SUPPORTED = TIZEN_ERROR_NOT_SUPPORTED, /**< API is not supported */ } http_error_code_e; @@ -588,6 +589,21 @@ int http_transaction_resume(http_transaction_h http_transaction); int http_transaction_pause(http_transaction_h http_transaction, http_pause_type_e pause_type); /** + * @brief Cancels the transaction. + * @details This function cancels the transaction.\n + * The aborted callback is invoked after using it. + * @since_tizen 3.0 + * @param[in] http_transaction The http transaction handle + * @return 0 on success, otherwise negative error value + * @retval #HTTP_ERROR_NONE Successful + * @retval #HTTP_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #HTTP_ERROR_INVALID_OPERATION Invalid operation + * @retval #HTTP_ERROR_OPERATION_FAILED Operation failed + * @retval #HTTP_ERROR_NOT_SUPPORTED Not Supported + */ +int http_transaction_cancel(http_transaction_h http_transaction); + +/** * @brief Sets ready to write event for a transaction. * @since_tizen 3.0 * @param[in] http_transaction The http transaction handle @@ -875,6 +891,20 @@ int http_transaction_request_get_cookie(http_transaction_h http_transaction, cha int http_transaction_request_write_body(http_transaction_h http_transaction, const char *body); /** + * @brief Sets the file path for uploading a file. + * @since_tizen 3.0 + * @remarks It is used with #HTTP_METHOD_PUT + * @param[in] http_transaction The http transaction handle + * @param[in] file_path The path for file + * @return 0 on success, otherwise negative error value + * @retval #HTTP_ERROR_NONE Successful + * @retval #HTTP_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #HTTP_ERROR_INVALID_OPERATION Invalid operation + * @retval #HTTP_ERROR_NOT_SUPPORTED Not Supported + */ +int http_transaction_request_set_upload_file(http_transaction_h http_transaction, const char *file_path); + +/** * @} */ @@ -1018,6 +1048,7 @@ int http_transaction_get_http_auth_scheme(http_transaction_h http_transaction, h */ int http_transaction_get_realm(http_transaction_h http_transaction, char **realm); + /** * @} */ diff --git a/include/http_internal.h b/include/http_internal.h index 8b81451..1ddafdc 100644 --- a/include/http_internal.h +++ b/include/http_internal.h @@ -8,20 +8,6 @@ typedef enum { } http_formdata_type_e; /** - * @brief Sets file path for uploading a file. - * @since_tizen 3.0 - * @remarks It is used with #HTTP_METHOD_PUT - * @param[in] http_transaction The http transaction handle - * @param[in] file_path The path for file - * @return 0 on success, otherwise negative error value - * @retval #HTTP_ERROR_NONE Successful - * @retval #HTTP_ERROR_INVALID_PARAMETER Invalid parameter - * @retval #HTTP_ERROR_INVALID_OPERATION Invalid operation - * @retval #HTTP_ERROR_NOT_SUPPORTED Not Supported - */ -int http_transaction_request_set_upload_file(http_transaction_h http_transaction, const char *file_path); - -/** * @brief Adds the multipart/form-data. * @since_tizen 3.0 * @remarks It is used with #HTTP_METHOD_POST diff --git a/include/http_private.h b/include/http_private.h index dfab58f..078ebec 100644 --- a/include/http_private.h +++ b/include/http_private.h @@ -154,6 +154,7 @@ typedef struct { bool verify_peer; gchar *ca_path; gchar error[CURL_ERROR_SIZE]; + bool cancel; /*Authentication Info*/ bool auth_required; diff --git a/packaging/capi-network-http.spec b/packaging/capi-network-http.spec index f35dbbe..9235e48 100644 --- a/packaging/capi-network-http.spec +++ b/packaging/capi-network-http.spec @@ -1,6 +1,6 @@ Name: capi-network-http Summary: Http Framework -Version: 0.0.13 +Version: 0.0.14 Release: 0 Group: System/Network License: Apache-2.0 diff --git a/src/http_session.c b/src/http_session.c index 74bac8c..832b603 100644 --- a/src/http_session.c +++ b/src/http_session.c @@ -91,6 +91,10 @@ void _check_curl_multi_status(gpointer user_data) if (transaction->aborted_cb) transaction->aborted_cb(transaction, HTTP_ERROR_OPERATION_TIMEDOUT, transaction->aborted_user_data); break; + case CURLE_ABORTED_BY_CALLBACK: + if (transaction->aborted_cb) + transaction->aborted_cb(transaction, HTTP_ERROR_CANCELED, transaction->aborted_user_data); + break; default: if (transaction->aborted_cb) transaction->aborted_cb(transaction, HTTP_ERROR_OPERATION_FAILED, transaction->aborted_user_data); diff --git a/src/http_transaction.c b/src/http_transaction.c index 8757690..ff61814 100644 --- a/src/http_transaction.c +++ b/src/http_transaction.c @@ -171,7 +171,7 @@ int __progress_cb(void *user_data, double dltotal, double dlnow, double ultotal, transaction->progress_cb(transaction, total_download, current_download, total_upload, current_upload, transaction->progress_user_data); - return 0; + return transaction->cancel; } //LCOV_EXCL_START @@ -743,6 +743,20 @@ API int http_transaction_resume(http_transaction_h http_transaction) } //LCOV_EXCL_STOP +API int http_transaction_cancel(http_transaction_h http_transaction) +{ + _retvm_if(_http_is_init() == false, HTTP_ERROR_INVALID_OPERATION, + "http isn't initialized"); + _retvm_if(http_transaction == NULL, HTTP_ERROR_INVALID_PARAMETER, + "parameter(http_transaction) is NULL\n"); + + __http_transaction_h *transaction = (__http_transaction_h *)http_transaction; + + transaction->cancel = true; + + return HTTP_ERROR_NONE; +} + API int http_transaction_set_progress_cb(http_transaction_h http_transaction, http_transaction_progress_cb progress_cb, void* user_data) { _retvm_if(_http_is_init() == false, HTTP_ERROR_INVALID_OPERATION, diff --git a/test/http_test.c b/test/http_test.c index 9d15c99..2130823 100644 --- a/test/http_test.c +++ b/test/http_test.c @@ -33,6 +33,8 @@ FILE* fp1 = NULL; FILE* fp2 = NULL; http_session_h session = NULL; +int cancel = 0; + void _register_callbacks(http_transaction_h transaction); void _write_message_body(http_transaction_h transaction, const char *file_path); @@ -105,13 +107,21 @@ void __transaction_aborted_cb(http_transaction_h transaction, int reason, void * { PRG("transaction_aborted_cb", transaction); DBG("aborted reason: %d\n", reason); + + int ret = http_transaction_destroy(transaction); + if (ret == HTTP_ERROR_NONE) DBG("Success to close transaction\n"); + else DBG("Fail to close transaction\n"); + + transaction = NULL; + cancel = 0; } void __transaction_progress_cb(http_transaction_h transaction, double download_total, double download_now, double upload_total, double upload_now, void *user_data) { PRG("__transaction_progress_cb", transaction); - DBG("Download ====>: DOWN(%lf/%lf)\n", download_total, download_now); - DBG("Upload ====>: UP(%lf/%lf)\n", upload_total, upload_now); + + if (cancel) + http_transaction_cancel(transaction); } void _register_callbacks(http_transaction_h transaction) @@ -424,6 +434,42 @@ int test_post_chunk(void) return 1; } +int test_cancel(void) +{ + char uri[1024]; + int ret; + http_transaction_h transaction = NULL; + http_method_e method; + + cancel = 1; + + printf("Input uri: "); + ret = scanf("%1023s", uri); + + ret = http_session_open_transaction(session, HTTP_METHOD_GET, &transaction); + if (ret != 0) { + ERR("Fail to open transaction", ret); + return 0; + } + + http_transaction_request_get_method(transaction, &method); + ret = http_transaction_request_set_uri(transaction, uri); + if (ret != 0) { + ERR("Fail to set URI", ret); + return 0; + } + + _register_callbacks(transaction); + ret = http_transaction_submit(transaction); + + if (ret != 0) { + ERR("Fail to submit transaction", ret); + return 0; + } + + return 1; +} + gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data) { int rv; @@ -450,6 +496,7 @@ gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data) printf("9 - Simple Authentication GET\n"); printf("a - Upload file (PUT)\n"); printf("b - Upload file (POST)\n"); + printf("c - Cancel\n"); printf("0 - Exit \n"); printf("ENTER - Show options menu.......\n"); } @@ -488,6 +535,9 @@ gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data) case 'b': rv = test_post_chunk(); break; + case 'c': + rv = test_cancel(); + break; } if (rv == 1) -- 2.7.4