Http server: add options to ws https to redirect all requests to a given url.

This commit is contained in:
Benjamin Sergeant 2019-09-26 09:10:30 -07:00
parent 3cd7c0194f
commit 8a662b35e1
6 changed files with 40 additions and 5 deletions

View File

@ -1 +1 @@
6.2.7
6.2.8

View File

@ -1,6 +1,14 @@
# Changelog
All notable changes to this project will be documented in this file.
## [6.2.8] - 2019-09-26
- Http server: add options to ws https to redirect all requests to a given url.
```
ws httpd -L --redirect_url https://www.google.com
```
## [6.2.7] - 2019-09-25
- Stop having ws send subcommand send a binary message in text mode, which would cause error in `make ws_test` shell script test.

View File

@ -6,4 +6,4 @@
#pragma once
#define IX_WEBSOCKET_VERSION "6.2.7"
#define IX_WEBSOCKET_VERSION "6.2.8"

View File

@ -63,6 +63,7 @@ int main(int argc, char** argv)
std::string redisHosts("127.0.0.1");
std::string redisPassword;
std::string appsConfigPath("appsConfig.json");
std::string redirectUrl;
bool headersOnly = false;
bool followRedirects = false;
bool verbose = false;
@ -75,6 +76,7 @@ int main(int argc, char** argv)
bool disablePerMessageDeflate = false;
bool greetings = false;
bool binaryMode = false;
bool redirect = false;
int port = 8008;
int redisPort = 6379;
int statsdPort = 8125;
@ -238,6 +240,8 @@ int main(int argc, char** argv)
CLI::App* httpServerApp = app.add_subcommand("httpd", "HTTP server");
httpServerApp->add_option("--port", port, "Port");
httpServerApp->add_option("--host", hostname, "Hostname");
httpServerApp->add_flag("-L", redirect, "Redirect all request to redirect_url");
httpServerApp->add_option("--redirect_url", redirectUrl, "Url to redirect to");
CLI::App* autobahnApp = app.add_subcommand("autobahn", "Test client Autobahn compliance");
autobahnApp->add_option("--url", url, "url");
@ -362,7 +366,7 @@ int main(int argc, char** argv)
}
else if (app.got_subcommand("httpd"))
{
ret = ix::ws_httpd_main(port, hostname);
ret = ix::ws_httpd_main(port, hostname, redirect, redirectUrl);
}
else if (app.got_subcommand("autobahn"))
{

View File

@ -108,7 +108,10 @@ namespace ix
bool verbose,
const std::string& appsConfigPath);
int ws_httpd_main(int port, const std::string& hostname);
int ws_httpd_main(int port,
const std::string& hostname,
bool redirect,
const std::string& redirectUrl);
int ws_autobahn_main(const std::string& url, bool quiet);

View File

@ -13,12 +13,32 @@
namespace ix
{
int ws_httpd_main(int port, const std::string& hostname)
int ws_httpd_main(int port,
const std::string& hostname,
bool redirect,
const std::string& redirectUrl)
{
spdlog::info("Listening on {}:{}", hostname, port);
ix::HttpServer server(port, hostname);
if (redirect)
{
//
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections
//
server.setOnConnectionCallback(
[redirectUrl](
HttpRequestPtr request,
std::shared_ptr<ConnectionState> /*connectionState*/) -> HttpResponsePtr {
WebSocketHttpHeaders headers;
headers["Location"] = redirectUrl;
return std::make_shared<HttpResponse>(
301, "OK", HttpErrorCode::Ok, headers, std::string());
});
}
auto res = server.listen();
if (!res.first)
{