]> gitweb.ps.run Git - matrix_esp_thesis/blob - src/matrix_http_mongoose.c
020b4c8cf638256edc9db6a521b454f7c71c4f98
[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             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     mg_http_connect(&conn->mgr, client->server, MatrixHttpCallback, client);\r
75 \r
76     while (! conn->connected)\r
77         mg_mgr_poll(&conn->mgr, 1000);\r
78 \r
79     return conn->connected;\r
80 }\r
81 \r
82 bool\r
83 MatrixHttpDeinit(\r
84     MatrixClient * client)\r
85 {\r
86     MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
87     \r
88     mg_mgr_free(&conn->mgr);\r
89 \r
90     free(conn);\r
91 \r
92     return true;\r
93 }\r
94 \r
95 bool\r
96 MatrixHttpGet(\r
97     MatrixClient * client,\r
98     const char * url,\r
99     char * outResponseBuffer, int outResponseCap,\r
100     bool authenticated)\r
101 {\r
102     MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
103 \r
104     conn->dataReceived = false;\r
105 \r
106     struct mg_str host = mg_url_host(client->server);\r
107 \r
108     static char authorizationHeader[AUTHORIZATION_HEADER_LEN];\r
109     if (authenticated)\r
110         sprintf(authorizationHeader,\r
111             "Authorization: Bearer %s\r\n", client->accessToken);\r
112         // sprintf_s(authorizationHeader, AUTHORIZATION_HEADER_LEN,\r
113         //     "Authorization: Bearer %s\r\n", client->accessToken);\r
114     else\r
115         authorizationHeader[0] = '\0';\r
116 \r
117     mg_printf(conn->connection,\r
118         "GET %s HTTP/1.1\r\n"\r
119         "Host: %.*s\r\n"\r
120         "%s"\r
121         "\r\n",\r
122         url,\r
123         host.len, host.ptr,\r
124         authorizationHeader);\r
125 \r
126     conn->data = outResponseBuffer;\r
127     conn->dataCap = outResponseCap;\r
128     \r
129     while (! conn->dataReceived)\r
130         mg_mgr_poll(&conn->mgr, 1000);\r
131 \r
132     return conn->dataReceived;\r
133 }\r
134 \r
135 bool\r
136 MatrixHttpPost(\r
137     MatrixClient * client,\r
138     const char * url,\r
139     const char * requestBuffer,\r
140     char * outResponseBuffer, int outResponseCap,\r
141     bool authenticated)\r
142 {\r
143     MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
144 \r
145     conn->dataReceived = false;\r
146 \r
147     struct mg_str host = mg_url_host(client->server);\r
148 \r
149     static char authorizationHeader[AUTHORIZATION_HEADER_LEN];\r
150     if (authenticated)\r
151         sprintf(authorizationHeader,\r
152             "Authorization: Bearer %s\r\n", client->accessToken);\r
153     else\r
154         authorizationHeader[0] = '\0';\r
155 \r
156     mg_printf(conn->connection,\r
157             "POST %s HTTP/1.0\r\n"\r
158             "Host: %.*s\r\n"\r
159             "%s"\r
160             "Content-Type: application/json\r\n"\r
161             "Content-Length: %d\r\n"\r
162             "\r\n"\r
163             "%s"\r
164             "\r\n",\r
165             url,\r
166             host.len, host.ptr,\r
167             authorizationHeader,\r
168             strlen(requestBuffer),\r
169             requestBuffer);\r
170 \r
171     conn->data = outResponseBuffer;\r
172     conn->dataCap = outResponseCap;\r
173     \r
174     while (! conn->dataReceived)\r
175         mg_mgr_poll(&conn->mgr, 1000);\r
176 \r
177     return conn->dataReceived;\r
178 }\r
179 \r
180 bool\r
181 MatrixHttpPut(\r
182     MatrixClient * client,\r
183     const char * url,\r
184     const char * requestBuffer,\r
185     char * outResponseBuffer, int outResponseCap,\r
186     bool authenticated)\r
187 {\r
188     MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
189 \r
190     conn->dataReceived = false;\r
191 \r
192     struct mg_str host = mg_url_host(client->server);\r
193 \r
194     static char authorizationHeader[AUTHORIZATION_HEADER_LEN];\r
195     if (authenticated)\r
196         sprintf(authorizationHeader,\r
197             "Authorization: Bearer %s\r\n", client->accessToken);\r
198     else\r
199         authorizationHeader[0] = '\0';\r
200 \r
201     mg_printf(conn->connection,\r
202             "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     conn->data = outResponseBuffer;\r
217     conn->dataCap = outResponseCap;\r
218     \r
219     while (! conn->dataReceived)\r
220         mg_mgr_poll(&conn->mgr, 1000);\r
221 \r
222     return conn->dataReceived;\r
223 }\r