2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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.
17 #include "cloud/http_request.h"
21 #include <curl/curl.h>
26 #define BASE_URL "http://son.tizen.online"
27 #define CONFIG_CLOUD_GROUP "cloud"
28 #define CONFIG_URL "url"
30 static size_t _response_write(void *ptr, size_t size, size_t nmemb, void *data);
32 int http_request_get(const char *url, char **response, long *response_code)
34 retvm_if(!url, -1, "GET request URL is NULL!");
35 retvm_if(!response, -1, "GET request response is null");
38 CURLcode res = CURLE_OK;
40 curl = curl_easy_init();
41 retvm_if(!curl, -1, "Failed to initialize curl!");
43 curl_easy_setopt(curl, CURLOPT_URL, url);
45 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER , 1);
46 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST , 1);
47 curl_easy_setopt(curl, CURLOPT_CAINFO, "ca-bundle.crt");
49 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _response_write);
50 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)response);
52 res = curl_easy_perform(curl);
53 if (res != CURLE_OK) {
54 _E("curl_easy_perform() failed: %s", curl_easy_strerror(res));
55 curl_easy_cleanup(curl);
60 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &_response_code);
62 *response_code = _response_code;
65 curl_easy_cleanup(curl);
70 int http_request_post(const char *url, const char *json, char **response, long *response_code)
72 retvm_if(!url, -1, "POST request URL is NULL!");
73 retvm_if(!json, -1, "POST request JSON message is NULL!");
75 CURL *curl = curl_easy_init();
76 retvm_if(!curl, -1, "Failed to initialize curl!");
78 char *_response = NULL;
79 struct curl_slist *headers = curl_slist_append(NULL, "Content-Type: application/json");
81 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
82 curl_easy_setopt(curl, CURLOPT_URL, url);
83 curl_easy_setopt(curl, CURLOPT_POST, 1L);
84 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json);
85 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _response_write);
86 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&_response);
88 CURLcode res = CURLE_OK;
89 res = curl_easy_perform(curl);
90 if (res != CURLE_OK) {
91 _E("curl_easy_perform() failed: %s", curl_easy_strerror(res));
92 curl_slist_free_all(headers);
93 curl_easy_cleanup(curl);
98 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &_response_code);
100 *response_code = _response_code;
104 *response = _response;
110 curl_slist_free_all(headers);
111 curl_easy_cleanup(curl);
116 static size_t _response_write(void *ptr, size_t size, size_t nmemb, void *data)
118 char **received = (char **)data;
119 size_t real_size = size * nmemb;
120 const char *response_msg = (const char *)ptr;
122 if (received && real_size > 0) {
124 char *temp = strndup(response_msg, real_size);
125 size_t length = strlen(*received) + strlen(temp);
126 char *new = g_malloc(length * sizeof(char));
127 snprintf(new, length, "%s%s", *received, temp);
133 *received = g_strndup((const char *)ptr, real_size);
137 _E("Failed to get response, response size : %lu", real_size);
143 char *http_request_get_url(char *default_api, char *api_key)
147 char *url_with_api = calloc(PATH_MAX, sizeof(char));
149 bool modified = config_get_string_or_set_default(CONFIG_CLOUD_GROUP, CONFIG_URL, BASE_URL, &url);
150 modified |= config_get_string_or_set_default(CONFIG_CLOUD_GROUP, api_key, default_api, &api);
152 snprintf(url_with_api, PATH_MAX, "%s%s", url, api);