Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix faulty https redirection. #281

Merged
merged 3 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions include/crow/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ namespace crow
validate();

#ifdef CROW_ENABLE_SSL
if (use_ssl_)
if (ssl_used_)
{
ssl_server_ = std::move(std::unique_ptr<ssl_server_t>(new ssl_server_t(this, bindaddr_, port_, server_name_, &middlewares_, concurrency_, timeout_, &ssl_context_)));
ssl_server_->set_tick_function(tick_interval_, tick_function_);
Expand All @@ -304,7 +304,7 @@ namespace crow
void stop()
{
#ifdef CROW_ENABLE_SSL
if (use_ssl_)
if (ssl_used_)
{
if (ssl_server_) {
ssl_server_->stop();
Expand All @@ -331,7 +331,7 @@ namespace crow
///use certificate and key files for SSL
self_t& ssl_file(const std::string& crt_filename, const std::string& key_filename)
{
use_ssl_ = true;
ssl_used_ = true;
ssl_context_.set_verify_mode(boost::asio::ssl::verify_peer);
ssl_context_.set_verify_mode(boost::asio::ssl::verify_client_once);
ssl_context_.use_certificate_file(crt_filename, ssl_context_t::pem);
Expand All @@ -347,7 +347,7 @@ namespace crow
///use .pem file for SSL
self_t& ssl_file(const std::string& pem_filename)
{
use_ssl_ = true;
ssl_used_ = true;
ssl_context_.set_verify_mode(boost::asio::ssl::verify_peer);
ssl_context_.set_verify_mode(boost::asio::ssl::verify_client_once);
ssl_context_.load_verify_file(pem_filename);
Expand All @@ -361,15 +361,15 @@ namespace crow

self_t& ssl(boost::asio::ssl::context&& ctx)
{
use_ssl_ = true;
ssl_used_ = true;
ssl_context_ = std::move(ctx);
return *this;
}


bool use_ssl_{false};
ssl_context_t ssl_context_{boost::asio::ssl::context::sslv23};

bool ssl_used() const
{
return ssl_used_;
}
#else
template <typename T, typename ... Remain>
self_t& ssl_file(T&&, Remain&&...)
Expand All @@ -392,6 +392,11 @@ namespace crow
"Define CROW_ENABLE_SSL to enable ssl support.");
return *this;
}

bool ssl_used() const
{
return false;
}
#endif

// middleware
Expand Down Expand Up @@ -441,7 +446,10 @@ namespace crow

#ifdef CROW_ENABLE_SSL
std::unique_ptr<ssl_server_t> ssl_server_;
bool ssl_used_{false};
ssl_context_t ssl_context_{boost::asio::ssl::context::sslv23};
#endif

std::unique_ptr<server_t> server_;

std::vector<int> signals_{SIGINT, SIGTERM};
Expand Down
7 changes: 4 additions & 3 deletions include/crow/http_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,11 @@ namespace crow
if (!location.empty() && location.find("://", 0) == std::string::npos)
{
#ifdef CROW_ENABLE_SSL
location.insert(0, "https://" + req_.get_header_value("Host"));
#else
location.insert(0, "http://" + req_.get_header_value("Host"));
if (handler_->ssl_used())
location.insert(0, "https://" + req_.get_header_value("Host"));
else
#endif
location.insert(0, "http://" + req_.get_header_value("Host"));
res.set_header("location", location);
}

Expand Down
7 changes: 2 additions & 5 deletions include/crow/http_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ namespace crow
port_ = acceptor_.local_endpoint().port();
handler_->port(port_);

CROW_LOG_INFO << server_name_ << " server is running at " << bindaddr_ <<":" << acceptor_.local_endpoint().port()

CROW_LOG_INFO << server_name_ << " server is running at " << (handler_->ssl_used() ? "https://" : "http://") << bindaddr_ <<":" << acceptor_.local_endpoint().port()
<< " using " << concurrency_ << " threads";
CROW_LOG_INFO << "Call `app.loglevel(crow::LogLevel::Warning)` to hide Info level logs.";

Expand Down Expand Up @@ -231,10 +232,6 @@ namespace crow

std::tuple<Middlewares...>* middlewares_;

#ifdef CROW_ENABLE_SSL
bool use_ssl_{false};
boost::asio::ssl::context ssl_context_{boost::asio::ssl::context::sslv23};
#endif
typename Adaptor::context* adaptor_ctx_;
};
}