]> gitweb.ps.run Git - matrix_esp_thesis/blob - src/matrix_http_mongoose.c
mongoose as http client
[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, int * outResponseLen)\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     *outResponseLen = conn->dataLen;\r
114 \r
115     return conn->dataReceived;\r
116 }\r
117 \r
118 bool\r
119 MatrixHttpPost(\r
120     MatrixClient * client,\r
121     const char * url,\r
122     char * requestBuffer, int requestLen,\r
123     char * outResponseBuffer, int outResponseCap, int * outResponseLen)\r
124 {\r
125     MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
126 \r
127     conn->dataReceived = false;\r
128 \r
129     struct mg_str host = mg_url_host(client->server);\r
130 \r
131     mg_printf(conn->connection,\r
132             "POST %s HTTP/1.0\r\n"\r
133             "Host: %.*s\r\n"\r
134             "Content-Type: application/json\r\n"\r
135             "Content-Length: %d\r\n"\r
136             "\r\n"\r
137             "%s"\r
138             "\r\n",\r
139             url,\r
140             host.len, host.ptr,\r
141             requestLen,\r
142             requestBuffer);\r
143 \r
144     conn->data = outResponseBuffer;\r
145     conn->dataCap = outResponseCap;\r
146     \r
147     while (! conn->dataReceived)\r
148         mg_mgr_poll(&conn->mgr, 1000);\r
149 \r
150     *outResponseLen = conn->dataLen;\r
151 \r
152     return conn->dataReceived;\r
153 }