2014-03-31 16:51:50 +00:00
|
|
|
#pragma once
|
|
|
|
#include <boost/asio.hpp>
|
2014-04-01 12:25:16 +00:00
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2014-08-06 23:31:27 +00:00
|
|
|
#include <boost/lexical_cast.hpp>
|
2014-05-02 05:17:49 +00:00
|
|
|
#include <atomic>
|
|
|
|
#include <chrono>
|
2014-07-29 11:20:50 +00:00
|
|
|
#include <array>
|
|
|
|
|
2014-08-06 23:31:27 +00:00
|
|
|
#include "http_parser_merged.h"
|
2014-04-01 12:25:16 +00:00
|
|
|
|
2014-05-02 05:17:49 +00:00
|
|
|
#include "datetime.h"
|
2014-03-31 16:51:50 +00:00
|
|
|
#include "parser.h"
|
2014-04-01 12:25:16 +00:00
|
|
|
#include "http_response.h"
|
2014-05-20 16:17:56 +00:00
|
|
|
#include "logging.h"
|
2014-07-29 11:20:50 +00:00
|
|
|
#include "settings.h"
|
2014-03-31 16:51:50 +00:00
|
|
|
|
2014-04-26 17:19:59 +00:00
|
|
|
namespace crow
|
2014-03-31 16:51:50 +00:00
|
|
|
{
|
|
|
|
using namespace boost;
|
|
|
|
using tcp = asio::ip::tcp;
|
2014-07-29 11:20:50 +00:00
|
|
|
#ifdef CROW_ENABLE_DEBUG
|
|
|
|
static int connectionCount;
|
|
|
|
#endif
|
2014-03-31 16:51:50 +00:00
|
|
|
template <typename Handler>
|
2014-07-29 11:20:50 +00:00
|
|
|
class Connection : public std::enable_shared_from_this<Connection<Handler>>
|
2014-03-31 16:51:50 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-05-02 05:17:49 +00:00
|
|
|
Connection(tcp::socket&& socket, Handler* handler, const std::string& server_name)
|
|
|
|
: socket_(std::move(socket)),
|
|
|
|
handler_(handler),
|
|
|
|
parser_(this),
|
2014-07-29 11:20:50 +00:00
|
|
|
server_name_(server_name),
|
2014-08-06 23:31:27 +00:00
|
|
|
deadline_(socket_.get_io_service()),
|
|
|
|
address_str_(boost::lexical_cast<std::string>(socket_.remote_endpoint()))
|
2014-07-29 11:20:50 +00:00
|
|
|
{
|
|
|
|
#ifdef CROW_ENABLE_DEBUG
|
|
|
|
connectionCount ++;
|
|
|
|
CROW_LOG_DEBUG << "Connection open, total " << connectionCount << ", " << this;
|
|
|
|
#endif
|
|
|
|
}
|
2014-08-05 18:54:38 +00:00
|
|
|
|
2014-07-29 11:20:50 +00:00
|
|
|
~Connection()
|
2014-03-31 16:51:50 +00:00
|
|
|
{
|
2014-08-05 18:54:38 +00:00
|
|
|
res.complete_request_handler_ = nullptr;
|
|
|
|
#ifdef CROW_ENABLE_DEBUG
|
2014-07-29 11:20:50 +00:00
|
|
|
connectionCount --;
|
|
|
|
CROW_LOG_DEBUG << "Connection closed, total " << connectionCount << ", " << this;
|
|
|
|
#endif
|
2014-08-05 18:54:38 +00:00
|
|
|
}
|
2014-03-31 16:51:50 +00:00
|
|
|
|
|
|
|
void start()
|
|
|
|
{
|
2014-07-29 11:20:50 +00:00
|
|
|
auto self = this->shared_from_this();
|
|
|
|
start_deadline();
|
|
|
|
|
2014-03-31 16:51:50 +00:00
|
|
|
do_read();
|
|
|
|
}
|
|
|
|
|
2014-05-02 12:54:25 +00:00
|
|
|
void handle_header()
|
|
|
|
{
|
|
|
|
// HTTP 1.1 Expect: 100-continue
|
|
|
|
if (parser_.check_version(1, 1) && parser_.headers.count("expect") && parser_.headers["expect"] == "100-continue")
|
|
|
|
{
|
|
|
|
buffers_.clear();
|
|
|
|
static std::string expect_100_continue = "HTTP/1.1 100 Continue\r\n\r\n";
|
|
|
|
buffers_.emplace_back(expect_100_continue.data(), expect_100_continue.size());
|
|
|
|
do_write();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 12:25:16 +00:00
|
|
|
void handle()
|
|
|
|
{
|
2014-05-02 09:22:02 +00:00
|
|
|
bool is_invalid_request = false;
|
|
|
|
|
2014-04-01 13:58:52 +00:00
|
|
|
request req = parser_.to_request();
|
2014-05-02 12:54:25 +00:00
|
|
|
if (parser_.check_version(1, 0))
|
2014-04-20 08:45:10 +00:00
|
|
|
{
|
|
|
|
// HTTP/1.0
|
|
|
|
if (!(req.headers.count("connection") && boost::iequals(req.headers["connection"],"Keep-Alive")))
|
|
|
|
close_connection_ = true;
|
|
|
|
}
|
2014-05-02 12:54:25 +00:00
|
|
|
else if (parser_.check_version(1, 1))
|
2014-04-20 08:45:10 +00:00
|
|
|
{
|
|
|
|
// HTTP/1.1
|
|
|
|
if (req.headers.count("connection") && req.headers["connection"] == "close")
|
|
|
|
close_connection_ = true;
|
2014-05-02 09:22:02 +00:00
|
|
|
if (!req.headers.count("host"))
|
|
|
|
{
|
|
|
|
is_invalid_request = true;
|
|
|
|
res = response(400);
|
|
|
|
}
|
2014-04-20 08:45:10 +00:00
|
|
|
}
|
|
|
|
|
2014-08-06 23:31:27 +00:00
|
|
|
CROW_LOG_INFO << "Request: " << address_str_ << " " << this << " HTTP/" << parser_.http_major << "." << parser_.http_minor << ' '
|
2014-08-05 18:54:38 +00:00
|
|
|
<< method_name(req.method) << " " << req.url;
|
|
|
|
|
|
|
|
|
2014-05-02 09:22:02 +00:00
|
|
|
if (!is_invalid_request)
|
|
|
|
{
|
2014-08-05 18:54:38 +00:00
|
|
|
deadline_.cancel();
|
|
|
|
auto self = this->shared_from_this();
|
|
|
|
res.complete_request_handler_ = [self]{ self->complete_request(); };
|
2014-08-06 16:18:33 +00:00
|
|
|
res.is_alive_helper_ = [this]()->bool{ return socket_.is_open(); };
|
2014-08-05 18:54:38 +00:00
|
|
|
handler_->handle(req, res);
|
2014-05-02 09:22:02 +00:00
|
|
|
}
|
2014-08-05 18:54:38 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
complete_request();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void complete_request()
|
|
|
|
{
|
|
|
|
CROW_LOG_INFO << "Response: " << this << ' ' << res.code << ' ' << close_connection_;
|
|
|
|
|
|
|
|
auto self = this->shared_from_this();
|
|
|
|
res.complete_request_handler_ = nullptr;
|
2014-08-10 21:25:32 +00:00
|
|
|
|
|
|
|
if (!socket_.is_open())
|
|
|
|
return;
|
2014-08-05 18:54:38 +00:00
|
|
|
|
|
|
|
static std::unordered_map<int, std::string> statusCodes = {
|
|
|
|
{200, "HTTP/1.1 200 OK\r\n"},
|
|
|
|
{201, "HTTP/1.1 201 Created\r\n"},
|
|
|
|
{202, "HTTP/1.1 202 Accepted\r\n"},
|
|
|
|
{204, "HTTP/1.1 204 No Content\r\n"},
|
2014-04-01 12:25:16 +00:00
|
|
|
|
2014-08-05 18:54:38 +00:00
|
|
|
{300, "HTTP/1.1 300 Multiple Choices\r\n"},
|
|
|
|
{301, "HTTP/1.1 301 Moved Permanently\r\n"},
|
|
|
|
{302, "HTTP/1.1 302 Moved Temporarily\r\n"},
|
|
|
|
{304, "HTTP/1.1 304 Not Modified\r\n"},
|
|
|
|
|
|
|
|
{400, "HTTP/1.1 400 Bad Request\r\n"},
|
|
|
|
{401, "HTTP/1.1 401 Unauthorized\r\n"},
|
|
|
|
{403, "HTTP/1.1 403 Forbidden\r\n"},
|
|
|
|
{404, "HTTP/1.1 404 Not Found\r\n"},
|
|
|
|
|
|
|
|
{500, "HTTP/1.1 500 Internal Server Error\r\n"},
|
|
|
|
{501, "HTTP/1.1 501 Not Implemented\r\n"},
|
|
|
|
{502, "HTTP/1.1 502 Bad Gateway\r\n"},
|
|
|
|
{503, "HTTP/1.1 503 Service Unavailable\r\n"},
|
|
|
|
};
|
2014-04-20 08:45:10 +00:00
|
|
|
|
2014-04-01 12:25:16 +00:00
|
|
|
static std::string seperator = ": ";
|
|
|
|
static std::string crlf = "\r\n";
|
|
|
|
|
2014-04-17 06:50:28 +00:00
|
|
|
buffers_.clear();
|
|
|
|
buffers_.reserve(4*(res.headers.size()+4)+3);
|
2014-04-01 12:25:16 +00:00
|
|
|
|
2014-04-22 11:19:03 +00:00
|
|
|
if (res.body.empty() && res.json_value.t() == json::type::Object)
|
2014-04-17 14:06:41 +00:00
|
|
|
{
|
2014-04-18 22:12:56 +00:00
|
|
|
res.body = json::dump(res.json_value);
|
2014-04-17 14:06:41 +00:00
|
|
|
}
|
|
|
|
|
2014-04-17 06:50:28 +00:00
|
|
|
if (!statusCodes.count(res.code))
|
|
|
|
res.code = 500;
|
|
|
|
{
|
|
|
|
auto& status = statusCodes.find(res.code)->second;
|
|
|
|
buffers_.emplace_back(status.data(), status.size());
|
|
|
|
}
|
2014-04-14 20:11:37 +00:00
|
|
|
|
2014-04-17 09:18:02 +00:00
|
|
|
if (res.code >= 400 && res.body.empty())
|
2014-04-14 20:11:37 +00:00
|
|
|
res.body = statusCodes[res.code].substr(9);
|
2014-04-01 12:25:16 +00:00
|
|
|
|
|
|
|
bool has_content_length = false;
|
2014-04-17 06:50:28 +00:00
|
|
|
bool has_date = false;
|
|
|
|
bool has_server = false;
|
|
|
|
|
2014-04-01 12:25:16 +00:00
|
|
|
for(auto& kv : res.headers)
|
|
|
|
{
|
2014-04-17 06:50:28 +00:00
|
|
|
buffers_.emplace_back(kv.first.data(), kv.first.size());
|
|
|
|
buffers_.emplace_back(seperator.data(), seperator.size());
|
|
|
|
buffers_.emplace_back(kv.second.data(), kv.second.size());
|
|
|
|
buffers_.emplace_back(crlf.data(), crlf.size());
|
2014-04-01 12:25:16 +00:00
|
|
|
|
|
|
|
if (boost::iequals(kv.first, "content-length"))
|
|
|
|
has_content_length = true;
|
2014-04-17 06:50:28 +00:00
|
|
|
if (boost::iequals(kv.first, "date"))
|
|
|
|
has_date = true;
|
|
|
|
if (boost::iequals(kv.first, "server"))
|
|
|
|
has_server = true;
|
2014-04-01 12:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!has_content_length)
|
2014-04-17 06:50:28 +00:00
|
|
|
{
|
2014-08-01 21:30:02 +00:00
|
|
|
content_length_ = std::to_string(res.body.size());
|
2014-05-02 05:17:49 +00:00
|
|
|
static std::string content_length_tag = "Content-Length: ";
|
|
|
|
buffers_.emplace_back(content_length_tag.data(), content_length_tag.size());
|
|
|
|
buffers_.emplace_back(content_length_.data(), content_length_.size());
|
2014-04-17 06:50:28 +00:00
|
|
|
buffers_.emplace_back(crlf.data(), crlf.size());
|
|
|
|
}
|
|
|
|
if (!has_server)
|
|
|
|
{
|
2014-05-02 05:17:49 +00:00
|
|
|
static std::string server_tag = "Server: ";
|
|
|
|
buffers_.emplace_back(server_tag.data(), server_tag.size());
|
|
|
|
buffers_.emplace_back(server_name_.data(), server_name_.size());
|
2014-04-17 06:50:28 +00:00
|
|
|
buffers_.emplace_back(crlf.data(), crlf.size());
|
|
|
|
}
|
|
|
|
if (!has_date)
|
|
|
|
{
|
2014-05-02 05:17:49 +00:00
|
|
|
static std::string date_tag = "Date: ";
|
|
|
|
date_str_ = get_cached_date_str();
|
|
|
|
buffers_.emplace_back(date_tag.data(), date_tag.size());
|
|
|
|
buffers_.emplace_back(date_str_.data(), date_str_.size());
|
2014-04-18 22:27:05 +00:00
|
|
|
buffers_.emplace_back(crlf.data(), crlf.size());
|
2014-04-17 06:50:28 +00:00
|
|
|
}
|
2014-04-01 12:25:16 +00:00
|
|
|
|
2014-04-17 06:50:28 +00:00
|
|
|
buffers_.emplace_back(crlf.data(), crlf.size());
|
|
|
|
buffers_.emplace_back(res.body.data(), res.body.size());
|
2014-04-01 12:25:16 +00:00
|
|
|
|
2014-04-17 06:50:28 +00:00
|
|
|
do_write();
|
2014-08-05 18:54:38 +00:00
|
|
|
res.clear();
|
2014-04-01 12:25:16 +00:00
|
|
|
}
|
|
|
|
|
2014-03-31 16:51:50 +00:00
|
|
|
private:
|
2014-05-02 05:17:49 +00:00
|
|
|
static std::string get_cached_date_str()
|
|
|
|
{
|
|
|
|
using namespace std::chrono;
|
2014-05-02 05:28:20 +00:00
|
|
|
thread_local auto last = steady_clock::now();
|
2014-05-02 05:17:49 +00:00
|
|
|
thread_local std::string date_str = DateTime().str();
|
|
|
|
|
2014-05-02 05:28:20 +00:00
|
|
|
if (steady_clock::now() - last >= seconds(1))
|
2014-05-02 05:17:49 +00:00
|
|
|
{
|
2014-05-02 05:28:20 +00:00
|
|
|
last = steady_clock::now();
|
2014-05-02 05:17:49 +00:00
|
|
|
date_str = DateTime().str();
|
|
|
|
}
|
|
|
|
return date_str;
|
|
|
|
}
|
|
|
|
|
2014-03-31 16:51:50 +00:00
|
|
|
void do_read()
|
|
|
|
{
|
2014-07-29 11:20:50 +00:00
|
|
|
auto self = this->shared_from_this();
|
2014-03-31 16:51:50 +00:00
|
|
|
socket_.async_read_some(boost::asio::buffer(buffer_),
|
2014-07-29 11:20:50 +00:00
|
|
|
[self, this](const boost::system::error_code& ec, std::size_t bytes_transferred)
|
2014-03-31 16:51:50 +00:00
|
|
|
{
|
2014-07-29 11:20:50 +00:00
|
|
|
bool error_while_reading = true;
|
2014-03-31 16:51:50 +00:00
|
|
|
if (!ec)
|
|
|
|
{
|
2014-04-01 12:25:16 +00:00
|
|
|
bool ret = parser_.feed(buffer_.data(), bytes_transferred);
|
2014-04-17 09:18:02 +00:00
|
|
|
if (ret)
|
2014-07-29 11:20:50 +00:00
|
|
|
{
|
2014-04-17 09:18:02 +00:00
|
|
|
do_read();
|
2014-07-29 11:20:50 +00:00
|
|
|
error_while_reading = false;
|
|
|
|
}
|
2014-04-01 12:25:16 +00:00
|
|
|
}
|
2014-07-29 11:20:50 +00:00
|
|
|
|
|
|
|
if (error_while_reading)
|
2014-04-01 12:25:16 +00:00
|
|
|
{
|
2014-07-29 11:20:50 +00:00
|
|
|
deadline_.cancel();
|
2014-04-10 16:43:33 +00:00
|
|
|
parser_.done();
|
2014-04-01 12:25:16 +00:00
|
|
|
socket_.close();
|
2014-07-29 11:20:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
start_deadline();
|
2014-04-01 12:25:16 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-04-17 06:50:28 +00:00
|
|
|
void do_write()
|
2014-04-01 12:25:16 +00:00
|
|
|
{
|
2014-07-29 11:20:50 +00:00
|
|
|
auto self = this->shared_from_this();
|
2014-04-17 06:50:28 +00:00
|
|
|
boost::asio::async_write(socket_, buffers_,
|
2014-07-29 11:20:50 +00:00
|
|
|
[&, self](const boost::system::error_code& ec, std::size_t bytes_transferred)
|
2014-04-01 12:25:16 +00:00
|
|
|
{
|
|
|
|
if (!ec)
|
|
|
|
{
|
2014-07-29 11:20:50 +00:00
|
|
|
start_deadline();
|
2014-04-01 12:25:16 +00:00
|
|
|
if (close_connection_)
|
|
|
|
{
|
2014-07-29 11:20:50 +00:00
|
|
|
socket_.close();
|
2014-04-01 12:25:16 +00:00
|
|
|
}
|
2014-03-31 16:51:50 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-07-29 11:20:50 +00:00
|
|
|
void start_deadline(int timeout = 5)
|
|
|
|
{
|
|
|
|
deadline_.expires_from_now(boost::posix_time::seconds(timeout));
|
|
|
|
auto self = this->shared_from_this();
|
|
|
|
deadline_.async_wait([self, this](const boost::system::error_code& ec)
|
|
|
|
{
|
|
|
|
if (ec || !socket_.is_open())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bool is_deadline_passed = deadline_.expires_at() <= boost::asio::deadline_timer::traits_type::now();
|
|
|
|
if (is_deadline_passed)
|
|
|
|
{
|
|
|
|
socket_.close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-03-31 16:51:50 +00:00
|
|
|
private:
|
|
|
|
tcp::socket socket_;
|
|
|
|
Handler* handler_;
|
|
|
|
|
2014-07-29 11:20:50 +00:00
|
|
|
std::array<char, 8192> buffer_;
|
2014-03-31 16:51:50 +00:00
|
|
|
|
2014-04-01 12:25:16 +00:00
|
|
|
HTTPParser<Connection> parser_;
|
|
|
|
response res;
|
|
|
|
|
|
|
|
bool close_connection_ = false;
|
2014-04-17 06:50:28 +00:00
|
|
|
|
|
|
|
const std::string& server_name_;
|
|
|
|
std::vector<boost::asio::const_buffer> buffers_;
|
2014-05-02 05:17:49 +00:00
|
|
|
|
|
|
|
std::string content_length_;
|
|
|
|
std::string date_str_;
|
2014-07-29 11:20:50 +00:00
|
|
|
|
|
|
|
boost::asio::deadline_timer deadline_;
|
2014-08-06 23:31:27 +00:00
|
|
|
std::string address_str_;
|
2014-03-31 16:51:50 +00:00
|
|
|
};
|
2014-07-29 11:20:50 +00:00
|
|
|
|
2014-03-31 16:51:50 +00:00
|
|
|
}
|