]> gitweb.ps.run Git - matrix_esp_thesis/commitdiff
fix mongoose http layer
authorPatrick <patrick.schoenberger@posteo.de>
Mon, 13 Nov 2023 21:27:20 +0000 (22:27 +0100)
committerPatrick <patrick.schoenberger@posteo.de>
Mon, 13 Nov 2023 21:27:20 +0000 (22:27 +0100)
src/matrix_http_mongoose.c

index 452e26c59fb0b26de08671e449e16caaa029feec..3b6970cb0abee4d64a9e5d6518a2acc49c3fa7a9 100644 (file)
 typedef struct MatrixHttpConnection {\r
     struct mg_mgr mgr;\r
     struct mg_connection * connection;\r
+    \r
+    const char * host;\r
+    const char * accessToken;\r
+\r
     bool connected;\r
     char * data;\r
     int dataCap;\r
@@ -26,15 +30,14 @@ MatrixHttpCallback(
     void *ev_data,\r
     void *fn_data)\r
 {\r
-    MatrixClient * client = (MatrixClient *)fn_data;\r
-    MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
+    MatrixHttpConnection * conn = (MatrixHttpConnection *)fn_data;\r
 \r
     if (ev == MG_EV_CONNECT)\r
     {\r
-        struct mg_str host = mg_url_host(client->server);\r
+        struct mg_str host = mg_url_host(conn->host);\r
 \r
         // If s_url is https://, tell client connection to use TLS\r
-        if (mg_url_is_ssl(client->server))\r
+        if (mg_url_is_ssl(conn->host))\r
         {\r
             static struct mg_tls_opts opts;\r
             opts.srvname = host;\r
@@ -70,67 +73,74 @@ MatrixHttpCallback(
 }\r
 \r
 bool\r
-MatrixHttpInit(\r
-    MatrixClient * client)\r
-{\r
-    MatrixHttpConnection * conn =\r
-        (MatrixHttpConnection *)malloc(sizeof(MatrixHttpConnection));\r
+MatrixHttpConnect(\r
+    MatrixHttpConnection * hc)\r
+{    \r
+    //struct mg_connection * c =\r
+        mg_http_connect(&hc->mgr, hc->host, MatrixHttpCallback, hc);\r
 \r
-    client->httpUserData = conn;\r
-    \r
-    mg_mgr_init(&conn->mgr);\r
+    while (! hc->connected)\r
+        mg_mgr_poll(&hc->mgr, 1000);\r
 \r
-    return MatrixHttpConnect(client);\r
+    return hc->connected;\r
 }\r
 \r
 bool\r
-MatrixHttpConnect(\r
-    MatrixClient * client)\r
+MatrixHttpInit(\r
+    MatrixHttpConnection ** hc,\r
+    const char * host)\r
 {\r
-    MatrixHttpConnection * conn =\r
-        (MatrixHttpConnection *)client->httpUserData;\r
+    *hc = (MatrixHttpConnection *)calloc(1, sizeof(MatrixHttpConnection));\r
     \r
-    //struct mg_connection * c =\r
-        mg_http_connect(&conn->mgr, client->server, MatrixHttpCallback, client);\r
+    (*hc)->host = host;\r
+    \r
+    mg_mgr_init(&(*hc)->mgr);\r
 \r
-    while (! conn->connected)\r
-        mg_mgr_poll(&conn->mgr, 1000);\r
+    MatrixHttpConnect(*hc);\r
 \r
-    return conn->connected;\r
+    return true;\r
 }\r
 \r
 bool\r
 MatrixHttpDeinit(\r
-    MatrixClient * client)\r
+    MatrixHttpConnection ** hc)\r
 {\r
-    MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
-    \r
-    mg_mgr_free(&conn->mgr);\r
+    mg_mgr_free(&(*hc)->mgr);\r
 \r
-    free(conn);\r
+    free(*hc);\r
+    *hc = NULL;\r
+\r
+    return true;\r
+}\r
+\r
+bool\r
+MatrixHttpSetAccessToken(\r
+    MatrixHttpConnection * hc,\r
+    const char * accessToken)\r
+{\r
+    hc->accessToken = accessToken;\r
 \r
     return true;\r
 }\r
 \r
 bool\r
 MatrixHttpGet(\r
-    MatrixClient * client,\r
+    MatrixHttpConnection * hc,\r
     const char * url,\r
     char * outResponseBuffer, int outResponseCap,\r
     bool authenticated)\r
 {\r
-    MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
-    if (! conn->connected)\r
-        MatrixHttpConnect(client);\r
+    if (! hc->connected)\r
+        MatrixHttpConnect(hc);\r
 \r
-    conn->dataReceived = false;\r
+    hc->dataReceived = false;\r
 \r
-    struct mg_str host = mg_url_host(client->server);\r
+    struct mg_str host = mg_url_host(hc->host);\r
 \r
     static char authorizationHeader[AUTHORIZATION_HEADER_LEN];\r
     if (authenticated)\r
         sprintf(authorizationHeader,\r
-            "Authorization: Bearer %s\r\n", client->accessToken);\r
+            "Authorization: Bearer %s\r\n", hc->accessToken);\r
         // sprintf_s(authorizationHeader, AUTHORIZATION_HEADER_LEN,\r
         //     "Authorization: Bearer %s\r\n", client->accessToken);\r
     else\r
@@ -145,7 +155,7 @@ MatrixHttpGet(
         host.len, host.ptr,\r
         authorizationHeader);\r
 \r
-    mg_printf(conn->connection,\r
+    mg_printf(hc->connection,\r
         "GET %s HTTP/1.1\r\n"\r
         "Host: %.*s\r\n"\r
         "%s"\r
@@ -154,35 +164,34 @@ MatrixHttpGet(
         host.len, host.ptr,\r
         authorizationHeader);\r
 \r
-    conn->data = outResponseBuffer;\r
-    conn->dataCap = outResponseCap;\r
+    hc->data = outResponseBuffer;\r
+    hc->dataCap = outResponseCap;\r
     \r
-    while (! conn->dataReceived)\r
-        mg_mgr_poll(&conn->mgr, 1000);\r
+    while (! hc->dataReceived)\r
+        mg_mgr_poll(&hc->mgr, 1000);\r
 \r
-    return conn->dataReceived;\r
+    return hc->dataReceived;\r
 }\r
 \r
 bool\r
 MatrixHttpPost(\r
-    MatrixClient * client,\r
+    MatrixHttpConnection * hc,\r
     const char * url,\r
     const char * requestBuffer,\r
     char * outResponseBuffer, int outResponseCap,\r
     bool authenticated)\r
 {\r
-    MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
-    if (! conn->connected)\r
-        MatrixHttpConnect(client);\r
+    if (! hc->connected)\r
+        MatrixHttpConnect(hc);\r
 \r
-    conn->dataReceived = false;\r
+    hc->dataReceived = false;\r
 \r
-    struct mg_str host = mg_url_host(client->server);\r
+    struct mg_str host = mg_url_host(hc->host);\r
 \r
     static char authorizationHeader[AUTHORIZATION_HEADER_LEN];\r
     if (authenticated)\r
         sprintf(authorizationHeader,\r
-            "Authorization: Bearer %s\r\n", client->accessToken);\r
+            "Authorization: Bearer %s\r\n", hc->accessToken);\r
     else\r
         authorizationHeader[0] = '\0';\r
 \r
@@ -201,7 +210,7 @@ MatrixHttpPost(
             strlen(requestBuffer),\r
             requestBuffer);\r
 \r
-    mg_printf(conn->connection,\r
+    mg_printf(hc->connection,\r
             "POST %s HTTP/1.0\r\n"\r
             "Host: %.*s\r\n"\r
             "%s"\r
@@ -216,35 +225,34 @@ MatrixHttpPost(
             strlen(requestBuffer),\r
             requestBuffer);\r
 \r
-    conn->data = outResponseBuffer;\r
-    conn->dataCap = outResponseCap;\r
+    hc->data = outResponseBuffer;\r
+    hc->dataCap = outResponseCap;\r
     \r
-    while (! conn->dataReceived)\r
-        mg_mgr_poll(&conn->mgr, 1000);\r
+    while (! hc->dataReceived)\r
+        mg_mgr_poll(&hc->mgr, 1000);\r
 \r
-    return conn->dataReceived;\r
+    return hc->dataReceived;\r
 }\r
 \r
 bool\r
 MatrixHttpPut(\r
-    MatrixClient * client,\r
+    MatrixHttpConnection * hc,\r
     const char * url,\r
     const char * requestBuffer,\r
     char * outResponseBuffer, int outResponseCap,\r
     bool authenticated)\r
 {\r
-    MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
-    if (! conn->connected)\r
-        MatrixHttpConnect(client);\r
+    if (! hc->connected)\r
+        MatrixHttpConnect(hc);\r
 \r
-    conn->dataReceived = false;\r
+    hc->dataReceived = false;\r
 \r
-    struct mg_str host = mg_url_host(client->server);\r
+    struct mg_str host = mg_url_host(hc->host);\r
 \r
     static char authorizationHeader[AUTHORIZATION_HEADER_LEN];\r
     if (authenticated)\r
         sprintf(authorizationHeader,\r
-            "Authorization: Bearer %s\r\n", client->accessToken);\r
+            "Authorization: Bearer %s\r\n", hc->accessToken);\r
     else\r
         authorizationHeader[0] = '\0';\r
 \r
@@ -264,7 +272,7 @@ MatrixHttpPut(
             strlen(requestBuffer),\r
             requestBuffer);\r
 \r
-    mg_printf(conn->connection,\r
+    mg_printf(hc->connection,\r
             "PUT %s HTTP/1.0\r\n"\r
             "Host: %.*s\r\n"\r
             "%s"\r
@@ -279,11 +287,11 @@ MatrixHttpPut(
             strlen(requestBuffer),\r
             requestBuffer);\r
 \r
-    conn->data = outResponseBuffer;\r
-    conn->dataCap = outResponseCap;\r
+    hc->data = outResponseBuffer;\r
+    hc->dataCap = outResponseCap;\r
     \r
-    while (! conn->dataReceived)\r
-        mg_mgr_poll(&conn->mgr, 1000);\r
+    while (! hc->dataReceived)\r
+        mg_mgr_poll(&hc->mgr, 1000);\r
 \r
-    return conn->dataReceived;\r
+    return hc->dataReceived;\r
 }\r