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

Commit

Permalink
feat(endpoint): Provide HTTP(S) response for endpoint
Browse files Browse the repository at this point in the history
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
  • Loading branch information
splasky committed Jul 17, 2020
1 parent 6bbaf56 commit 4144839
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 38 deletions.
5 changes: 0 additions & 5 deletions endpoint/connectivity/conn_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,3 @@ status_t set_get_request(char const *const path, char const *const host, const u

return SC_OK;
}

int parser_body_callback(http_parser *parser, const char *at, size_t length) {
ta_log_debug("HTTP Response: %s\n", at);
return 0;
}
14 changes: 0 additions & 14 deletions endpoint/connectivity/conn_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ extern "C" {

#include <stdbool.h>
#include "common/ta_errors.h"
#include "http_parser.h"
#include "mbedtls/certs.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/entropy.h"
Expand Down Expand Up @@ -140,19 +139,6 @@ status_t set_post_request(char const *const path, char const *const host, const
*/
status_t set_get_request(char const *const path, char const *const host, const uint32_t port, char **out);

/**
* @brief Callback function for http parser
*
* @param[in] parser HTTP(S) parser
* @param[in] at HTTP(S) message to parse
* @param[in] length Length of text at
*
* @return
* - 0 on success
* - non-zero on error
*/
int parser_body_callback(http_parser *parser, const char *at, size_t length);

#ifdef __cplusplus
}
#endif
Expand Down
24 changes: 23 additions & 1 deletion endpoint/endpoint_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ void endpoint_init() {

// Logger initialization of other included components
cipher_logger_init();
https_logger_init();
}

void endpoint_destroy() {
// Logger release of other included components
cipher_logger_release();
https_logger_release();

// Logger release of endpoint
logger_helper_release(logger_id);
Expand Down Expand Up @@ -118,10 +120,30 @@ status_t send_transaction_information(const char* host, const char* port, const
return SC_ENDPOINT_SEND_TRANSFER;
}

if (send_https_msg(ta_host, ta_port, SEND_TRANSACTION_API, req_body, seed) != SC_OK) {
https_response_t req = {
.buffer = req_body,
.len = ret,
};

https_response_t res = {0};
https_ctx_t https_ctx = {
.host = ta_host,
.port = atoi(ta_port),
.api = SEND_TRANSACTION_API,
.ssl_seed = seed,
.s_req = &req,
.s_res = &res,
};

if (send_https_msg(&https_ctx) != SC_OK) {
ta_log_error("http message sending error.\n");
return SC_ENDPOINT_SEND_TRANSFER;
}

if (https_ctx.s_res->buffer != NULL) {
ta_log_debug("HTTP Response: %s\n", https_ctx.s_res->buffer);
free(https_ctx.s_res->buffer);
}

return SC_OK;
}
47 changes: 38 additions & 9 deletions endpoint/https.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,59 @@ void https_logger_init() { logger_id = logger_helper_enable(HTTPS_LOGGER, LOGGER

int https_logger_release() {
logger_helper_release(logger_id);
if (logger_helper_destroy() != RC_OK) {
ta_log_error("Destroying logger failed %s.\n", HTTPS_LOGGER);
return EXIT_FAILURE;
return 0;
}

static int https_parser_callback(http_parser* parser, const char* at, size_t length) {
if (parser == NULL || parser->data == NULL) {
ta_log_error("http(s) parser: parser or parser->data cannot be NULL");
/* Returning a non-zero value indicates error to the parser */
return 1;
}
https_response_t* my_data = (https_response_t*)parser->data;

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

status_t send_https_msg(char const *host, char const *port, char const *api, const char *msg, const char *ssl_seed) {
status_t send_https_msg(https_ctx_t* ctx) {
char res[4096] = {0};
char *req = NULL;
char* req = NULL;
status_t ret = SC_OK;

set_post_request(api, host, atoi(port), msg, &req);
const char* host = ctx->host;
const char* api = ctx->api;
const int port = ctx->port;
const char* ssl_seed = ctx->ssl_seed;
const char* msg = ctx->s_req->buffer;

set_post_request(api, host, ctx->port, msg, &req);

http_parser_settings settings = {};
settings.on_body = parser_body_callback;
settings.on_body = https_parser_callback;

https_response_t my_data = {0};
parser.data = &my_data;

#ifdef ENDPOINT_HTTPS
connect_info_t info = {.https = true};
#else
connect_info_t info = {.https = false};
#endif

ret = http_open(&info, ssl_seed, host, port);
char net_port[12] = {0};
snprintf(net_port, 12, "%d", port);
ret = http_open(&info, ssl_seed, host, net_port);
if (ret != SC_OK) {
ta_log_error("http(s) open error, return code %d\n", ret);
return ret;
goto exit;
}

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

ctx->s_res->buffer = my_data.buffer;
ctx->s_res->len = my_data.len;

exit:
http_close(&info);
free(req);
Expand Down
27 changes: 20 additions & 7 deletions endpoint/https.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef UTILS_HTTPS_H
#define UTILS_HTTPS_H

#include <stddef.h>
#include "common/ta_errors.h"

#ifdef __cplusplus
Expand All @@ -19,6 +20,22 @@ extern "C" {
* @file endpoint/https.h
*/

/* struct type of HTTP(S) response */
typedef struct {
char* buffer; /**< Message body **/
size_t len; /**< Length of message body */
} https_response_t;

/* struct type of HTTP(S) context */
typedef struct {
const char* host; /**< HTTP(S) host */
const int port; /**< HTTP(S) port */
const char* api; /**< API path for POST or GET request to HTTP(S) server, i.e "transaction/". It must be in string. */
const char* ssl_seed; /**< Seed for ssl connection. This column is optional. */
https_response_t* s_req; /**< [in] The message to send */
https_response_t* s_res; /**< [out] The message body of HTTP(S) response */
} https_ctx_t;

/**
* @brief Initialize logger of HTTP(S)
*/
Expand All @@ -34,17 +51,13 @@ void https_logger_init();
int https_logger_release();

/**
* @brief Send message via HTTP(S) protocol
* @brief Send a POST request message via HTTP(S) protocol
*
* @param[in] host HTTP(S) host
* @param[in] port HTTP(S) port
* @param[in] api API path for POST request to HTTP(S) server, i.e "transaction/". It must be in string.
* @param[in] msg Message to send
* @param[in] ssl_seed Seed for ssl connection
* @param[in, out] ctx The pointer points to http context
*
* @return #status_t
*/
status_t send_https_msg(char const *host, char const *port, char const *api, const char *msg, const char *ssl_seed);
status_t send_https_msg(https_ctx_t* ctx);

#ifdef __cplusplus
}
Expand Down
1 change: 1 addition & 0 deletions endpoint/unit-test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ cc_test(
"test_http.c",
],
deps = [
"//endpoint:https",
"//endpoint/connectivity:conn_http",
"//tests:test_define",
],
Expand Down
5 changes: 4 additions & 1 deletion endpoint/unit-test/test_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <string.h>
#include "common/ta_errors.h"
#include "endpoint/connectivity/conn_http.h"
#include "endpoint/https.h"
#include "http_parser.h"
#include "tests/test_define.h"

Expand Down Expand Up @@ -31,11 +32,13 @@ Content-Length: 224\r\n\
#define BUF_SIZE 4096

static char* req = NULL;
static https_response_t my_data;

void setUp(void) { conn_http_logger_init(); }

void tearDown(void) {
conn_http_logger_release();
free(my_data.buffer);
free(req);
}

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

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

Expand Down
2 changes: 1 addition & 1 deletion tests/endpoint/test-endpoint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

set -uo pipefail
set -euo pipefail

# Create endpoint app
make EP_TA_HOST=node.deviceproof.org EP_TA_PORT=5566 legato
Expand Down

0 comments on commit 4144839

Please sign in to comment.