From 19d4ae4a659adb53d48fcb71f86047f37f5e42d1 Mon Sep 17 00:00:00 2001 From: Jeonghoon Park Date: Mon, 6 Nov 2017 18:42:35 +0900 Subject: [PATCH] add get function for web module Change-Id: If745547f92bc5303abbca9dd94a2b8bf053a1781 --- inc/webutil.h | 1 + src/webutil.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/inc/webutil.h b/inc/webutil.h index 2d3e437..0a78fdb 100644 --- a/inc/webutil.h +++ b/inc/webutil.h @@ -67,6 +67,7 @@ struct _web_util_sensor_data_s { int web_util_noti_init(void); void web_util_noti_fini(void); int web_util_noti_post(const char *resource, const char *json_data); +int web_util_noti_get(const char *resource, char **res); int web_util_json_init(void); int web_util_json_fini(void); diff --git a/src/webutil.c b/src/webutil.c index ea5ae9b..2c55298 100644 --- a/src/webutil.c +++ b/src/webutil.c @@ -28,6 +28,7 @@ #include "webutil.h" #define URI_PATH_LEN 64 +#define REQ_CON_TIMEOUT 5L typedef struct _wu_json_handle { JsonBuilder *builder; @@ -37,7 +38,7 @@ typedef struct _wu_json_handle { static wu_json_handle Json_h = {NULL, false, false}; -static size_t _response_write_callback(char *ptr, size_t size, size_t nmemb, void *userdata) +static size_t _post_response_write_callback(char *ptr, size_t size, size_t nmemb, void *userdata) { size_t res_size = 0; @@ -50,6 +51,21 @@ static size_t _response_write_callback(char *ptr, size_t size, size_t nmemb, voi return res_size; } +static size_t _get_response_write_callback(void *ptr, size_t size, size_t nmemb, void *data) +{ + size_t res_size = 0; + char **received = (char **)data; + + res_size = size*nmemb; + + if (received && res_size > 0) + *received = strndup((char *)ptr, size*nmemb); + else + _E("fail to get response [res size : %d]", res_size); + + return res_size; +} + int web_util_noti_init(void) { int ret = 0; @@ -96,7 +112,8 @@ int web_util_noti_post(const char *resource, const char *json_data) curl_easy_setopt(curl, CURLOPT_POST, 1L); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_data); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _response_write_callback); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _post_response_write_callback); + curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, REQ_CON_TIMEOUT); response = curl_easy_perform(curl); @@ -113,6 +130,42 @@ int web_util_noti_post(const char *resource, const char *json_data) return ret; } +int web_util_noti_get(const char *resource, char **res) +{ + int ret = 0; + CURL *curl = NULL; + CURLcode response = CURLE_OK; + + retv_if(resource == NULL, -1); + + _I("GET to [%s]", resource); + + curl = curl_easy_init(); + + if (!curl) { + _E("fail to init curl"); + return -1; + } + + curl_easy_setopt(curl, CURLOPT_URL, resource); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _get_response_write_callback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)res); + curl_easy_setopt(curl, CURLOPT_USERAGENT, "tizen-iot-agent/1.0"); + curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, REQ_CON_TIMEOUT); + + response = curl_easy_perform(curl); + + if (response != CURLE_OK) { + _E("curl_easy_perform() failed: %s", + curl_easy_strerror(response)); + /* What should we do here, if response is kind of errors? */ + ret = -1; + } + + curl_easy_cleanup(curl); + + return ret; +} int web_util_json_init(void) { -- 2.7.4