From 8a662b35e197ddb82d9e8f5e33b0aced283fbbeb Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Thu, 26 Sep 2019 09:10:30 -0700 Subject: [PATCH] Http server: add options to ws https to redirect all requests to a given url. --- DOCKER_VERSION | 2 +- docs/CHANGELOG.md | 8 ++++++++ ixwebsocket/IXWebSocketVersion.h | 2 +- ws/ws.cpp | 6 +++++- ws/ws.h | 5 ++++- ws/ws_httpd.cpp | 22 +++++++++++++++++++++- 6 files changed, 40 insertions(+), 5 deletions(-) diff --git a/DOCKER_VERSION b/DOCKER_VERSION index d938f420..2036a714 100644 --- a/DOCKER_VERSION +++ b/DOCKER_VERSION @@ -1 +1 @@ -6.2.7 +6.2.8 diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 2a497846..71fcc86d 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -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. diff --git a/ixwebsocket/IXWebSocketVersion.h b/ixwebsocket/IXWebSocketVersion.h index b64535c2..d84b5a62 100644 --- a/ixwebsocket/IXWebSocketVersion.h +++ b/ixwebsocket/IXWebSocketVersion.h @@ -6,4 +6,4 @@ #pragma once -#define IX_WEBSOCKET_VERSION "6.2.7" +#define IX_WEBSOCKET_VERSION "6.2.8" diff --git a/ws/ws.cpp b/ws/ws.cpp index cd6ef4bc..7aaf7bd0 100644 --- a/ws/ws.cpp +++ b/ws/ws.cpp @@ -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")) { diff --git a/ws/ws.h b/ws/ws.h index b56537cf..5f66d936 100644 --- a/ws/ws.h +++ b/ws/ws.h @@ -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); diff --git a/ws/ws_httpd.cpp b/ws/ws_httpd.cpp index c06850d4..aad3cbba 100644 --- a/ws/ws_httpd.cpp +++ b/ws/ws_httpd.cpp @@ -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*/) -> HttpResponsePtr { + WebSocketHttpHeaders headers; + headers["Location"] = redirectUrl; + + return std::make_shared( + 301, "OK", HttpErrorCode::Ok, headers, std::string()); + }); + } + auto res = server.listen(); if (!res.first) {