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