]> gitweb.ps.run Git - matrix_esp_thesis/blob - src/matrix_http_mongoose.c
get send encrypted to send :)
[matrix_esp_thesis] / src / matrix_http_mongoose.c
1 #include "matrix.h"\r
2 \r
3 #include <stdio.h>\r
4 #include <stdlib.h>\r
5 #include <string.h>\r
6 #include <stdbool.h>\r
7 #include <mongoose.h>\r
8 \r
9 //#define HTTP_DATA_SIZE 1024\r
10 #define AUTHORIZATION_HEADER_LEN 64\r
11 \r
12 typedef struct MatrixHttpConnection {\r
13     struct mg_mgr mgr;\r
14     struct mg_connection * connection;\r
15     bool connected;\r
16     char * data;\r
17     int dataCap;\r
18     int dataLen;\r
19     bool dataReceived;\r
20 } MatrixHttpConnection;\r
21 \r
22 static void\r
23 MatrixHttpCallback(\r
24     struct mg_connection *c,\r
25     int ev,\r
26     void *ev_data,\r
27     void *fn_data)\r
28 {\r
29     MatrixClient * client = (MatrixClient *)fn_data;\r
30     MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
31 \r
32     if (ev == MG_EV_CONNECT)\r
33     {\r
34         struct mg_str host = mg_url_host(client->server);\r
35 \r
36         // If s_url is https://, tell client connection to use TLS\r
37         if (mg_url_is_ssl(client->server))\r
38         {\r
39             static struct mg_tls_opts opts;\r
40             opts.srvname = host;\r
41             mg_tls_init(c, &opts);\r
42         }\r
43 \r
44         conn->connection = c;\r
45         conn->connected = true;\r
46     }\r
47     if (ev == MG_EV_HTTP_MSG)\r
48     {\r
49         // Response\r
50         struct mg_http_message *hm = (struct mg_http_message *)ev_data;\r
51         // memcpy_s(client->data, 1024, hm->message.ptr, hm->message.len);\r
52         // client->dataLen = hm->message.len;\r
53         memcpy(conn->data, hm->body.ptr, hm->body.len);\r
54         // memcpy_s(conn->data, conn->dataCap, hm->body.ptr, hm->body.len);\r
55         conn->data[hm->body.len] = '\0';\r
56         conn->dataLen = hm->body.len;\r
57         conn->dataReceived = true;\r
58 \r
59         printf("received[%d]:\n%.*s\n", conn->dataLen, conn->dataLen, conn->data);\r
60     }\r
61 }\r
62 \r
63 bool\r
64 MatrixHttpInit(\r
65     MatrixClient * client)\r
66 {\r
67     MatrixHttpConnection * conn =\r
68         (MatrixHttpConnection *)malloc(sizeof(MatrixHttpConnection));\r
69 \r
70     client->httpUserData = conn;\r
71     \r
72     mg_mgr_init(&conn->mgr);\r
73 \r
74     struct mg_connection * c =\r
75         mg_http_connect(&conn->mgr, client->server, MatrixHttpCallback, client);\r
76 \r
77     while (! conn->connected)\r
78         mg_mgr_poll(&conn->mgr, 1000);\r
79 \r
80     return conn->connected;\r
81 }\r
82 \r
83 bool\r
84 MatrixHttpDeinit(\r
85     MatrixClient * client)\r
86 {\r
87     MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
88     \r
89     mg_mgr_free(&conn->mgr);\r
90 \r
91     free(conn);\r
92 \r
93     return true;\r
94 }\r
95 \r
96 bool\r
97 MatrixHttpGet(\r
98     MatrixClient * client,\r
99     const char * url,\r
100     char * outResponseBuffer, int outResponseCap,\r
101     bool authenticated)\r
102 {\r
103     MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
104 \r
105     conn->dataReceived = false;\r
106 \r
107     struct mg_str host = mg_url_host(client->server);\r
108 \r
109     static char authorizationHeader[AUTHORIZATION_HEADER_LEN];\r
110     if (authenticated)\r
111         sprintf(authorizationHeader,\r
112             "Authorization: Bearer %s\r\n", client->accessToken);\r
113         // sprintf_s(authorizationHeader, AUTHORIZATION_HEADER_LEN,\r
114         //     "Authorization: Bearer %s\r\n", client->accessToken);\r
115     else\r
116         authorizationHeader[0] = '\0';\r
117 \r
118     mg_printf(conn->connection,\r
119         "GET %s HTTP/1.1\r\n"\r
120         "Host: %.*s\r\n"\r
121         "%s"\r
122         "\r\n",\r
123         url,\r
124         host.len, host.ptr,\r
125         authorizationHeader);\r
126 \r
127     conn->data = outResponseBuffer;\r
128     conn->dataCap = outResponseCap;\r
129     \r
130     while (! conn->dataReceived)\r
131         mg_mgr_poll(&conn->mgr, 1000);\r
132 \r
133     return conn->dataReceived;\r
134 }\r
135 \r
136 bool\r
137 MatrixHttpPost(\r
138     MatrixClient * client,\r
139     const char * url,\r
140     const char * requestBuffer,\r
141     char * outResponseBuffer, int outResponseCap,\r
142     bool authenticated)\r
143 {\r
144     MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
145 \r
146     conn->dataReceived = false;\r
147 \r
148     struct mg_str host = mg_url_host(client->server);\r
149 \r
150     static char authorizationHeader[AUTHORIZATION_HEADER_LEN];\r
151     if (authenticated)\r
152         sprintf(authorizationHeader,\r
153             "Authorization: Bearer %s\r\n", client->accessToken);\r
154     else\r
155         authorizationHeader[0] = '\0';\r
156 \r
157     mg_printf(conn->connection,\r
158             "POST %s HTTP/1.0\r\n"\r
159             "Host: %.*s\r\n"\r
160             "%s"\r
161             "Content-Type: application/json\r\n"\r
162             "Content-Length: %d\r\n"\r
163             "\r\n"\r
164             "%s"\r
165             "\r\n",\r
166             url,\r
167             host.len, host.ptr,\r
168             authorizationHeader,\r
169             strlen(requestBuffer),\r
170             requestBuffer);\r
171 \r
172     conn->data = outResponseBuffer;\r
173     conn->dataCap = outResponseCap;\r
174     \r
175     while (! conn->dataReceived)\r
176         mg_mgr_poll(&conn->mgr, 1000);\r
177 \r
178     return conn->dataReceived;\r
179 }\r
180 \r
181 bool\r
182 MatrixHttpPut(\r
183     MatrixClient * client,\r
184     const char * url,\r
185     const char * requestBuffer,\r
186     char * outResponseBuffer, int outResponseCap,\r
187     bool authenticated)\r
188 {\r
189     MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
190 \r
191     conn->dataReceived = false;\r
192 \r
193     struct mg_str host = mg_url_host(client->server);\r
194 \r
195     static char authorizationHeader[AUTHORIZATION_HEADER_LEN];\r
196     if (authenticated)\r
197         sprintf(authorizationHeader,\r
198             "Authorization: Bearer %s\r\n", client->accessToken);\r
199     else\r
200         authorizationHeader[0] = '\0';\r
201 \r
202     printf("PUT %s HTTP/1.0\r\n"\r
203             "Host: %.*s\r\n"\r
204             "%s"\r
205             "Content-Type: application/json\r\n"\r
206             "Content-Length: %d\r\n"\r
207             "\r\n"\r
208             "%s"\r
209             "\r\n",\r
210             url,\r
211             host.len, host.ptr,\r
212             authorizationHeader,\r
213             strlen(requestBuffer),\r
214             requestBuffer);\r
215 \r
216     mg_printf(conn->connection,\r
217             "PUT %s HTTP/1.0\r\n"\r
218             "Host: %.*s\r\n"\r
219             "%s"\r
220             "Content-Type: application/json\r\n"\r
221             "Content-Length: %d\r\n"\r
222             "\r\n"\r
223             "%s"\r
224             "\r\n",\r
225             url,\r
226             host.len, host.ptr,\r
227             authorizationHeader,\r
228             strlen(requestBuffer),\r
229             requestBuffer);\r
230 \r
231     conn->data = outResponseBuffer;\r
232     conn->dataCap = outResponseCap;\r
233     \r
234     while (! conn->dataReceived)\r
235         mg_mgr_poll(&conn->mgr, 1000);\r
236 \r
237     return conn->dataReceived;\r
238 }\r