| 
									
										
										
										
											2021-05-25 22:13:15 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch> | 
					
						
							| 
									
										
										
										
											2024-03-18 16:22:27 +13:00
										 |  |  |  * Copyright (c) 2023-2024, Shannon Booth <shannon@serenityos.org> | 
					
						
							| 
									
										
										
										
											2021-05-25 22:13:15 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/Optional.h>
 | 
					
						
							|  |  |  | #include <AK/StringView.h>
 | 
					
						
							| 
									
										
										
										
											2024-08-05 16:03:53 +01:00
										 |  |  | #include <LibTextCodec/Encoder.h>
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:22:27 +13:00
										 |  |  | #include <LibURL/URL.h>
 | 
					
						
							| 
									
										
										
										
											2021-05-25 22:13:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:22:27 +13:00
										 |  |  | namespace URL { | 
					
						
							| 
									
										
										
										
											2021-05-25 22:13:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 12:40:04 +02:00
										 |  |  | #define ENUMERATE_STATES                 \
 | 
					
						
							|  |  |  |     STATE(SchemeStart)                   \ | 
					
						
							|  |  |  |     STATE(Scheme)                        \ | 
					
						
							|  |  |  |     STATE(NoScheme)                      \ | 
					
						
							|  |  |  |     STATE(SpecialRelativeOrAuthority)    \ | 
					
						
							|  |  |  |     STATE(PathOrAuthority)               \ | 
					
						
							|  |  |  |     STATE(Relative)                      \ | 
					
						
							|  |  |  |     STATE(RelativeSlash)                 \ | 
					
						
							|  |  |  |     STATE(SpecialAuthoritySlashes)       \ | 
					
						
							|  |  |  |     STATE(SpecialAuthorityIgnoreSlashes) \ | 
					
						
							|  |  |  |     STATE(Authority)                     \ | 
					
						
							|  |  |  |     STATE(Host)                          \ | 
					
						
							|  |  |  |     STATE(Hostname)                      \ | 
					
						
							|  |  |  |     STATE(Port)                          \ | 
					
						
							|  |  |  |     STATE(File)                          \ | 
					
						
							|  |  |  |     STATE(FileSlash)                     \ | 
					
						
							|  |  |  |     STATE(FileHost)                      \ | 
					
						
							|  |  |  |     STATE(PathStart)                     \ | 
					
						
							|  |  |  |     STATE(Path)                          \ | 
					
						
							|  |  |  |     STATE(CannotBeABaseUrlPath)          \ | 
					
						
							|  |  |  |     STATE(Query)                         \ | 
					
						
							|  |  |  |     STATE(Fragment) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:22:27 +13:00
										 |  |  | class Parser { | 
					
						
							| 
									
										
										
										
											2021-05-25 22:13:15 +02:00
										 |  |  | public: | 
					
						
							|  |  |  |     enum class State { | 
					
						
							| 
									
										
										
										
											2021-06-03 12:40:04 +02:00
										 |  |  | #define STATE(state) state,
 | 
					
						
							|  |  |  |         ENUMERATE_STATES | 
					
						
							|  |  |  | #undef STATE
 | 
					
						
							| 
									
										
										
										
											2021-05-25 22:13:15 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 12:40:04 +02:00
										 |  |  |     static char const* state_name(State const& state) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         switch (state) { | 
					
						
							|  |  |  | #define STATE(state)   \
 | 
					
						
							|  |  |  |     case State::state: \ | 
					
						
							|  |  |  |         return #state; | 
					
						
							|  |  |  |             ENUMERATE_STATES | 
					
						
							|  |  |  | #undef STATE
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-15 14:29:20 +12:00
										 |  |  |     // https://url.spec.whatwg.org/#concept-basic-url-parser
 | 
					
						
							| 
									
										
										
										
											2024-08-05 21:47:45 +01:00
										 |  |  |     static URL basic_parse(StringView input, Optional<URL> const& base_url = {}, Optional<URL> url = {}, Optional<State> state_override = {}, Optional<StringView> encoding = {}); | 
					
						
							| 
									
										
										
										
											2021-05-25 22:13:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-25 14:11:34 +12:00
										 |  |  |     // https://url.spec.whatwg.org/#string-percent-encode-after-encoding
 | 
					
						
							| 
									
										
										
										
											2024-08-05 16:03:53 +01:00
										 |  |  |     static ErrorOr<String> percent_encode_after_encoding(TextCodec::Encoder&, StringView input, PercentEncodeSet percent_encode_set, bool space_as_plus = false); | 
					
						
							| 
									
										
										
										
											2023-06-25 14:11:34 +12:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-26 21:05:35 +12:00
										 |  |  |     // https://url.spec.whatwg.org/#concept-host-serializer
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:22:27 +13:00
										 |  |  |     static ErrorOr<String> serialize_host(Host const&); | 
					
						
							| 
									
										
										
										
											2023-09-17 13:15:52 +12:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // https://url.spec.whatwg.org/#shorten-a-urls-path
 | 
					
						
							|  |  |  |     static void shorten_urls_path(URL&); | 
					
						
							| 
									
										
										
										
											2021-05-25 22:13:15 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 12:40:04 +02:00
										 |  |  | #undef ENUMERATE_STATES
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-25 22:13:15 +02:00
										 |  |  | } |