Minor cleanups

This commit is contained in:
eidheim 2018-07-29 22:27:10 +02:00
commit f39f21b56f
3 changed files with 15 additions and 17 deletions

View file

@ -412,6 +412,20 @@ namespace SimpleWeb {
return connection; return connection;
} }
std::pair<std::string, unsigned short> parse_host_port(const std::string &host_port, unsigned short default_port) const noexcept {
std::pair<std::string, unsigned short> parsed_host_port;
std::size_t host_end = host_port.find(':');
if(host_end == std::string::npos) {
parsed_host_port.first = host_port;
parsed_host_port.second = default_port;
}
else {
parsed_host_port.first = host_port.substr(0, host_end);
parsed_host_port.second = static_cast<unsigned short>(stoul(host_port.substr(host_end + 1)));
}
return parsed_host_port;
}
virtual std::shared_ptr<Connection> create_connection() noexcept = 0; virtual std::shared_ptr<Connection> create_connection() noexcept = 0;
virtual void connect(const std::shared_ptr<Session> &) = 0; virtual void connect(const std::shared_ptr<Session> &) = 0;
@ -434,20 +448,6 @@ namespace SimpleWeb {
return streambuf; return streambuf;
} }
std::pair<std::string, unsigned short> parse_host_port(const std::string &host_port, unsigned short default_port) const noexcept {
std::pair<std::string, unsigned short> parsed_host_port;
std::size_t host_end = host_port.find(':');
if(host_end == std::string::npos) {
parsed_host_port.first = host_port;
parsed_host_port.second = default_port;
}
else {
parsed_host_port.first = host_port.substr(0, host_end);
parsed_host_port.second = static_cast<unsigned short>(stoul(host_port.substr(host_end + 1)));
}
return parsed_host_port;
}
void write(const std::shared_ptr<Session> &session) { void write(const std::shared_ptr<Session> &session) {
session->connection->set_timeout(); session->connection->set_timeout();
asio::async_write(*session->connection->socket, session->request_streambuf->data(), [this, session](const error_code &ec, std::size_t /*bytes_transferred*/) { asio::async_write(*session->connection->socket, session->request_streambuf->data(), [this, session](const error_code &ec, std::size_t /*bytes_transferred*/) {

View file

@ -91,7 +91,7 @@ namespace SimpleWeb {
if(!ResponseMessage::parse(response->content, response->http_version, response->status_code, response->header)) if(!ResponseMessage::parse(response->content, response->http_version, response->status_code, response->header))
session->callback(session->connection, make_error_code::make_error_code(errc::protocol_error)); session->callback(session->connection, make_error_code::make_error_code(errc::protocol_error));
else { else {
if(response->status_code.empty() || response->status_code.compare(0, 3, "200") != 0) if(response->status_code.compare(0, 3, "200") != 0)
session->callback(session->connection, make_error_code::make_error_code(errc::permission_denied)); session->callback(session->connection, make_error_code::make_error_code(errc::permission_denied));
else else
this->handshake(session); this->handshake(session);

View file

@ -228,7 +228,6 @@ namespace SimpleWeb {
public: public:
/// Parse request line and header fields /// Parse request line and header fields
static bool parse(std::istream &stream, std::string &method, std::string &path, std::string &query_string, std::string &version, CaseInsensitiveMultimap &header) noexcept { static bool parse(std::istream &stream, std::string &method, std::string &path, std::string &query_string, std::string &version, CaseInsensitiveMultimap &header) noexcept {
header.clear();
std::string line; std::string line;
std::size_t method_end; std::size_t method_end;
if(getline(stream, line) && (method_end = line.find(' ')) != std::string::npos) { if(getline(stream, line) && (method_end = line.find(' ')) != std::string::npos) {
@ -276,7 +275,6 @@ namespace SimpleWeb {
public: public:
/// Parse status line and header fields /// Parse status line and header fields
static bool parse(std::istream &stream, std::string &version, std::string &status_code, CaseInsensitiveMultimap &header) noexcept { static bool parse(std::istream &stream, std::string &version, std::string &status_code, CaseInsensitiveMultimap &header) noexcept {
header.clear();
std::string line; std::string line;
std::size_t version_end; std::size_t version_end;
if(getline(stream, line) && (version_end = line.find(' ')) != std::string::npos) { if(getline(stream, line) && (version_end = line.find(' ')) != std::string::npos) {