main.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibCore/ArgsParser.h>
  8. #include <LibCore/EventLoop.h>
  9. #include <LibCore/File.h>
  10. #include <LibCore/MappedFile.h>
  11. #include <LibCore/System.h>
  12. #include <LibCore/TCPServer.h>
  13. #include <LibHTTP/HttpRequest.h>
  14. #include <LibMain/Main.h>
  15. #include <WebServer/Client.h>
  16. #include <WebServer/Configuration.h>
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. ErrorOr<int> serenity_main(Main::Arguments arguments)
  20. {
  21. String default_listen_address = "0.0.0.0";
  22. u16 default_port = 8000;
  23. String root_path = "/www";
  24. String listen_address = default_listen_address;
  25. int port = default_port;
  26. String username;
  27. String password;
  28. Core::ArgsParser args_parser;
  29. args_parser.add_option(listen_address, "IP address to listen on", "listen-address", 'l', "listen_address");
  30. args_parser.add_option(port, "Port to listen on", "port", 'p', "port");
  31. args_parser.add_option(username, "HTTP basic authentication username", "user", 'U', "username");
  32. args_parser.add_option(password, "HTTP basic authentication password", "pass", 'P', "password");
  33. args_parser.add_positional_argument(root_path, "Path to serve the contents of", "path", Core::ArgsParser::Required::No);
  34. args_parser.parse(arguments);
  35. auto ipv4_address = IPv4Address::from_string(listen_address);
  36. if (!ipv4_address.has_value()) {
  37. warnln("Invalid listen address: {}", listen_address);
  38. return 1;
  39. }
  40. if ((u16)port != port) {
  41. warnln("Invalid port number: {}", port);
  42. return 1;
  43. }
  44. if (username.is_empty() != password.is_empty()) {
  45. warnln("Both username and password are required for HTTP basic authentication.");
  46. return 1;
  47. }
  48. auto real_root_path = Core::File::real_path_for(root_path);
  49. if (!Core::File::exists(real_root_path)) {
  50. warnln("Root path does not exist: '{}'", root_path);
  51. return 1;
  52. }
  53. TRY(Core::System::pledge("stdio accept rpath inet unix"));
  54. WebServer::Configuration configuration(real_root_path);
  55. if (!username.is_empty() && !password.is_empty())
  56. configuration.set_credentials(HTTP::HttpRequest::BasicAuthenticationCredentials { username, password });
  57. Core::EventLoop loop;
  58. auto server = TRY(Core::TCPServer::try_create());
  59. server->on_ready_to_accept = [&] {
  60. auto maybe_client_socket = server->accept();
  61. if (maybe_client_socket.is_error()) {
  62. warnln("Failed to accept the client: {}", maybe_client_socket.error());
  63. return;
  64. }
  65. auto maybe_buffered_socket = Core::Stream::BufferedTCPSocket::create(maybe_client_socket.release_value());
  66. if (maybe_buffered_socket.is_error()) {
  67. warnln("Could not obtain a buffered socket for the client: {}", maybe_buffered_socket.error());
  68. return;
  69. }
  70. // FIXME: Propagate errors
  71. MUST(maybe_buffered_socket.value()->set_blocking(true));
  72. auto client = WebServer::Client::construct(maybe_buffered_socket.release_value(), server);
  73. client->start();
  74. };
  75. TRY(server->listen(ipv4_address.value(), port));
  76. outln("Listening on {}:{}", ipv4_address.value(), port);
  77. TRY(Core::System::unveil("/res/icons", "r"));
  78. TRY(Core::System::unveil(real_root_path.characters(), "r"));
  79. TRY(Core::System::unveil(nullptr, nullptr));
  80. TRY(Core::System::pledge("stdio accept rpath"));
  81. return loop.exec();
  82. }