From: pradeep kumar B Date: Tue, 2 Feb 2016 06:15:12 +0000 (+0530) Subject: [capi-http] Implemented the add/remove header fields X-Git-Tag: submit/tizen/20160511.063207~8 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=44b4b88fb4bed085af4f49bf75623a3806afdbb9;p=platform%2Fcore%2Fapi%2Fhttp.git [capi-http] Implemented the add/remove header fields Change-Id: I294fdcc1b45c88fc9a61639bddc4a46103f12f8e Signed-off-by: pradeep kumar B --- diff --git a/include/http_private.h b/include/http_private.h index 8bb26ee..f1c0821 100644 --- a/include/http_private.h +++ b/include/http_private.h @@ -84,6 +84,7 @@ static const int _MAX_HTTP_TRANSACTIONS_PER_SESSION_PIPE = 5; typedef struct { struct curl_slist *header_list; + GHashTable *hash_table; } __http_header_h; typedef struct { @@ -149,6 +150,7 @@ void print_curl_multi_errorCode(CURLMcode code); gchar* _get_http_method(http_method_e method); http_method_e _get_method(gchar* method); gchar* _get_proxy(); +struct curl_slist* _get_header_list(http_transaction_h http_transaction); #ifdef __cplusplus } diff --git a/packaging/capi-network-http.spec b/packaging/capi-network-http.spec index d63b1c4..250a093 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.1 +Version: 0.0.2 Release: 0 Group: System/Network License: Apache-2.0 diff --git a/src/http_header.c b/src/http_header.c index 283a819..aa465cd 100644 --- a/src/http_header.c +++ b/src/http_header.c @@ -15,4 +15,84 @@ */ #include "http.h" -#include "http_private.h" \ No newline at end of file +#include "http_private.h" + +struct curl_slist* _get_header_list(http_transaction_h http_transaction) +{ + __http_transaction_h *transaction = (__http_transaction_h *)http_transaction; + __http_header_h *header = transaction->header; + + gchar* header_str = NULL; + GHashTableIter iter; + gpointer key = NULL; + gpointer value = NULL; + gint size = 0; + + size = g_hash_table_size(header->hash_table); + DBG("Header table Size: %d\n", size); + + g_hash_table_iter_init(&iter, header->hash_table); + + while(g_hash_table_iter_next (&iter, &key, &value)) { + header_str = (gchar *)malloc(sizeof(gchar) * (strlen(key) + 1 + 1 + strlen(value) + 1) ); + sprintf(header_str, "%s: %s", (gchar*)key, (gchar*)value); + DBG("Header Field: %s\n", header_str); + header->header_list = curl_slist_append(header->header_list, header_str); + free(header_str); + } + + return header->header_list; +} + +API int http_header_add_field(http_transaction_h http_transaction, const char *field_name, const char* field_value) +{ + _retvm_if(http_transaction == NULL, HTTP_ERROR_INVALID_PARAMETER, + "parameter(http_transaction) is NULL\n"); + _retvm_if(field_name == NULL, HTTP_ERROR_INVALID_PARAMETER, + "parameter(field_name) is NULL\n"); + _retvm_if(field_value == NULL, HTTP_ERROR_INVALID_PARAMETER, + "parameter(field_value) is NULL\n"); + + __http_transaction_h *transaction = (__http_transaction_h *)http_transaction; + __http_header_h *header = transaction->header; + + if (!header->hash_table) { + header->hash_table = g_hash_table_new(g_str_hash, g_str_equal); + } + + g_hash_table_insert(header->hash_table, (char*)field_name, (char*)field_value); + + return HTTP_ERROR_NONE; +} + +API int http_header_remove_field(http_transaction_h http_transaction, const char *field_name) +{ + _retvm_if(http_transaction == NULL, HTTP_ERROR_INVALID_PARAMETER, + "parameter(http_transaction) is NULL\n"); + _retvm_if(field_name == NULL, HTTP_ERROR_INVALID_PARAMETER, + "parameter(field_name) is NULL\n"); + + __http_transaction_h *transaction = (__http_transaction_h *)http_transaction; + __http_header_h *header = transaction->header; + + g_hash_table_remove(header->hash_table, field_name); + + return HTTP_ERROR_NONE; +} + +API int http_header_get_field_value(http_transaction_h http_transaction, const char *field_name, char **field_value) +{ + _retvm_if(http_transaction == NULL, HTTP_ERROR_INVALID_PARAMETER, + "parameter(http_transaction) is NULL\n"); + _retvm_if(field_name == NULL, HTTP_ERROR_INVALID_PARAMETER, + "parameter(field_name) is NULL\n"); + _retvm_if(field_value == NULL, HTTP_ERROR_INVALID_PARAMETER, + "parameter(field_value) is NULL\n"); + + __http_transaction_h *transaction = (__http_transaction_h *)http_transaction; + __http_header_h *header = transaction->header; + + *field_value = g_hash_table_lookup(header->hash_table, field_name); + + return HTTP_ERROR_NONE; +} diff --git a/src/http_transaction.c b/src/http_transaction.c index c33d535..0bd8320 100644 --- a/src/http_transaction.c +++ b/src/http_transaction.c @@ -85,6 +85,7 @@ int _transaction_submit(gpointer user_data) CURLMcode ret = CURLM_OK; gchar *proxy_addr = NULL; + struct curl_slist* header_list = NULL; transaction->easy_handle = curl_easy_init(); @@ -110,6 +111,10 @@ int _transaction_submit(gpointer user_data) if (transaction->interface_name) curl_easy_setopt(transaction->easy_handle, CURLOPT_INTERFACE, transaction->interface_name); + header_list = _get_header_list(transaction); + if (header_list) + curl_easy_setopt(transaction->easy_handle, CURLOPT_HTTPHEADER, header_list); + if (request->encoding) curl_easy_setopt(transaction->easy_handle, CURLOPT_ENCODING, request->encoding); @@ -226,6 +231,9 @@ API int http_open_transaction(http_session_h http_session, http_method_e method, transaction->request->body = NULL; transaction->request->http_version = HTTP_VERSION_1_1; + transaction->header->header_list = NULL; + transaction->header->hash_table = NULL; + transaction->thread = NULL; *http_transaction = (http_transaction_h)transaction; @@ -317,7 +325,20 @@ API int http_transaction_close(http_transaction_h http_transaction) free(request); } free(response); - free(header); + + if (header) { + if (header->header_list != NULL) { + curl_slist_free_all(header->header_list); + header->header_list = NULL; + } + + if (header->hash_table != NULL) { + g_hash_table_destroy(header->hash_table); + header->hash_table = NULL; + } + + free(header); + } free(transaction); transaction = NULL; diff --git a/test/http_test.c b/test/http_test.c index 87a2b4d..49b7feb 100644 --- a/test/http_test.c +++ b/test/http_test.c @@ -117,6 +117,26 @@ void transaction_aborted_callback(int reason) } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +int add_http_header(http_transaction_h transaction_handle) +{ + http_header_add_field(transaction_handle, "Connection", "close"); + http_header_add_field(transaction_handle, "Accept-Charset", "ISO-8859-1,UTF-8;q=0.7,*;q=0.7"); + http_header_add_field(transaction_handle, "Cache-Control", "no-cache"); + http_header_add_field(transaction_handle, "Accept-Language", "en-us;q=0.3"); + + return 0; +} + +int remove_http_header(http_transaction_h transaction_handle) +{ + http_header_remove_field(transaction_handle, "Connection"); + http_header_remove_field(transaction_handle, "Accept-Charset"); + http_header_remove_field(transaction_handle, "Cache-Control"); + http_header_remove_field(transaction_handle, "Accept-Language"); + + return 0; +} + http_transaction_h create_http_request(http_session_h session_handle, gchar* host_url, http_transaction_header_cb header_cb, http_transaction_body_cb body_cb, http_transaction_write_cb write_cb, http_transaction_completed_cb completed_cb, http_transaction_aborted_cb aborted_cb) { @@ -129,6 +149,7 @@ http_transaction_h create_http_request(http_session_h session_handle, gchar* hos (http_transaction_aborted_cb)aborted_cb, &transaction_handle); http_request_set_uri(transaction_handle, host_url); + add_http_header(transaction_handle); return transaction_handle; } @@ -146,7 +167,6 @@ int main() http_transaction_h transaction_handle1 = NULL; http_transaction_h transaction_handle2 = NULL; - DBG("########################## main:Enter#########################################\n"); mainloop = g_main_loop_new(NULL, FALSE); @@ -165,6 +185,9 @@ int main() g_main_loop_run(mainloop); + remove_http_header(transaction_handle1); + remove_http_header(transaction_handle2); + http_transaction_close(transaction_handle1); transaction_handle1 = NULL; http_transaction_close(transaction_handle2);