| 
									
										
										
										
											2022-07-10 19:28:47 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2022, Linus Groh <linusg@serenityos.org> | 
					
						
							|  |  |  |  * Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/ByteBuffer.h>
 | 
					
						
							|  |  |  | #include <AK/StringView.h>
 | 
					
						
							|  |  |  | #include <LibRegex/Regex.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Fetch/Infrastructure/HTTP/Methods.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Infra/ByteSequences.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-17 23:52:02 +01:00
										 |  |  | namespace Web::Fetch::Infrastructure { | 
					
						
							| 
									
										
										
										
											2022-07-10 19:28:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // https://fetch.spec.whatwg.org/#concept-method
 | 
					
						
							|  |  |  | bool is_method(ReadonlyBytes method) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // A method is a byte sequence that matches the method token production.
 | 
					
						
							|  |  |  |     Regex<ECMA262Parser> regex { R"~~~(^[A-Za-z0-9!#$%&'*+\-.^_`|~]+$)~~~" }; | 
					
						
							|  |  |  |     return regex.has_match(StringView { method }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://fetch.spec.whatwg.org/#cors-safelisted-method
 | 
					
						
							|  |  |  | bool is_cors_safelisted_method(ReadonlyBytes method) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // A CORS-safelisted method is a method that is `GET`, `HEAD`, or `POST`.
 | 
					
						
							|  |  |  |     return StringView { method }.is_one_of("GET"sv, "HEAD"sv, "POST"sv); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://fetch.spec.whatwg.org/#forbidden-method
 | 
					
						
							|  |  |  | bool is_forbidden_method(ReadonlyBytes method) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // A forbidden method is a method that is a byte-case-insensitive match for `CONNECT`, `TRACE`, or `TRACK`.
 | 
					
						
							| 
									
										
										
										
											2023-03-10 08:48:54 +01:00
										 |  |  |     return StringView { method }.is_one_of_ignoring_ascii_case("CONNECT"sv, "TRACE"sv, "TRACK"sv); | 
					
						
							| 
									
										
										
										
											2022-07-10 19:28:47 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://fetch.spec.whatwg.org/#concept-method-normalize
 | 
					
						
							| 
									
										
										
										
											2024-04-26 13:26:55 -04:00
										 |  |  | ByteBuffer normalize_method(ReadonlyBytes method) | 
					
						
							| 
									
										
										
										
											2022-07-10 19:28:47 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     // To normalize a method, if it is a byte-case-insensitive match for `DELETE`, `GET`, `HEAD`, `OPTIONS`, `POST`, or `PUT`, byte-uppercase it.
 | 
					
						
							| 
									
										
										
										
											2024-04-26 13:26:55 -04:00
										 |  |  |     auto bytes = MUST(ByteBuffer::copy(method)); | 
					
						
							| 
									
										
										
										
											2023-03-10 08:48:54 +01:00
										 |  |  |     if (StringView { method }.is_one_of_ignoring_ascii_case("DELETE"sv, "GET"sv, "HEAD"sv, "OPTIONS"sv, "POST"sv, "PUT"sv)) | 
					
						
							| 
									
										
										
										
											2022-07-10 19:28:47 +02:00
										 |  |  |         Infra::byte_uppercase(bytes); | 
					
						
							|  |  |  |     return bytes; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |