Skip to content

Commit 926d994

Browse files
Disable caching of XHR ArrayBuffer and/or env variable
- Disable caching of ArrayBuffer XHR. - Setting the environment variable WPE_DISABLE_XHR_RESPONSE_CACHING disables the memory cache for xhr responses. This is useful to reduce the memory footprint when the responses are quite big and cannot be reused. XMLHttpReques: allow disabling the cache for selected protocols
1 parent 0e9a5f8 commit 926d994

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

Source/WebCore/xml/XMLHttpRequest.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,29 @@ static void logConsoleError(ScriptExecutionContext* context, const String& messa
9898
context->addConsoleMessage(MessageSource::JS, MessageLevel::Error, message);
9999
}
100100

101+
static bool shouldDisableCacheForRequest(const URL& url)
102+
{
103+
static Vector<String> s_protocolsNotCached;
104+
static std::once_flag s_onceFlag;
105+
std::call_once(s_onceFlag,
106+
[] {
107+
// The env var contains a comma separated list of protocols that need to disable the cache,
108+
// for example WPE_DISABLE_XHR_RESPONSE_CACHING_FOR_PROTOCOLS="dvb,echo,file"
109+
String s(String::fromLatin1(std::getenv("WPE_DISABLE_XHR_RESPONSE_CACHING_FOR_PROTOCOLS")));
110+
if (!s.isEmpty()) {
111+
s_protocolsNotCached.appendVector(s.split(','));
112+
}
113+
});
114+
115+
if (getenv("WPE_DISABLE_XHR_RESPONSE_CACHING"))
116+
return true;
117+
118+
if (s_protocolsNotCached.contains(url.protocol().toString()))
119+
return true;
120+
121+
return false;
122+
}
123+
101124
Ref<XMLHttpRequest> XMLHttpRequest::create(ScriptExecutionContext& context)
102125
{
103126
auto xmlHttpRequest = adoptRef(*new XMLHttpRequest(context));
@@ -627,6 +650,12 @@ ExceptionOr<void> XMLHttpRequest::createRequest()
627650
options.filteringPolicy = ResponseFilteringPolicy::Enable;
628651
options.contentEncodingSniffingPolicy = ContentEncodingSniffingPolicy::Disable;
629652

653+
if (responseType() == ResponseType::Arraybuffer || shouldDisableCacheForRequest(m_url.url())) {
654+
options.dataBufferingPolicy = DataBufferingPolicy::DoNotBufferData;
655+
options.cachingPolicy = CachingPolicy::DisallowCaching;
656+
request.setCachePolicy(ResourceRequestCachePolicy::DoNotUseAnyCache);
657+
}
658+
630659
if (m_timeoutMilliseconds) {
631660
if (!m_async)
632661
request.setTimeoutInterval(m_timeoutMilliseconds / 1000.0);

0 commit comments

Comments
 (0)