Skip to content

Commit 95d23d0

Browse files
trflynn89awesomekling
authored andcommitted
RequestServer: Pass the Request object to disk cache entry factories
This object will be needed in a future commit to store requests awaiting other requests to finish. Doing this in a separate commit just to make that commit less noisy.
1 parent 5384f84 commit 95d23d0

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

Services/RequestServer/Cache/DiskCache.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <LibURL/URL.h>
1111
#include <RequestServer/Cache/DiskCache.h>
1212
#include <RequestServer/Cache/Utilities.h>
13+
#include <RequestServer/Request.h>
1314

1415
namespace RequestServer {
1516

@@ -32,45 +33,45 @@ DiskCache::DiskCache(NonnullRefPtr<Database::Database> database, LexicalPath cac
3233
{
3334
}
3435

35-
Optional<CacheEntryWriter&> DiskCache::create_entry(URL::URL const& url, StringView method, UnixDateTime request_time)
36+
Optional<CacheEntryWriter&> DiskCache::create_entry(Request& request)
3637
{
37-
if (!is_cacheable(method))
38+
if (!is_cacheable(request.method()))
3839
return {};
3940

40-
auto serialized_url = serialize_url_for_cache_storage(url);
41-
auto cache_key = create_cache_key(serialized_url, method);
41+
auto serialized_url = serialize_url_for_cache_storage(request.url());
42+
auto cache_key = create_cache_key(serialized_url, request.method());
4243

43-
auto cache_entry = CacheEntryWriter::create(*this, m_index, cache_key, move(serialized_url), request_time);
44+
auto cache_entry = CacheEntryWriter::create(*this, m_index, cache_key, move(serialized_url), request.request_start_time());
4445
if (cache_entry.is_error()) {
45-
dbgln("\033[31;1mUnable to create cache entry for\033[0m {}: {}", url, cache_entry.error());
46+
dbgln("\033[31;1mUnable to create cache entry for\033[0m {}: {}", request.url(), cache_entry.error());
4647
return {};
4748
}
4849

49-
dbgln("\033[32;1mCreated disk cache entry for\033[0m {}", url);
50+
dbgln("\033[32;1mCreated disk cache entry for\033[0m {}", request.url());
5051

5152
auto address = reinterpret_cast<FlatPtr>(cache_entry.value().ptr());
5253
m_open_cache_entries.set(address, cache_entry.release_value());
5354

5455
return static_cast<CacheEntryWriter&>(**m_open_cache_entries.get(address));
5556
}
5657

57-
Optional<CacheEntryReader&> DiskCache::open_entry(URL::URL const& url, StringView method)
58+
Optional<CacheEntryReader&> DiskCache::open_entry(Request& request)
5859
{
59-
if (!is_cacheable(method))
60+
if (!is_cacheable(request.method()))
6061
return {};
6162

62-
auto serialized_url = serialize_url_for_cache_storage(url);
63-
auto cache_key = create_cache_key(serialized_url, method);
63+
auto serialized_url = serialize_url_for_cache_storage(request.url());
64+
auto cache_key = create_cache_key(serialized_url, request.method());
6465

6566
auto index_entry = m_index.find_entry(cache_key);
6667
if (!index_entry.has_value()) {
67-
dbgln("\033[35;1mNo disk cache entry for\033[0m {}", url);
68+
dbgln("\033[35;1mNo disk cache entry for\033[0m {}", request.url());
6869
return {};
6970
}
7071

7172
auto cache_entry = CacheEntryReader::create(*this, m_index, cache_key, index_entry->data_size);
7273
if (cache_entry.is_error()) {
73-
dbgln("\033[31;1mUnable to open cache entry for\033[0m {}: {}", url, cache_entry.error());
74+
dbgln("\033[31;1mUnable to open cache entry for\033[0m {}: {}", request.url(), cache_entry.error());
7475
m_index.remove_entry(cache_key);
7576
return {};
7677
}
@@ -79,12 +80,12 @@ Optional<CacheEntryReader&> DiskCache::open_entry(URL::URL const& url, StringVie
7980
auto current_age = calculate_age(cache_entry.value()->headers(), index_entry->request_time, index_entry->response_time);
8081

8182
if (!is_response_fresh(freshness_lifetime, current_age)) {
82-
dbgln("\033[33;1mCache entry expired for\033[0m {} (lifetime={}s age={}s)", url, freshness_lifetime.to_seconds(), current_age.to_seconds());
83+
dbgln("\033[33;1mCache entry expired for\033[0m {} (lifetime={}s age={}s)", request.url(), freshness_lifetime.to_seconds(), current_age.to_seconds());
8384
cache_entry.value()->remove();
8485
return {};
8586
}
8687

87-
dbgln("\033[32;1mOpened disk cache entry for\033[0m {} (lifetime={}s age={}s) ({} bytes)", url, freshness_lifetime.to_seconds(), current_age.to_seconds(), index_entry->data_size);
88+
dbgln("\033[32;1mOpened disk cache entry for\033[0m {} (lifetime={}s age={}s) ({} bytes)", request.url(), freshness_lifetime.to_seconds(), current_age.to_seconds(), index_entry->data_size);
8889

8990
auto address = reinterpret_cast<FlatPtr>(cache_entry.value().ptr());
9091
m_open_cache_entries.set(address, cache_entry.release_value());

Services/RequestServer/Cache/DiskCache.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class DiskCache {
2323
public:
2424
static ErrorOr<DiskCache> create();
2525

26-
Optional<CacheEntryWriter&> create_entry(URL::URL const&, StringView method, UnixDateTime request_time);
27-
Optional<CacheEntryReader&> open_entry(URL::URL const&, StringView method);
26+
Optional<CacheEntryWriter&> create_entry(Request&);
27+
Optional<CacheEntryReader&> open_entry(Request&);
2828
void clear_cache();
2929

3030
LexicalPath const& cache_directory() { return m_cache_directory; }

Services/RequestServer/Request.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,13 @@ void Request::process()
167167
void Request::handle_initial_state()
168168
{
169169
if (m_disk_cache.has_value()) {
170-
m_cache_entry_reader = m_disk_cache->open_entry(m_url, m_method);
170+
m_cache_entry_reader = m_disk_cache->open_entry(*this);
171171
if (m_cache_entry_reader.has_value()) {
172172
transition_to_state(State::ReadCache);
173173
return;
174174
}
175175

176-
m_cache_entry_writer = m_disk_cache->create_entry(m_url, m_method, m_request_start_time);
176+
m_cache_entry_writer = m_disk_cache->create_entry(*this);
177177
}
178178

179179
transition_to_state(State::DNSLookup);

Services/RequestServer/Request.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ class Request : public Weakable<Request> {
5050

5151
~Request();
5252

53+
URL::URL const& url() const { return m_url; }
54+
ByteString const& method() const { return m_method; }
55+
UnixDateTime request_start_time() const { return m_request_start_time; }
56+
5357
void notify_fetch_complete(Badge<ConnectionFromClient>, int result_code);
5458

5559
private:

0 commit comments

Comments
 (0)