-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_request.h
194 lines (158 loc) · 4.72 KB
/
http_request.h
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
#pragma once
#include "nanosock.h"
#include <cctype>
namespace nano {
namespace http {
struct BadSend : public std::exception {
const char* what() const noexcept {
return "nanosock: sending when a session is in progress";
}
};
struct BadTransfer : public std::exception {
const char* what() const noexcept {
return "nanosock: reading when nothing was sent";
}
};
struct Request {
nano::Socket sock;
nano::Buffer buff;
nano::Reader<> read_space;
nano::Reader<> read_line;
nano::Reader<nano::AnyOf<nano::Marker, nano::Marker>> read_key;
nano::Reader<Count> read_body;
std::string host;
std::string version;
std::string code;
std::string key;
std::string val;
size_t content_length = 0;
Request(const std::string& host, unsigned int port, unsigned int timeout = 0) :
sock(host, port, timeout),
read_space(" "),
read_line("\r\n"),
read_key(":", "\r\n"),
read_body(0),
host(host)
{}
enum {
STATE_SEND,
STATE_VERSION,
STATE_CODE,
STATE_FLAIR,
STATE_KEY,
STATE_VAL,
STATE_BODY
} state = STATE_SEND;
nano::Socket& socket() { return sock; }
bool drained() const { return buff.drained(); }
bool valid() const { return (state == STATE_SEND); }
void send(const std::string& method, const std::string& path, const std::string& body) {
if (state != STATE_SEND) {
throw BadSend();
}
std::string request = method;
request += " ";
request += path;
request += " HTTP/1.1\r\n";
if (body.size() > 0) {
request += "Content-Length: ";
request += std::to_string(body.size());
request += "\r\n";
}
request += "Host: ";
request += host;
request += "\r\n\r\n";
sock.send(request);
if (body.size() > 0) {
sock.send(body);
}
state = STATE_VERSION;
}
bool transfer(auto& responder, bool blocking = true) {
switch (state) {
case STATE_VERSION: {
if (read_space(buff, sock, [this](const std::string& part) {
version += part;
}, blocking)) {
responder.version(version);
version.clear();
state = STATE_CODE;
}
break;
}
case STATE_CODE: {
if (read_space(buff, sock, [this](const std::string& part) {
code += part;
}, blocking)) {
responder.code(code);
code.clear();
state = STATE_FLAIR;
}
break;
}
case STATE_FLAIR: {
if (read_line(buff, sock, [](const std::string&) {}, blocking)) {
state = STATE_KEY;
}
break;
}
case STATE_KEY: {
if (read_key(buff, sock, [this](const std::string& part) {
key += part;
}, blocking)) {
if (key == "\r\n") {
if (content_length == 0) {
state = STATE_SEND;
} else {
read_body.marker.n = content_length;
state = STATE_BODY;
}
} else {
state = STATE_VAL;
}
}
break;
}
case STATE_VAL: {
if (read_line(buff, sock, [this](const std::string& part) {
val += part;
}, blocking)) {
for (char& c : key) {
c = std::tolower(static_cast<unsigned char>(c));
}
if (key.starts_with("content-length")) {
content_length = std::stoul(val);
}
responder.header(key, val);
key.clear();
val.clear();
state = STATE_KEY;
}
break;
}
case STATE_BODY: {
if (read_body(buff, sock, [&responder](const std::string& body) {
responder.body(body);
}, blocking)) {
state = STATE_SEND;
}
break;
}
case STATE_SEND: {
throw BadTransfer();
}
}
return (state == STATE_SEND);
}
};
template <typename RES>
void send(const std::string& host, unsigned int port, unsigned int timeout,
const std::string& method, const std::string& path, const std::string& body,
RES& responder) {
Request req(host, port, timeout);
req.send(method, path, body);
while (!req.transfer(responder))
;
}
}
}