-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.cpp
More file actions
74 lines (65 loc) · 2.01 KB
/
Copy pathrequest.cpp
File metadata and controls
74 lines (65 loc) · 2.01 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
#include <server.hpp>
namespace webframe::server
{
request::request(struct evhttp_request *req) : _req(req)
{
_headers = evhttp_request_get_input_headers(_req);
struct evbuffer *input_buffer = evhttp_request_get_input_buffer(_req);
size_t body_size = evbuffer_get_length(input_buffer);
_body.resize(body_size);
evbuffer_copyout(input_buffer, _body.data(), body_size);
}
webframe::method request::get_method() const
{
webframe::method method;
switch(evhttp_request_get_command(_req))
{
case EVHTTP_REQ_GET:
method = webframe::method::http_get;
break;
case EVHTTP_REQ_POST:
method = webframe::method::http_post;
break;
case EVHTTP_REQ_PUT:
method = webframe::method::http_put;
break;
case EVHTTP_REQ_DELETE:
method = webframe::method::http_delete;
break;
default:
method = static_cast<webframe::method>(-1);
break;
}
return method;
}
std::string request::get_uri() const
{
const char *uri = evhttp_request_get_uri(_req);
return std::string(uri);
}
std::string request::get_path() const
{
const evhttp_uri *uri = evhttp_request_get_evhttp_uri(_req);
const char *path = evhttp_uri_get_path(uri);
return path ? path : "";
}
bool request::get_header(const std::string &key, std::string &value) const
{
const char *header_value = evhttp_find_header(_headers, key.c_str());
bool result(false);
if (header_value)
{
value = header_value;
result = true;
}
return result;
}
std::pair<const uint8_t *, size_t> request::get_body() const
{
return { _body.data(), _body.size() };
}
void request::read_body(const std::function<void(const uint8_t *, size_t)> &callback) const
{
callback(_body.data(), _body.size());
}
}