]> gitweb.ps.run Git - matrix_esp_thesis/blobdiff - src/matrix_http_mongoose.c
send example, http PUT
[matrix_esp_thesis] / src / matrix_http_mongoose.c
index d44787c7760137dd3fb78aa1744f9d5849040813..8d575e5213bf37407c5590a85c03ce72123e74be 100644 (file)
@@ -6,6 +6,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
@@ -48,6 +49,7 @@ MatrixHttpCallback(
         // 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->data[hm->body.len] = '\0';\r
         conn->dataLen = hm->body.len;\r
         conn->dataReceived = true;\r
     }\r
@@ -89,7 +91,8 @@ bool
 MatrixHttpGet(\r
     MatrixClient * client,\r
     const char * url,\r
-    char * outResponseBuffer, int outResponseCap)\r
+    char * outResponseBuffer, int outResponseCap,\r
+    bool authenticated)\r
 {\r
     MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
 \r
@@ -97,12 +100,19 @@ MatrixHttpGet(
 \r
     struct mg_str host = mg_url_host(client->server);\r
 \r
+    static char authorizationHeader[AUTHORIZATION_HEADER_LEN] = "\0";\r
+    if (authenticated)\r
+        sprintf_s(authorizationHeader, AUTHORIZATION_HEADER_LEN,\r
+            "Authorization: Bearer %s\r\n", client->accessTokenBuffer);\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
@@ -118,7 +128,8 @@ MatrixHttpPost(
     MatrixClient * client,\r
     const char * url,\r
     const char * requestBuffer,\r
-    char * outResponseBuffer, int outResponseCap)\r
+    char * outResponseBuffer, int outResponseCap,\r
+    bool authenticated)\r
 {\r
     MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
 \r
@@ -126,9 +137,60 @@ MatrixHttpPost(
 \r
     struct mg_str host = mg_url_host(client->server);\r
 \r
+    static char authorizationHeader[AUTHORIZATION_HEADER_LEN] = "\0";\r
+    if (authenticated)\r
+        sprintf_s(authorizationHeader, AUTHORIZATION_HEADER_LEN,\r
+            "Authorization: Bearer %s\r\n", client->accessTokenBuffer);\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
+            "%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
+}\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
+\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_s(authorizationHeader, AUTHORIZATION_HEADER_LEN,\r
+            "Authorization: Bearer %s\r\n", client->accessTokenBuffer);\r
+    else\r
+        authorizationHeader[0] = '\0';\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
@@ -136,6 +198,7 @@ MatrixHttpPost(
             "\r\n",\r
             url,\r
             host.len, host.ptr,\r
+            authorizationHeader,\r
             strlen(requestBuffer),\r
             requestBuffer);\r
 \r
@@ -146,4 +209,4 @@ MatrixHttpPost(
         mg_mgr_poll(&conn->mgr, 1000);\r
 \r
     return conn->dataReceived;\r
-}
\ No newline at end of file
+}\r