Cloud API added
[apps/native/gear-racing-car.git] / src / cloud / http_request.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://floralicense.org/license/
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 "cloud/http_request.h"
18 #include <glib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <curl/curl.h>
22 #include "log.h"
23
24 static size_t _response_write(void *ptr, size_t size, size_t nmemb, void *data);
25
26 int http_request_get(const char *url, char **response, long *response_code)
27 {
28     retvm_if(!url, -1, "GET request URL is NULL!");
29     retvm_if(!response, -1, "GET request response is null");
30
31     CURL *curl = NULL;
32     CURLcode res = CURLE_OK;
33
34     curl = curl_easy_init();
35     retvm_if(!curl, -1, "Failed to initialize curl!");
36
37     curl_easy_setopt(curl, CURLOPT_URL, url);
38     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _response_write);
39     curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)response);
40
41     res = curl_easy_perform(curl);
42     if (res != CURLE_OK) {
43         _E("curl_easy_perform() failed: %s", curl_easy_strerror(res));
44         curl_easy_cleanup(curl);
45         return -1;
46     }
47
48     long _response_code;
49     curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &_response_code);
50     if (_response_code) {
51         *response_code = _response_code;
52     }
53
54     curl_easy_cleanup(curl);
55
56     return 0;
57 }
58
59 int http_request_post(const char *url, const char *json, char **response, long *response_code)
60 {
61     retvm_if(!url, -1, "POST request URL is NULL!");
62     retvm_if(!json, -1, "POST request JSON message is NULL!");
63
64     CURL *curl = curl_easy_init();
65     retvm_if(!curl, -1, "Failed to initialize curl!");
66
67     char *_response = NULL;
68     struct curl_slist *headers = curl_slist_append(NULL, "Content-Type: application/json");
69
70     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
71     curl_easy_setopt(curl, CURLOPT_URL, url);
72     curl_easy_setopt(curl, CURLOPT_POST, 1L);
73     curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json);
74     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _response_write);
75     curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&_response);
76
77     CURLcode res = CURLE_OK;
78     res = curl_easy_perform(curl);
79     if (res != CURLE_OK) {
80         _E("curl_easy_perform() failed: %s", curl_easy_strerror(res));
81         curl_slist_free_all(headers);
82         curl_easy_cleanup(curl);
83         return -1;
84     }
85
86     long _response_code;
87     curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &_response_code);
88     if (response_code) {
89         *response_code = _response_code;
90     }
91
92     if (response) {
93         *response = _response;
94     }
95     else {
96         g_free(_response);
97     }
98
99     curl_slist_free_all(headers);
100     curl_easy_cleanup(curl);
101
102     return 0;
103 }
104
105 static size_t _response_write(void *ptr, size_t size, size_t nmemb, void *data)
106 {
107     char **received = (char **)data;
108     size_t real_size = size * nmemb;
109     const char *response_msg = (const char *)ptr;
110
111     if (received && real_size > 0) {
112         if (*received) {
113             char *temp = strndup(response_msg, real_size);
114             size_t length = strlen(*received) + strlen(temp);
115             char *new = g_malloc(length * sizeof(char));
116             snprintf(new, length, "%s%s", *received, temp);
117             g_free(temp);
118             g_free(*received);
119             *received = new;
120         }
121         else {
122             *received = g_strndup((const char *)ptr, real_size);
123         }
124     }
125     else {
126         _E("Failed to get response, response size : %lu", real_size);
127     }
128
129     return real_size;
130 }