Add NetworkService support for CefURLRequest (see issue #2622).

Requests created using CefURLRequest::Create are not associated with a
browser/frame. When originating from the render process these requests cannot be
intercepted and consequently only http(s) and blob requests are supported. To
work around this limitation a new CefFrame::CreateURLRequest method has been
added that allows the request to be associated with that browser/frame for
interception purposes.

This change also fixes an issue with the NetworkService implementation where
redirected requests could result in two parallel requests being sent to the
target server.

To test: URLRequestTest.* tests pass with NetworkService enabled.
This commit is contained in:
Marshall Greenblatt
2019-05-10 18:14:48 -04:00
parent f9b042c375
commit ba0e1b5719
43 changed files with 1637 additions and 344 deletions

View File

@@ -44,6 +44,9 @@ bool GetCookieDomain(const GURL& url,
const char kHTTPLocationHeaderName[] = "Location";
const char kHTTPSetCookieHeaderName[] = "Set-Cookie";
const char kContentTypeApplicationFormURLEncoded[] =
"application/x-www-form-urlencoded";
const char kHTTPHeaderSep[] = ": ";
std::string MakeHeader(const std::string& name, const std::string& value) {

View File

@@ -29,6 +29,9 @@ namespace net_service {
extern const char kHTTPLocationHeaderName[];
extern const char kHTTPSetCookieHeaderName[];
// HTTP header values.
extern const char kContentTypeApplicationFormURLEncoded[];
// Make a header name/value pair.
std::string MakeHeader(const std::string& name, const std::string& value);

View File

@@ -12,6 +12,7 @@
#include "libcef/common/cef_messages.h"
#include "libcef/common/net/http_header_utils.h"
#include "libcef/common/net/upload_data.h"
#include "libcef/common/net_service/net_service_util.h"
#include "libcef/common/request_impl.h"
#include "base/command_line.h"
@@ -54,7 +55,6 @@ const char kCacheControlLowerCase[] = "cache-control";
const char kCacheControlDirectiveNoCacheLowerCase[] = "no-cache";
const char kCacheControlDirectiveNoStoreLowerCase[] = "no-store";
const char kCacheControlDirectiveOnlyIfCachedLowerCase[] = "only-if-cached";
const char kApplicationFormURLEncoded[] = "application/x-www-form-urlencoded";
// Mask of values that configure the cache policy.
const int kURCachePolicyMask =
@@ -549,6 +549,35 @@ void CefRequestImpl::Get(network::ResourceRequest* request,
ShouldSet(kChangedFirstPartyForCookies, changed_only)) {
request->site_for_cookies = first_party_for_cookies_;
}
if (ShouldSet(kChangedFlags, changed_only)) {
int flags = flags_;
if (!(flags & kURCachePolicyMask)) {
// Only consider the Cache-Control directives when a cache policy is not
// explicitly set on the request.
flags |= GetCacheControlHeaderPolicy(headermap_);
}
int net_flags = 0;
if (flags & UR_FLAG_SKIP_CACHE) {
net_flags |= net::LOAD_BYPASS_CACHE;
}
if (flags & UR_FLAG_ONLY_FROM_CACHE) {
net_flags |= net::LOAD_ONLY_FROM_CACHE | net::LOAD_SKIP_CACHE_VALIDATION;
}
if (flags & UR_FLAG_DISABLE_CACHE) {
net_flags |= net::LOAD_DISABLE_CACHE;
}
if (!(flags & UR_FLAG_ALLOW_STORED_CREDENTIALS)) {
net_flags |= net::LOAD_DO_NOT_SEND_AUTH_DATA |
net::LOAD_DO_NOT_SEND_COOKIES |
net::LOAD_DO_NOT_SAVE_COOKIES;
}
request->load_flags = net_flags;
}
}
void CefRequestImpl::Set(const net::RedirectInfo& redirect_info) {
@@ -771,7 +800,8 @@ void CefRequestImpl::Get(const CefMsg_LoadRequest_Params& params,
.length() == 0) {
request.SetHttpHeaderField(
blink::WebString::FromASCII(net::HttpRequestHeaders::kContentType),
blink::WebString::FromASCII(kApplicationFormURLEncoded));
blink::WebString::FromASCII(
net_service::kContentTypeApplicationFormURLEncoded));
}
blink::WebHTTPBody body;
@@ -869,7 +899,7 @@ void CefRequestImpl::Get(net::URLFetcher& fetcher,
if (elements.size() == 1) {
// Default to URL encoding if not specified.
if (content_type.empty())
content_type = kApplicationFormURLEncoded;
content_type = net_service::kContentTypeApplicationFormURLEncoded;
CefPostDataElementImpl* impl =
static_cast<CefPostDataElementImpl*>(elements[0].get());

View File

@@ -93,7 +93,7 @@ class CefRequestImpl : public CefRequest {
// Populate this object from the ResourceRequest object.
void Set(const network::ResourceRequest* request, uint64 identifier);
// Populate the URLRequest object from this object.
// Populate the ResourceRequest object from this object.
// If |changed_only| is true then only the changed fields will be updated.
void Get(network::ResourceRequest* request, bool changed_only) const;
@@ -131,7 +131,8 @@ class CefRequestImpl : public CefRequest {
void Get(CefNavigateParams& params) const;
// Populate the URLFetcher object from this object.
// Called from CefBrowserURLRequest::Context::ContinueOnOriginatingThread().
// Called from
// CefBrowserURLRequestOld::Context::ContinueOnOriginatingThread().
void Get(net::URLFetcher& fetcher, int64& upload_data_size) const;
void SetReadOnly(bool read_only);

View File

@@ -162,10 +162,16 @@ void CefResponseImpl::SetResponseHeaders(
status_code_ = headers.response_code();
status_text_ = headers.GetStatusText();
std::string mime_type, charset;
headers.GetMimeTypeAndCharset(&mime_type, &charset);
mime_type_ = mime_type;
charset_ = charset;
if (headers.IsRedirect(nullptr)) {
// Don't report Content-Type header values for redirects.
mime_type_.clear();
charset_.clear();
} else {
std::string mime_type, charset;
headers.GetMimeTypeAndCharset(&mime_type, &charset);
mime_type_ = mime_type;
charset_ = charset;
}
}
void CefResponseImpl::Set(const blink::WebURLResponse& response) {

View File

@@ -3,8 +3,10 @@
// be found in the LICENSE file.
#include "include/cef_urlrequest.h"
#include "libcef/browser/browser_urlrequest_impl.h"
#include "libcef/browser/net/browser_urlrequest_old_impl.h"
#include "libcef/browser/net_service/browser_urlrequest_impl.h"
#include "libcef/common/content_client.h"
#include "libcef/common/net_service/util.h"
#include "libcef/common/task_runner_impl.h"
#include "libcef/renderer/render_urlrequest_impl.h"
@@ -29,15 +31,22 @@ CefRefPtr<CefURLRequest> CefURLRequest::Create(
if (CefContentClient::Get()->browser()) {
// In the browser process.
CefRefPtr<CefBrowserURLRequest> impl =
new CefBrowserURLRequest(request, client, request_context);
if (impl->Start())
return impl.get();
if (net_service::IsEnabled()) {
CefRefPtr<CefBrowserURLRequest> impl =
new CefBrowserURLRequest(nullptr, request, client, request_context);
if (impl->Start())
return impl.get();
} else {
CefRefPtr<CefBrowserURLRequestOld> impl =
new CefBrowserURLRequestOld(request, client, request_context);
if (impl->Start())
return impl.get();
}
return NULL;
} else if (CefContentClient::Get()->renderer()) {
// In the render process.
CefRefPtr<CefRenderURLRequest> impl =
new CefRenderURLRequest(request, client);
new CefRenderURLRequest(nullptr, request, client);
if (impl->Start())
return impl.get();
return NULL;