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[]) { ...@@ -62,10 +62,9 @@ int main(int argc, char *argv[]) {
server.tls(argv[3], argv[4]); server.tls(argv[3], argv[4]);
} }
server.listen("*", port, [](const std::shared_ptr<request> &req, server.listen("*", port, [](const request &req, const response &res) {
const std::shared_ptr<response> &res) { res.write_head(200, {header{"foo", "bar"}});
res->write_head(200, {header{"foo", "bar"}}); res.end("hello, world");
res->end("hello, world");
}); });
} catch (std::exception &e) { } catch (std::exception &e) {
std::cerr << "exception: " << e.what() << "\n"; std::cerr << "exception: " << e.what() << "\n";
......
...@@ -66,12 +66,12 @@ int main(int argc, char *argv[]) { ...@@ -66,12 +66,12 @@ int main(int argc, char *argv[]) {
server.tls(argv[4], argv[5]); server.tls(argv[4], argv[5]);
} }
server.listen("*", port, [&docroot](const std::shared_ptr<request> &req, server.listen("*", port,
const std::shared_ptr<response> &res) { [&docroot](const request &req, const response &res) {
auto path = percent_decode(req->path()); auto path = percent_decode(req.path());
if (!check_path(path)) { if (!check_path(path)) {
res->write_head(404); res.write_head(404);
res->end(); res.end();
return; return;
} }
...@@ -82,8 +82,8 @@ int main(int argc, char *argv[]) { ...@@ -82,8 +82,8 @@ int main(int argc, char *argv[]) {
path = docroot + path; path = docroot + path;
auto fd = open(path.c_str(), O_RDONLY); auto fd = open(path.c_str(), O_RDONLY);
if (fd == -1) { if (fd == -1) {
res->write_head(404); res.write_head(404);
res->end(); res.end();
return; return;
} }
...@@ -95,8 +95,8 @@ int main(int argc, char *argv[]) { ...@@ -95,8 +95,8 @@ int main(int argc, char *argv[]) {
header{"content-length", std::to_string(stbuf.st_size)}); header{"content-length", std::to_string(stbuf.st_size)});
headers.push_back(header{"last-modified", http_date(stbuf.st_mtime)}); headers.push_back(header{"last-modified", http_date(stbuf.st_mtime)});
} }
res->write_head(200, std::move(headers)); res.write_head(200, std::move(headers));
res->end(file_reader_from_fd(fd)); res.end(file_reader_from_fd(fd));
}); });
} catch (std::exception &e) { } catch (std::exception &e) {
std::cerr << "exception: " << e.what() << "\n"; std::cerr << "exception: " << e.what() << "\n";
......
...@@ -32,8 +32,7 @@ namespace nghttp2 { ...@@ -32,8 +32,7 @@ namespace nghttp2 {
namespace asio_http2 { namespace asio_http2 {
namespace client { 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); request_.impl().stream(this);
} }
......
This diff is collapsed.
...@@ -60,7 +60,6 @@ public: ...@@ -60,7 +60,6 @@ public:
std::vector<header> headers = {}); std::vector<header> headers = {});
bool pushed() const; bool pushed() const;
bool closed() 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);
...@@ -73,12 +72,12 @@ public: ...@@ -73,12 +72,12 @@ public:
void host(std::string host); void host(std::string host);
void path(std::string path); void path(std::string path);
void pushed(bool f); void pushed(bool f);
void handler(std::weak_ptr<http2_handler> h); void stream(http2_stream *s);
void stream(std::weak_ptr<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);
void call_on_end(); void call_on_end();
private: private:
http2_stream *stream_;
std::vector<header> headers_; std::vector<header> headers_;
std::string method_; std::string method_;
std::string scheme_; std::string scheme_;
...@@ -87,8 +86,6 @@ private: ...@@ -87,8 +86,6 @@ private:
std::string path_; std::string path_;
data_cb on_data_cb_; data_cb on_data_cb_;
void_cb on_end_cb_; void_cb on_end_cb_;
std::weak_ptr<http2_handler> handler_;
std::weak_ptr<http2_stream> stream_;
bool pushed_; bool pushed_;
}; };
...@@ -99,35 +96,35 @@ public: ...@@ -99,35 +96,35 @@ public:
void end(std::string data = ""); void end(std::string data = "");
void end(read_cb cb); void end(read_cb cb);
void resume(); void resume();
bool closed() const;
unsigned int status_code() const; unsigned int status_code() const;
const std::vector<header> &headers() const; const std::vector<header> &headers() const;
bool started() const; bool started() const;
void handler(std::weak_ptr<http2_handler> h); void stream(http2_stream *s);
void stream(std::weak_ptr<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_;
std::vector<header> headers_; std::vector<header> headers_;
read_cb read_cb_; read_cb read_cb_;
std::weak_ptr<http2_handler> handler_;
std::weak_ptr<http2_stream> stream_;
unsigned int status_code_; unsigned int status_code_;
bool started_; bool started_;
}; };
class http2_stream { class http2_stream {
public: public:
http2_stream(int32_t stream_id); http2_stream(http2_handler *h, int32_t stream_id);
int32_t get_stream_id() const; int32_t get_stream_id() const;
const std::shared_ptr<request> &get_request(); request &request();
const std::shared_ptr<response> &get_response(); response &response();
http2_handler *handler() const;
private: private:
std::shared_ptr<request> request_; http2_handler *handler_;
std::shared_ptr<response> response_; class request request_;
class response response_;
int32_t stream_id_; int32_t stream_id_;
}; };
...@@ -148,9 +145,9 @@ public: ...@@ -148,9 +145,9 @@ public:
int start(); 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); 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); void call_on_request(http2_stream &stream);
......
...@@ -133,24 +133,21 @@ public: ...@@ -133,24 +133,21 @@ public:
const std::string &path() const; 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); void on_data(data_cb cb) const;
// Sets callback when request was completed. // 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 // 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 |headers|. 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,
std::vector<header> headers = {}); 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;
// Returns true if stream has been closed.
bool closed() const;
// Application must not call this directly. // Application must not call this directly.
request_impl &impl(); request_impl &impl();
...@@ -165,18 +162,19 @@ public: ...@@ -165,18 +162,19 @@ public:
// 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 |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 // Sends |data| as request body. No further call of end() is
// allowed. // allowed.
void end(std::string data = ""); void end(std::string data = "") const;
// Sets callback |cb| as a generator of the response body. No // Sets callback |cb| as a generator of the response body. No
// further call of end() is allowed. // further call of end() is allowed.
void end(read_cb cb); void end(read_cb cb) const;
// Resumes deferred response. // Resumes deferred response.
void resume(); void resume() const;
// Returns status code. // Returns status code.
unsigned int status_code() const; unsigned int status_code() const;
...@@ -193,8 +191,7 @@ private: ...@@ -193,8 +191,7 @@ private:
// This is so called request callback. Called every time request is // This is so called request callback. Called every time request is
// received. // received.
typedef std::function<void(const std::shared_ptr<request> &, typedef std::function<void(const request &, const response &)> request_cb;
const std::shared_ptr<response> &)> request_cb;
class http2_impl; 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