X-Git-Url: https://gitweb.ps.run/matrix_esp_thesis/blobdiff_plain/ad9d01050b7b6d592a83ce14eeef7068bd981028..504241758d7b832af61939beaf61b0e0574174c4:/src/matrix_http_mongoose.c diff --git a/src/matrix_http_mongoose.c b/src/matrix_http_mongoose.c index 020b4c8..452e26c 100644 --- a/src/matrix_http_mongoose.c +++ b/src/matrix_http_mongoose.c @@ -36,13 +36,17 @@ MatrixHttpCallback( // If s_url is https://, tell client connection to use TLS if (mg_url_is_ssl(client->server)) { - struct mg_tls_opts opts; + static struct mg_tls_opts opts; opts.srvname = host; mg_tls_init(c, &opts); } conn->connection = c; conn->connected = true; + } + if (ev == MG_EV_HTTP_CHUNK) + { + } if (ev == MG_EV_HTTP_MSG) { @@ -56,7 +60,12 @@ MatrixHttpCallback( conn->dataLen = hm->body.len; conn->dataReceived = true; - printf("received[%d]:\n%.*s\n", conn->dataLen, conn->dataLen, conn->data); + //printf("received[%d]:\n%.*s\n", conn->dataLen, conn->dataLen, conn->data); + } + if (ev == MG_EV_CLOSE) + { + conn->connection = NULL; + conn->connected = false; } } @@ -71,7 +80,18 @@ MatrixHttpInit( mg_mgr_init(&conn->mgr); - mg_http_connect(&conn->mgr, client->server, MatrixHttpCallback, client); + return MatrixHttpConnect(client); +} + +bool +MatrixHttpConnect( + MatrixClient * client) +{ + MatrixHttpConnection * conn = + (MatrixHttpConnection *)client->httpUserData; + + //struct mg_connection * c = + mg_http_connect(&conn->mgr, client->server, MatrixHttpCallback, client); while (! conn->connected) mg_mgr_poll(&conn->mgr, 1000); @@ -100,6 +120,8 @@ MatrixHttpGet( bool authenticated) { MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData; + if (! conn->connected) + MatrixHttpConnect(client); conn->dataReceived = false; @@ -114,6 +136,15 @@ MatrixHttpGet( else authorizationHeader[0] = '\0'; + printf( + "GET %s HTTP/1.1\r\n" + "Host: %.*s\r\n" + "%s" + "\r\n", + url, + host.len, host.ptr, + authorizationHeader); + mg_printf(conn->connection, "GET %s HTTP/1.1\r\n" "Host: %.*s\r\n" @@ -141,6 +172,8 @@ MatrixHttpPost( bool authenticated) { MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData; + if (! conn->connected) + MatrixHttpConnect(client); conn->dataReceived = false; @@ -153,6 +186,21 @@ MatrixHttpPost( else authorizationHeader[0] = '\0'; + printf( + "POST %s HTTP/1.0\r\n" + "Host: %.*s\r\n" + "%s" + "Content-Type: application/json\r\n" + "Content-Length: %d\r\n" + "\r\n" + "%s" + "\r\n", + url, + host.len, host.ptr, + authorizationHeader, + strlen(requestBuffer), + requestBuffer); + mg_printf(conn->connection, "POST %s HTTP/1.0\r\n" "Host: %.*s\r\n" @@ -186,6 +234,8 @@ MatrixHttpPut( bool authenticated) { MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData; + if (! conn->connected) + MatrixHttpConnect(client); conn->dataReceived = false; @@ -198,6 +248,22 @@ MatrixHttpPut( else authorizationHeader[0] = '\0'; + + printf( + "PUT %s HTTP/1.0\r\n" + "Host: %.*s\r\n" + "%s" + "Content-Type: application/json\r\n" + "Content-Length: %d\r\n" + "\r\n" + "%s" + "\r\n", + url, + host.len, host.ptr, + authorizationHeader, + strlen(requestBuffer), + requestBuffer); + mg_printf(conn->connection, "PUT %s HTTP/1.0\r\n" "Host: %.*s\r\n"