Commit d5004d77 authored by octal's avatar octal

Renamed Response to ResponseWriter so that server now uses ResponseWriter and client Response

parent e2e6f19f
...@@ -52,8 +52,6 @@ public: ...@@ -52,8 +52,6 @@ public:
return; return;
} }
std::cout << "I am in the same thread, wouhou" << std::endl;
asyncWriteImpl(fd, flags, BufferHolder(buffer), std::move(resolve), std::move(reject)); asyncWriteImpl(fd, flags, BufferHolder(buffer), std::move(resolve), std::move(reject));
}); });
...@@ -317,7 +315,7 @@ public: ...@@ -317,7 +315,7 @@ public:
static Options options(); static Options options();
void init(const Options& options); void init(const Options& options);
RequestBuilder newRequest(std::string resource); RequestBuilder request(std::string resource);
Async::Promise<Response> get( Async::Promise<Response> get(
const Http::Request& request, const Http::Request& request,
......
...@@ -29,6 +29,7 @@ namespace Private { ...@@ -29,6 +29,7 @@ namespace Private {
class ParserBase; class ParserBase;
template<typename T> struct Parser; template<typename T> struct Parser;
class RequestLineStep; class RequestLineStep;
class ResponseLineStep;
class HeadersStep; class HeadersStep;
class BodyStep; class BodyStep;
} }
...@@ -44,6 +45,10 @@ std::basic_ostream<CharT, Traits>& crlf(std::basic_ostream<CharT, Traits>& os) { ...@@ -44,6 +45,10 @@ std::basic_ostream<CharT, Traits>& crlf(std::basic_ostream<CharT, Traits>& os) {
// 4. HTTP Message // 4. HTTP Message
class Message { class Message {
public: public:
friend class Private::HeadersStep;
friend class Private::BodyStep;
friend class Private::ParserBase;
Message(); Message();
Message(const Message& other) = default; Message(const Message& other) = default;
...@@ -52,6 +57,7 @@ public: ...@@ -52,6 +57,7 @@ public:
Message(Message&& other) = default; Message(Message&& other) = default;
Message& operator=(Message&& other) = default; Message& operator=(Message&& other) = default;
protected:
Version version_; Version version_;
Code code_; Code code_;
...@@ -85,12 +91,9 @@ namespace Uri { ...@@ -85,12 +91,9 @@ namespace Uri {
class RequestBuilder; class RequestBuilder;
// 5. Request // 5. Request
class Request : private Message { class Request : public Message {
public: public:
friend class Private::RequestLineStep; friend class Private::RequestLineStep;
friend class Private::HeadersStep;
friend class Private::BodyStep;
friend class Private::ParserBase;
friend class Private::Parser<Http::Request>; friend class Private::Parser<Http::Request>;
friend class RequestBuilder; friend class RequestBuilder;
...@@ -101,7 +104,6 @@ public: ...@@ -101,7 +104,6 @@ public:
Request(Request&& other) = default; Request(Request&& other) = default;
Request& operator=(Request&& other) = default; Request& operator=(Request&& other) = default;
Version version() const; Version version() const;
Method method() const; Method method() const;
std::string resource() const; std::string resource() const;
...@@ -171,7 +173,7 @@ private: ...@@ -171,7 +173,7 @@ private:
}; };
class Handler; class Handler;
class Response; class ResponseWrite;
class Timeout { class Timeout {
public: public:
...@@ -227,9 +229,9 @@ private: ...@@ -227,9 +229,9 @@ private:
bool armed; bool armed;
}; };
class ResponseStream : private Message { class ResponseStream : public Message {
public: public:
friend class Response; friend class ResponseWriter;
ResponseStream(ResponseStream&& other) ResponseStream(ResponseStream&& other)
: Message(std::move(other)) : Message(std::move(other))
...@@ -315,31 +317,13 @@ operator<<(ResponseStream& stream, ResponseStream & (*func)(ResponseStream &)) { ...@@ -315,31 +317,13 @@ operator<<(ResponseStream& stream, ResponseStream & (*func)(ResponseStream &)) {
// @Investigate public inheritence // @Investigate public inheritence
class Response : public Message { class Response : public Message {
public: public:
friend class Handler; friend class Private::ResponseLineStep;
friend class Timeout; friend class Private::Parser<Http::Response>;
friend class Private::ParserBase;
friend class Private::Parser<Response>;
friend Async::Promise<ssize_t> serveFile(Response&, const char *, const Mime::MediaType&);
static constexpr size_t DefaultStreamSize = 512; Response(const Response& other) = default;
Response& operator=(const Response& other) = default;
// C++11: std::weak_ptr move constructor is C++14 only so the default Response(Response&& other) = default;
// version of move constructor / assignement operator does not work and we Response& operator=(Response&& other) = default;
// have to define it ourself
Response(Response&& other)
: Message(std::move(other))
, peer_(other.peer_)
, buf_(std::move(other.buf_))
, transport_(other.transport_)
{ }
Response& operator=(Response&& other) {
Message::operator=(std::move(other));
peer_ = std::move(other.peer_);
transport_ = other.transport_;
buf_ = std::move(other.buf_);
return *this;
}
const Header::Collection& headers() const { const Header::Collection& headers() const {
return headers_; return headers_;
...@@ -361,6 +345,48 @@ public: ...@@ -361,6 +345,48 @@ public:
return code_; return code_;
} }
std::string body() const {
return body_;
}
protected:
Response()
: Message()
{ }
};
class ResponseWriter : public Response {
public:
static constexpr size_t DefaultStreamSize = 512;
friend Async::Promise<ssize_t> serveFile(ResponseWriter&, const char *, const Mime::MediaType&);
friend class Handler;
friend class Timeout;
friend class Private::ResponseLineStep;
friend class Private::Parser<Http::Response>;
ResponseWriter(const ResponseWriter& other) = delete;
ResponseWriter& operator=(const ResponseWriter& other) = delete;
//
// C++11: std::weak_ptr move constructor is C++14 only so the default
// version of move constructor / assignement operator does not work and we
// have to define it ourself
ResponseWriter(ResponseWriter&& other)
: Response(std::move(other))
, peer_(other.peer_)
, buf_(std::move(other.buf_))
, transport_(other.transport_)
{ }
ResponseWriter& operator=(ResponseWriter&& other) {
Response::operator=(std::move(other));
peer_ = std::move(other.peer_);
transport_ = other.transport_;
buf_ = std::move(other.buf_);
return *this;
}
void setMime(const Mime::MediaType& mime) { void setMime(const Mime::MediaType& mime) {
auto ct = headers_.tryGet<Header::ContentType>(); auto ct = headers_.tryGet<Header::ContentType>();
if (ct) if (ct)
...@@ -428,14 +454,14 @@ public: ...@@ -428,14 +454,14 @@ public:
} }
private: private:
Response() ResponseWriter()
: Message() : Response()
, buf_(0) , buf_(0)
, transport_(nullptr) , transport_(nullptr)
{ } { }
Response(Tcp::Transport* transport) ResponseWriter(Tcp::Transport* transport)
: Message() : Response()
, buf_(DefaultStreamSize) , buf_(DefaultStreamSize)
, transport_(transport) , transport_(transport)
{ } { }
...@@ -463,7 +489,7 @@ private: ...@@ -463,7 +489,7 @@ private:
}; };
Async::Promise<ssize_t> serveFile( Async::Promise<ssize_t> serveFile(
Response& response, const char *fileName, ResponseWriter& response, const char *fileName,
const Mime::MediaType& contentType = Mime::MediaType()); const Mime::MediaType& contentType = Mime::MediaType());
namespace Private { namespace Private {
...@@ -641,9 +667,9 @@ public: ...@@ -641,9 +667,9 @@ public:
void onConnection(const std::shared_ptr<Tcp::Peer>& peer); void onConnection(const std::shared_ptr<Tcp::Peer>& peer);
void onDisconnection(const std::shared_ptr<Tcp::Peer>& peer); void onDisconnection(const std::shared_ptr<Tcp::Peer>& peer);
virtual void onRequest(const Request& request, Response response, Timeout timeout) = 0; virtual void onRequest(const Request& request, ResponseWriter response, Timeout timeout) = 0;
virtual void onTimeout(const Request& request, Response response); virtual void onTimeout(const Request& request, ResponseWriter response);
private: private:
Private::Parser<Http::Request>& getParser(const std::shared_ptr<Tcp::Peer>& peer) const; Private::Parser<Http::Request>& getParser(const std::shared_ptr<Tcp::Peer>& peer) const;
......
...@@ -135,7 +135,7 @@ namespace Private { ...@@ -135,7 +135,7 @@ namespace Private {
void onRequest( void onRequest(
const Http::Request& req, const Http::Request& req,
Http::Response response, Http::ResponseWriter response,
Http::Timeout timeout); Http::Timeout timeout);
private: private:
......
...@@ -38,7 +38,7 @@ class MyHandler : public Net::Http::Handler { ...@@ -38,7 +38,7 @@ class MyHandler : public Net::Http::Handler {
void onRequest( void onRequest(
const Net::Http::Request& req, const Net::Http::Request& req,
Net::Http::Response response, Net::Http::ResponseWriter response,
Net::Http::Timeout timeout) { Net::Http::Timeout timeout) {
if (req.resource() == "/ping") { if (req.resource() == "/ping") {
...@@ -99,7 +99,7 @@ class MyHandler : public Net::Http::Handler { ...@@ -99,7 +99,7 @@ class MyHandler : public Net::Http::Handler {
} }
void onTimeout(const Net::Http::Request& req, Net::Http::Response response) { void onTimeout(const Net::Http::Request& req, Net::Http::ResponseWriter response) {
response response
.send(Net::Http::Code::Request_Timeout, "Timeout") .send(Net::Http::Code::Request_Timeout, "Timeout")
.then([=](ssize_t) { }, ExceptionPrinter()); .then([=](ssize_t) { }, ExceptionPrinter());
...@@ -163,7 +163,7 @@ private: ...@@ -163,7 +163,7 @@ private:
namespace Generic { namespace Generic {
void handleReady(const Rest::Request&, Http::Response response) { void handleReady(const Rest::Request&, Http::ResponseWriter response) {
response.send(Http::Code::Ok, "1"); response.send(Http::Code::Ok, "1");
} }
...@@ -347,14 +347,13 @@ int main() { ...@@ -347,14 +347,13 @@ int main() {
using namespace Net::Http; using namespace Net::Http;
client.init(opts); client.init(opts);
client.get( client.get(client
client .request("/off/octal/nask")
.newRequest("/off/octal/nask")
.header<Header::ContentType>(MIME(Text, Plain)) .header<Header::ContentType>(MIME(Text, Plain))
.cookie(Cookie("FOO", "bar"))) .cookie(Cookie("FOO", "bar")))
.then([](const Http::Response& response) { .then([](const Http::Response& response) {
std::cout << "code = " << response.code() << std::endl; std::cout << "code = " << response.code() << std::endl;
std::cout << "body = " << response.body_ << std::endl; std::cout << "body = " << response.body() << std::endl;
}, Async::NoExcept); }, Async::NoExcept);
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
} }
......
...@@ -498,7 +498,7 @@ Client::init(const Client::Options& options) { ...@@ -498,7 +498,7 @@ Client::init(const Client::Options& options) {
} }
RequestBuilder RequestBuilder
Client::newRequest(std::string val) { Client::request(std::string val) {
RequestBuilder builder; RequestBuilder builder;
builder.resource(std::move(val)); builder.resource(std::move(val));
return builder; return builder;
......
...@@ -638,7 +638,7 @@ ResponseStream::ends() { ...@@ -638,7 +638,7 @@ ResponseStream::ends() {
} }
Async::Promise<ssize_t> Async::Promise<ssize_t>
Response::putOnWire(const char* data, size_t len) ResponseWriter::putOnWire(const char* data, size_t len)
{ {
try { try {
std::ostream os(&buf_); std::ostream os(&buf_);
...@@ -678,7 +678,7 @@ Response::putOnWire(const char* data, size_t len) ...@@ -678,7 +678,7 @@ Response::putOnWire(const char* data, size_t len)
} }
Async::Promise<ssize_t> Async::Promise<ssize_t>
serveFile(Response& response, const char* fileName, const Mime::MediaType& contentType) serveFile(ResponseWriter& response, const char* fileName, const Mime::MediaType& contentType)
{ {
struct stat sb; struct stat sb;
...@@ -756,7 +756,7 @@ Handler::onInput(const char* buffer, size_t len, const std::shared_ptr<Tcp::Peer ...@@ -756,7 +756,7 @@ Handler::onInput(const char* buffer, size_t len, const std::shared_ptr<Tcp::Peer
auto state = parser.parse(); auto state = parser.parse();
if (state == Private::State::Done) { if (state == Private::State::Done) {
Response response(transport()); ResponseWriter response(transport());
response.associatePeer(peer); response.associatePeer(peer);
Timeout timeout(transport(), this, peer, parser.request); Timeout timeout(transport(), this, peer, parser.request);
...@@ -768,13 +768,13 @@ Handler::onInput(const char* buffer, size_t len, const std::shared_ptr<Tcp::Peer ...@@ -768,13 +768,13 @@ Handler::onInput(const char* buffer, size_t len, const std::shared_ptr<Tcp::Peer
parser.reset(); parser.reset();
} }
} catch (const HttpError &err) { } catch (const HttpError &err) {
Response response(transport()); ResponseWriter response(transport());
response.associatePeer(peer); response.associatePeer(peer);
response.send(static_cast<Code>(err.code()), err.reason()); response.send(static_cast<Code>(err.code()), err.reason());
getParser(peer).reset(); getParser(peer).reset();
} }
catch (const std::exception& e) { catch (const std::exception& e) {
Response response(transport()); ResponseWriter response(transport());
response.associatePeer(peer); response.associatePeer(peer);
response.send(Code::Internal_Server_Error, e.what()); response.send(Code::Internal_Server_Error, e.what());
getParser(peer).reset(); getParser(peer).reset();
...@@ -791,14 +791,14 @@ Handler::onDisconnection(const shared_ptr<Tcp::Peer>& peer) { ...@@ -791,14 +791,14 @@ Handler::onDisconnection(const shared_ptr<Tcp::Peer>& peer) {
} }
void void
Handler::onTimeout(const Request& request, Response response) { Handler::onTimeout(const Request& request, ResponseWriter response) {
} }
void void
Timeout::onTimeout(uint64_t numWakeup) { Timeout::onTimeout(uint64_t numWakeup) {
if (!peer.lock()) return; if (!peer.lock()) return;
Response response(transport); ResponseWriter response(transport);
response.associatePeer(peer); response.associatePeer(peer);
handler->onTimeout(request, std::move(response)); handler->onTimeout(request, std::move(response));
......
...@@ -218,7 +218,7 @@ HttpHandler::HttpHandler( ...@@ -218,7 +218,7 @@ HttpHandler::HttpHandler(
void void
HttpHandler::onRequest( HttpHandler::onRequest(
const Http::Request& req, const Http::Request& req,
Http::Response response, Http::ResponseWriter response,
Http::Timeout timeout) Http::Timeout timeout)
{ {
auto& r = routes[req.method()]; auto& r = routes[req.method()];
......
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