1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
#include "matrix.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <mongoose.h>
//#define HTTP_DATA_SIZE 1024
typedef struct MatrixHttpConnection {
struct mg_mgr mgr;
struct mg_connection * connection;
bool connected;
char * data;
int dataCap;
int dataLen;
bool dataReceived;
} MatrixHttpConnection;
static void
MatrixHttpCallback(
struct mg_connection *c,
int ev,
void *ev_data,
void *fn_data)
{
MatrixClient * client = (MatrixClient *)fn_data;
MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
if (ev == MG_EV_CONNECT)
{
struct mg_str host = mg_url_host(client->server);
// If s_url is https://, tell client connection to use TLS
if (mg_url_is_ssl(client->server))
{
struct mg_tls_opts opts = {.srvname = host};
mg_tls_init(c, &opts);
}
conn->connection = c;
conn->connected = true;
}
if (ev == MG_EV_HTTP_MSG)
{
// Response
struct mg_http_message *hm = (struct mg_http_message *)ev_data;
// memcpy_s(client->data, 1024, hm->message.ptr, hm->message.len);
// client->dataLen = hm->message.len;
memcpy_s(conn->data, conn->dataCap, hm->body.ptr, hm->body.len);
conn->dataLen = hm->body.len;
conn->dataReceived = true;
}
}
bool
MatrixHttpInit(
MatrixClient * client)
{
MatrixHttpConnection * conn =
(MatrixHttpConnection *)malloc(sizeof(MatrixHttpConnection));
client->httpUserData = conn;
mg_mgr_init(&conn->mgr);
mg_http_connect(&conn->mgr, client->server, MatrixHttpCallback, client);
while (! conn->connected)
mg_mgr_poll(&conn->mgr, 1000);
return conn->connected;
}
bool
MatrixHttpDeinit(
MatrixClient * client)
{
MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
mg_mgr_free(&conn->mgr);
free(conn);
return true;
}
bool
MatrixHttpGet(
MatrixClient * client,
const char * url,
char * outResponseBuffer, int outResponseCap)
{
MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
conn->dataReceived = false;
struct mg_str host = mg_url_host(client->server);
mg_printf(conn->connection,
"GET %s HTTP/1.1\r\n"
"Host: %.*s\r\n"
"\r\n",
url,
host.len, host.ptr);
conn->data = outResponseBuffer;
conn->dataCap = outResponseCap;
while (! conn->dataReceived)
mg_mgr_poll(&conn->mgr, 1000);
return conn->dataReceived;
}
bool
MatrixHttpPost(
MatrixClient * client,
const char * url,
const char * requestBuffer,
char * outResponseBuffer, int outResponseCap)
{
MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;
conn->dataReceived = false;
struct mg_str host = mg_url_host(client->server);
mg_printf(conn->connection,
"POST %s HTTP/1.0\r\n"
"Host: %.*s\r\n"
"Content-Type: application/json\r\n"
"Content-Length: %d\r\n"
"\r\n"
"%s"
"\r\n",
url,
host.len, host.ptr,
strlen(requestBuffer),
requestBuffer);
conn->data = outResponseBuffer;
conn->dataCap = outResponseCap;
while (! conn->dataReceived)
mg_mgr_poll(&conn->mgr, 1000);
return conn->dataReceived;
}
|