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>
24 static size_t _response_write(void *ptr, size_t size, size_t nmemb, void *data);
26 int http_request_get(const char *url, char **response, long *response_code)
28 retvm_if(!url, -1, "GET request URL is NULL!");
29 retvm_if(!response, -1, "GET request response is null");
32 CURLcode res = CURLE_OK;
34 curl = curl_easy_init();
35 retvm_if(!curl, -1, "Failed to initialize curl!");
37 curl_easy_setopt(curl, CURLOPT_URL, url);
39 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER , 1);
40 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST , 1);
41 curl_easy_setopt(curl, CURLOPT_CAINFO, "ca-bundle.crt");
43 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _response_write);
44 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)response);
46 res = curl_easy_perform(curl);
47 if (res != CURLE_OK) {
48 _E("curl_easy_perform() failed: %s", curl_easy_strerror(res));
49 curl_easy_cleanup(curl);
54 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &_response_code);
56 *response_code = _response_code;
59 curl_easy_cleanup(curl);
64 int http_request_post(const char *url, const char *json, char **response, long *response_code)
66 retvm_if(!url, -1, "POST request URL is NULL!");
67 retvm_if(!json, -1, "POST request JSON message is NULL!");
69 CURL *curl = curl_easy_init();
70 retvm_if(!curl, -1, "Failed to initialize curl!");
72 char *_response = NULL;
73 struct curl_slist *headers = curl_slist_append(NULL, "Content-Type: application/json");
75 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
76 curl_easy_setopt(curl, CURLOPT_URL, url);
77 curl_easy_setopt(curl, CURLOPT_POST, 1L);
78 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json);
79 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _response_write);
80 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&_response);
82 CURLcode res = CURLE_OK;
83 res = curl_easy_perform(curl);
84 if (res != CURLE_OK) {
85 _E("curl_easy_perform() failed: %s", curl_easy_strerror(res));
86 curl_slist_free_all(headers);
87 curl_easy_cleanup(curl);
92 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &_response_code);
94 *response_code = _response_code;
98 *response = _response;
104 curl_slist_free_all(headers);
105 curl_easy_cleanup(curl);
110 static size_t _response_write(void *ptr, size_t size, size_t nmemb, void *data)
112 char **received = (char **)data;
113 size_t real_size = size * nmemb;
114 const char *response_msg = (const char *)ptr;
116 if (received && real_size > 0) {
118 char *temp = strndup(response_msg, real_size);
119 size_t length = strlen(*received) + strlen(temp);
120 char *new = g_malloc(length * sizeof(char));
121 snprintf(new, length, "%s%s", *received, temp);
127 *received = g_strndup((const char *)ptr, real_size);
131 _E("Failed to get response, response size : %lu", real_size);