Commit b0c1986a authored by Tatsuhiro Tsujikawa's avatar Tatsuhiro Tsujikawa

asio: Avoid shared_ptr for request and response

parent 9671eaa8
......@@ -62,10 +62,9 @@ int main(int argc, char *argv[]) {
server.tls(argv[3], argv[4]);
}
server.listen("*", port, [](const std::shared_ptr<request> &req,
const std::shared_ptr<response> &res) {
res->write_head(200, {header{"foo", "bar"}});
res->end("hello, world");
server.listen("*", port, [](const request &req, const response &res) {
res.write_head(200, {header{"foo", "bar"}});
res.end("hello, world");
});
} catch (std::exception &e) {
std::cerr << "exception: " << e.what() << "\n";
......
......@@ -66,12 +66,12 @@ int main(int argc, char *argv[]) {
server.tls(argv[4], argv[5]);
}
server.listen("*", port, [&docroot](const std::shared_ptr<request> &req,
const std::shared_ptr<response> &res) {
auto path = percent_decode(req->path());
server.listen("*", port,
[&docroot](const request &req, const response &res) {
auto path = percent_decode(req.path());
if (!check_path(path)) {
res->write_head(404);
res->end();
res.write_head(404);
res.end();
return;
}
......@@ -82,8 +82,8 @@ int main(int argc, char *argv[]) {
path = docroot + path;
auto fd = open(path.c_str(), O_RDONLY);
if (fd == -1) {
res->write_head(404);
res->end();
res.write_head(404);
res.end();
return;
}
......@@ -95,8 +95,8 @@ int main(int argc, char *argv[]) {
header{"content-length", std::to_string(stbuf.st_size)});
headers.push_back(header{"last-modified", http_date(stbuf.st_mtime)});
}
res->write_head(200, std::move(headers));
res->end(file_reader_from_fd(fd));
res.write_head(200, std::move(headers));
res.end(file_reader_from_fd(fd));
});
} catch (std::exception &e) {
std::cerr << "exception: " << e.what() << "\n";
......
......@@ -32,8 +32,7 @@ namespace nghttp2 {
namespace asio_http2 {
namespace client {
stream::stream(session_impl *sess) : sess_(sess), stream_id_(0)
{
stream::stream(session_impl *sess) : sess_(sess), stream_id_(0) {
request_.impl().stream(this);
}
......
This diff is collapsed.
......@@ -60,7 +60,6 @@ public:
std::vector<header> headers = {});
bool pushed() const;
bool closed() const;
void on_data(data_cb cb);
void on_end(void_cb cb);
......@@ -73,12 +72,12 @@ public:
void host(std::string host);
void path(std::string path);
void pushed(bool f);
void handler(std::weak_ptr<http2_handler> h);
void stream(std::weak_ptr<http2_stream> s);
void stream(http2_stream *s);
void call_on_data(const uint8_t *data, std::size_t len);
void call_on_end();
private:
http2_stream *stream_;
std::vector<header> headers_;
std::string method_;
std::string scheme_;
......@@ -87,8 +86,6 @@ private:
std::string path_;
data_cb on_data_cb_;
void_cb on_end_cb_;
std::weak_ptr<http2_handler> handler_;
std::weak_ptr<http2_stream> stream_;
bool pushed_;
};
......@@ -99,35 +96,35 @@ public:
void end(std::string data = "");
void end(read_cb cb);
void resume();
bool closed() const;
unsigned int status_code() const;
const std::vector<header> &headers() const;
bool started() const;
void handler(std::weak_ptr<http2_handler> h);
void stream(std::weak_ptr<http2_stream> s);
void stream(http2_stream *s);
read_cb::result_type call_read(uint8_t *data, std::size_t len);
private:
http2_stream *stream_;
std::vector<header> headers_;
read_cb read_cb_;
std::weak_ptr<http2_handler> handler_;
std::weak_ptr<http2_stream> stream_;
unsigned int status_code_;
bool started_;
};
class http2_stream {
public:
http2_stream(int32_t stream_id);
http2_stream(http2_handler *h, int32_t stream_id);
int32_t get_stream_id() const;
const std::shared_ptr<request> &get_request();
const std::shared_ptr<response> &get_response();
request &request();
response &response();
http2_handler *handler() const;
private:
std::shared_ptr<request> request_;
std::shared_ptr<response> response_;
http2_handler *handler_;
class request request_;
class response response_;
int32_t stream_id_;
};
......@@ -148,9 +145,9 @@ public:
int start();
std::shared_ptr<http2_stream> create_stream(int32_t stream_id);
http2_stream *create_stream(int32_t stream_id);
void close_stream(int32_t stream_id);
std::shared_ptr<http2_stream> find_stream(int32_t stream_id);
http2_stream *find_stream(int32_t stream_id);
void call_on_request(http2_stream &stream);
......
......@@ -133,24 +133,21 @@ public:
const std::string &path() const;
// Sets callback when chunk of request body is received.
void on_data(data_cb cb);
void on_data(data_cb cb) const;
// Sets callback when request was completed.
void on_end(void_cb cb);
void on_end(void_cb cb) const;
// Pushes resource denoted by |path| using |method|. The additional
// headers can be given in |headers|. request_cb will be called for
// pushed resource later on. This function returns true if it
// succeeds, or false.
bool push(std::string method, std::string path,
std::vector<header> headers = {});
std::vector<header> headers = {}) const;
// Returns true if this is pushed request.
bool pushed() const;
// Returns true if stream has been closed.
bool closed() const;
// Application must not call this directly.
request_impl &impl();
......@@ -165,18 +162,19 @@ public:
// Write response header using |status_code| (e.g., 200) and
// additional headers in |headers|.
void write_head(unsigned int status_code, std::vector<header> headers = {});
void write_head(unsigned int status_code,
std::vector<header> headers = {}) const;
// Sends |data| as request body. No further call of end() is
// allowed.
void end(std::string data = "");
void end(std::string data = "") const;
// Sets callback |cb| as a generator of the response body. No
// further call of end() is allowed.
void end(read_cb cb);
void end(read_cb cb) const;
// Resumes deferred response.
void resume();
void resume() const;
// Returns status code.
unsigned int status_code() const;
......@@ -193,8 +191,7 @@ private:
// This is so called request callback. Called every time request is
// received.
typedef std::function<void(const std::shared_ptr<request> &,
const std::shared_ptr<response> &)> request_cb;
typedef std::function<void(const request &, const response &)> request_cb;
class http2_impl;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment