2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "http_private.h"
20 struct curl_slist* _get_header_list(http_transaction_h http_transaction)
22 __http_transaction_h *transaction = (__http_transaction_h *)http_transaction;
23 __http_header_h *header = transaction->header;
25 gchar* header_str = NULL;
28 gpointer value = NULL;
30 if (!header->hash_table)
33 g_hash_table_iter_init(&iter, header->hash_table);
35 while (g_hash_table_iter_next(&iter, &key, &value)) {
36 header_str = (gchar *)malloc(sizeof(gchar) * (strlen(key) + 1 + 1 + strlen(value) + 1));
37 sprintf(header_str, "%s: %s", (gchar*)key, (gchar*)value);
38 DBG("Header Field: %s\n", header_str);
39 header->header_list = curl_slist_append(header->header_list, header_str);
43 return header->header_list;
46 API int http_transaction_header_add_field(http_transaction_h http_transaction, const char *field_name, const char* field_value)
48 _retvm_if(_http_is_init() == false, HTTP_ERROR_INVALID_OPERATION,
49 "http isn't initialized");
50 _retvm_if(http_transaction == NULL, HTTP_ERROR_INVALID_PARAMETER,
51 "parameter(http_transaction) is NULL\n");
52 _retvm_if(field_name == NULL, HTTP_ERROR_INVALID_PARAMETER,
53 "parameter(field_name) is NULL\n");
54 _retvm_if(field_value == NULL, HTTP_ERROR_INVALID_PARAMETER,
55 "parameter(field_value) is NULL\n");
57 __http_transaction_h *transaction = (__http_transaction_h *)http_transaction;
58 __http_header_h *header = transaction->header;
60 if (!header->hash_table)
61 header->hash_table = g_hash_table_new(g_str_hash, g_str_equal);
63 g_hash_table_insert(header->hash_table, g_strdup(field_name), g_strdup(field_value));
65 return HTTP_ERROR_NONE;
68 API int http_transaction_header_remove_field(http_transaction_h http_transaction, const char *field_name)
70 _retvm_if(_http_is_init() == false, HTTP_ERROR_INVALID_OPERATION,
71 "http isn't initialized");
72 _retvm_if(http_transaction == NULL, HTTP_ERROR_INVALID_PARAMETER,
73 "parameter(http_transaction) is NULL\n");
74 _retvm_if(field_name == NULL, HTTP_ERROR_INVALID_PARAMETER,
75 "parameter(field_name) is NULL\n");
77 __http_transaction_h *transaction = (__http_transaction_h *)http_transaction;
78 __http_header_h *header = transaction->header;
79 gpointer orig_key = NULL;
80 gpointer orig_value = NULL;
82 _retvm_if(header->hash_table == NULL, HTTP_ERROR_INVALID_OPERATION,
83 "There are no custom header\n");
85 g_hash_table_lookup_extended(header->hash_table, field_name, &orig_key, &orig_value);
86 if (g_hash_table_remove(header->hash_table, field_name)) {
93 return HTTP_ERROR_NONE;
95 ERR("field_name doesn't exist!!");
96 return HTTP_ERROR_INVALID_OPERATION;
100 API int http_transaction_header_get_field_value(http_transaction_h http_transaction, const char *field_name, char **field_value)
102 _retvm_if(_http_is_init() == false, HTTP_ERROR_INVALID_OPERATION,
103 "http isn't initialized");
104 _retvm_if(http_transaction == NULL, HTTP_ERROR_INVALID_PARAMETER,
105 "parameter(http_transaction) is NULL\n");
106 _retvm_if(field_name == NULL, HTTP_ERROR_INVALID_PARAMETER,
107 "parameter(field_name) is NULL\n");
108 _retvm_if(field_value == NULL, HTTP_ERROR_INVALID_PARAMETER,
109 "parameter(field_value) is NULL\n");
111 __http_transaction_h *transaction = (__http_transaction_h *)http_transaction;
112 __http_header_h *header = transaction->header;
115 if (header->hash_table == NULL) {
117 return HTTP_ERROR_INVALID_OPERATION;
120 value = g_hash_table_lookup(header->hash_table, field_name);
122 ERR("filed_name doesn't exist!!");
123 return HTTP_ERROR_INVALID_OPERATION;
125 *field_value = g_strdup(value);
127 return HTTP_ERROR_NONE;