Sending lap time to cloud
[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
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");
42
43     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _response_write);
44     curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)response);
45
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);
50         return -1;
51     }
52
53     long _response_code;
54     curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &_response_code);
55     if (_response_code) {
56         *response_code = _response_code;
57     }
58
59     curl_easy_cleanup(curl);
60
61     return 0;
62 }
63
64 int http_request_post(const char *url, const char *json, char **response, long *response_code)
65 {
66     retvm_if(!url, -1, "POST request URL is NULL!");
67     retvm_if(!json, -1, "POST request JSON message is NULL!");
68
69     CURL *curl = curl_easy_init();
70     retvm_if(!curl, -1, "Failed to initialize curl!");
71
72     char *_response = NULL;
73     struct curl_slist *headers = curl_slist_append(NULL, "Content-Type: application/json");
74
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);
81
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);
88         return -1;
89     }
90
91     long _response_code;
92     curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &_response_code);
93     if (response_code) {
94         *response_code = _response_code;
95     }
96
97     if (response) {
98         *response = _response;
99     }
100     else {
101         g_free(_response);
102     }
103
104     curl_slist_free_all(headers);
105     curl_easy_cleanup(curl);
106
107     return 0;
108 }
109
110 static size_t _response_write(void *ptr, size_t size, size_t nmemb, void *data)
111 {
112     char **received = (char **)data;
113     size_t real_size = size * nmemb;
114     const char *response_msg = (const char *)ptr;
115
116     if (received && real_size > 0) {
117         if (*received) {
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);
122             g_free(temp);
123             g_free(*received);
124             *received = new;
125         }
126         else {
127             *received = g_strndup((const char *)ptr, real_size);
128         }
129     }
130     else {
131         _E("Failed to get response, response size : %lu", real_size);
132     }
133
134     return real_size;
135 }