add more functions for http module 98/182598/3
authorJeonghoon Park <jh1979.park@samsung.com>
Tue, 26 Jun 2018 11:10:58 +0000 (20:10 +0900)
committerJeonghoon Park <jh1979.park@samsung.com>
Tue, 26 Jun 2018 11:23:21 +0000 (20:23 +0900)
Change-Id: If29500db8e59434d34e3f5e2c07e71e473175c30

daemon/include/ttd-http.h
daemon/src/ttd-http.c

index 5f95b7e..64af81d 100644 (file)
 
 int ttd_http_init(void);
 int ttd_http_fini(void);
+
+/* basic functions */
+int ttd_http_get(const char *url, char **response, long *res_code);
+int ttd_http_post(
+       const char *url, const char *post_body, long *res_code, char **res_msg);
+int ttd_http_file_post(const char *url,
+       const char *filename, const char *postname, long *res_code, char **res_msg);
+int ttd_http_data_post(const char *url,
+       const void *data, unsigned int data_len, const char *data_name,
+       const char *postname, long *res_code, char **res_msg);
+
+/* helper functions */
 int ttd_http_command_get(char **cmd_json, long *res_code);
 int ttd_http_report_post(const char *result_json, long *res_code);
 int ttd_http_appdata_post(
        const char *project, const char *data, long *res_code);
+int ttd_http_logfile_post(const char *filename, long *res_code, char **res_msg);
 
 #endif /* __TT_DAEMON_HTTP_H__ */
index ea7f298..a26857e 100644 (file)
@@ -115,7 +115,7 @@ static const char *__get_url_by_type(ttd_url_type_e type)
        return urls[type];
 }
 
-static size_t _get_cmd_write(void *ptr, size_t size, size_t nmemb, void *data)
+static size_t _response_write(void *ptr, size_t size, size_t nmemb, void *data)
 {
        size_t res_size = 0;
        char **received = data;
@@ -139,19 +139,16 @@ static size_t _get_cmd_write(void *ptr, size_t size, size_t nmemb, void *data)
        return res_size;
 }
 
-int ttd_http_command_get(char **cmd_json, long *res_code)
+int ttd_http_get(const char *url, char **response, long *res_code)
 {
        int ret = 0;
        CURL *curl = NULL;
        CURLcode res = CURLE_OK;
        long r_code = 0;
-       const char *url = NULL;
 
        retvm_if(!http_initialized, -1, "http is not initialized yet");
-       retvm_if(!cmd_json, -1, "cmd_json is null");
-
-       url = __get_url_by_type(TTD_URL_CMD);
-       retvm_if(!url, -1, "failed to get url for command GET");
+       retvm_if(!response, -1, "response is null");
+       retvm_if(!url, -1, "url is null");
 
        curl = curl_easy_init();
 
@@ -159,33 +156,47 @@ int ttd_http_command_get(char **cmd_json, long *res_code)
 
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_CAPATH, CERT_FILE_PATH);
-       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _get_cmd_write);
-       curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)cmd_json);
+       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _response_write);
+       curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)response);
 
        res = curl_easy_perform(curl);
        if (res != CURLE_OK) {
                _E("curl_easy_perform() failed: %s", curl_easy_strerror(res));
-               g_free(*cmd_json);
                ret = -1;
        }
        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &r_code);
        if (res_code)
                *res_code = r_code;
 
-       _D("received code[%ld], cmd : %s", r_code, *cmd_json ? *cmd_json : "NULL");
+       _D("received code[%ld]", r_code);
+       _D("response : %s", *response ? *response : "NULL");
 
        curl_easy_cleanup(curl);
 
        return ret;
 }
 
-static int __ttd_http_post(const char *url,
-       const char *msg_body, struct curl_slist *headers, long *res_code)
+int ttd_http_command_get(char **cmd_json, long *res_code)
+{
+       const char *url = NULL;
+
+       retvm_if(!cmd_json, -1, "cmd_json is null");
+
+       url = __get_url_by_type(TTD_URL_CMD);
+       retvm_if(!url, -1, "failed to get url for command GET");
+
+       return ttd_http_get(url, cmd_json, res_code);
+}
+
+static int
+__ttd_http_post(const char *url, const char *msg_body,
+       struct curl_slist *headers, long *res_code, char **res_msg)
 {
        int ret = 0;
        CURL *curl = NULL;
        CURLcode res = CURLE_OK;
        long r_code = 0;
+       char *response = NULL;
 
        retvm_if(!http_initialized, -1, "http is not initialized yet");
        retvm_if(!url, -1, "url is null");
@@ -203,6 +214,8 @@ static int __ttd_http_post(const char *url,
        curl_easy_setopt(curl, CURLOPT_CAPATH, CERT_FILE_PATH);
        curl_easy_setopt(curl, CURLOPT_POST, 1L);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, msg_body);
+       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _response_write);
+       curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);
 
        res = curl_easy_perform(curl);
        if (res != CURLE_OK) {
@@ -214,6 +227,13 @@ static int __ttd_http_post(const char *url,
        if (res_code)
                *res_code = r_code;
 
+       _D("response code - [%d]", r_code);
+       _D("response message - %s", response ? response : "NULL");
+       if (res_msg)
+               *res_msg = response;
+       else
+               g_free(response);
+
        if (headers)
                curl_slist_free_all(headers);
        curl_easy_cleanup(curl);
@@ -221,6 +241,62 @@ static int __ttd_http_post(const char *url,
        return ret;
 }
 
+static int
+__ttd_http_form_post(const char *url, struct curl_httppost *formpost,
+       long *res_code, char **res_msg)
+{
+       int ret = 0;
+       CURL *curl = NULL;
+       CURLcode res = CURLE_OK;
+       long r_code = 0;
+       char *response = NULL;
+
+       retvm_if(!http_initialized, -1, "http is not initialized yet");
+       retvm_if(!url, -1, "url is null");
+       retvm_if(!formpost, -1, "formpost is null");
+
+       curl = curl_easy_init();
+
+       retvm_if(!curl, NULL, "failed to curl_easy_init()");
+
+       curl_easy_setopt(curl, CURLOPT_URL, url);
+       curl_easy_setopt(curl, CURLOPT_CAPATH, CERT_FILE_PATH);
+       curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
+       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _response_write);
+       curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);
+
+       res = curl_easy_perform(curl);
+       if (res != CURLE_OK) {
+               _E("curl_easy_perform() failed: %s", curl_easy_strerror(res));
+               ret = -1;
+       }
+
+       curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &r_code);
+       if (res_code)
+               *res_code = r_code;
+
+       _D("response code - [%d]", r_code);
+       _D("response message - %s", response ? response : "NULL");
+       if (res_msg)
+               *res_msg = response;
+       else
+               g_free(response);
+
+       curl_easy_cleanup(curl);
+       curl_formfree(formpost);
+
+       return ret;
+}
+
+int ttd_http_post(
+       const char *url, const char *post_body, long *res_code, char **res_msg)
+{
+       retvm_if(!url, -1, "url is NULL");
+       retvm_if(!post_body, -1, "post_body is NULL");
+
+       return __ttd_http_post(url, post_body, NULL, res_code, res_msg);
+}
+
 int ttd_http_report_post(const char *result_json, long *res_code)
 {
        struct curl_slist *headers = NULL;
@@ -237,7 +313,7 @@ int ttd_http_report_post(const char *result_json, long *res_code)
        headers = curl_slist_append(headers, "Content-Type: application/json");
        /* add here http headers to set new one */
 
-       return __ttd_http_post(url, result_json, headers, res_code);
+       return __ttd_http_post(url, result_json, headers, res_code, NULL);
 }
 
 int ttd_http_appdata_post(
@@ -263,8 +339,78 @@ int ttd_http_appdata_post(
        headers = curl_slist_append(headers, "Content-Type: application/json");
        /* add here http headers to set new one */
 
-       ret = __ttd_http_post(p_url, data_json, headers, res_code);
+       ret = __ttd_http_post(p_url, data_json, headers, res_code, NULL);
        g_free(p_url);
 
        return ret;
 }
+
+int ttd_http_file_post(const char *url,
+       const char *filename, const char *postname, long *res_code, char **res_msg)
+{
+       struct curl_httppost *formpost = NULL;
+       struct curl_httppost *lastptr = NULL;
+       char *basename = NULL;
+
+       retvm_if(!filename, -1, "filename is NULL");
+       retvm_if(!postname, -1, "postname is NULL");
+
+       basename = g_path_get_basename(filename);
+       retvm_if(!basename, -1, "failed to get basename from %s", filename);
+
+       curl_formadd(&formpost, &lastptr,
+               CURLFORM_COPYNAME, "content-type:",
+               CURLFORM_COPYCONTENTS, "multipart/form-data",
+               CURLFORM_END);
+
+       curl_formadd(&formpost, &lastptr,
+               CURLFORM_COPYNAME, postname,
+               CURLFORM_FILE, filename,
+               CURLFORM_END);
+
+       curl_formadd(&formpost, &lastptr,
+               CURLFORM_COPYNAME, "filename",
+               CURLFORM_COPYCONTENTS, basename,
+               CURLFORM_END);
+
+       g_free(basename);
+
+       return __ttd_http_form_post(url, formpost, res_code, res_msg);
+}
+
+int ttd_http_data_post(const char *url,
+       const void *data, unsigned int data_len, const char *data_name,
+       const char *postname, long *res_code, char **res_msg)
+{
+       struct curl_httppost *formpost = NULL;
+       struct curl_httppost *lastptr = NULL;
+
+       retvm_if(!data, -1, "data is NULL");
+       retvm_if(!data_len, -1, "data_len is 0");
+       retvm_if(!data_name, -1, "data_name is NULL");
+       retvm_if(!postname, -1, "postname is NULL");
+
+       curl_formadd(&formpost, &lastptr,
+               CURLFORM_COPYNAME, "content-type:",
+               CURLFORM_COPYCONTENTS, "multipart/form-data",
+               CURLFORM_END);
+
+       curl_formadd(&formpost, &lastptr,
+               CURLFORM_COPYNAME, postname,
+               CURLFORM_BUFFER, data_name,
+               CURLFORM_BUFFERPTR, data,
+               CURLFORM_BUFFERLENGTH, data_len,
+               CURLFORM_END);
+
+       return __ttd_http_form_post(url, formpost, res_code, res_msg);
+}
+
+int ttd_http_logfile_post(const char *filename, long *res_code, char **res_msg)
+{
+       const char *url = NULL;
+
+       url = __get_url_by_type(TTD_URL_LOG);
+       retvm_if(!url, -1, "failed to get url for log");
+
+       return ttd_http_file_post(url, filename, "logFile", res_code, res_msg);
+}