diff options
author | Jeonghoon Park <jh1979.park@samsung.com> | 2017-11-06 18:42:35 +0900 |
---|---|---|
committer | Jeonghoon Park <jh1979.park@samsung.com> | 2017-11-06 18:44:47 +0900 |
commit | 19d4ae4a659adb53d48fcb71f86047f37f5e42d1 (patch) | |
tree | ed74ea65f14ee5a50a762e2406aad3cde1a3befb /src/webutil.c | |
parent | 1e88e5abf48bb32c6c7cd83be55e58e3f4477952 (diff) | |
download | rcc-19d4ae4a659adb53d48fcb71f86047f37f5e42d1.tar.gz rcc-19d4ae4a659adb53d48fcb71f86047f37f5e42d1.tar.bz2 rcc-19d4ae4a659adb53d48fcb71f86047f37f5e42d1.zip |
add get function for web module
Change-Id: If745547f92bc5303abbca9dd94a2b8bf053a1781
Diffstat (limited to 'src/webutil.c')
-rw-r--r-- | src/webutil.c | 57 |
1 files changed, 55 insertions, 2 deletions
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) { |