asio: Use header_map in server code
This commit is contained in:
parent
35817876fe
commit
6753b6d61e
|
@ -63,7 +63,7 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
server.listen("*", port, [](const request &req, const response &res) {
|
server.listen("*", port, [](const request &req, const response &res) {
|
||||||
res.write_head(200, {header{"foo", "bar"}});
|
res.write_head(200, {{"foo", {"bar"}}});
|
||||||
res.end("hello, world");
|
res.end("hello, world");
|
||||||
});
|
});
|
||||||
} catch (std::exception &e) {
|
} catch (std::exception &e) {
|
||||||
|
|
|
@ -87,15 +87,16 @@ int main(int argc, char *argv[]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto headers = std::vector<header>();
|
auto header = header_map();
|
||||||
|
|
||||||
struct stat stbuf;
|
struct stat stbuf;
|
||||||
if (stat(path.c_str(), &stbuf) == 0) {
|
if (stat(path.c_str(), &stbuf) == 0) {
|
||||||
headers.push_back(
|
header.emplace("content-length",
|
||||||
header{"content-length", std::to_string(stbuf.st_size)});
|
header_value(std::to_string(stbuf.st_size)));
|
||||||
headers.push_back(header{"last-modified", http_date(stbuf.st_mtime)});
|
header.emplace("last-modified",
|
||||||
|
header_value(http_date(stbuf.st_mtime)));
|
||||||
}
|
}
|
||||||
res.write_head(200, std::move(headers));
|
res.write_head(200, std::move(header));
|
||||||
res.end(file_reader_from_fd(fd));
|
res.end(file_reader_from_fd(fd));
|
||||||
});
|
});
|
||||||
} catch (std::exception &e) {
|
} catch (std::exception &e) {
|
||||||
|
|
|
@ -41,7 +41,7 @@ extern std::shared_ptr<std::string> cached_date;
|
||||||
|
|
||||||
request::request() : impl_(make_unique<request_impl>()) {}
|
request::request() : impl_(make_unique<request_impl>()) {}
|
||||||
|
|
||||||
const std::vector<header> &request::headers() const { return impl_->headers(); }
|
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(); }
|
||||||
|
|
||||||
|
@ -53,9 +53,8 @@ const std::string &request::host() const { return impl_->host(); }
|
||||||
|
|
||||||
const std::string &request::path() const { return impl_->path(); }
|
const std::string &request::path() const { return impl_->path(); }
|
||||||
|
|
||||||
bool request::push(std::string method, std::string path,
|
bool request::push(std::string method, std::string path, header_map h) const {
|
||||||
std::vector<header> headers) const {
|
return impl_->push(std::move(method), std::move(path), std::move(h));
|
||||||
return impl_->push(std::move(method), std::move(path), std::move(headers));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool request::pushed() const { return impl_->pushed(); }
|
bool request::pushed() const { return impl_->pushed(); }
|
||||||
|
@ -70,9 +69,8 @@ request_impl &request::impl() const { return *impl_; }
|
||||||
|
|
||||||
response::response() : impl_(make_unique<response_impl>()) {}
|
response::response() : impl_(make_unique<response_impl>()) {}
|
||||||
|
|
||||||
void response::write_head(unsigned int status_code,
|
void response::write_head(unsigned int status_code, header_map h) const {
|
||||||
std::vector<header> headers) const {
|
impl_->write_head(status_code, std::move(h));
|
||||||
impl_->write_head(status_code, std::move(headers));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void response::end(std::string data) const { impl_->end(std::move(data)); }
|
void response::end(std::string data) const { impl_->end(std::move(data)); }
|
||||||
|
@ -89,7 +87,7 @@ response_impl &response::impl() const { return *impl_; }
|
||||||
|
|
||||||
request_impl::request_impl() : stream_(nullptr), pushed_(false) {}
|
request_impl::request_impl() : stream_(nullptr), pushed_(false) {}
|
||||||
|
|
||||||
const std::vector<header> &request_impl::headers() const { return headers_; }
|
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_; }
|
||||||
|
|
||||||
|
@ -101,13 +99,9 @@ const std::string &request_impl::host() const { return host_; }
|
||||||
|
|
||||||
const std::string &request_impl::path() const { return path_; }
|
const std::string &request_impl::path() const { return path_; }
|
||||||
|
|
||||||
void request_impl::set_header(std::vector<header> headers) {
|
void request_impl::header(header_map h) { header_ = std::move(h); }
|
||||||
headers_ = std::move(headers);
|
|
||||||
}
|
|
||||||
|
|
||||||
void request_impl::add_header(std::string name, std::string value) {
|
header_map &request_impl::header() { return header_; }
|
||||||
headers_.push_back(header{std::move(name), std::move(value)});
|
|
||||||
}
|
|
||||||
|
|
||||||
void request_impl::method(std::string arg) { method_ = std::move(arg); }
|
void request_impl::method(std::string arg) { method_ = std::move(arg); }
|
||||||
|
|
||||||
|
@ -119,11 +113,10 @@ void request_impl::host(std::string arg) { host_ = std::move(arg); }
|
||||||
|
|
||||||
void request_impl::path(std::string arg) { path_ = std::move(arg); }
|
void request_impl::path(std::string arg) { path_ = std::move(arg); }
|
||||||
|
|
||||||
bool request_impl::push(std::string method, std::string path,
|
bool request_impl::push(std::string method, std::string path, header_map h) {
|
||||||
std::vector<header> headers) {
|
|
||||||
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),
|
||||||
std::move(headers));
|
std::move(h));
|
||||||
return rv == 0;
|
return rv == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,10 +147,9 @@ response_impl::response_impl()
|
||||||
|
|
||||||
unsigned int response_impl::status_code() const { return status_code_; }
|
unsigned int response_impl::status_code() const { return status_code_; }
|
||||||
|
|
||||||
void response_impl::write_head(unsigned int status_code,
|
void response_impl::write_head(unsigned int status_code, header_map h) {
|
||||||
std::vector<header> headers) {
|
|
||||||
status_code_ = status_code;
|
status_code_ = status_code;
|
||||||
headers_ = std::move(headers);
|
header_ = std::move(h);
|
||||||
}
|
}
|
||||||
|
|
||||||
void response_impl::end(std::string data) {
|
void response_impl::end(std::string data) {
|
||||||
|
@ -199,7 +191,7 @@ void response_impl::resume() {
|
||||||
|
|
||||||
bool response_impl::started() const { return started_; }
|
bool response_impl::started() const { return started_; }
|
||||||
|
|
||||||
const std::vector<header> &response_impl::headers() const { return headers_; }
|
const header_map &response_impl::header() const { return header_; }
|
||||||
|
|
||||||
void response_impl::stream(http2_stream *s) { stream_ = s; }
|
void response_impl::stream(http2_stream *s) { stream_ = s; }
|
||||||
|
|
||||||
|
@ -287,8 +279,9 @@ int on_header_callback(nghttp2_session *session, const nghttp2_frame *frame,
|
||||||
req.host(std::string(value, value + valuelen));
|
req.host(std::string(value, value + valuelen));
|
||||||
// fall through
|
// fall through
|
||||||
default:
|
default:
|
||||||
req.add_header(std::string(name, name + namelen),
|
req.header().emplace(std::string(name, name + namelen),
|
||||||
std::string(value, value + valuelen));
|
header_value(std::string(value, value + valuelen),
|
||||||
|
flags & NGHTTP2_NV_FLAG_NO_INDEX));
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -490,15 +483,16 @@ int http2_handler::start_response(http2_stream &stream) {
|
||||||
int rv;
|
int rv;
|
||||||
|
|
||||||
auto &res = stream.response().impl();
|
auto &res = stream.response().impl();
|
||||||
auto &headers = res.headers();
|
auto &header = res.header();
|
||||||
auto nva = std::vector<nghttp2_nv>();
|
auto nva = std::vector<nghttp2_nv>();
|
||||||
nva.reserve(2 + headers.size());
|
nva.reserve(2 + header.size());
|
||||||
auto status = util::utos(res.status_code());
|
auto status = util::utos(res.status_code());
|
||||||
auto date = cached_date;
|
auto date = cached_date;
|
||||||
nva.push_back(nghttp2::http2::make_nv_ls(":status", status));
|
nva.push_back(nghttp2::http2::make_nv_ls(":status", status));
|
||||||
nva.push_back(nghttp2::http2::make_nv_ls("date", *date));
|
nva.push_back(nghttp2::http2::make_nv_ls("date", *date));
|
||||||
for (auto &hd : headers) {
|
for (auto &hd : header) {
|
||||||
nva.push_back(nghttp2::http2::make_nv(hd.name, hd.value));
|
nva.push_back(nghttp2::http2::make_nv(hd.first, hd.second.value,
|
||||||
|
hd.second.sensitive));
|
||||||
}
|
}
|
||||||
|
|
||||||
nghttp2_data_provider prd;
|
nghttp2_data_provider prd;
|
||||||
|
@ -555,13 +549,13 @@ 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, std::vector<header> headers) {
|
std::string path, 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 + headers.size());
|
nva.reserve(5 + 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.scheme()));
|
||||||
if (!req.authority().empty()) {
|
if (!req.authority().empty()) {
|
||||||
|
@ -572,8 +566,9 @@ int http2_handler::push_promise(http2_stream &stream, std::string method,
|
||||||
nva.push_back(nghttp2::http2::make_nv_ls("host", req.host()));
|
nva.push_back(nghttp2::http2::make_nv_ls("host", req.host()));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto &hd : headers) {
|
for (auto &hd : h) {
|
||||||
nva.push_back(nghttp2::http2::make_nv(hd.name, hd.value));
|
nva.push_back(nghttp2::http2::make_nv(hd.first, hd.second.value,
|
||||||
|
hd.second.sensitive));
|
||||||
}
|
}
|
||||||
|
|
||||||
rv = nghttp2_submit_push_promise(session_, NGHTTP2_FLAG_NONE,
|
rv = nghttp2_submit_push_promise(session_, NGHTTP2_FLAG_NONE,
|
||||||
|
@ -592,9 +587,9 @@ int http2_handler::push_promise(http2_stream &stream, std::string method,
|
||||||
promised_req.authority(req.authority());
|
promised_req.authority(req.authority());
|
||||||
promised_req.path(std::move(path));
|
promised_req.path(std::move(path));
|
||||||
promised_req.host(req.host());
|
promised_req.host(req.host());
|
||||||
promised_req.set_header(std::move(headers));
|
promised_req.header(std::move(h));
|
||||||
if (!req.host().empty()) {
|
if (!req.host().empty()) {
|
||||||
promised_req.add_header("host", req.host());
|
promised_req.header().emplace("host", header_value(req.host()));
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -47,23 +47,22 @@ class request_impl {
|
||||||
public:
|
public:
|
||||||
request_impl();
|
request_impl();
|
||||||
|
|
||||||
const std::vector<header> &headers() const;
|
const header_map &header() const;
|
||||||
const std::string &method() const;
|
const std::string &method() const;
|
||||||
const std::string &scheme() const;
|
const std::string &scheme() const;
|
||||||
const std::string &authority() const;
|
const std::string &authority() const;
|
||||||
const std::string &host() const;
|
const std::string &host() const;
|
||||||
const std::string &path() const;
|
const std::string &path() const;
|
||||||
|
|
||||||
bool push(std::string method, std::string path,
|
bool push(std::string method, std::string path, header_map h = {});
|
||||||
std::vector<header> headers = {});
|
|
||||||
|
|
||||||
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 set_header(std::vector<header> headers);
|
void header(header_map h);
|
||||||
void add_header(std::string name, std::string value);
|
header_map &header();
|
||||||
void method(std::string method);
|
void method(std::string method);
|
||||||
void scheme(std::string scheme);
|
void scheme(std::string scheme);
|
||||||
void authority(std::string authority);
|
void authority(std::string authority);
|
||||||
|
@ -76,7 +75,7 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
http2_stream *stream_;
|
http2_stream *stream_;
|
||||||
std::vector<header> headers_;
|
header_map header_;
|
||||||
std::string method_;
|
std::string method_;
|
||||||
std::string scheme_;
|
std::string scheme_;
|
||||||
std::string authority_;
|
std::string authority_;
|
||||||
|
@ -90,20 +89,20 @@ private:
|
||||||
class response_impl {
|
class response_impl {
|
||||||
public:
|
public:
|
||||||
response_impl();
|
response_impl();
|
||||||
void write_head(unsigned int status_code, std::vector<header> headers = {});
|
void write_head(unsigned int status_code, header_map h = {});
|
||||||
void end(std::string data = "");
|
void end(std::string data = "");
|
||||||
void end(read_cb cb);
|
void end(read_cb cb);
|
||||||
void resume();
|
void resume();
|
||||||
|
|
||||||
unsigned int status_code() const;
|
unsigned int status_code() const;
|
||||||
const std::vector<header> &headers() const;
|
const header_map &header() const;
|
||||||
bool started() const;
|
bool started() const;
|
||||||
void stream(http2_stream *s);
|
void stream(http2_stream *s);
|
||||||
read_cb::result_type call_read(uint8_t *data, std::size_t len);
|
read_cb::result_type call_read(uint8_t *data, std::size_t len);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
http2_stream *stream_;
|
http2_stream *stream_;
|
||||||
std::vector<header> headers_;
|
header_map header_;
|
||||||
read_cb read_cb_;
|
read_cb read_cb_;
|
||||||
unsigned int status_code_;
|
unsigned int status_code_;
|
||||||
bool started_;
|
bool started_;
|
||||||
|
@ -164,7 +163,7 @@ 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, std::string path,
|
||||||
std::vector<header> headers);
|
header_map h);
|
||||||
|
|
||||||
boost::asio::io_service &io_service();
|
boost::asio::io_service &io_service();
|
||||||
|
|
||||||
|
|
|
@ -42,8 +42,8 @@ public:
|
||||||
request();
|
request();
|
||||||
|
|
||||||
// Returns request headers. The pusedo headers, which start with
|
// Returns request headers. The pusedo headers, which start with
|
||||||
// colon (;), are exluced from this list.
|
// colon (:), are exluced from this list.
|
||||||
const std::vector<header> &headers() const;
|
const header_map &header() const;
|
||||||
|
|
||||||
// Returns method (e.g., GET).
|
// Returns method (e.g., GET).
|
||||||
const std::string &method() const;
|
const std::string &method() const;
|
||||||
|
@ -70,11 +70,10 @@ public:
|
||||||
void on_end(void_cb cb) const;
|
void on_end(void_cb cb) const;
|
||||||
|
|
||||||
// Pushes resource denoted by |path| using |method|. The additional
|
// Pushes resource denoted by |path| using |method|. The additional
|
||||||
// headers can be given in |headers|. 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,
|
bool push(std::string method, std::string path, header_map h = {}) const;
|
||||||
std::vector<header> headers = {}) const;
|
|
||||||
|
|
||||||
// Returns true if this is pushed request.
|
// Returns true if this is pushed request.
|
||||||
bool pushed() const;
|
bool pushed() const;
|
||||||
|
@ -92,9 +91,8 @@ public:
|
||||||
response();
|
response();
|
||||||
|
|
||||||
// Write response header using |status_code| (e.g., 200) and
|
// Write response header using |status_code| (e.g., 200) and
|
||||||
// additional headers in |headers|.
|
// additional headers in |h|.
|
||||||
void write_head(unsigned int status_code,
|
void write_head(unsigned int status_code, header_map h = {}) const;
|
||||||
std::vector<header> headers = {}) const;
|
|
||||||
|
|
||||||
// Sends |data| as request body. No further call of end() is
|
// Sends |data| as request body. No further call of end() is
|
||||||
// allowed.
|
// allowed.
|
||||||
|
|
Loading…
Reference in New Issue