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