From: Patrick Date: Sun, 28 May 2023 13:07:34 +0000 (+0200) Subject: mongoose as http client X-Git-Url: https://gitweb.ps.run/matrix_esp_thesis/commitdiff_plain/5cb22046a33f24c1a696990f95e13d534efef497?ds=sidebyside mongoose as http client --- diff --git a/Makefile b/Makefile index f30f057..82c259a 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,24 @@ CC=gcc C_OPTS=-Wall -Wextra -pedantic -C_OPTS+=-I src/ -C_OPTS+=-I ext/olm/include/ -C_OPTS+=-I ext/mjson/src C_OPTS+=src/matrix.c -C_OPTS+=src/matrix_http_curl.c +C_OPTS+=src/matrix_http_mongoose.c C_OPTS+=ext/mjson/src/mjson.c -C_OPTS+=-l curl +C_OPTS+=ext/mongoose/mongoose.c +C_OPTS+=-I src/ +C_OPTS+=-I ext/olm/include/ +C_OPTS+=-I ext/mjson/src/ +C_OPTS+=-I ext/mongoose/ +C_OPTS+=-l ws2_32 +C_OPTS+=-l ssl +C_OPTS+=-l crypto +C_OPTS+=-D MG_ENABLE_OPENSSL=1 +C_OPTS+=-g +# C_OPTS+=-I ext/curl/include/ +# C_OPTS+=-L ext/curl/build/lib/ +# C_OPTS+=-l curl + +#C_OPTS+=-Wl,--verbose out/examples/%: examples/%.c src/* $(CC) -o out/examples/$* examples/$*.c $(C_OPTS) diff --git a/examples/Login.c b/examples/Login.c index 69ac3a1..1c8e7cd 100644 --- a/examples/Login.c +++ b/examples/Login.c @@ -1,6 +1,6 @@ #include #include -#include +#include #define SERVER "https://matrix.org" #define USERNAME "pscho" @@ -14,8 +14,7 @@ main() MatrixClient client; MatrixClientInit(&client, SERVER, strlen(SERVER)); - curl_global_init(CURL_GLOBAL_DEFAULT); - client.httpUserData = (void *)curl_easy_init(); + MatrixHttpInit(&client); MatrixClientLoginPassword(&client, USERNAME, strlen(USERNAME), @@ -27,8 +26,7 @@ main() printf("Expires in (ms): %.*s\n", client.expireMsLen, client.expireMsBuffer); printf("Refresh Token: %.*s\n", client.refreshTokenLen, client.refreshTokenBuffer); - curl_easy_cleanup((CURL *)client.httpUserData); - curl_global_cleanup(); + MatrixHttpDeinit(&client); return 0; } \ No newline at end of file diff --git a/ext/mongoose b/ext/mongoose index 45e4b3c..96707ac 160000 --- a/ext/mongoose +++ b/ext/mongoose @@ -1 +1 @@ -Subproject commit 45e4b3c423c7ee0f378b1b8682eccc533bed3f0d +Subproject commit 96707acb4e991255702c7f36809cbd6e20a412fe 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