mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-30 00:31:14 +00:00
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".
This commit is contained in:
parent
4190fd2199
commit
2deffeb74d
Notes:
sideshowbarker
2024-07-18 17:10:42 +09:00
Author: https://github.com/EdwinHoksberg 🔰 Commit: https://github.com/SerenityOS/serenity/commit/2deffeb74de Pull-request: https://github.com/SerenityOS/serenity/pull/7575 Reviewed-by: https://github.com/linusg
|
@ -15,16 +15,25 @@
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
|
String default_listen_address = "0.0.0.0";
|
||||||
u16 default_port = 8000;
|
u16 default_port = 8000;
|
||||||
const char* root_path = "/www";
|
const char* root_path = "/www";
|
||||||
|
|
||||||
|
String listen_address = default_listen_address;
|
||||||
int port = default_port;
|
int port = default_port;
|
||||||
|
|
||||||
Core::ArgsParser args_parser;
|
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_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.add_positional_argument(root_path, "Path to serve the contents of", "path", Core::ArgsParser::Required::No);
|
||||||
args_parser.parse(argc, argv);
|
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) {
|
if ((u16)port != port) {
|
||||||
printf("Warning: invalid port number: %d\n", port);
|
printf("Warning: invalid port number: %d\n", port);
|
||||||
port = default_port;
|
port = default_port;
|
||||||
|
@ -53,12 +62,12 @@ int main(int argc, char** argv)
|
||||||
client->start();
|
client->start();
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!server->listen({}, port)) {
|
if (!server->listen(ipv4_address.value(), port)) {
|
||||||
warnln("Failed to listen on 0.0.0.0:{}", port);
|
warnln("Failed to listen on {}:{}", ipv4_address.value(), port);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
outln("Listening on 0.0.0.0:{}", port);
|
outln("Listening on {}:{}", ipv4_address.value(), port);
|
||||||
|
|
||||||
if (unveil("/res/icons", "r") < 0) {
|
if (unveil("/res/icons", "r") < 0) {
|
||||||
perror("unveil");
|
perror("unveil");
|
||||||
|
|
Loading…
Reference in a new issue