Config added for the cloud comunication
[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 <stdbool.h>
23 #include "log.h"
24 #include "config.h"
25
26 #define BASE_URL "http://son.tizen.online"
27 #define CONFIG_CLOUD_GROUP "cloud"
28 #define CONFIG_URL "url"
29
30 static size_t _response_write(void *ptr, size_t size, size_t nmemb, void *data);
31
32 int http_request_get(const char *url, char **response, long *response_code)
33 {
34     retvm_if(!url, -1, "GET request URL is NULL!");
35     retvm_if(!response, -1, "GET request response is null");
36
37     CURL *curl = NULL;
38     CURLcode res = CURLE_OK;
39
40     curl = curl_easy_init();
41     retvm_if(!curl, -1, "Failed to initialize curl!");
42
43     curl_easy_setopt(curl, CURLOPT_URL, url);
44
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");
48
49     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _response_write);
50     curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)response);
51
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);
56         return -1;
57     }
58
59     long _response_code;
60     curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &_response_code);
61     if (_response_code) {
62         *response_code = _response_code;
63     }
64
65     curl_easy_cleanup(curl);
66
67     return 0;
68 }
69
70 int http_request_post(const char *url, const char *json, char **response, long *response_code)
71 {
72     retvm_if(!url, -1, "POST request URL is NULL!");
73     retvm_if(!json, -1, "POST request JSON message is NULL!");
74
75     CURL *curl = curl_easy_init();
76     retvm_if(!curl, -1, "Failed to initialize curl!");
77
78     char *_response = NULL;
79     struct curl_slist *headers = curl_slist_append(NULL, "Content-Type: application/json");
80
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);
87
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);
94         return -1;
95     }
96
97     long _response_code;
98     curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &_response_code);
99     if (response_code) {
100         *response_code = _response_code;
101     }
102
103     if (response) {
104         *response = _response;
105     }
106     else {
107         g_free(_response);
108     }
109
110     curl_slist_free_all(headers);
111     curl_easy_cleanup(curl);
112
113     return 0;
114 }
115
116 static size_t _response_write(void *ptr, size_t size, size_t nmemb, void *data)
117 {
118     char **received = (char **)data;
119     size_t real_size = size * nmemb;
120     const char *response_msg = (const char *)ptr;
121
122     if (received && real_size > 0) {
123         if (*received) {
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);
128             g_free(temp);
129             g_free(*received);
130             *received = new;
131         }
132         else {
133             *received = g_strndup((const char *)ptr, real_size);
134         }
135     }
136     else {
137         _E("Failed to get response, response size : %lu", real_size);
138     }
139
140     return real_size;
141 }
142
143 char *http_request_get_url(char *default_api, char *api_key)
144 {
145         char *url;
146         char *api;
147         char *url_with_api = calloc(PATH_MAX, sizeof(char));
148
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);
151
152         snprintf(url_with_api, PATH_MAX, "%s%s", url, api);
153
154         if (modified) {
155                 config_save();
156         }
157
158         return url_with_api;
159 }