]> gitweb.ps.run Git - matrix_esp_thesis/blobdiff - src/matrix_http_mongoose.c
work on SendEncrypted
[matrix_esp_thesis] / src / matrix_http_mongoose.c
index 3faefa9231d0379f25546641c86cef5d98afe704..17edbc8cdcfabe7b53e453a05b057ae71520429f 100644 (file)
@@ -1,4 +1,5 @@
 #include "matrix.h"\r
+\r
 #include <stdio.h>\r
 #include <stdlib.h>\r
 #include <string.h>\r
@@ -6,6 +7,7 @@
 #include <mongoose.h>\r
 \r
 //#define HTTP_DATA_SIZE 1024\r
+#define AUTHORIZATION_HEADER_LEN 64\r
 \r
 typedef struct MatrixHttpConnection {\r
     struct mg_mgr mgr;\r
@@ -34,7 +36,8 @@ MatrixHttpCallback(
         // 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
+            static struct mg_tls_opts opts;\r
+            opts.srvname = host;\r
             mg_tls_init(c, &opts);\r
         }\r
 \r
@@ -47,9 +50,18 @@ MatrixHttpCallback(
         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
+        memcpy(conn->data, hm->body.ptr, hm->body.len);\r
+        // memcpy_s(conn->data, conn->dataCap, hm->body.ptr, hm->body.len);\r
+        conn->data[hm->body.len] = '\0';\r
         conn->dataLen = hm->body.len;\r
         conn->dataReceived = true;\r
+\r
+        printf("received[%d]:\n%.*s\n", conn->dataLen, conn->dataLen, conn->data);\r
+    }\r
+    if (ev == MG_EV_CLOSE)\r
+    {\r
+        conn->connection = NULL;\r
+        conn->connected = false;\r
     }\r
 }\r
 \r
@@ -64,7 +76,18 @@ MatrixHttpInit(
     \r
     mg_mgr_init(&conn->mgr);\r
 \r
-    mg_http_connect(&conn->mgr, client->server, MatrixHttpCallback, client);\r
+    return MatrixHttpConnect(client);\r
+}\r
+\r
+bool\r
+MatrixHttpConnect(\r
+    MatrixClient * client)\r
+{\r
+    MatrixHttpConnection * conn =\r
+        (MatrixHttpConnection *)client->httpUserData;\r
+    \r
+    struct mg_connection * c =\r
+        mg_http_connect(&conn->mgr, client->server, MatrixHttpCallback, client);\r
 \r
     while (! conn->connected)\r
         mg_mgr_poll(&conn->mgr, 1000);\r
@@ -89,20 +112,34 @@ bool
 MatrixHttpGet(\r
     MatrixClient * client,\r
     const char * url,\r
-    char * outResponseBuffer, int outResponseCap, int * outResponseLen)\r
+    char * outResponseBuffer, int outResponseCap,\r
+    bool authenticated)\r
 {\r
     MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
+    if (! conn->connected)\r
+        MatrixHttpConnect(client);\r
 \r
     conn->dataReceived = false;\r
 \r
     struct mg_str host = mg_url_host(client->server);\r
 \r
+    static char authorizationHeader[AUTHORIZATION_HEADER_LEN];\r
+    if (authenticated)\r
+        sprintf(authorizationHeader,\r
+            "Authorization: Bearer %s\r\n", client->accessToken);\r
+        // sprintf_s(authorizationHeader, AUTHORIZATION_HEADER_LEN,\r
+        //     "Authorization: Bearer %s\r\n", client->accessToken);\r
+    else\r
+        authorizationHeader[0] = '\0';\r
+\r
     mg_printf(conn->connection,\r
         "GET %s HTTP/1.1\r\n"\r
         "Host: %.*s\r\n"\r
+        "%s"\r
         "\r\n",\r
         url,\r
-        host.len, host.ptr);\r
+        host.len, host.ptr,\r
+        authorizationHeader);\r
 \r
     conn->data = outResponseBuffer;\r
     conn->dataCap = outResponseCap;\r
@@ -110,8 +147,6 @@ MatrixHttpGet(
     while (! conn->dataReceived)\r
         mg_mgr_poll(&conn->mgr, 1000);\r
 \r
-    *outResponseLen = conn->dataLen;\r
-\r
     return conn->dataReceived;\r
 }\r
 \r
@@ -119,18 +154,29 @@ bool
 MatrixHttpPost(\r
     MatrixClient * client,\r
     const char * url,\r
-    char * requestBuffer, int requestLen,\r
-    char * outResponseBuffer, int outResponseCap, int * outResponseLen)\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
 \r
     conn->dataReceived = false;\r
 \r
     struct mg_str host = mg_url_host(client->server);\r
 \r
+    static char authorizationHeader[AUTHORIZATION_HEADER_LEN];\r
+    if (authenticated)\r
+        sprintf(authorizationHeader,\r
+            "Authorization: Bearer %s\r\n", client->accessToken);\r
+    else\r
+        authorizationHeader[0] = '\0';\r
+\r
     mg_printf(conn->connection,\r
             "POST %s HTTP/1.0\r\n"\r
             "Host: %.*s\r\n"\r
+            "%s"\r
             "Content-Type: application/json\r\n"\r
             "Content-Length: %d\r\n"\r
             "\r\n"\r
@@ -138,7 +184,8 @@ MatrixHttpPost(
             "\r\n",\r
             url,\r
             host.len, host.ptr,\r
-            requestLen,\r
+            authorizationHeader,\r
+            strlen(requestBuffer),\r
             requestBuffer);\r
 \r
     conn->data = outResponseBuffer;\r
@@ -147,7 +194,66 @@ MatrixHttpPost(
     while (! conn->dataReceived)\r
         mg_mgr_poll(&conn->mgr, 1000);\r
 \r
-    *outResponseLen = conn->dataLen;\r
+    return conn->dataReceived;\r
+}\r
+\r
+bool\r
+MatrixHttpPut(\r
+    MatrixClient * client,\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
+\r
+    conn->dataReceived = false;\r
+\r
+    struct mg_str host = mg_url_host(client->server);\r
+\r
+    static char authorizationHeader[AUTHORIZATION_HEADER_LEN];\r
+    if (authenticated)\r
+        sprintf(authorizationHeader,\r
+            "Authorization: Bearer %s\r\n", client->accessToken);\r
+    else\r
+        authorizationHeader[0] = '\0';\r
+\r
+    printf("PUT %s HTTP/1.0\r\n"\r
+            "Host: %.*s\r\n"\r
+            "%s"\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
+            authorizationHeader,\r
+            strlen(requestBuffer),\r
+            requestBuffer);\r
+\r
+    mg_printf(conn->connection,\r
+            "PUT %s HTTP/1.0\r\n"\r
+            "Host: %.*s\r\n"\r
+            "%s"\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
+            authorizationHeader,\r
+            strlen(requestBuffer),\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
     return conn->dataReceived;\r
-}
\ No newline at end of file
+}\r