[Bug fix] Add error exception for header table
[platform/core/api/http.git] / src / http_header.c
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "http.h"
18 #include "http_private.h"
19
20 struct curl_slist* _get_header_list(http_transaction_h http_transaction)
21 {
22         __http_transaction_h *transaction = (__http_transaction_h *)http_transaction;
23         __http_header_h *header = transaction->header;
24
25         gchar* header_str = NULL;
26         GHashTableIter iter;
27         gpointer key = NULL;
28         gpointer value = NULL;
29
30         if (!header->hash_table)
31                 return NULL;
32
33         g_hash_table_iter_init(&iter, header->hash_table);
34
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);
40                 free(header_str);
41         }
42
43         return header->header_list;
44 }
45
46 API int http_transaction_header_add_field(http_transaction_h http_transaction, const char *field_name, const char* field_value)
47 {
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");
56
57         __http_transaction_h *transaction = (__http_transaction_h *)http_transaction;
58         __http_header_h *header = transaction->header;
59
60         if (!header->hash_table)
61                 header->hash_table = g_hash_table_new(g_str_hash, g_str_equal);
62
63         g_hash_table_insert(header->hash_table, g_strdup(field_name), g_strdup(field_value));
64
65         return HTTP_ERROR_NONE;
66 }
67
68 API int http_transaction_header_remove_field(http_transaction_h http_transaction, const char *field_name)
69 {
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");
76
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;
81
82         _retvm_if(header->hash_table == NULL, HTTP_ERROR_INVALID_OPERATION,
83                         "There are no custom header\n");
84
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)) {
87                 if (orig_key)
88                         g_free(orig_key);
89
90                 if (orig_value)
91                         g_free(orig_value);
92
93                 return HTTP_ERROR_NONE;
94         } else {
95                 ERR("field_name doesn't exist!!");
96                 return HTTP_ERROR_INVALID_OPERATION;
97         }
98 }
99
100 API int http_transaction_header_get_field_value(http_transaction_h http_transaction, const char *field_name, char **field_value)
101 {
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");
110
111         __http_transaction_h *transaction = (__http_transaction_h *)http_transaction;
112         __http_header_h *header = transaction->header;
113         const char *value;
114
115         if (header->hash_table == NULL) {
116                 *field_value = NULL;
117                 return HTTP_ERROR_INVALID_OPERATION;
118         }
119
120         value = g_hash_table_lookup(header->hash_table, field_name);
121         if (value == NULL) {
122                 ERR("filed_name doesn't exist!!");
123                 return HTTP_ERROR_INVALID_OPERATION;
124         }
125         *field_value = g_strdup(value);
126
127         return HTTP_ERROR_NONE;
128 }