summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorEdwin Hoksberg <mail@edwinhoksberg.nl>2021-05-30 01:00:53 +0200
committerLinus Groh <mail@linusgroh.de>2021-05-30 13:03:59 +0100
commit2deffeb74de0985dd4fabce235377c93cf01ddb1 (patch)
tree8a205d39605bb92b7d1642e17678d78402da5647 /Userland
parent4190fd219930a6c8b631c396ccafe5190f821414 (diff)
downloadserenity-2deffeb74de0985dd4fabce235377c93cf01ddb1.zip
WebServer: Add optional listen address argument
With this we can make the Webserver listen on another address than the default of "0.0.0.0".
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Services/WebServer/main.cpp15
1 files changed, 12 insertions, 3 deletions
diff --git a/Userland/Services/WebServer/main.cpp b/Userland/Services/WebServer/main.cpp
index 8fe7589ad6..69f1226c59 100644
--- a/Userland/Services/WebServer/main.cpp
+++ b/Userland/Services/WebServer/main.cpp
@@ -15,16 +15,25 @@
int main(int argc, char** argv)
{
+ String default_listen_address = "0.0.0.0";
u16 default_port = 8000;
const char* root_path = "/www";
+ String listen_address = default_listen_address;
int port = default_port;
Core::ArgsParser args_parser;
+ args_parser.add_option(listen_address, "IP address to listen on", "listen-address", 'l', "listen_address");
args_parser.add_option(port, "Port to listen on", "port", 'p', "port");
args_parser.add_positional_argument(root_path, "Path to serve the contents of", "path", Core::ArgsParser::Required::No);
args_parser.parse(argc, argv);
+ auto ipv4_address = IPv4Address::from_string(listen_address);
+ if (!ipv4_address.has_value()) {
+ warnln("Invalid listen address: {}", listen_address);
+ return 1;
+ }
+
if ((u16)port != port) {
printf("Warning: invalid port number: %d\n", port);
port = default_port;
@@ -53,12 +62,12 @@ int main(int argc, char** argv)
client->start();
};
- if (!server->listen({}, port)) {
- warnln("Failed to listen on 0.0.0.0:{}", port);
+ if (!server->listen(ipv4_address.value(), port)) {
+ warnln("Failed to listen on {}:{}", ipv4_address.value(), port);
return 1;
}
- outln("Listening on 0.0.0.0:{}", port);
+ outln("Listening on {}:{}", ipv4_address.value(), port);
if (unveil("/res/icons", "r") < 0) {
perror("unveil");