2023-08-01 20:39:19 +00:00
/*
* Copyright ( c ) 2018 - 2020 , Andreas Kling < kling @ serenityos . org >
* Copyright ( c ) 2023 , Andrew Kaster < akaster @ serenityos . org >
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
# include <AK/LexicalPath.h>
# include <AK/OwnPtr.h>
# include <LibCore/ArgsParser.h>
# include <LibCore/EventLoop.h>
# include <LibCore/LocalServer.h>
2024-08-01 11:03:03 +00:00
# include <LibCore/Process.h>
2023-08-01 20:39:19 +00:00
# include <LibCore/System.h>
# include <LibFileSystem/FileSystem.h>
# include <LibIPC/SingleServer.h>
# include <LibMain/Main.h>
# include <LibTLS/Certificate.h>
# include <RequestServer/ConnectionFromClient.h>
# include <RequestServer/HttpProtocol.h>
# include <RequestServer/HttpsProtocol.h>
2024-04-22 16:57:38 +00:00
# if defined(AK_OS_MACOS)
# include <LibCore / Platform / ProcessStatisticsMach.h>
# endif
2024-07-14 16:29:33 +00:00
static ErrorOr < ByteString > find_certificates ( StringView serenity_resource_root )
2023-08-01 20:39:19 +00:00
{
2024-02-23 20:40:16 +00:00
auto cert_path = ByteString : : formatted ( " {}/ladybird/cacert.pem " , serenity_resource_root ) ;
if ( ! FileSystem : : exists ( cert_path ) )
2024-09-05 11:06:15 +00:00
return Error : : from_string_literal ( " Don't know how to load certs! " ) ;
2023-08-01 20:39:19 +00:00
return cert_path ;
}
ErrorOr < int > serenity_main ( Main : : Arguments arguments )
{
2023-12-11 18:19:41 +00:00
AK : : set_rich_debug_enabled ( true ) ;
2023-08-02 23:21:17 +00:00
StringView serenity_resource_root ;
2024-02-05 16:34:51 +00:00
Vector < ByteString > certificates ;
2024-04-22 16:57:38 +00:00
StringView mach_server_name ;
2024-08-01 11:03:03 +00:00
bool wait_for_debugger = false ;
2023-08-01 20:39:19 +00:00
Core : : ArgsParser args_parser ;
2024-02-05 16:34:51 +00:00
args_parser . add_option ( certificates , " Path to a certificate file " , " certificate " , ' C ' , " certificate " ) ;
2023-08-02 23:21:17 +00:00
args_parser . add_option ( serenity_resource_root , " Absolute path to directory for serenity resources " , " serenity-resource-root " , ' r ' , " serenity-resource-root " ) ;
2024-04-22 16:57:38 +00:00
args_parser . add_option ( mach_server_name , " Mach server name " , " mach-server-name " , 0 , " mach_server_name " ) ;
2024-08-01 11:03:03 +00:00
args_parser . add_option ( wait_for_debugger , " Wait for debugger " , " wait-for-debugger " ) ;
2023-08-01 20:39:19 +00:00
args_parser . parse ( arguments ) ;
2024-08-01 11:03:03 +00:00
if ( wait_for_debugger )
Core : : Process : : wait_for_debugger_and_break ( ) ;
2023-08-02 23:21:17 +00:00
// Ensure the certificates are read out here.
2024-02-05 16:34:51 +00:00
if ( certificates . is_empty ( ) )
certificates . append ( TRY ( find_certificates ( serenity_resource_root ) ) ) ;
2024-07-06 21:12:39 +00:00
DefaultRootCACertificates : : set_default_certificate_paths ( certificates . span ( ) ) ;
[[maybe_unused]] auto & certs = DefaultRootCACertificates : : the ( ) ;
2023-08-02 23:21:17 +00:00
2023-08-01 20:39:19 +00:00
Core : : EventLoop event_loop ;
2024-04-22 16:57:38 +00:00
# if defined(AK_OS_MACOS)
if ( ! mach_server_name . is_empty ( ) )
Core : : Platform : : register_with_mach_server ( mach_server_name ) ;
# endif
2024-03-05 22:15:41 +00:00
RequestServer : : HttpProtocol : : install ( ) ;
RequestServer : : HttpsProtocol : : install ( ) ;
2023-08-01 20:39:19 +00:00
auto client = TRY ( IPC : : take_over_accepted_client_from_system_server < RequestServer : : ConnectionFromClient > ( ) ) ;
2024-03-05 22:15:41 +00:00
return event_loop . exec ( ) ;
2023-08-01 20:39:19 +00:00
}