]> gitweb.ps.run Git - matrix_esp_thesis/commitdiff
mongoose as http client
authorPatrick <patrick.schoenberger@posteo.de>
Sun, 28 May 2023 13:07:34 +0000 (15:07 +0200)
committerPatrick <patrick.schoenberger@posteo.de>
Sun, 28 May 2023 13:07:34 +0000 (15:07 +0200)
Makefile
examples/Login.c
ext/mongoose
src/matrix.c
src/matrix.h
src/matrix_http_mongoose.c [new file with mode: 0644]

index f30f0578072bf46f15d4c4505bc7a06009997b5e..82c259ab65a27aa1862edd8649c5fb4aa731e656 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,13 +1,24 @@
 CC=gcc\r
 \r
 C_OPTS=-Wall -Wextra -pedantic\r
-C_OPTS+=-I src/\r
-C_OPTS+=-I ext/olm/include/\r
-C_OPTS+=-I ext/mjson/src\r
 C_OPTS+=src/matrix.c\r
-C_OPTS+=src/matrix_http_curl.c\r
+C_OPTS+=src/matrix_http_mongoose.c\r
 C_OPTS+=ext/mjson/src/mjson.c\r
-C_OPTS+=-l curl\r
+C_OPTS+=ext/mongoose/mongoose.c\r
+C_OPTS+=-I src/\r
+C_OPTS+=-I ext/olm/include/\r
+C_OPTS+=-I ext/mjson/src/\r
+C_OPTS+=-I ext/mongoose/\r
+C_OPTS+=-l ws2_32\r
+C_OPTS+=-l ssl\r
+C_OPTS+=-l crypto\r
+C_OPTS+=-D MG_ENABLE_OPENSSL=1\r
+C_OPTS+=-g\r
+# C_OPTS+=-I ext/curl/include/\r
+# C_OPTS+=-L ext/curl/build/lib/\r
+# C_OPTS+=-l curl\r
+\r
+#C_OPTS+=-Wl,--verbose\r
 \r
 out/examples/%: examples/%.c src/*\r
        $(CC) -o out/examples/$* examples/$*.c $(C_OPTS)\r
index 69ac3a11e21169aa08f120bcc1a73ea3ab998aa9..1c8e7cd9637816632c98d00e0412b48de9950938 100644 (file)
@@ -1,6 +1,6 @@
 #include <stdio.h>\r
 #include <matrix.h>\r
-#include <curl/curl.h>\r
+#include <mongoose.h>\r
 \r
 #define SERVER      "https://matrix.org"\r
 #define USERNAME    "pscho"\r
@@ -14,8 +14,7 @@ main()
     MatrixClient client;\r
     MatrixClientInit(&client, SERVER, strlen(SERVER));\r
     \r
-    curl_global_init(CURL_GLOBAL_DEFAULT);\r
-    client.httpUserData = (void *)curl_easy_init();\r
+    MatrixHttpInit(&client);\r
 \r
     MatrixClientLoginPassword(&client,\r
         USERNAME, strlen(USERNAME),\r
@@ -27,8 +26,7 @@ main()
     printf("Expires in (ms): %.*s\n", client.expireMsLen, client.expireMsBuffer);\r
     printf("Refresh Token: %.*s\n", client.refreshTokenLen, client.refreshTokenBuffer);\r
     \r
-    curl_easy_cleanup((CURL *)client.httpUserData);\r
-    curl_global_cleanup();\r
+    MatrixHttpDeinit(&client);\r
 \r
     return 0;\r
 }
\ No newline at end of file
index 45e4b3c423c7ee0f378b1b8682eccc533bed3f0d..96707acb4e991255702c7f36809cbd6e20a412fe 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 45e4b3c423c7ee0f378b1b8682eccc533bed3f0d
+Subproject commit 96707acb4e991255702c7f36809cbd6e20a412fe
index 90132af5f8a9b81c8a3dc755b6d5b1a12c7d63cd..790f0f572e77e446cbe56f86b64cb338ad378d44 100644 (file)
@@ -1,5 +1,6 @@
 #include "matrix.h"\r
 \r
+#include <stdio.h>\r
 #include <mjson.h>\r
 \r
 \r
@@ -11,8 +12,8 @@
 bool\r
 MatrixClientInit(\r
     MatrixClient * client,\r
-    char * server, int serverLen\r
-{\r
+    char * server, int serverLen)\r
+{\r
     strcpy_s(\r
         client->server,\r
         SERVER_SIZE,\r
@@ -29,8 +30,8 @@ MatrixClientLoginPassword(
     MatrixClient * client,\r
     char * username, int usernameLen,\r
     char * password, int passwordLen,\r
-    char * displayName, int displayNameLen\r
-{\r
+    char * displayName, int displayNameLen)\r
+{\r
     static char requestBuffer[LOGIN_REQUEST_SIZE];\r
 \r
     int requestLen =\r
index d37474f3017899ce4579e768d5f4cc918939aae8..b7f3a5ba2f91a7c318ce2b47f434b2f91c6a3ba2 100644 (file)
 \r
 \r
 typedef struct MatrixClient {\r
-    void * httpUserData;\r
     OlmAccount * olmAccount;\r
     OlmSession * olmSession;\r
+    \r
     char server[SERVER_SIZE]; int serverLen;\r
     char accessTokenBuffer[ACCESS_TOKEN_SIZE]; int accessTokenLen;\r
     char deviceIdBuffer[DEVICE_ID_SIZE]; int deviceIdLen;\r
     char expireMsBuffer[EXPIRE_MS_SIZE]; int expireMsLen;\r
     char refreshTokenBuffer[REFRESH_TOKEN_SIZE]; int refreshTokenLen;\r
+\r
+    void * httpUserData;\r
 } MatrixClient;\r
 \r
 bool\r
 MatrixClientInit(\r
     MatrixClient * client,\r
-    char * server, int serverLen\r
-);\r
+    char * server, int serverLen);\r
 \r
 bool\r
 MatrixClientLoginPassword(\r
     MatrixClient * client,\r
     char * username, int usernameLen,\r
     char * password, int passwordLen,\r
-    char * displayName, int displayNameLen\r
-);\r
+    char * displayName, int displayNameLen);\r
+\r
+bool\r
+MatrixHttpInit(\r
+    MatrixClient * client);\r
+\r
+bool\r
+MatrixHttpDeinit(\r
+    MatrixClient * client);\r
+\r
+bool\r
+MatrixHttpGet(\r
+    MatrixClient * client,\r
+    const char * url,\r
+    char * outResponseBuffer, int outResponseCap, int * outResponseLen);\r
 \r
 bool\r
 MatrixHttpPost(\r
     MatrixClient * client,\r
     const char * url,\r
     char * requestBuffer, int requestLen,\r
-    char * outResponseBuffer, int outResponseCap, int * outResponseLen\r
-);\r
+    char * outResponseBuffer, int outResponseCap, int * outResponseLen);\r
 \r
 #endif\r
diff --git a/src/matrix_http_mongoose.c b/src/matrix_http_mongoose.c
new file mode 100644 (file)
index 0000000..3faefa9
--- /dev/null
@@ -0,0 +1,153 @@
+#include "matrix.h"\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <string.h>\r
+#include <stdbool.h>\r
+#include <mongoose.h>\r
+\r
+//#define HTTP_DATA_SIZE 1024\r
+\r
+typedef struct MatrixHttpConnection {\r
+    struct mg_mgr mgr;\r
+    struct mg_connection * connection;\r
+    bool connected;\r
+    char * data;\r
+    int dataCap;\r
+    int dataLen;\r
+    bool dataReceived;\r
+} MatrixHttpConnection;\r
+\r
+static void\r
+MatrixHttpCallback(\r
+    struct mg_connection *c,\r
+    int ev,\r
+    void *ev_data,\r
+    void *fn_data)\r
+{\r
+    MatrixClient * client = (MatrixClient *)fn_data;\r
+    MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
+\r
+    if (ev == MG_EV_CONNECT)\r
+    {\r
+        struct mg_str host = mg_url_host(client->server);\r
+\r
+        // If s_url is https://, tell client connection to use TLS\r
+        if (mg_url_is_ssl(client->server))\r
+        {\r
+            struct mg_tls_opts opts = {.srvname = host};\r
+            mg_tls_init(c, &opts);\r
+        }\r
+\r
+        conn->connection = c;\r
+        conn->connected = true;\r
+    }\r
+    if (ev == MG_EV_HTTP_MSG)\r
+    {\r
+        // Response\r
+        struct mg_http_message *hm = (struct mg_http_message *)ev_data;\r
+        // memcpy_s(client->data, 1024, hm->message.ptr, hm->message.len);\r
+        // client->dataLen = hm->message.len;\r
+        memcpy_s(conn->data, conn->dataCap, hm->body.ptr, hm->body.len);\r
+        conn->dataLen = hm->body.len;\r
+        conn->dataReceived = true;\r
+    }\r
+}\r
+\r
+bool\r
+MatrixHttpInit(\r
+    MatrixClient * client)\r
+{\r
+    MatrixHttpConnection * conn =\r
+        (MatrixHttpConnection *)malloc(sizeof(MatrixHttpConnection));\r
+\r
+    client->httpUserData = conn;\r
+    \r
+    mg_mgr_init(&conn->mgr);\r
+\r
+    mg_http_connect(&conn->mgr, client->server, MatrixHttpCallback, client);\r
+\r
+    while (! conn->connected)\r
+        mg_mgr_poll(&conn->mgr, 1000);\r
+\r
+    return conn->connected;\r
+}\r
+\r
+bool\r
+MatrixHttpDeinit(\r
+    MatrixClient * client)\r
+{\r
+    MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
+    \r
+    mg_mgr_free(&conn->mgr);\r
+\r
+    free(conn);\r
+\r
+    return true;\r
+}\r
+\r
+bool\r
+MatrixHttpGet(\r
+    MatrixClient * client,\r
+    const char * url,\r
+    char * outResponseBuffer, int outResponseCap, int * outResponseLen)\r
+{\r
+    MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
+\r
+    conn->dataReceived = false;\r
+\r
+    struct mg_str host = mg_url_host(client->server);\r
+\r
+    mg_printf(conn->connection,\r
+        "GET %s HTTP/1.1\r\n"\r
+        "Host: %.*s\r\n"\r
+        "\r\n",\r
+        url,\r
+        host.len, host.ptr);\r
+\r
+    conn->data = outResponseBuffer;\r
+    conn->dataCap = outResponseCap;\r
+    \r
+    while (! conn->dataReceived)\r
+        mg_mgr_poll(&conn->mgr, 1000);\r
+\r
+    *outResponseLen = conn->dataLen;\r
+\r
+    return conn->dataReceived;\r
+}\r
+\r
+bool\r
+MatrixHttpPost(\r
+    MatrixClient * client,\r
+    const char * url,\r
+    char * requestBuffer, int requestLen,\r
+    char * outResponseBuffer, int outResponseCap, int * outResponseLen)\r
+{\r
+    MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
+\r
+    conn->dataReceived = false;\r
+\r
+    struct mg_str host = mg_url_host(client->server);\r
+\r
+    mg_printf(conn->connection,\r
+            "POST %s HTTP/1.0\r\n"\r
+            "Host: %.*s\r\n"\r
+            "Content-Type: application/json\r\n"\r
+            "Content-Length: %d\r\n"\r
+            "\r\n"\r
+            "%s"\r
+            "\r\n",\r
+            url,\r
+            host.len, host.ptr,\r
+            requestLen,\r
+            requestBuffer);\r
+\r
+    conn->data = outResponseBuffer;\r
+    conn->dataCap = outResponseCap;\r
+    \r
+    while (! conn->dataReceived)\r
+        mg_mgr_poll(&conn->mgr, 1000);\r
+\r
+    *outResponseLen = conn->dataLen;\r
+\r
+    return conn->dataReceived;\r
+}
\ No newline at end of file