-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.cpp
316 lines (272 loc) · 7.82 KB
/
http.cpp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* Pufflux for the Adafruit Feather M0 WiFi - ATSAMD21 + ATWINC1500
* (Product ID: 3010)
*
* Copyright 2013-2022 Shaw Terwilliger <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <WiFi101.h>
#include "http.h"
#include "config.h"
static int http_request_id = 0;
// Reads one CRLF-terminated HTTP line from the client into a buffer
// and terminates it. Returns the number of bytes written to buf not
// including the terminator.
size_t http_read_line(WiFiClient *client, char *buf, size_t bufsize) {
bool last_char_was_cr = false;
char *start = buf;
// end leaves room for the terminator
char *end = start + bufsize - 1;
while (buf < end && client->connected()) {
int c = client->read();
if (c == -1) {
continue;
}
if (c == '\r') {
last_char_was_cr = true;
continue;
}
if (last_char_was_cr && c == '\n') {
// Done reading the line or the connection closed
break;
}
last_char_was_cr = false;
*buf++ = c;
}
*buf++ = '\0';
return (buf - start) - 1;
}
// Mutate a terminated string that contains an HTTP header and get
// pointers to the string parts. Returns true if the header was parsed
// successfully, false if it wasn't a valid header.
bool parse_header(char *str, char **name, char **value) {
*name = str;
while (*str != '\0') {
if (*str == ':') {
// If we haven't advanced past the name, then the name would be
// empty and that's invalid.
if (*name == str) {
return false;
}
// Terminate the name part and point to the value after it
*str = '\0';
str++;
// Walk past any leading spaces in the value
while (*str == ' ') {
str++;
}
*value = str;
return true;
}
str++;
}
// Never found a colon
return false;
}
bool parse_url(url_parts *parts, const char *url) {
typedef enum {
in_scheme,
in_colon_and_slashes,
in_host,
in_port,
in_path_and_query,
} parse_state;
char *scheme = parts->scheme;
const char *scheme_last = parts->scheme + sizeof(parts->scheme) - 1;
memset(parts->scheme, '\0', sizeof(parts->scheme));
char *host = parts->host;
const char *host_last = parts->host + sizeof(parts->host) - 1;
memset(parts->host, '\0', sizeof(parts->host));
char port_buf[6];
char *port = port_buf;
const char *port_last = port + sizeof(port_buf) - 1;
memset(port, '\0', sizeof(port_buf));
parts->port = 0;
char *path_and_query = parts->path_and_query;
const char *path_and_query_last = parts->path_and_query + sizeof(parts->path_and_query) - 1;
memset(parts->path_and_query, '\0', sizeof(parts->path_and_query));
parse_state state = in_scheme;
char c;
while ((c = *url) != '\0') {
switch (state) {
case in_scheme:
if (c == ':') {
state = in_colon_and_slashes;
} else {
// Write if enough room to preserve terminator
if (scheme < scheme_last) {
*scheme++ = c;
}
url++;
}
break;
case in_colon_and_slashes:
if (c == ':' || c == '/') {
url++;
} else {
state = in_host;
}
break;
case in_host:
if (c == ':') {
state = in_port;
url++;
} else if (c == '/') {
state = in_path_and_query;
} else {
// Write if enough room to preserve terminator
if (host < host_last) {
*host++ = c;
}
url++;
}
break;
case in_port:
if (c == '/') {
// If we read anything, convert it
if (port > port_buf) {
parts->port = atoi(port_buf);
}
state = in_path_and_query;
} else {
// Write if enough room to preserve terminator
if (port < port_last) {
*port++ = c;
}
url++;
}
break;
case in_path_and_query:
// Write if enough room to preserve terminator
if (path_and_query < path_and_query_last) {
*path_and_query++ = c;
}
url++;
break;
}
}
return state == in_path_and_query;
}
void http_request_init(http_request *req) {
req->host = "";
req->port = 80;
req->ssl = false;
req->path_and_query = "";
req->headers = NULL;
req->header_cb = NULL;
req->body_cb = NULL;
req->id = http_request_id++;
req->status = 0;
req->client = NULL;
}
#define DBG() print_dbg_prefix(req);
void print_dbg_prefix(http_request *req) {
Serial.print("http #");
Serial.print(req->id, DEC);
Serial.print(": ");
}
void http_request_disconnect(http_request *req) {
if (req->client != NULL) {
req->client->stop();
delete req->client;
DBG();
Serial.println("disconnected");
}
req->client = NULL;
}
bool http_request_connect(http_request *req) {
http_request_disconnect(req);
DBG();
Serial.print("host: ");
Serial.println(req->host);
DBG();
Serial.print("uri: ");
Serial.println(req->path_and_query);
if (req->ssl) {
req->client = new WiFiSSLClient();
DBG();
Serial.println("connecting (https)");
return req->client->connectSSL(req->host, req->port);
} else {
req->client = new WiFiClient();
DBG();
Serial.println("connecting (http)");
return req->client->connect(req->host, req->port);
}
}
void http_get(http_request *req) {
DBG();
Serial.println("get");
if (!http_request_connect(req)) {
req->status = HTTP_STATUS_CONNECT_ERR;
DBG();
Serial.println("connect failed");
} else {
req->client->print("GET ");
req->client->print(req->path_and_query);
req->client->println(" HTTP/1.1");
req->client->print("Host: ");
req->client->println(req->host);
req->client->println("Connection: close");
req->client->println("Accept: */*");
req->client->print("User-Agent: ");
req->client->println(NWS_USER_AGENT);
req->client->println();
req->client->flush();
char line[1024];
size_t read = http_read_line(req->client, line, sizeof(line));
// 12 chars is enough for "HTTP/1.1 200"
if (read < 12 || (strncmp(line, "HTTP/1.0 ", 9) != 0 && strncmp(line, "HTTP/1.1 ", 9) != 0)) {
req->status = HTTP_STATUS_MALFROMED_RESPONSE_LINE;
DBG();
Serial.print("malformed response line: ");
Serial.println(line);
http_request_disconnect(req);
return;
}
req->status = atoi(line + 9);
// Read headers until we read an empty line
do {
// Read headers
read = http_read_line(req->client, line, sizeof(line));
if (read > 0 && req->header_cb != NULL) {
char *header;
char *value;
if (!parse_header(line, &header, &value)) {
req->status = HTTP_STATUS_MALFROMED_RESPONSE_HEADER;
DBG();
Serial.print("malformed response header: ");
Serial.println(line);
http_request_disconnect(req);
return;
}
DBG();
Serial.print("header: ");
Serial.print(header);
Serial.print(": ");
Serial.println(value);
req->header_cb(req, header, value);
}
} while (read > 0);
// Now read the body
if (req->body_cb != NULL) {
DBG();
Serial.println("invoking body cb");
req->body_cb(req);
}
DBG();
Serial.println("success");
}
http_request_disconnect(req);
}