-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.cpp
More file actions
220 lines (180 loc) · 7.58 KB
/
http.cpp
File metadata and controls
220 lines (180 loc) · 7.58 KB
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
#include "http.h"
namespace HTTP {
namespace Common {
bool parse_status_line(WiFiClient &client, uint16_t *status_code){
String status = client.readStringUntil('\n');
// Serial.print("[HTTPClient::parse_status_line] status line: ");
// Serial.println(status);
if (sscanf(status.c_str(),
"HTTP/%*s %u %*s\r",
status_code) != 1){
Serial.print("[HTTP::Common::parse_status_line] can't parse status line, line is ");
Serial.println(status);
return false;
} else {
// Serial.print("[HTTPClient::parse_status_line] status code is ");
// Serial.println(status_code);
return true;
}
}
bool parse_headers(WiFiClient &client, HTTP::Type::headers_t &headers){
while (true){
String header = client.readStringUntil('\n');
//Serial.print("[HTTPClient::parse_header] header: ");
//Serial.println(header);
if (header == "\r"){
return true;
}
char header_key[10240], header_content[10240];
if (sscanf(header.c_str(),
"%10239[^:\r\n]: %10239[^\r\n]\r",
header_key,
header_content) != 2){
Serial.print("[HTTP::Common::parse_header] can't parse header, header is ");
Serial.println(header);
return false;
} else {
// Serial.print("[HTTPClient::parse_header] key is \"");
// Serial.print(header_key);
// Serial.print("\", content is \"");
// Serial.print(header_content);
// Serial.println("\"");
headers[std::string(header_key)] = header_content;
}
}
}
long determine_body_length(HTTP::Type::headers_t &headers){
auto length_iter = headers.find("Content-Length");
long length = -1;
if (length_iter != headers.end()){
length = std::stol((*length_iter).second);
}
return length;
}
bool read_body(WiFiClient &client, long length, std::string &dest){
if (length < 0){
Serial.println("[HTTP::Common::read_body] length is null, using fallback");
// if the server doesn't supply Content-Length
while (client.available()){
dest += client.read();
}
} else {
// Serial.print("[HTTPClient::read_body] Content-Length is ");
// Serial.println(length);
char tmp_response[length + 1];
client.readBytes(tmp_response, length);
dest.assign(tmp_response, length);
Serial.print("[HTTPClient::read_body] body is ");
Serial.println(dest.c_str());
}
return true;
}
bool parse_request_line(WiFiClient &client, uint8_t *method, std::string &path){
String status = client.readStringUntil('\n');
// Serial.print("[HTTPClient::parse_status_line] status line: ");
// Serial.println(status);
char method_buf[5], path_buf[10240];
if (sscanf(status.c_str(),
"%4s %10239[^ ] HTTP/%*s\r",
method_buf, path_buf) != 2){
Serial.print("[HTTP::Common::parse_request_line] can't parse request line, line is ");
Serial.println(status);
return false;
}
path_buf[10239] = '\0'; // paranoid
if (strcmp(method_buf, "GET") == 0){
*method = REQUEST_METHOD_GET;
} else if (strcmp(method_buf, "POST") == 0){
*method = REQUEST_METHOD_POST;
} else {
return false;
}
path.assign(path_buf, strlen(path_buf));
return true;
}
};
namespace Shortcut {
HTTP::Response POST(WiFiClient &client,
uint8_t method,
std::string &host,
const char url[],
HTTP::Type::post_data &data){
std::ostringstream data_stream;
for (auto pair = data.begin(); pair != data.end(); ++pair){
data_stream << (*pair).first << "=" << (*pair).second << "&";
}
std::string data_str = data_stream.str();
HTTP::Request req;
req.method = method;
req.headers["Host"] = host;
req.path = url;
req.body = data_str;
req.headers["Connection"] = "Keep-Alive";
req.headers["Content-Type"] = "application/x-www-form-urlencoded";
req.headers["Content-Length"] = std::to_string(data_str.length());
HTTP::Response resp;
Serial.println("[HTTP::Shortcut::POST] Request data is ");
Serial.println(req.render().c_str());
//if (client.connect(host.c_str(), port)){
client.print(req.render().c_str());
resp.success = resp.parse(client);
//} else {
// Serial.println("[HTTP::Shortcut::POST] can't connect to host");
//}
return resp;
}
}
bool Request::parse(WiFiClient &client){
client.setTimeout(HTTP::TIMEOUT);
if (!HTTP::Common::parse_request_line(client, &method, path)
|| !HTTP::Common::parse_headers(client, headers)){
return false;
}
if (method == REQUEST_METHOD_POST){
long length = HTTP::Common::determine_body_length(headers);
HTTP::Common::read_body(client, length, body);
}
return true;
}
std::string Request::render(){
std::ostringstream request_data;
if (method == REQUEST_METHOD_POST){
request_data << "POST ";
} else {
request_data << "GET ";
}
request_data << path << " HTTP/1.1\r\n";
for (auto header = headers.begin(); header != headers.end(); ++header){
request_data << (*header).first << ": " << (*header).second << "\r\n";
}
request_data << "\r\n";
request_data << body;
return request_data.str();
}
bool Response::parse(WiFiClient &client){
client.setTimeout(HTTP::TIMEOUT);
if ((!HTTP::Common::parse_status_line(client, &status_code))
|| (!HTTP::Common::parse_headers(client, headers))){
return false;
}
long length = HTTP::Common::determine_body_length(headers);
HTTP::Common::read_body(client, length, body);
return true;
}
std::string Response::render(){
std::map<uint16_t, std::string> STATUS_CODE_NAME = {
{200, "OK"},
{400, "Bad Request"},
{404, "Not Found"},
{500, "Internal Server Error"}
};
std::ostringstream request_data;
request_data << "HTTP/1.1 " << status_code << " " << STATUS_CODE_NAME[status_code] << "\r\n";
for (auto header = headers.begin(); header != headers.end(); ++header){
request_data << (*header).first << ": " << (*header).second << "\r\n";
}
request_data << "\r\n";
request_data << body;
return request_data.str();
}
}