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