]> gitweb.ps.run Git - matrix_esp_thesis/blobdiff - src/matrix_http_mongoose.c
remove newline
[matrix_esp_thesis] / src / matrix_http_mongoose.c
index 2e500d69c5557ae35dff29bacb2a89bd4b8e4845..2f5d768febc131c94712abed94d934d9f138f14f 100644 (file)
@@ -6,12 +6,15 @@
 #include <stdbool.h>\r
 #include <mongoose.h>\r
 \r
 #include <stdbool.h>\r
 #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
     struct mg_connection * connection;\r
 #define AUTHORIZATION_HEADER_LEN 64\r
 \r
 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
     bool connected;\r
     char * data;\r
     int dataCap;\r
@@ -26,15 +29,14 @@ MatrixHttpCallback(
     void *ev_data,\r
     void *fn_data)\r
 {\r
     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
 \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
 \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
         {\r
             static struct mg_tls_opts opts;\r
             opts.srvname = host;\r
@@ -43,79 +45,102 @@ MatrixHttpCallback(
 \r
         conn->connection = c;\r
         conn->connected = true;\r
 \r
         conn->connection = c;\r
         conn->connected = true;\r
+    }\r
+    if (ev == MG_EV_HTTP_CHUNK)\r
+    {\r
+        \r
     }\r
     if (ev == MG_EV_HTTP_MSG)\r
     {\r
         // Response\r
         struct mg_http_message *hm = (struct mg_http_message *)ev_data;\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
+        \r
         memcpy(conn->data, 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
+        \r
         conn->data[hm->body.len] = '\0';\r
         conn->dataLen = hm->body.len;\r
         conn->dataReceived = true;\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
     }\r
 }\r
 \r
+bool\r
+MatrixHttpConnect(\r
+    MatrixHttpConnection * hc)\r
+{    \r
+    //struct mg_connection * c =\r
+        mg_http_connect(&hc->mgr, hc->host, MatrixHttpCallback, hc);\r
+\r
+    while (! hc->connected)\r
+        mg_mgr_poll(&hc->mgr, 1000);\r
+\r
+    return hc->connected;\r
+}\r
+\r
 bool\r
 MatrixHttpInit(\r
 bool\r
 MatrixHttpInit(\r
-    MatrixClient * client)\r
+    MatrixHttpConnection ** hc,\r
+    const char * host)\r
 {\r
 {\r
-    MatrixHttpConnection * conn =\r
-        (MatrixHttpConnection *)malloc(sizeof(MatrixHttpConnection));\r
-\r
-    client->httpUserData = conn;\r
+    *hc = (MatrixHttpConnection *)calloc(1, sizeof(MatrixHttpConnection));\r
     \r
     \r
-    mg_mgr_init(&conn->mgr);\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
 \r
-    while (! conn->connected)\r
-        mg_mgr_poll(&conn->mgr, 1000);\r
+    MatrixHttpConnect(*hc);\r
 \r
 \r
-    return conn->connected;\r
+    return true;\r
 }\r
 \r
 bool\r
 MatrixHttpDeinit(\r
 }\r
 \r
 bool\r
 MatrixHttpDeinit(\r
-    MatrixClient * client)\r
+    MatrixHttpConnection ** hc)\r
 {\r
 {\r
-    MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
-    \r
-    mg_mgr_free(&conn->mgr);\r
+    mg_mgr_free(&(*hc)->mgr);\r
 \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
 \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
     const char * url,\r
     char * outResponseBuffer, int outResponseCap,\r
     bool authenticated)\r
 {\r
-    MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
+    if (! hc->connected)\r
+        MatrixHttpConnect(hc);\r
 \r
 \r
-    conn->dataReceived = false;\r
+    hc->dataReceived = false;\r
 \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
 \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
+        snprintf(authorizationHeader, AUTHORIZATION_HEADER_LEN,\r
+            "Authorization: Bearer %s\r\n", hc->accessToken);\r
     else\r
         authorizationHeader[0] = '\0';\r
 \r
     else\r
         authorizationHeader[0] = '\0';\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
         "GET %s HTTP/1.1\r\n"\r
         "Host: %.*s\r\n"\r
         "%s"\r
@@ -124,37 +149,38 @@ MatrixHttpGet(
         host.len, host.ptr,\r
         authorizationHeader);\r
 \r
         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
     \r
-    while (! conn->dataReceived)\r
-        mg_mgr_poll(&conn->mgr, 1000);\r
+    while (! hc->dataReceived)\r
+        mg_mgr_poll(&hc->mgr, 1000);\r
 \r
 \r
-    return conn->dataReceived;\r
+    return hc->dataReceived;\r
 }\r
 \r
 bool\r
 MatrixHttpPost(\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
     const char * url,\r
     const char * requestBuffer,\r
     char * outResponseBuffer, int outResponseCap,\r
     bool authenticated)\r
 {\r
-    MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
+    if (! hc->connected)\r
+        MatrixHttpConnect(hc);\r
 \r
 \r
-    conn->dataReceived = false;\r
+    hc->dataReceived = false;\r
 \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
 \r
     static char authorizationHeader[AUTHORIZATION_HEADER_LEN];\r
     if (authenticated)\r
-        sprintf(authorizationHeader,\r
-            "Authorization: Bearer %s\r\n", client->accessToken);\r
+        snprintf(authorizationHeader, AUTHORIZATION_HEADER_LEN,\r
+            "Authorization: Bearer %s\r\n", hc->accessToken);\r
     else\r
         authorizationHeader[0] = '\0';\r
 \r
     else\r
         authorizationHeader[0] = '\0';\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
             "POST %s HTTP/1.0\r\n"\r
             "Host: %.*s\r\n"\r
             "%s"\r
@@ -164,56 +190,43 @@ MatrixHttpPost(
             "%s"\r
             "\r\n",\r
             url,\r
             "%s"\r
             "\r\n",\r
             url,\r
-            host.len, host.ptr,\r
+            (int)host.len, host.ptr,\r
             authorizationHeader,\r
             authorizationHeader,\r
-            strlen(requestBuffer),\r
+            (int)strlen(requestBuffer),\r
             requestBuffer);\r
 \r
             requestBuffer);\r
 \r
-    conn->data = outResponseBuffer;\r
-    conn->dataCap = outResponseCap;\r
+    hc->data = outResponseBuffer;\r
+    hc->dataCap = outResponseCap;\r
     \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
 \r
-    return conn->dataReceived;\r
+    return hc->dataReceived;\r
 }\r
 \r
 bool\r
 MatrixHttpPut(\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
     const char * url,\r
     const char * requestBuffer,\r
     char * outResponseBuffer, int outResponseCap,\r
     bool authenticated)\r
 {\r
-    MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
+    if (! hc->connected)\r
+        MatrixHttpConnect(hc);\r
 \r
 \r
-    conn->dataReceived = false;\r
+    hc->dataReceived = false;\r
 \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
 \r
     static char authorizationHeader[AUTHORIZATION_HEADER_LEN];\r
     if (authenticated)\r
-        sprintf(authorizationHeader,\r
-            "Authorization: Bearer %s\r\n", client->accessToken);\r
+        snprintf(authorizationHeader, AUTHORIZATION_HEADER_LEN,\r
+            "Authorization: Bearer %s\r\n", hc->accessToken);\r
     else\r
         authorizationHeader[0] = '\0';\r
 \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
+    mg_printf(hc->connection,\r
             "PUT %s HTTP/1.0\r\n"\r
             "Host: %.*s\r\n"\r
             "%s"\r
             "PUT %s HTTP/1.0\r\n"\r
             "Host: %.*s\r\n"\r
             "%s"\r
@@ -223,16 +236,16 @@ MatrixHttpPut(
             "%s"\r
             "\r\n",\r
             url,\r
             "%s"\r
             "\r\n",\r
             url,\r
-            host.len, host.ptr,\r
+            (int)host.len, host.ptr,\r
             authorizationHeader,\r
             authorizationHeader,\r
-            strlen(requestBuffer),\r
+            (int)strlen(requestBuffer),\r
             requestBuffer);\r
 \r
             requestBuffer);\r
 \r
-    conn->data = outResponseBuffer;\r
-    conn->dataCap = outResponseCap;\r
+    hc->data = outResponseBuffer;\r
+    hc->dataCap = outResponseCap;\r
     \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
 \r
-    return conn->dataReceived;\r
+    return hc->dataReceived;\r
 }\r
 }\r