/src/pdns/ext/yahttp/yahttp/reqresp.hpp
Line | Count | Source |
1 | | #if __cplusplus >= 201103L |
2 | | #include <functional> |
3 | | #define HAVE_CPP_FUNC_PTR |
4 | | namespace funcptr = std; |
5 | | #else |
6 | | #ifdef HAVE_BOOST |
7 | | #include <boost/function.hpp> |
8 | | namespace funcptr = boost; |
9 | | #define HAVE_CPP_FUNC_PTR |
10 | | #endif |
11 | | #endif |
12 | | |
13 | | #include <fstream> |
14 | | #include <cctype> |
15 | | |
16 | | #ifndef WIN32 |
17 | | #include <cstdio> |
18 | | #include <unistd.h> |
19 | | #endif |
20 | | |
21 | | #include <algorithm> |
22 | | |
23 | | #ifndef YAHTTP_MAX_HEADER_SIZE |
24 | 12.1k | #define YAHTTP_MAX_HEADER_SIZE (100 * 1024) |
25 | | #endif |
26 | | |
27 | | #ifndef YAHTTP_MAX_REQUEST_SIZE |
28 | 12.1k | #define YAHTTP_MAX_REQUEST_SIZE 2097152 |
29 | | #endif |
30 | | |
31 | | #ifndef YAHTTP_MAX_RESPONSE_SIZE |
32 | 12.1k | #define YAHTTP_MAX_RESPONSE_SIZE 2097152 |
33 | | #endif |
34 | | |
35 | 18.4k | #define YAHTTP_TYPE_REQUEST 1 |
36 | 754 | #define YAHTTP_TYPE_RESPONSE 2 |
37 | | |
38 | | namespace YaHTTP { |
39 | | typedef std::map<std::string,Cookie,ASCIICINullSafeComparator> strcookie_map_t; //<! String to Cookie map |
40 | | |
41 | | typedef enum { |
42 | | urlencoded, |
43 | | multipart |
44 | | } postformat_t; //<! Enumeration of possible post encodings, url encoding or multipart |
45 | | |
46 | | /*! Base class for request and response */ |
47 | | class HTTPBase { |
48 | | public: |
49 | | /*! Default renderer for request/response, simply copies body to response */ |
50 | | class SendBodyRender { |
51 | | public: |
52 | 12.1k | SendBodyRender() {}; |
53 | | |
54 | 0 | size_t operator()(const HTTPBase *doc, std::ostream& os, bool chunked) const { |
55 | 0 | if (chunked) { |
56 | 0 | std::string::size_type i,cl; |
57 | 0 | for(i=0;i<doc->body.length();i+=1024) { |
58 | 0 | cl = std::min(static_cast<std::string::size_type>(1024), doc->body.length()-i); // for less than 1k blocks |
59 | 0 | os << std::hex << cl << std::dec << "\r\n"; |
60 | 0 | os << doc->body.substr(i, cl) << "\r\n"; |
61 | 0 | } |
62 | 0 | os << 0 << "\r\n\r\n"; // last chunk |
63 | 0 | } else { |
64 | 0 | os << doc->body; |
65 | 0 | } |
66 | 0 | return doc->body.length(); |
67 | 0 | }; //<! writes body to ostream and returns length |
68 | | }; |
69 | | /* Simple sendfile renderer which streams file to ostream */ |
70 | | class SendFileRender { |
71 | | public: |
72 | 0 | SendFileRender(const std::string& path_) { |
73 | 0 | this->path = path_; |
74 | 0 | }; |
75 | | |
76 | 0 | size_t operator()(const HTTPBase *doc __attribute__((unused)), std::ostream& os, bool chunked) const { |
77 | 0 | char buf[4096]; |
78 | 0 | size_t n,k; |
79 | 0 | #if __cplusplus >= 201103L |
80 | 0 | std::ifstream ifs(path, std::ifstream::binary); |
81 | 0 | #else |
82 | 0 | std::ifstream ifs(path.c_str(), std::ifstream::binary); |
83 | 0 | #endif |
84 | 0 | n = 0; |
85 | 0 |
|
86 | 0 | while(ifs.good()) { |
87 | 0 | ifs.read(buf, sizeof buf); |
88 | 0 | n += (k = ifs.gcount()); |
89 | 0 | if (k > 0) { |
90 | 0 | if (chunked) os << std::hex << k << std::dec << "\r\n"; |
91 | 0 | os.write(buf, k); |
92 | 0 | if (chunked) os << "\r\n"; |
93 | 0 | } |
94 | 0 | } |
95 | 0 | if (chunked) os << 0 << "\r\n\r\n"; |
96 | 0 | return n; |
97 | 0 | }; //<! writes file to ostream and returns length |
98 | | |
99 | | std::string path; //<! File to send |
100 | | }; |
101 | | |
102 | 4.04k | HTTPBase() { |
103 | 4.04k | HTTPBase::initialize(); |
104 | 4.04k | }; |
105 | 4.04k | virtual ~HTTPBase() = default; |
106 | | |
107 | 12.1k | virtual void initialize() { |
108 | 12.1k | kind = 0; |
109 | 12.1k | status = 0; |
110 | 12.1k | #ifdef HAVE_CPP_FUNC_PTR |
111 | 12.1k | renderer = SendBodyRender(); |
112 | 12.1k | #endif |
113 | 12.1k | max_request_size = YAHTTP_MAX_REQUEST_SIZE; |
114 | 12.1k | max_response_size = YAHTTP_MAX_RESPONSE_SIZE; |
115 | 12.1k | max_header_size = YAHTTP_MAX_HEADER_SIZE; |
116 | 12.1k | url = ""; |
117 | 12.1k | method = ""; |
118 | 12.1k | statusText = ""; |
119 | 12.1k | jar.clear(); |
120 | 12.1k | headers.clear(); |
121 | 12.1k | parameters.clear(); |
122 | 12.1k | getvars.clear(); |
123 | 12.1k | postvars.clear(); |
124 | 12.1k | body = ""; |
125 | 12.1k | routeName = ""; |
126 | 12.1k | version = 11; // default to version 1.1 |
127 | 12.1k | is_multipart = false; |
128 | 12.1k | } |
129 | | protected: |
130 | 0 | HTTPBase(const HTTPBase& rhs) { |
131 | 0 | this->url = rhs.url; this->kind = rhs.kind; |
132 | 0 | this->status = rhs.status; this->statusText = rhs.statusText; |
133 | 0 | this->method = rhs.method; this->headers = rhs.headers; |
134 | 0 | this->jar = rhs.jar; this->postvars = rhs.postvars; |
135 | 0 | this->parameters = rhs.parameters; this->getvars = rhs.getvars; |
136 | 0 | this->body = rhs.body; this->max_request_size = rhs.max_request_size; |
137 | 0 | this->max_response_size = rhs.max_response_size; this->version = rhs.version; |
138 | 0 | this->max_header_size = rhs.max_header_size; |
139 | 0 | #ifdef HAVE_CPP_FUNC_PTR |
140 | 0 | this->renderer = rhs.renderer; |
141 | 0 | #endif |
142 | 0 | this->is_multipart = rhs.is_multipart; |
143 | 0 | }; |
144 | 0 | virtual HTTPBase& operator=(const HTTPBase& rhs) { |
145 | 0 | this->url = rhs.url; this->kind = rhs.kind; |
146 | 0 | this->status = rhs.status; this->statusText = rhs.statusText; |
147 | 0 | this->method = rhs.method; this->headers = rhs.headers; |
148 | 0 | this->jar = rhs.jar; this->postvars = rhs.postvars; |
149 | 0 | this->parameters = rhs.parameters; this->getvars = rhs.getvars; |
150 | 0 | this->body = rhs.body; this->max_request_size = rhs.max_request_size; |
151 | 0 | this->max_response_size = rhs.max_response_size; this->version = rhs.version; |
152 | 0 | this->max_header_size = rhs.max_header_size; |
153 | 0 | #ifdef HAVE_CPP_FUNC_PTR |
154 | 0 | this->renderer = rhs.renderer; |
155 | 0 | #endif |
156 | 0 | this->is_multipart = rhs.is_multipart; |
157 | 0 | return *this; |
158 | 0 | }; |
159 | | public: |
160 | | URL url; //<! URL of this request/response |
161 | | int kind; //<! Type of object (1 = request, 2 = response) |
162 | | int status; //<! status code |
163 | | int version; //<! http version 9 = 0.9, 10 = 1.0, 11 = 1.1 |
164 | | std::string statusText; //<! textual representation of status code |
165 | | std::string method; //<! http verb |
166 | | strstr_map_t headers; //<! map of header(s) |
167 | | CookieJar jar; //<! cookies |
168 | | strstr_map_t postvars; //<! map of POST variables (from POST body) |
169 | | strstr_map_t getvars; //<! map of GET variables (from URL) |
170 | | // these two are for Router |
171 | | strstr_map_t parameters; //<! map of route parameters (only if you use YaHTTP::Router) |
172 | | std::string routeName; //<! name of the current route (only if you use YaHTTP::Router) |
173 | | |
174 | | std::string body; //<! the actual content |
175 | | |
176 | | size_t max_request_size; //<! maximum size of request |
177 | | size_t max_response_size; //<! maximum size of response |
178 | | size_t max_header_size; //<! maximum size of headers |
179 | | bool is_multipart; //<! if the request is multipart, prevents Content-Length header |
180 | | #ifdef HAVE_CPP_FUNC_PTR |
181 | | funcptr::function<size_t(const HTTPBase*,std::ostream&,bool)> renderer; //<! rendering function |
182 | | #endif |
183 | | void write(std::ostream& os) const; //<! writes request to the given output stream |
184 | | |
185 | 0 | strstr_map_t& GET() { return getvars; }; //<! acccessor for getvars |
186 | 0 | strstr_map_t& POST() { return postvars; }; //<! accessor for postvars |
187 | 0 | strcookie_map_t& COOKIES() { return jar.cookies; }; //<! accessor for cookies |
188 | | |
189 | 0 | std::string versionStr(int version_) const { |
190 | 0 | switch(version_) { |
191 | 0 | case 9: return "0.9"; |
192 | 0 | case 10: return "1.0"; |
193 | 0 | case 11: return "1.1"; |
194 | 0 | default: throw YaHTTP::Error("Unsupported version"); |
195 | 0 | } |
196 | 0 | }; |
197 | | |
198 | 0 | std::string str() const { |
199 | 0 | std::ostringstream oss; |
200 | 0 | write(oss); |
201 | 0 | return oss.str(); |
202 | 0 | }; //<! return string representation of this object |
203 | | }; |
204 | | |
205 | | /*! Response class, represents a HTTP Response document */ |
206 | | class Response: public HTTPBase { |
207 | | public: |
208 | 0 | Response() { Response::initialize(); }; |
209 | 0 | Response(const HTTPBase& rhs): HTTPBase(rhs) { |
210 | 0 | this->kind = YAHTTP_TYPE_RESPONSE; |
211 | 0 | }; |
212 | 0 | Response& operator=(const HTTPBase& rhs) override { |
213 | 0 | HTTPBase::operator=(rhs); |
214 | 0 | this->kind = YAHTTP_TYPE_RESPONSE; |
215 | 0 | return *this; |
216 | 0 | }; |
217 | 0 | void initialize() override { |
218 | 0 | HTTPBase::initialize(); |
219 | 0 | this->kind = YAHTTP_TYPE_RESPONSE; |
220 | 0 | } |
221 | 0 | void initialize(const HTTPBase& rhs) { |
222 | 0 | HTTPBase::initialize(); |
223 | 0 | this->kind = YAHTTP_TYPE_RESPONSE; |
224 | 0 | // copy SOME attributes |
225 | 0 | this->url = rhs.url; |
226 | 0 | this->method = rhs.method; |
227 | 0 | this->jar = rhs.jar; |
228 | 0 | this->version = rhs.version; |
229 | 0 | } |
230 | | friend std::ostream& operator<<(std::ostream& os, const Response &resp); |
231 | | friend std::istream& operator>>(std::istream& is, Response &resp); |
232 | | }; |
233 | | |
234 | | /* Request class, represents a HTTP Request document */ |
235 | | class Request: public HTTPBase { |
236 | | public: |
237 | 4.04k | Request() { Request::initialize(); }; |
238 | 0 | Request(const HTTPBase& rhs): HTTPBase(rhs) { |
239 | 0 | this->kind = YAHTTP_TYPE_REQUEST; |
240 | 0 | }; |
241 | 0 | Request& operator=(const HTTPBase& rhs) override { |
242 | 0 | HTTPBase::operator=(rhs); |
243 | 0 | this->kind = YAHTTP_TYPE_REQUEST; |
244 | 0 | return *this; |
245 | 0 | }; |
246 | 8.09k | void initialize() override { |
247 | 8.09k | HTTPBase::initialize(); |
248 | 8.09k | this->kind = YAHTTP_TYPE_REQUEST; |
249 | 8.09k | } |
250 | 0 | void initialize(const HTTPBase& rhs) { |
251 | 0 | HTTPBase::initialize(); |
252 | 0 | this->kind = YAHTTP_TYPE_REQUEST; |
253 | 0 | // copy SOME attributes |
254 | 0 | this->url = rhs.url; |
255 | 0 | this->method = rhs.method; |
256 | 0 | this->jar = rhs.jar; |
257 | 0 | this->version = rhs.version; |
258 | 0 | } |
259 | 0 | void setup(const std::string& method_, const std::string& url_) { |
260 | 0 | this->url.parse(url_); |
261 | 0 | this->headers["host"] = this->url.host.find(':') == std::string::npos ? this->url.host : "[" + this->url.host + "]"; |
262 | 0 | this->method = method_; |
263 | 0 | std::transform(this->method.begin(), this->method.end(), this->method.begin(), ::toupper); |
264 | 0 | this->headers["user-agent"] = "YaHTTP v1.0"; |
265 | 0 | }; //<! Set some initial things for a request |
266 | | |
267 | 0 | void preparePost(postformat_t format = urlencoded) { |
268 | 0 | std::ostringstream postbuf; |
269 | 0 | if (format == urlencoded) { |
270 | 0 | for(strstr_map_t::const_iterator i = POST().begin(); i != POST().end(); i++) { |
271 | 0 | postbuf << Utility::encodeURL(i->first, false) << "=" << Utility::encodeURL(i->second, false) << "&"; |
272 | 0 | } |
273 | 0 | // remove last bit |
274 | 0 | if (postbuf.str().length()>0) |
275 | 0 | body = postbuf.str().substr(0, postbuf.str().length()-1); |
276 | 0 | else |
277 | 0 | body = ""; |
278 | 0 | headers["content-type"] = "application/x-www-form-urlencoded; charset=utf-8"; |
279 | 0 | } else if (format == multipart) { |
280 | 0 | headers["content-type"] = "multipart/form-data; boundary=YaHTTP-12ca543"; |
281 | 0 | this->is_multipart = true; |
282 | 0 | for(strstr_map_t::const_iterator i = POST().begin(); i != POST().end(); i++) { |
283 | 0 | postbuf << "--YaHTTP-12ca543\r\nContent-Disposition: form-data; name=\"" << Utility::encodeURL(i->first, false) << "\"; charset=UTF-8\r\nContent-Length: " << i->second.size() << "\r\n\r\n" |
284 | 0 | << Utility::encodeURL(i->second, false) << "\r\n"; |
285 | 0 | } |
286 | 0 | postbuf << "--"; |
287 | 0 | body = postbuf.str(); |
288 | 0 | } |
289 | 0 |
|
290 | 0 | postbuf.str(""); |
291 | 0 | postbuf << body.length(); |
292 | 0 | // set method and change headers |
293 | 0 | method = "POST"; |
294 | 0 | if (!this->is_multipart) |
295 | 0 | headers["content-length"] = postbuf.str(); |
296 | 0 | }; //<! convert all postvars into string and stuff it into body |
297 | | |
298 | | friend std::ostream& operator<<(std::ostream& os, const Request &resp); |
299 | | friend std::istream& operator>>(std::istream& is, Request &resp); |
300 | | }; |
301 | | |
302 | | /*! Asynchronous HTTP document loader */ |
303 | | template <class T> |
304 | | class AsyncLoader { |
305 | | public: |
306 | | T* target; //<! target to populate |
307 | | int state; //<! reader state |
308 | | size_t pos; //<! reader position |
309 | | |
310 | | std::string buffer; //<! read buffer |
311 | | bool chunked; //<! whether we are parsing chunked data |
312 | | size_t chunk_size; //<! expected size of next chunk |
313 | | std::ostringstream bodybuf; //<! buffer for body |
314 | | size_t maxbody; //<! maximum size of body |
315 | | size_t minbody; //<! minimum size of body |
316 | | size_t headersize; |
317 | | size_t bodysize; |
318 | | bool hasBody; //<! are we expecting body |
319 | | |
320 | | void keyValuePair(const std::string &keyvalue, std::string &key, std::string &value); //<! key value pair parser helper |
321 | | |
322 | 4.04k | void initialize(T* target_) { |
323 | 4.04k | chunked = false; chunk_size = 0; |
324 | 4.04k | bodybuf.str(""); minbody = 0; maxbody = 0; |
325 | 4.04k | pos = 0; state = 0; this->target = target_; |
326 | 4.04k | hasBody = false; |
327 | 4.04k | buffer = ""; |
328 | 4.04k | headersize = 0; |
329 | 4.04k | bodysize = 0; |
330 | 4.04k | this->target->initialize(); |
331 | 4.04k | }; //<! Initialize the parser for target and clear state YaHTTP::AsyncLoader<YaHTTP::Request>::initialize(YaHTTP::Request*) Line | Count | Source | 322 | 4.04k | void initialize(T* target_) { | 323 | 4.04k | chunked = false; chunk_size = 0; | 324 | 4.04k | bodybuf.str(""); minbody = 0; maxbody = 0; | 325 | 4.04k | pos = 0; state = 0; this->target = target_; | 326 | 4.04k | hasBody = false; | 327 | 4.04k | buffer = ""; | 328 | 4.04k | headersize = 0; | 329 | 4.04k | bodysize = 0; | 330 | 4.04k | this->target->initialize(); | 331 | 4.04k | }; //<! Initialize the parser for target and clear state |
Unexecuted instantiation: YaHTTP::AsyncLoader<YaHTTP::Response>::initialize(YaHTTP::Response*) |
332 | | bool feed(const std::string& somedata); //<! Feed data to the parser |
333 | 650 | bool ready() { |
334 | 650 | return (chunked == true && state == 3) || // if it's chunked we get end of data indication |
335 | 642 | (chunked == false && state > 1 && |
336 | 582 | (!hasBody || (bodysize <= maxbody && bodysize >= minbody))); |
337 | 650 | }; //<! whether we have received enough data YaHTTP::AsyncLoader<YaHTTP::Request>::ready() Line | Count | Source | 333 | 650 | bool ready() { | 334 | 650 | return (chunked == true && state == 3) || // if it's chunked we get end of data indication | 335 | 642 | (chunked == false && state > 1 && | 336 | 582 | (!hasBody || (bodysize <= maxbody && bodysize >= minbody))); | 337 | 650 | }; //<! whether we have received enough data |
Unexecuted instantiation: YaHTTP::AsyncLoader<YaHTTP::Response>::ready() |
338 | 378 | void finalize() { |
339 | 378 | bodybuf.flush(); |
340 | 378 | if (ready()) { |
341 | 378 | std::string body = bodybuf.str(); |
342 | 378 | strstr_map_t::iterator cpos = target->headers.find("content-type"); |
343 | 378 | if (cpos != target->headers.end() && Utility::iequals(cpos->second, "application/x-www-form-urlencoded", 32)) { |
344 | 82 | target->postvars = Utility::parseUrlParameters(body); |
345 | 82 | } |
346 | 378 | target->body = std::move(body); |
347 | 378 | } |
348 | 378 | bodybuf.str(""); |
349 | 378 | bodysize = 0; |
350 | | this->target = NULL; |
351 | 378 | }; //<! finalize and release target YaHTTP::AsyncLoader<YaHTTP::Request>::finalize() Line | Count | Source | 338 | 378 | void finalize() { | 339 | 378 | bodybuf.flush(); | 340 | 378 | if (ready()) { | 341 | 378 | std::string body = bodybuf.str(); | 342 | 378 | strstr_map_t::iterator cpos = target->headers.find("content-type"); | 343 | 378 | if (cpos != target->headers.end() && Utility::iequals(cpos->second, "application/x-www-form-urlencoded", 32)) { | 344 | 82 | target->postvars = Utility::parseUrlParameters(body); | 345 | 82 | } | 346 | 378 | target->body = std::move(body); | 347 | 378 | } | 348 | 378 | bodybuf.str(""); | 349 | 378 | bodysize = 0; | 350 | | this->target = NULL; | 351 | 378 | }; //<! finalize and release target |
Unexecuted instantiation: YaHTTP::AsyncLoader<YaHTTP::Response>::finalize() |
352 | | }; |
353 | | |
354 | | /*! Asynchronous HTTP response loader */ |
355 | | class AsyncResponseLoader: public AsyncLoader<Response> { |
356 | | }; |
357 | | |
358 | | /*! Asynchronous HTTP request loader */ |
359 | | class AsyncRequestLoader: public AsyncLoader<Request> { |
360 | | }; |
361 | | |
362 | | }; |