9
9
#include "https.h"
10
10
#include <stdlib.h>
11
11
#include <string.h>
12
+ #include "common/logger.h"
12
13
#include "common/ta_errors.h"
13
14
#include "http_parser.h"
14
15
#include "utils/connectivity/conn_http.h"
15
16
16
17
static http_parser parser ;
17
18
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
+
18
34
status_t send_https_msg (char const * host , char const * port , char const * api , const char * msg , const char * ssl_seed ) {
19
35
char res [4096 ] = {0 };
20
36
char * req = NULL ;
21
37
status_t ret = SC_OK ;
22
38
23
39
set_post_request (api , host , atoi (port ), msg , & req );
24
- http_parser_settings settings ;
40
+ http_parser_settings settings = {} ;
25
41
settings .on_body = parser_body_callback ;
26
42
27
43
#ifdef ENDPOINT_HTTPS
@@ -30,18 +46,34 @@ status_t send_https_msg(char const *host, char const *port, char const *api, con
30
46
connect_info_t info = {.https = false};
31
47
#endif
32
48
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
+
38
67
http_parser_init (& parser , HTTP_RESPONSE );
39
68
http_parser_execute (& parser , & settings , res , strlen (res ));
40
69
41
70
if (parser .status_code != SC_HTTP_OK ) {
71
+ ta_log_error ("http(s): fail to parse response\n" );
42
72
ret = SC_UTILS_HTTPS_RESPONSE_ERROR ;
43
73
}
44
74
75
+ exit :
76
+ http_close (& info );
45
77
free (req );
46
78
return ret ;
47
79
}
0 commit comments