Initial version
[platform/core/service/cloud/ua-client.git] / src / ua_http.cpp
1 #include <glib.h>
2 #include <curl/curl.h>
3
4 #include <ua_client.h>
5
6 #define UA_FIRMWARE_DOWNLOAD_PATH  "/opt/usr/data/fota/"
7 #define UA_FIRMWARE_DOWNLOAD_FILE  UA_FIRMWARE_DOWNLOAD_PATH"delta.tar"
8
9
10 static size_t _gather_data(void *downloaded_data,
11                 size_t size,
12                 size_t nmemb,
13                 void *user_data)
14 {
15         size_t total_size = size * nmemb;
16         g_byte_array_append((GByteArray *)user_data, (const unsigned char *) downloaded_data, total_size);
17         return total_size;
18 }
19
20
21 void _curl_set_response(CURL *curl,
22                 GByteArray *response_header,
23                 GByteArray *response_body,
24                 char **res_header,
25                 char **res_body,
26                 void *user_data)
27 {
28         CURLcode curl_ret_code;
29
30         long response = 0;
31         curl_ret_code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
32         if (CURLE_OK != curl_ret_code)
33                 return;
34
35         char *tmp_header = g_strndup((const gchar *)response_header->data, response_header->len);
36         char *tmp_body = g_strndup((const gchar *)response_body->data, response_body->len);
37
38         *res_header = tmp_header;
39         *res_body = tmp_body;
40 }
41
42
43 void _curl_set_common_option(CURL *curl,
44                 const char *url,
45                 GByteArray **response_header_ptr,
46                 GByteArray **response_body_ptr)
47 {
48         UA_LOG("_curl_set_common_option()");
49         CURLcode curl_ret_code;
50
51         UA_LOG("set URL = [%s]", url);
52         curl_ret_code = curl_easy_setopt(curl, CURLOPT_URL, url);
53         if (CURLE_OK != curl_ret_code) {
54                 UA_LOG("curl_easy_setopt: CURLOPT_URL failed!! curl_ret_code[%d]", curl_ret_code);
55         }
56
57         GByteArray *response_header_data = g_byte_array_new();
58         curl_ret_code = curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, _gather_data);
59         if (CURLE_OK != curl_ret_code) {
60                 UA_LOG("curl_easy_setopt: CURLOPT_HEADERFUNCTION failed!! curl_ret_code[%d]", curl_ret_code);
61         }
62
63         curl_ret_code = curl_easy_setopt(curl, CURLOPT_HEADERDATA, response_header_data);
64         if (CURLE_OK != curl_ret_code) {
65                 UA_LOG("curl_easy_setopt: CURLOPT_HEADERDATA failed!! curl_ret_code[%d]", curl_ret_code);
66         }
67
68         GByteArray *response_body_data = g_byte_array_new();
69
70         curl_ret_code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _gather_data);
71         if (CURLE_OK != curl_ret_code) {
72                 UA_LOG("curl_easy_setopt: CURLOPT_WRITEFUNCTION failed!! curl_ret_code[%d]", curl_ret_code);
73         }
74
75         curl_ret_code = curl_easy_setopt(curl, CURLOPT_WRITEDATA, response_body_data);
76         if (CURLE_OK != curl_ret_code) {
77                 UA_LOG("curl_easy_setopt: CURLOPT_WRITEDATA failed!! curl_ret_code[%d]", curl_ret_code);
78         }
79
80         curl_ret_code = curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
81         if (CURLE_OK != curl_ret_code) {
82                 UA_LOG("curl_easy_setopt: CURLOPT_NOPROGRESS failed!! curl_ret_code[%d]", curl_ret_code);
83         }
84
85         *response_header_ptr = response_header_data;
86         *response_body_ptr = response_body_data;
87
88 #if 0
89         curl_ret_code = curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
90         if (CURLE_OK != curl_ret_code) {
91                 UA_LOG("curl_easy_setopt: CURLOPT_VERBOSE failed!! curl_ret_code[%d]", curl_ret_code);
92         }
93
94         curl_ret_code = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
95         if (CURLE_OK != curl_ret_code) {
96                 UA_LOG("curl_easy_setopt: CURLOPT_SSL_VERIFYPEER failed!! curl_ret_code[%d]", curl_ret_code);
97         }
98
99         curl_ret_code = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, FALSE);
100         if (CURLE_OK != curl_ret_code) {
101                 UA_LOG("curl_easy_setopt: CURLOPT_SSL_VERIFYHOST failed!! curl_ret_code[%d]", curl_ret_code);
102         }
103
104         curl_ret_code = curl_easy_setopt(curl, CURLOPT_CERTINFO, 0L);
105         if (CURLE_OK != curl_ret_code) {
106                 UA_LOG("curl_easy_setopt: CURLOPT_CERTINFO failed!! curl_ret_code[%d]", curl_ret_code);
107         }
108 #endif
109 }
110
111
112 static void _curl_set_request_headers(CURL *curl)
113 {
114         struct curl_slist *header = NULL;
115
116         char *tmp_header = NULL;
117
118         tmp_header = g_strconcat("Content-Type: ", "application/json", NULL);
119         UA_LOG("header=[%s]", tmp_header);
120         header = curl_slist_append(header, tmp_header);
121         g_free(tmp_header);
122
123         curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
124 }
125
126
127 int ua_http_send_request(rest_req_type type, char *req_url, char **res_header, char **res_body)
128 {
129         UA_LOG("Enter http_send_request()");
130
131         CURL *curl;
132         GByteArray *response_header = NULL;
133         GByteArray *response_body= NULL;
134         CURLcode error_code;
135         int ret = 0;
136
137         // Start a libcurl easy session
138         curl = curl_easy_init();
139
140         UA_LOG("curl_easy_init()");
141
142         _curl_set_request_headers(curl);
143
144         if (type == UA_HTTP_GET) {
145                 curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
146         } else if (type == UA_HTTP_POST) {
147                 curl_easy_setopt(curl, CURLOPT_HTTPPOST, 1);
148         } else {
149                 return -1;
150         }
151
152         _curl_set_common_option(curl, (const char *)req_url, &response_header, &response_body);
153
154         UA_LOG("Start curl_easy_perform......");
155         error_code = curl_easy_perform(curl);
156         UA_LOG("curl_easy_perform(curl): %s (%d)", curl_easy_strerror(error_code), error_code);
157
158         if (error_code == CURLE_ABORTED_BY_CALLBACK) {
159                 ret = -1;
160                 goto _END_OF_FUNC_;
161         } else if (error_code != CURLE_OK) {
162                 ret = -1;
163                 goto _END_OF_FUNC_;
164         }
165
166         _curl_set_response(curl, response_header, response_body, res_header, res_body, NULL);
167
168 _END_OF_FUNC_:
169         if (response_header) {
170                 g_byte_array_free(response_header, TRUE);
171         }
172         if (response_body) {
173                 g_byte_array_free(response_body, TRUE);
174         }
175
176         curl_easy_cleanup(curl);
177         return ret;
178 }
179
180
181 int ua_http_download_file(const char *download_url)
182 {
183         UA_LOG("http_download_file() enter");
184
185     if (!download_url)
186         return -1;
187
188     int ret = 0;
189     CURL *curl;
190     FILE *fp;
191     CURLcode error_code;
192
193     curl = curl_easy_init();
194     if (curl)
195     {
196         fp = fopen(UA_FIRMWARE_DOWNLOAD_FILE, "wb");
197         curl_easy_setopt(curl, CURLOPT_URL, download_url);
198         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
199         curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
200         error_code = curl_easy_perform(curl);
201         UA_LOG("curl_easy_perform() [%d]", error_code);
202         curl_easy_cleanup(curl);
203         fclose(fp);
204
205         if (error_code != CURLE_OK) {
206                 remove(UA_FIRMWARE_DOWNLOAD_FILE);
207                 ret = -1;
208         }
209     } else {
210         ret = -1;
211     }
212
213     return ret;
214 }