2020-01-18 08:38:21 +00:00
/*
2024-10-04 11:19:50 +00:00
* Copyright ( c ) 2018 - 2020 , Andreas Kling < andreas @ ladybird . org >
2021-05-23 21:31:16 +00:00
* Copyright ( c ) 2021 , Max Wipfli < mail @ maxwipfli . ch >
2024-03-18 03:22:27 +00:00
* Copyright ( c ) 2023 - 2024 , Shannon Booth < shannon @ serenityos . org >
2020-01-18 08:38:21 +00:00
*
2021-04-22 08:24:48 +00:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-01-18 08:38:21 +00:00
*/
2019-08-10 15:27:56 +00:00
# pragma once
2023-12-16 14:19:34 +00:00
# include <AK/ByteString.h>
2024-09-10 09:05:56 +00:00
# include <AK/CopyOnWrite.h>
2023-02-28 20:35:41 +00:00
# include <AK/String.h>
2019-08-10 15:27:56 +00:00
# include <AK/StringView.h>
2021-11-10 10:05:21 +00:00
# include <AK/Vector.h>
2024-10-05 03:14:27 +00:00
# include <LibURL/Host.h>
2024-10-05 03:54:27 +00:00
# include <LibURL/Origin.h>
2019-08-10 15:27:56 +00:00
2022-09-25 18:54:06 +00:00
// On Linux distros that use mlibc `basename` is defined as a macro that expands to `__mlibc_gnu_basename` or `__mlibc_gnu_basename_c`, so we undefine it.
# if defined(AK_OS_LINUX) && defined(basename)
# undef basename
# endif
2024-03-18 03:22:27 +00:00
namespace URL {
enum class PercentEncodeSet {
C0Control ,
Fragment ,
Query ,
SpecialQuery ,
Path ,
Userinfo ,
Component ,
ApplicationXWWWFormUrlencoded ,
EncodeURI
} ;
enum class ExcludeFragment {
No ,
Yes
} ;
2024-05-05 08:32:20 +00:00
// https://w3c.github.io/FileAPI/#blob-url-entry
// NOTE: This represents the raw bytes behind a 'Blob' (and does not yet support a MediaSourceQuery).
struct BlobURLEntry {
String type ;
ByteBuffer byte_buffer ;
2024-10-05 03:54:27 +00:00
Origin environment_origin ;
2024-05-05 08:32:20 +00:00
} ;
2024-03-18 03:22:27 +00:00
void append_percent_encoded_if_necessary ( StringBuilder & , u32 code_point , PercentEncodeSet set = PercentEncodeSet : : Userinfo ) ;
void append_percent_encoded ( StringBuilder & , u32 code_point ) ;
bool code_point_is_in_percent_encode_set ( u32 code_point , PercentEncodeSet ) ;
Optional < u16 > default_port_for_scheme ( StringView ) ;
bool is_special_scheme ( StringView ) ;
enum class SpaceAsPlus {
No ,
Yes ,
} ;
2024-08-10 01:12:19 +00:00
String percent_encode ( StringView input , PercentEncodeSet set = PercentEncodeSet : : Userinfo , SpaceAsPlus = SpaceAsPlus : : No ) ;
2024-03-18 03:22:27 +00:00
ByteString percent_decode ( StringView input ) ;
2019-08-10 15:27:56 +00:00
2023-07-23 08:10:32 +00:00
// https://url.spec.whatwg.org/#url-representation
// A URL is a struct that represents a universal identifier. To disambiguate from a valid URL string it can also be referred to as a URL record.
2019-08-10 15:27:56 +00:00
class URL {
2024-03-18 03:22:27 +00:00
friend class Parser ;
2021-05-25 20:13:15 +00:00
2019-08-10 15:27:56 +00:00
public :
2021-01-10 23:29:28 +00:00
URL ( ) = default ;
2021-11-10 23:55:02 +00:00
URL ( StringView ) ;
2023-12-16 14:19:34 +00:00
URL ( ByteString const & string )
2019-08-10 17:31:37 +00:00
: URL ( string . view ( ) )
{
}
2023-02-28 20:35:41 +00:00
URL ( String const & string )
: URL ( string . bytes_as_string_view ( ) )
{
}
2019-08-10 15:27:56 +00:00
2024-08-02 13:23:49 +00:00
bool is_valid ( ) const { return m_data - > valid ; }
2021-05-23 21:31:16 +00:00
2024-08-02 13:23:49 +00:00
String const & scheme ( ) const { return m_data - > scheme ; }
2024-08-04 10:02:02 +00:00
String const & username ( ) const { return m_data - > username ; }
String const & password ( ) const { return m_data - > password ; }
2024-08-02 13:23:49 +00:00
Host const & host ( ) const { return m_data - > host ; }
2023-07-27 09:40:41 +00:00
ErrorOr < String > serialized_host ( ) const ;
2023-12-16 14:19:34 +00:00
ByteString basename ( ) const ;
2024-08-02 13:23:49 +00:00
Optional < String > const & query ( ) const { return m_data - > query ; }
Optional < String > const & fragment ( ) const { return m_data - > fragment ; }
Optional < u16 > port ( ) const { return m_data - > port ; }
2023-12-16 14:19:34 +00:00
ByteString path_segment_at_index ( size_t index ) const ;
2024-08-02 13:23:49 +00:00
size_t path_segment_count ( ) const { return m_data - > paths . size ( ) ; }
2023-04-13 22:06:58 +00:00
2024-08-02 13:23:49 +00:00
u16 port_or_default ( ) const { return m_data - > port . value_or ( default_port_for_scheme ( m_data - > scheme ) . value_or ( 0 ) ) ; }
bool cannot_be_a_base_url ( ) const { return m_data - > cannot_be_a_base_url ; }
2023-07-26 08:54:36 +00:00
bool cannot_have_a_username_or_password_or_port ( ) const ;
2019-08-10 15:27:56 +00:00
2024-08-02 13:23:49 +00:00
bool includes_credentials ( ) const { return ! m_data - > username . is_empty ( ) | | ! m_data - > password . is_empty ( ) ; }
bool is_special ( ) const { return is_special_scheme ( m_data - > scheme ) ; }
2021-05-25 20:05:01 +00:00
2023-08-12 04:52:41 +00:00
void set_scheme ( String ) ;
2024-08-10 01:12:19 +00:00
void set_username ( StringView ) ;
void set_password ( StringView ) ;
2023-07-27 09:40:41 +00:00
void set_host ( Host ) ;
2021-09-13 20:12:16 +00:00
void set_port ( Optional < u16 > ) ;
2023-12-16 14:19:34 +00:00
void set_paths ( Vector < ByteString > const & ) ;
2024-08-05 03:14:00 +00:00
Vector < String > const & paths ( ) const { return m_data - > paths ; }
2024-08-02 13:23:49 +00:00
void set_query ( Optional < String > query ) { m_data - > query = move ( query ) ; }
void set_fragment ( Optional < String > fragment ) { m_data - > fragment = move ( fragment ) ; }
void set_cannot_be_a_base_url ( bool value ) { m_data - > cannot_be_a_base_url = value ; }
2023-08-06 04:32:44 +00:00
void append_path ( StringView ) ;
2023-04-09 13:21:00 +00:00
void append_slash ( )
{
// NOTE: To indicate that we want to end the path with a slash, we have to append an empty path segment.
2024-08-02 13:23:49 +00:00
m_data - > paths . append ( String { } ) ;
2023-04-09 13:21:00 +00:00
}
2019-10-05 08:14:42 +00:00
2024-08-05 04:55:39 +00:00
String serialize_path ( ) const ;
2023-12-16 14:19:34 +00:00
ByteString serialize ( ExcludeFragment = ExcludeFragment : : No ) const ;
ByteString serialize_for_display ( ) const ;
ByteString to_byte_string ( ) const { return serialize ( ) ; }
2023-06-17 07:15:40 +00:00
ErrorOr < String > to_string ( ) const ;
2021-05-27 19:38:16 +00:00
2021-09-13 19:18:14 +00:00
// HTML origin
2023-12-16 14:19:34 +00:00
ByteString serialize_origin ( ) const ;
2021-09-13 19:18:14 +00:00
2021-06-01 08:58:27 +00:00
bool equals ( URL const & other , ExcludeFragment = ExcludeFragment : : No ) const ;
2021-05-27 19:38:16 +00:00
2023-02-13 17:42:27 +00:00
URL complete_url ( StringView ) const ;
2019-11-18 21:04:39 +00:00
2024-08-02 13:23:49 +00:00
[ [ nodiscard ] ] bool operator = = ( URL const & other ) const
{
if ( m_data . ptr ( ) = = other . m_data . ptr ( ) )
return true ;
return equals ( other , ExcludeFragment : : No ) ;
}
2020-06-01 19:50:07 +00:00
2024-08-02 13:23:49 +00:00
Optional < BlobURLEntry > const & blob_url_entry ( ) const { return m_data - > blob_url_entry ; }
void set_blob_url_entry ( Optional < BlobURLEntry > entry ) { m_data - > blob_url_entry = move ( entry ) ; }
2024-05-05 08:32:20 +00:00
2019-08-10 15:27:56 +00:00
private :
2020-04-11 21:07:23 +00:00
bool compute_validity ( ) const ;
2019-08-10 15:27:56 +00:00
2024-08-02 13:23:49 +00:00
struct Data : public RefCounted < Data > {
NonnullRefPtr < Data > clone ( )
{
auto clone = adopt_ref ( * new Data ) ;
clone - > valid = valid ;
clone - > scheme = scheme ;
clone - > username = username ;
clone - > password = password ;
clone - > host = host ;
clone - > port = port ;
clone - > paths = paths ;
clone - > query = query ;
clone - > fragment = fragment ;
clone - > cannot_be_a_base_url = cannot_be_a_base_url ;
clone - > blob_url_entry = blob_url_entry ;
return clone ;
}
bool valid { false } ;
// A URL’ s scheme is an ASCII string that identifies the type of URL and can be used to dispatch a URL for further processing after parsing. It is initially the empty string.
String scheme ;
// A URL’ s username is an ASCII string identifying a username. It is initially the empty string.
String username ;
// A URL’ s password is an ASCII string identifying a password. It is initially the empty string.
String password ;
// A URL’ s host is null or a host. It is initially null.
Host host ;
// A URL’ s port is either null or a 16-bit unsigned integer that identifies a networking port. It is initially null.
Optional < u16 > port ;
// A URL’ s path is either a URL path segment or a list of zero or more URL path segments, usually identifying a location. It is initially « ».
// A URL path segment is an ASCII string. It commonly refers to a directory or a file, but has no predefined meaning.
Vector < String > paths ;
// A URL’ s query is either null or an ASCII string. It is initially null.
Optional < String > query ;
// A URL’ s fragment is either null or an ASCII string that can be used for further processing on the resource the URL’ s other components identify. It is initially null.
Optional < String > fragment ;
bool cannot_be_a_base_url { false } ;
// https://url.spec.whatwg.org/#concept-url-blob-entry
// A URL also has an associated blob URL entry that is either null or a blob URL entry. It is initially null.
Optional < BlobURLEntry > blob_url_entry ;
} ;
2024-09-10 09:05:56 +00:00
AK : : CopyOnWrite < Data > m_data ;
2019-08-10 15:27:56 +00:00
} ;
2024-03-18 03:22:27 +00:00
URL create_with_url_or_path ( ByteString const & ) ;
URL create_with_file_scheme ( ByteString const & path , ByteString const & fragment = { } , ByteString const & hostname = { } ) ;
URL create_with_data ( StringView mime_type , StringView payload , bool is_base64 = false ) ;
}
2020-10-04 11:29:47 +00:00
template < >
2024-03-18 03:22:27 +00:00
struct AK : : Formatter < URL : : URL > : AK : : Formatter < StringView > {
ErrorOr < void > format ( FormatBuilder & builder , URL : : URL const & value )
2020-10-04 11:29:47 +00:00
{
2021-11-16 00:15:21 +00:00
return Formatter < StringView > : : format ( builder , value . serialize ( ) ) ;
2020-10-04 11:29:47 +00:00
}
} ;
2020-06-01 19:50:07 +00:00
template < >
2024-03-18 03:22:27 +00:00
struct AK : : Traits < URL : : URL > : public AK : : DefaultTraits < URL : : URL > {
static unsigned hash ( URL : : URL const & url ) { return url . to_byte_string ( ) . hash ( ) ; }
2020-06-01 19:50:07 +00:00
} ;