]> gitweb.ps.run Git - matrix_esp_thesis/blob - src/matrix_http_mongoose.c
d44787c7760137dd3fb78aa1744f9d5849040813
[matrix_esp_thesis] / src / matrix_http_mongoose.c
1 #include "matrix.h"\r
2 #include <stdio.h>\r
3 #include <stdlib.h>\r
4 #include <string.h>\r
5 #include <stdbool.h>\r
6 #include <mongoose.h>\r
7 \r
8 //#define HTTP_DATA_SIZE 1024\r
9 \r
10 typedef struct MatrixHttpConnection {\r
11     struct mg_mgr mgr;\r
12     struct mg_connection * connection;\r
13     bool connected;\r
14     char * data;\r
15     int dataCap;\r
16     int dataLen;\r
17     bool dataReceived;\r
18 } MatrixHttpConnection;\r
19 \r
20 static void\r
21 MatrixHttpCallback(\r
22     struct mg_connection *c,\r
23     int ev,\r
24     void *ev_data,\r
25     void *fn_data)\r
26 {\r
27     MatrixClient * client = (MatrixClient *)fn_data;\r
28     MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
29 \r
30     if (ev == MG_EV_CONNECT)\r
31     {\r
32         struct mg_str host = mg_url_host(client->server);\r
33 \r
34         // If s_url is https://, tell client connection to use TLS\r
35         if (mg_url_is_ssl(client->server))\r
36         {\r
37             struct mg_tls_opts opts = {.srvname = host};\r
38             mg_tls_init(c, &opts);\r
39         }\r
40 \r
41         conn->connection = c;\r
42         conn->connected = true;\r
43     }\r
44     if (ev == MG_EV_HTTP_MSG)\r
45     {\r
46         // Response\r
47         struct mg_http_message *hm = (struct mg_http_message *)ev_data;\r
48         // memcpy_s(client->data, 1024, hm->message.ptr, hm->message.len);\r
49         // client->dataLen = hm->message.len;\r
50         memcpy_s(conn->data, conn->dataCap, hm->body.ptr, hm->body.len);\r
51         conn->dataLen = hm->body.len;\r
52         conn->dataReceived = true;\r
53     }\r
54 }\r
55 \r
56 bool\r
57 MatrixHttpInit(\r
58     MatrixClient * client)\r
59 {\r
60     MatrixHttpConnection * conn =\r
61         (MatrixHttpConnection *)malloc(sizeof(MatrixHttpConnection));\r
62 \r
63     client->httpUserData = conn;\r
64     \r
65     mg_mgr_init(&conn->mgr);\r
66 \r
67     mg_http_connect(&conn->mgr, client->server, MatrixHttpCallback, client);\r
68 \r
69     while (! conn->connected)\r
70         mg_mgr_poll(&conn->mgr, 1000);\r
71 \r
72     return conn->connected;\r
73 }\r
74 \r
75 bool\r
76 MatrixHttpDeinit(\r
77     MatrixClient * client)\r
78 {\r
79     MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
80     \r
81     mg_mgr_free(&conn->mgr);\r
82 \r
83     free(conn);\r
84 \r
85     return true;\r
86 }\r
87 \r
88 bool\r
89 MatrixHttpGet(\r
90     MatrixClient * client,\r
91     const char * url,\r
92     char * outResponseBuffer, int outResponseCap)\r
93 {\r
94     MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
95 \r
96     conn->dataReceived = false;\r
97 \r
98     struct mg_str host = mg_url_host(client->server);\r
99 \r
100     mg_printf(conn->connection,\r
101         "GET %s HTTP/1.1\r\n"\r
102         "Host: %.*s\r\n"\r
103         "\r\n",\r
104         url,\r
105         host.len, host.ptr);\r
106 \r
107     conn->data = outResponseBuffer;\r
108     conn->dataCap = outResponseCap;\r
109     \r
110     while (! conn->dataReceived)\r
111         mg_mgr_poll(&conn->mgr, 1000);\r
112 \r
113     return conn->dataReceived;\r
114 }\r
115 \r
116 bool\r
117 MatrixHttpPost(\r
118     MatrixClient * client,\r
119     const char * url,\r
120     const char * requestBuffer,\r
121     char * outResponseBuffer, int outResponseCap)\r
122 {\r
123     MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
124 \r
125     conn->dataReceived = false;\r
126 \r
127     struct mg_str host = mg_url_host(client->server);\r
128 \r
129     mg_printf(conn->connection,\r
130             "POST %s HTTP/1.0\r\n"\r
131             "Host: %.*s\r\n"\r
132             "Content-Type: application/json\r\n"\r
133             "Content-Length: %d\r\n"\r
134             "\r\n"\r
135             "%s"\r
136             "\r\n",\r
137             url,\r
138             host.len, host.ptr,\r
139             strlen(requestBuffer),\r
140             requestBuffer);\r
141 \r
142     conn->data = outResponseBuffer;\r
143     conn->dataCap = outResponseCap;\r
144     \r
145     while (! conn->dataReceived)\r
146         mg_mgr_poll(&conn->mgr, 1000);\r
147 \r
148     return conn->dataReceived;\r
149 }