Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit 11d86ab

Browse files
author
HYChang
committed
fix(endpoint): Add check inside HTTP(S) module
The HTTP(S) module didn't check the return status from conn_http operations. This commit adds checks inside HTTP(S) module. Close #667
1 parent 7790ea2 commit 11d86ab

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

utils/https.c

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,35 @@
99
#include "https.h"
1010
#include <stdlib.h>
1111
#include <string.h>
12+
#include "common/logger.h"
1213
#include "common/ta_errors.h"
1314
#include "http_parser.h"
1415
#include "utils/connectivity/conn_http.h"
1516

1617
static http_parser parser;
1718

19+
#define HTTPS_LOGGER "https"
20+
static logger_id_t logger_id;
21+
22+
void https_logger_init() { logger_id = logger_helper_enable(HTTPS_LOGGER, LOGGER_DEBUG, true); }
23+
24+
int https_logger_release() {
25+
logger_helper_release(logger_id);
26+
if (logger_helper_destroy() != RC_OK) {
27+
ta_log_error("Destroying logger failed %s.\n", HTTPS_LOGGER);
28+
return EXIT_FAILURE;
29+
}
30+
31+
return 0;
32+
}
33+
1834
status_t send_https_msg(char const *host, char const *port, char const *api, const char *msg, const char *ssl_seed) {
1935
char res[4096] = {0};
2036
char *req = NULL;
2137
status_t ret = SC_OK;
2238

2339
set_post_request(api, host, atoi(port), msg, &req);
24-
http_parser_settings settings;
40+
http_parser_settings settings = {};
2541
settings.on_body = parser_body_callback;
2642

2743
#ifdef ENDPOINT_HTTPS
@@ -30,18 +46,34 @@ status_t send_https_msg(char const *host, char const *port, char const *api, con
3046
connect_info_t info = {.https = false};
3147
#endif
3248

33-
/* FIXME:Provide some checks here */
34-
http_open(&info, ssl_seed, host, port);
35-
http_send_request(&info, req);
36-
http_read_response(&info, res, sizeof(res) / sizeof(char));
37-
http_close(&info);
49+
ret = http_open(&info, ssl_seed, host, port);
50+
if (ret != SC_OK) {
51+
ta_log_error("http(s) open error, return code %d\n", ret);
52+
return ret;
53+
}
54+
55+
ret = http_send_request(&info, req);
56+
if (ret != SC_OK) {
57+
ta_log_error("http(s) send request error, return code %d\n", ret);
58+
goto exit;
59+
}
60+
61+
ret = http_read_response(&info, res, sizeof(res) / sizeof(char));
62+
if (ret != SC_OK) {
63+
ta_log_error("http(s) read response error, return code %d\n", ret);
64+
goto exit;
65+
}
66+
3867
http_parser_init(&parser, HTTP_RESPONSE);
3968
http_parser_execute(&parser, &settings, res, strlen(res));
4069

4170
if (parser.status_code != SC_HTTP_OK) {
71+
ta_log_error("http(s): fail to parse response\n");
4272
ret = SC_UTILS_HTTPS_RESPONSE_ERROR;
4373
}
4474

75+
exit:
76+
http_close(&info);
4577
free(req);
4678
return ret;
4779
}

utils/https.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@
1111

1212
#include "common/ta_errors.h"
1313

14+
/**
15+
* @brief Initialize logger of HTTP(S)
16+
*/
17+
void https_logger_init();
18+
19+
/**
20+
* @brief Release logger of https
21+
*
22+
* @return
23+
* - zero on success
24+
* - EXIT_FAILURE on error
25+
*/
26+
int https_logger_release();
27+
1428
/**
1529
* @brief Send message via HTTP(S) protocol
1630
*

0 commit comments

Comments
 (0)