From 5cb22046a33f24c1a696990f95e13d534efef497 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sun, 28 May 2023 15:07:34 +0200 Subject: mongoose as http client --- src/matrix.c | 9 +-- src/matrix.h | 27 +++++--- src/matrix_http_mongoose.c | 153 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 178 insertions(+), 11 deletions(-) create mode 100644 src/matrix_http_mongoose.c (limited to 'src') diff --git a/src/matrix.c b/src/matrix.c index 90132af..790f0f5 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -1,5 +1,6 @@ #include "matrix.h" +#include #include @@ -11,8 +12,8 @@ bool MatrixClientInit( MatrixClient * client, - char * server, int serverLen -) { + char * server, int serverLen) +{ strcpy_s( client->server, SERVER_SIZE, @@ -29,8 +30,8 @@ MatrixClientLoginPassword( MatrixClient * client, char * username, int usernameLen, char * password, int passwordLen, - char * displayName, int displayNameLen -) { + char * displayName, int displayNameLen) +{ static char requestBuffer[LOGIN_REQUEST_SIZE]; int requestLen = diff --git a/src/matrix.h b/src/matrix.h index d37474f..b7f3a5b 100644 --- a/src/matrix.h +++ b/src/matrix.h @@ -18,36 +18,49 @@ typedef struct MatrixClient { - void * httpUserData; OlmAccount * olmAccount; OlmSession * olmSession; + char server[SERVER_SIZE]; int serverLen; char accessTokenBuffer[ACCESS_TOKEN_SIZE]; int accessTokenLen; char deviceIdBuffer[DEVICE_ID_SIZE]; int deviceIdLen; char expireMsBuffer[EXPIRE_MS_SIZE]; int expireMsLen; char refreshTokenBuffer[REFRESH_TOKEN_SIZE]; int refreshTokenLen; + + void * httpUserData; } MatrixClient; bool MatrixClientInit( MatrixClient * client, - char * server, int serverLen -); + char * server, int serverLen); bool MatrixClientLoginPassword( MatrixClient * client, char * username, int usernameLen, char * password, int passwordLen, - char * displayName, int displayNameLen -); + char * displayName, int displayNameLen); + +bool +MatrixHttpInit( + MatrixClient * client); + +bool +MatrixHttpDeinit( + MatrixClient * client); + +bool +MatrixHttpGet( + MatrixClient * client, + const char * url, + char * outResponseBuffer, int outResponseCap, int * outResponseLen); bool MatrixHttpPost( MatrixClient * client, const char * url, char * requestBuffer, int requestLen, - char * outResponseBuffer, int outResponseCap, int * outResponseLen -); + char * outResponseBuffer, int outResponseCap, int * outResponseLen); #endif diff --git a/src/matrix_http_mongoose.c b/src/matrix_http_mongoose.c new file mode 100644 index 0000000..3faefa9 --- /dev/null +++ b/src/matrix_http_mongoose.c @@ -0,0 +1,153 @@ +#include "matrix.h" +#include +#include +#include +#include +#include + +//#define HTTP_DATA_SIZE 1024 + +typedef struct MatrixHttpConnection { + struct mg_mgr mgr; + struct mg_connection * connection; + bool connected; + char * data; + int dataCap; + int dataLen; + bool dataReceived; +} MatrixHttpConnection; + +static void +MatrixHttpCallback( + struct mg_connection *c, + int ev, + void *ev_data, + void *fn_data) +{ + MatrixClient * client = (MatrixClient *)fn_data; + MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData; + + if (ev == MG_EV_CONNECT) + { + struct mg_str host = mg_url_host(client->server); + + // If s_url is https://, tell client connection to use TLS + if (mg_url_is_ssl(client->server)) + { + struct mg_tls_opts opts = {.srvname = host}; + mg_tls_init(c, &opts); + } + + conn->connection = c; + conn->connected = true; + } + if (ev == MG_EV_HTTP_MSG) + { + // Response + struct mg_http_message *hm = (struct mg_http_message *)ev_data; + // memcpy_s(client->data, 1024, hm->message.ptr, hm->message.len); + // client->dataLen = hm->message.len; + memcpy_s(conn->data, conn->dataCap, hm->body.ptr, hm->body.len); + conn->dataLen = hm->body.len; + conn->dataReceived = true; + } +} + +bool +MatrixHttpInit( + MatrixClient * client) +{ + MatrixHttpConnection * conn = + (MatrixHttpConnection *)malloc(sizeof(MatrixHttpConnection)); + + client->httpUserData = conn; + + mg_mgr_init(&conn->mgr); + + mg_http_connect(&conn->mgr, client->server, MatrixHttpCallback, client); + + while (! conn->connected) + mg_mgr_poll(&conn->mgr, 1000); + + return conn->connected; +} + +bool +MatrixHttpDeinit( + MatrixClient * client) +{ + MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData; + + mg_mgr_free(&conn->mgr); + + free(conn); + + return true; +} + +bool +MatrixHttpGet( + MatrixClient * client, + const char * url, + char * outResponseBuffer, int outResponseCap, int * outResponseLen) +{ + MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData; + + conn->dataReceived = false; + + struct mg_str host = mg_url_host(client->server); + + mg_printf(conn->connection, + "GET %s HTTP/1.1\r\n" + "Host: %.*s\r\n" + "\r\n", + url, + host.len, host.ptr); + + conn->data = outResponseBuffer; + conn->dataCap = outResponseCap; + + while (! conn->dataReceived) + mg_mgr_poll(&conn->mgr, 1000); + + *outResponseLen = conn->dataLen; + + return conn->dataReceived; +} + +bool +MatrixHttpPost( + MatrixClient * client, + const char * url, + char * requestBuffer, int requestLen, + char * outResponseBuffer, int outResponseCap, int * outResponseLen) +{ + MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData; + + conn->dataReceived = false; + + struct mg_str host = mg_url_host(client->server); + + mg_printf(conn->connection, + "POST %s HTTP/1.0\r\n" + "Host: %.*s\r\n" + "Content-Type: application/json\r\n" + "Content-Length: %d\r\n" + "\r\n" + "%s" + "\r\n", + url, + host.len, host.ptr, + requestLen, + requestBuffer); + + conn->data = outResponseBuffer; + conn->dataCap = outResponseCap; + + while (! conn->dataReceived) + mg_mgr_poll(&conn->mgr, 1000); + + *outResponseLen = conn->dataLen; + + return conn->dataReceived; +} \ No newline at end of file -- cgit v1.2.3