asio: Use uri_ref in server code
This commit is contained in:
parent
0cda2282dd
commit
fc2170e488
|
@ -68,7 +68,7 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
server.listen("*", port,
|
server.listen("*", port,
|
||||||
[&docroot](const request &req, const response &res) {
|
[&docroot](const request &req, const response &res) {
|
||||||
auto path = percent_decode(req.path());
|
auto path = percent_decode(req.uri().path);
|
||||||
if (!check_path(path)) {
|
if (!check_path(path)) {
|
||||||
res.write_head(404);
|
res.write_head(404);
|
||||||
res.end();
|
res.end();
|
||||||
|
|
|
@ -45,13 +45,7 @@ const header_map &request::header() const { return impl_->header(); }
|
||||||
|
|
||||||
const std::string &request::method() const { return impl_->method(); }
|
const std::string &request::method() const { return impl_->method(); }
|
||||||
|
|
||||||
const std::string &request::scheme() const { return impl_->scheme(); }
|
const uri_ref &request::uri() const { return impl_->uri(); }
|
||||||
|
|
||||||
const std::string &request::authority() const { return impl_->authority(); }
|
|
||||||
|
|
||||||
const std::string &request::host() const { return impl_->host(); }
|
|
||||||
|
|
||||||
const std::string &request::path() const { return impl_->path(); }
|
|
||||||
|
|
||||||
bool request::push(std::string method, std::string path, header_map h) const {
|
bool request::push(std::string method, std::string path, header_map h) const {
|
||||||
return impl_->push(std::move(method), std::move(path), std::move(h));
|
return impl_->push(std::move(method), std::move(path), std::move(h));
|
||||||
|
@ -91,13 +85,9 @@ const header_map &request_impl::header() const { return header_; }
|
||||||
|
|
||||||
const std::string &request_impl::method() const { return method_; }
|
const std::string &request_impl::method() const { return method_; }
|
||||||
|
|
||||||
const std::string &request_impl::scheme() const { return scheme_; }
|
const uri_ref &request_impl::uri() const { return uri_; }
|
||||||
|
|
||||||
const std::string &request_impl::authority() const { return authority_; }
|
uri_ref &request_impl::uri() { return uri_; }
|
||||||
|
|
||||||
const std::string &request_impl::host() const { return host_; }
|
|
||||||
|
|
||||||
const std::string &request_impl::path() const { return path_; }
|
|
||||||
|
|
||||||
void request_impl::header(header_map h) { header_ = std::move(h); }
|
void request_impl::header(header_map h) { header_ = std::move(h); }
|
||||||
|
|
||||||
|
@ -105,14 +95,6 @@ header_map &request_impl::header() { return header_; }
|
||||||
|
|
||||||
void request_impl::method(std::string arg) { method_ = std::move(arg); }
|
void request_impl::method(std::string arg) { method_ = std::move(arg); }
|
||||||
|
|
||||||
void request_impl::scheme(std::string arg) { scheme_ = std::move(arg); }
|
|
||||||
|
|
||||||
void request_impl::authority(std::string arg) { authority_ = std::move(arg); }
|
|
||||||
|
|
||||||
void request_impl::host(std::string arg) { host_ = std::move(arg); }
|
|
||||||
|
|
||||||
void request_impl::path(std::string arg) { path_ = std::move(arg); }
|
|
||||||
|
|
||||||
bool request_impl::push(std::string method, std::string path, header_map h) {
|
bool request_impl::push(std::string method, std::string path, header_map h) {
|
||||||
auto handler = stream_->handler();
|
auto handler = stream_->handler();
|
||||||
auto rv = handler->push_promise(*stream_, std::move(method), std::move(path),
|
auto rv = handler->push_promise(*stream_, std::move(method), std::move(path),
|
||||||
|
@ -261,22 +243,25 @@ int on_header_callback(nghttp2_session *session, const nghttp2_frame *frame,
|
||||||
}
|
}
|
||||||
|
|
||||||
auto &req = stream->request().impl();
|
auto &req = stream->request().impl();
|
||||||
|
auto &uref = req.uri();
|
||||||
|
|
||||||
switch (nghttp2::http2::lookup_token(name, namelen)) {
|
switch (nghttp2::http2::lookup_token(name, namelen)) {
|
||||||
case nghttp2::http2::HD__METHOD:
|
case nghttp2::http2::HD__METHOD:
|
||||||
req.method(std::string(value, value + valuelen));
|
req.method(std::string(value, value + valuelen));
|
||||||
break;
|
break;
|
||||||
case nghttp2::http2::HD__SCHEME:
|
case nghttp2::http2::HD__SCHEME:
|
||||||
req.scheme(std::string(value, value + valuelen));
|
uref.scheme.assign(value, value + valuelen);
|
||||||
break;
|
break;
|
||||||
case nghttp2::http2::HD__AUTHORITY:
|
case nghttp2::http2::HD__AUTHORITY:
|
||||||
req.authority(std::string(value, value + valuelen));
|
uref.host.assign(value, value + valuelen);
|
||||||
break;
|
break;
|
||||||
case nghttp2::http2::HD__PATH:
|
case nghttp2::http2::HD__PATH:
|
||||||
req.path(std::string(value, value + valuelen));
|
split_path(uref, value, value + valuelen);
|
||||||
break;
|
break;
|
||||||
case nghttp2::http2::HD_HOST:
|
case nghttp2::http2::HD_HOST:
|
||||||
req.host(std::string(value, value + valuelen));
|
if (uref.host.empty()) {
|
||||||
|
uref.host.assign(value, value + valuelen);
|
||||||
|
}
|
||||||
// fall through
|
// fall through
|
||||||
default:
|
default:
|
||||||
req.header().emplace(std::string(name, name + namelen),
|
req.header().emplace(std::string(name, name + namelen),
|
||||||
|
@ -310,12 +295,6 @@ int on_frame_recv_callback(nghttp2_session *session, const nghttp2_frame *frame,
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto &req = stream->request().impl();
|
|
||||||
|
|
||||||
if (req.host().empty()) {
|
|
||||||
req.host(req.authority());
|
|
||||||
}
|
|
||||||
|
|
||||||
handler->call_on_request(*stream);
|
handler->call_on_request(*stream);
|
||||||
|
|
||||||
if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
|
if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
|
||||||
|
@ -549,22 +528,17 @@ void http2_handler::resume(http2_stream &stream) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int http2_handler::push_promise(http2_stream &stream, std::string method,
|
int http2_handler::push_promise(http2_stream &stream, std::string method,
|
||||||
std::string path, header_map h) {
|
std::string raw_path_query, header_map h) {
|
||||||
int rv;
|
int rv;
|
||||||
|
|
||||||
auto &req = stream.request().impl();
|
auto &req = stream.request().impl();
|
||||||
|
|
||||||
auto nva = std::vector<nghttp2_nv>();
|
auto nva = std::vector<nghttp2_nv>();
|
||||||
nva.reserve(5 + h.size());
|
nva.reserve(4 + h.size());
|
||||||
nva.push_back(nghttp2::http2::make_nv_ls(":method", method));
|
nva.push_back(nghttp2::http2::make_nv_ls(":method", method));
|
||||||
nva.push_back(nghttp2::http2::make_nv_ls(":scheme", req.scheme()));
|
nva.push_back(nghttp2::http2::make_nv_ls(":scheme", req.uri().scheme));
|
||||||
if (!req.authority().empty()) {
|
nva.push_back(nghttp2::http2::make_nv_ls(":authority", req.uri().host));
|
||||||
nva.push_back(nghttp2::http2::make_nv_ls(":authority", req.authority()));
|
nva.push_back(nghttp2::http2::make_nv_ls(":path", raw_path_query));
|
||||||
}
|
|
||||||
nva.push_back(nghttp2::http2::make_nv_ls(":path", path));
|
|
||||||
if (!req.host().empty()) {
|
|
||||||
nva.push_back(nghttp2::http2::make_nv_ls("host", req.host()));
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto &hd : h) {
|
for (auto &hd : h) {
|
||||||
nva.push_back(nghttp2::http2::make_nv(hd.first, hd.second.value,
|
nva.push_back(nghttp2::http2::make_nv(hd.first, hd.second.value,
|
||||||
|
@ -582,15 +556,12 @@ int http2_handler::push_promise(http2_stream &stream, std::string method,
|
||||||
auto promised_stream = create_stream(rv);
|
auto promised_stream = create_stream(rv);
|
||||||
auto &promised_req = promised_stream->request().impl();
|
auto &promised_req = promised_stream->request().impl();
|
||||||
promised_req.pushed(true);
|
promised_req.pushed(true);
|
||||||
promised_req.method(std::move(method));
|
|
||||||
promised_req.scheme(req.scheme());
|
|
||||||
promised_req.authority(req.authority());
|
|
||||||
promised_req.path(std::move(path));
|
|
||||||
promised_req.host(req.host());
|
|
||||||
promised_req.header(std::move(h));
|
promised_req.header(std::move(h));
|
||||||
if (!req.host().empty()) {
|
promised_req.method(std::move(method));
|
||||||
promised_req.header().emplace("host", header_value(req.host()));
|
auto &uref = promised_req.uri();
|
||||||
}
|
uref.scheme = req.uri().scheme;
|
||||||
|
uref.host = req.uri().host;
|
||||||
|
split_path(uref, std::begin(raw_path_query), std::end(raw_path_query));
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,27 +47,23 @@ class request_impl {
|
||||||
public:
|
public:
|
||||||
request_impl();
|
request_impl();
|
||||||
|
|
||||||
|
void header(header_map h);
|
||||||
const header_map &header() const;
|
const header_map &header() const;
|
||||||
const std::string &method() const;
|
header_map &header();
|
||||||
const std::string &scheme() const;
|
|
||||||
const std::string &authority() const;
|
|
||||||
const std::string &host() const;
|
|
||||||
const std::string &path() const;
|
|
||||||
|
|
||||||
bool push(std::string method, std::string path, header_map h = {});
|
void method(std::string method);
|
||||||
|
const std::string &method() const;
|
||||||
|
|
||||||
|
const uri_ref &uri() const;
|
||||||
|
uri_ref &uri();
|
||||||
|
|
||||||
|
bool push(std::string method, std::string raw_path_query, header_map h = {});
|
||||||
|
|
||||||
bool pushed() const;
|
bool pushed() const;
|
||||||
|
|
||||||
void on_data(data_cb cb);
|
void on_data(data_cb cb);
|
||||||
void on_end(void_cb cb);
|
void on_end(void_cb cb);
|
||||||
|
|
||||||
void header(header_map h);
|
|
||||||
header_map &header();
|
|
||||||
void method(std::string method);
|
|
||||||
void scheme(std::string scheme);
|
|
||||||
void authority(std::string authority);
|
|
||||||
void host(std::string host);
|
|
||||||
void path(std::string path);
|
|
||||||
void pushed(bool f);
|
void pushed(bool f);
|
||||||
void stream(http2_stream *s);
|
void stream(http2_stream *s);
|
||||||
void call_on_data(const uint8_t *data, std::size_t len);
|
void call_on_data(const uint8_t *data, std::size_t len);
|
||||||
|
@ -77,10 +73,7 @@ private:
|
||||||
http2_stream *stream_;
|
http2_stream *stream_;
|
||||||
header_map header_;
|
header_map header_;
|
||||||
std::string method_;
|
std::string method_;
|
||||||
std::string scheme_;
|
uri_ref uri_;
|
||||||
std::string authority_;
|
|
||||||
std::string host_;
|
|
||||||
std::string path_;
|
|
||||||
data_cb on_data_cb_;
|
data_cb on_data_cb_;
|
||||||
void_cb on_end_cb_;
|
void_cb on_end_cb_;
|
||||||
bool pushed_;
|
bool pushed_;
|
||||||
|
@ -162,8 +155,8 @@ public:
|
||||||
|
|
||||||
void resume(http2_stream &stream);
|
void resume(http2_stream &stream);
|
||||||
|
|
||||||
int push_promise(http2_stream &stream, std::string method, std::string path,
|
int push_promise(http2_stream &stream, std::string method,
|
||||||
header_map h);
|
std::string raw_path_query, header_map h);
|
||||||
|
|
||||||
boost::asio::io_service &io_service();
|
boost::asio::io_service &io_service();
|
||||||
|
|
||||||
|
|
|
@ -48,20 +48,8 @@ public:
|
||||||
// Returns method (e.g., GET).
|
// Returns method (e.g., GET).
|
||||||
const std::string &method() const;
|
const std::string &method() const;
|
||||||
|
|
||||||
// Returns scheme (e.g., https).
|
// Returns request URI, split into components.
|
||||||
const std::string &scheme() const;
|
const uri_ref &uri() const;
|
||||||
|
|
||||||
// Returns authority (e.g., example.org). This could be empty
|
|
||||||
// string. In this case, check host().
|
|
||||||
|
|
||||||
const std::string &authority() const;
|
|
||||||
|
|
||||||
// Returns host (e.g., example.org). If host header field is not
|
|
||||||
// present, this value is copied from authority().
|
|
||||||
const std::string &host() const;
|
|
||||||
|
|
||||||
// Returns path (e.g., /index.html).
|
|
||||||
const std::string &path() const;
|
|
||||||
|
|
||||||
// Sets callback when chunk of request body is received.
|
// Sets callback when chunk of request body is received.
|
||||||
void on_data(data_cb cb) const;
|
void on_data(data_cb cb) const;
|
||||||
|
@ -73,7 +61,8 @@ public:
|
||||||
// headers can be given in |h|. request_cb will be called for
|
// headers can be given in |h|. request_cb will be called for
|
||||||
// pushed resource later on. This function returns true if it
|
// pushed resource later on. This function returns true if it
|
||||||
// succeeds, or false.
|
// succeeds, or false.
|
||||||
bool push(std::string method, std::string path, header_map h = {}) const;
|
bool push(std::string method, std::string raw_path_query,
|
||||||
|
header_map h = {}) const;
|
||||||
|
|
||||||
// Returns true if this is pushed request.
|
// Returns true if this is pushed request.
|
||||||
bool pushed() const;
|
bool pushed() const;
|
||||||
|
|
Loading…
Reference in New Issue