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

Commit 4144839

Browse files
author
HYChang
committed
feat(endpoint): Provide HTTP(S) response for endpoint
The MAM protocol needs to handle the HTTP(S) response from HTTP(S) request. This commit provided the HTTP(S) response from the http_parser. The buffer of string insides the https_response_t should always be freed after the request. Close #704
1 parent 6bbaf56 commit 4144839

File tree

8 files changed

+87
-38
lines changed

8 files changed

+87
-38
lines changed

endpoint/connectivity/conn_http.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,3 @@ status_t set_get_request(char const *const path, char const *const host, const u
251251

252252
return SC_OK;
253253
}
254-
255-
int parser_body_callback(http_parser *parser, const char *at, size_t length) {
256-
ta_log_debug("HTTP Response: %s\n", at);
257-
return 0;
258-
}

endpoint/connectivity/conn_http.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ extern "C" {
1515

1616
#include <stdbool.h>
1717
#include "common/ta_errors.h"
18-
#include "http_parser.h"
1918
#include "mbedtls/certs.h"
2019
#include "mbedtls/ctr_drbg.h"
2120
#include "mbedtls/entropy.h"
@@ -140,19 +139,6 @@ status_t set_post_request(char const *const path, char const *const host, const
140139
*/
141140
status_t set_get_request(char const *const path, char const *const host, const uint32_t port, char **out);
142141

143-
/**
144-
* @brief Callback function for http parser
145-
*
146-
* @param[in] parser HTTP(S) parser
147-
* @param[in] at HTTP(S) message to parse
148-
* @param[in] length Length of text at
149-
*
150-
* @return
151-
* - 0 on success
152-
* - non-zero on error
153-
*/
154-
int parser_body_callback(http_parser *parser, const char *at, size_t length);
155-
156142
#ifdef __cplusplus
157143
}
158144
#endif

endpoint/endpoint_core.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ void endpoint_init() {
5353

5454
// Logger initialization of other included components
5555
cipher_logger_init();
56+
https_logger_init();
5657
}
5758

5859
void endpoint_destroy() {
5960
// Logger release of other included components
6061
cipher_logger_release();
62+
https_logger_release();
6163

6264
// Logger release of endpoint
6365
logger_helper_release(logger_id);
@@ -118,10 +120,30 @@ status_t send_transaction_information(const char* host, const char* port, const
118120
return SC_ENDPOINT_SEND_TRANSFER;
119121
}
120122

121-
if (send_https_msg(ta_host, ta_port, SEND_TRANSACTION_API, req_body, seed) != SC_OK) {
123+
https_response_t req = {
124+
.buffer = req_body,
125+
.len = ret,
126+
};
127+
128+
https_response_t res = {0};
129+
https_ctx_t https_ctx = {
130+
.host = ta_host,
131+
.port = atoi(ta_port),
132+
.api = SEND_TRANSACTION_API,
133+
.ssl_seed = seed,
134+
.s_req = &req,
135+
.s_res = &res,
136+
};
137+
138+
if (send_https_msg(&https_ctx) != SC_OK) {
122139
ta_log_error("http message sending error.\n");
123140
return SC_ENDPOINT_SEND_TRANSFER;
124141
}
125142

143+
if (https_ctx.s_res->buffer != NULL) {
144+
ta_log_debug("HTTP Response: %s\n", https_ctx.s_res->buffer);
145+
free(https_ctx.s_res->buffer);
146+
}
147+
126148
return SC_OK;
127149
}

endpoint/https.c

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,59 @@ void https_logger_init() { logger_id = logger_helper_enable(HTTPS_LOGGER, LOGGER
2323

2424
int https_logger_release() {
2525
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;
26+
return 0;
27+
}
28+
29+
static int https_parser_callback(http_parser* parser, const char* at, size_t length) {
30+
if (parser == NULL || parser->data == NULL) {
31+
ta_log_error("http(s) parser: parser or parser->data cannot be NULL");
32+
/* Returning a non-zero value indicates error to the parser */
33+
return 1;
2934
}
35+
https_response_t* my_data = (https_response_t*)parser->data;
3036

37+
my_data->buffer = malloc(sizeof(char) * (length + 1));
38+
if (my_data->buffer == NULL) {
39+
ta_log_error("http(s) parser: cannot allocate enough memory");
40+
/* Returning a non-zero value indicates error to the parser */
41+
return 1;
42+
}
43+
snprintf(my_data->buffer, length + 1, "%s", at);
44+
my_data->len = length;
3145
return 0;
3246
}
3347

34-
status_t send_https_msg(char const *host, char const *port, char const *api, const char *msg, const char *ssl_seed) {
48+
status_t send_https_msg(https_ctx_t* ctx) {
3549
char res[4096] = {0};
36-
char *req = NULL;
50+
char* req = NULL;
3751
status_t ret = SC_OK;
3852

39-
set_post_request(api, host, atoi(port), msg, &req);
53+
const char* host = ctx->host;
54+
const char* api = ctx->api;
55+
const int port = ctx->port;
56+
const char* ssl_seed = ctx->ssl_seed;
57+
const char* msg = ctx->s_req->buffer;
58+
59+
set_post_request(api, host, ctx->port, msg, &req);
60+
4061
http_parser_settings settings = {};
41-
settings.on_body = parser_body_callback;
62+
settings.on_body = https_parser_callback;
63+
64+
https_response_t my_data = {0};
65+
parser.data = &my_data;
4266

4367
#ifdef ENDPOINT_HTTPS
4468
connect_info_t info = {.https = true};
4569
#else
4670
connect_info_t info = {.https = false};
4771
#endif
4872

49-
ret = http_open(&info, ssl_seed, host, port);
73+
char net_port[12] = {0};
74+
snprintf(net_port, 12, "%d", port);
75+
ret = http_open(&info, ssl_seed, host, net_port);
5076
if (ret != SC_OK) {
5177
ta_log_error("http(s) open error, return code %d\n", ret);
52-
return ret;
78+
goto exit;
5379
}
5480

5581
ret = http_send_request(&info, req);
@@ -72,6 +98,9 @@ status_t send_https_msg(char const *host, char const *port, char const *api, con
7298
ret = SC_UTILS_HTTPS_RESPONSE_ERROR;
7399
}
74100

101+
ctx->s_res->buffer = my_data.buffer;
102+
ctx->s_res->len = my_data.len;
103+
75104
exit:
76105
http_close(&info);
77106
free(req);

endpoint/https.h

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef UTILS_HTTPS_H
1010
#define UTILS_HTTPS_H
1111

12+
#include <stddef.h>
1213
#include "common/ta_errors.h"
1314

1415
#ifdef __cplusplus
@@ -19,6 +20,22 @@ extern "C" {
1920
* @file endpoint/https.h
2021
*/
2122

23+
/* struct type of HTTP(S) response */
24+
typedef struct {
25+
char* buffer; /**< Message body **/
26+
size_t len; /**< Length of message body */
27+
} https_response_t;
28+
29+
/* struct type of HTTP(S) context */
30+
typedef struct {
31+
const char* host; /**< HTTP(S) host */
32+
const int port; /**< HTTP(S) port */
33+
const char* api; /**< API path for POST or GET request to HTTP(S) server, i.e "transaction/". It must be in string. */
34+
const char* ssl_seed; /**< Seed for ssl connection. This column is optional. */
35+
https_response_t* s_req; /**< [in] The message to send */
36+
https_response_t* s_res; /**< [out] The message body of HTTP(S) response */
37+
} https_ctx_t;
38+
2239
/**
2340
* @brief Initialize logger of HTTP(S)
2441
*/
@@ -34,17 +51,13 @@ void https_logger_init();
3451
int https_logger_release();
3552

3653
/**
37-
* @brief Send message via HTTP(S) protocol
54+
* @brief Send a POST request message via HTTP(S) protocol
3855
*
39-
* @param[in] host HTTP(S) host
40-
* @param[in] port HTTP(S) port
41-
* @param[in] api API path for POST request to HTTP(S) server, i.e "transaction/". It must be in string.
42-
* @param[in] msg Message to send
43-
* @param[in] ssl_seed Seed for ssl connection
56+
* @param[in, out] ctx The pointer points to http context
4457
*
4558
* @return #status_t
4659
*/
47-
status_t send_https_msg(char const *host, char const *port, char const *api, const char *msg, const char *ssl_seed);
60+
status_t send_https_msg(https_ctx_t* ctx);
4861

4962
#ifdef __cplusplus
5063
}

endpoint/unit-test/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ cc_test(
44
"test_http.c",
55
],
66
deps = [
7+
"//endpoint:https",
78
"//endpoint/connectivity:conn_http",
89
"//tests:test_define",
910
],

endpoint/unit-test/test_http.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <string.h>
33
#include "common/ta_errors.h"
44
#include "endpoint/connectivity/conn_http.h"
5+
#include "endpoint/https.h"
56
#include "http_parser.h"
67
#include "tests/test_define.h"
78

@@ -31,11 +32,13 @@ Content-Length: 224\r\n\
3132
#define BUF_SIZE 4096
3233

3334
static char* req = NULL;
35+
static https_response_t my_data;
3436

3537
void setUp(void) { conn_http_logger_init(); }
3638

3739
void tearDown(void) {
3840
conn_http_logger_release();
41+
free(my_data.buffer);
3942
free(req);
4043
}
4144

@@ -44,7 +47,7 @@ void test_http(void) {
4447
char post_message[BUF_SIZE] = {0}, response[BUF_SIZE] = {0};
4548
http_parser parser;
4649
http_parser_settings settings = {};
47-
settings.on_body = parser_body_callback;
50+
parser.data = &my_data;
4851

4952
snprintf(post_message, BUF_SIZE, "%s", TEST_POST_MESSAGE);
5053

tests/endpoint/test-endpoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22

3-
set -uo pipefail
3+
set -euo pipefail
44

55
# Create endpoint app
66
make EP_TA_HOST=node.deviceproof.org EP_TA_PORT=5566 legato

0 commit comments

Comments
 (0)