| 
									
										
										
										
											2020-06-01 21:58:29 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-06-01 21:58:29 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 11:52:59 +02:00
										 |  |  | #include <AK/ByteBuffer.h>
 | 
					
						
							|  |  |  | #include <AK/HashMap.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-11 21:15:15 -07:00
										 |  |  | #include <AK/Time.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-01 21:58:29 +02:00
										 |  |  | #include <AK/URL.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-11 21:15:15 -07:00
										 |  |  | #include <LibCore/ElapsedTimer.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-01 21:58:29 +02:00
										 |  |  | #include <LibWeb/Forward.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class LoadRequest { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     LoadRequest() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-14 10:34:18 -04:00
										 |  |  |     static LoadRequest create_for_url_on_page(const URL& url, Page* page); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-01 21:58:29 +02:00
										 |  |  |     bool is_valid() const { return m_url.is_valid(); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const URL& url() const { return m_url; } | 
					
						
							|  |  |  |     void set_url(const URL& url) { m_url = url; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 11:52:59 +02:00
										 |  |  |     const String& method() const { return m_method; } | 
					
						
							|  |  |  |     void set_method(const String& method) { m_method = method; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const ByteBuffer& body() const { return m_body; } | 
					
						
							|  |  |  |     void set_body(const ByteBuffer& body) { m_body = body; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-11 21:15:15 -07:00
										 |  |  |     void start_timer() { m_load_timer.start(); }; | 
					
						
							|  |  |  |     Time load_time() const { return m_load_timer.elapsed_time(); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 11:52:59 +02:00
										 |  |  |     unsigned hash() const | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-09-11 20:18:53 -07:00
										 |  |  |         auto body_hash = string_hash((const char*)m_body.data(), m_body.size()); | 
					
						
							|  |  |  |         auto body_and_headers_hash = pair_int_hash(body_hash, m_headers.hash()); | 
					
						
							|  |  |  |         auto url_and_method_hash = pair_int_hash(m_url.to_string().hash(), m_method.hash()); | 
					
						
							|  |  |  |         return pair_int_hash(body_and_headers_hash, url_and_method_hash); | 
					
						
							| 
									
										
										
										
											2020-09-28 11:52:59 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-06-01 21:58:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool operator==(const LoadRequest& other) const | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-09-28 11:52:59 +02:00
										 |  |  |         if (m_headers.size() != other.m_headers.size()) | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         for (auto& it : m_headers) { | 
					
						
							|  |  |  |             auto jt = other.m_headers.find(it.key); | 
					
						
							|  |  |  |             if (jt == other.m_headers.end()) | 
					
						
							|  |  |  |                 return false; | 
					
						
							| 
									
										
										
										
											2020-09-28 17:34:51 +02:00
										 |  |  |             if (it.value != jt->value) | 
					
						
							|  |  |  |                 return false; | 
					
						
							| 
									
										
										
										
											2020-09-28 11:52:59 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         return m_url == other.m_url && m_method == other.m_method && m_body == other.m_body; | 
					
						
							| 
									
										
										
										
											2020-06-01 21:58:29 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 11:52:59 +02:00
										 |  |  |     void set_header(const String& name, const String& value) { m_headers.set(name, value); } | 
					
						
							|  |  |  |     String header(const String& name) const { return m_headers.get(name).value_or({}); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const HashMap<String, String>& headers() const { return m_headers; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-01 21:58:29 +02:00
										 |  |  | private: | 
					
						
							|  |  |  |     URL m_url; | 
					
						
							| 
									
										
										
										
											2020-09-28 11:52:59 +02:00
										 |  |  |     String m_method { "GET" }; | 
					
						
							|  |  |  |     HashMap<String, String> m_headers; | 
					
						
							|  |  |  |     ByteBuffer m_body; | 
					
						
							| 
									
										
										
										
											2021-09-11 21:15:15 -07:00
										 |  |  |     Core::ElapsedTimer m_load_timer; | 
					
						
							| 
									
										
										
										
											2020-06-01 21:58:29 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace AK { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<> | 
					
						
							|  |  |  | struct Traits<Web::LoadRequest> : public GenericTraits<Web::LoadRequest> { | 
					
						
							|  |  |  |     static unsigned hash(const Web::LoadRequest& request) { return request.hash(); } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |