Implement NetworkService request interception/handling (see issue #2622).

Implementation notes:
- Chromium change: CookieMonster::SetCookieableSchemes needs to be called
  immediately after the CookieMonster is created in NetworkContext::
  ApplyContextParamsToBuilder. Add a Profile::GetCookieableSchemes method and
  NetworkContextParams.cookieable_schemes member (set from
  ProfileNetworkContextService::CreateNetworkContextParams) to support that.
- Chromium change: Add a ContentBrowserClient::HandleExternalProtocol variant
  that exposes additional NetworkService request information.
- GetResourceResponseFilter is not yet implemented.

API changes:
- Resource-related callbacks have been moved from CefRequestHandler to a new
  CefResourceRequestHandler interface which is returned via the
  GetResourceRequestHandler method. If the CefRequestHandler declines to handle
  a resource it can optionally be handled by the CefRequestContextHandler, if
  any, associated with the loading context.
- The OnProtocolExecution callback has been moved from CefRequestHandler to
  CefResourceRequestHandler and will be called if a custom scheme request is
  unhandled.
- Cookie send/save permission callbacks have been moved from CefRequestHandler
  and CefResourceHandler to CefResourceRequestHandler.
- New methods added to CefResourceHandler that better match NetworkService
  execution sequence expectations. The old methods are now deprecated.
- New methods added to CefRequest and CefResponse.

Known behavior changes with the NetworkService implementation:
- Modifying the |new_url| parameter in OnResourceRedirect will no longer result
  in the method being called an additional time (likely a bug in the old
  implementation).
- Modifying the request URL in OnResourceResponse would previously cause a
  redirect. This behavior is now deprecated because the NetworkService does not
  support this functionality when using default network loaders. Temporary
  support has been added in combination with CefResourceHandler usage only.
- Other changes to the request object in OnResourceResponse will now cause the
  request to be restarted. This means that OnBeforeResourceLoad, etc, will be
  called an additional time with the new request information.
- CefResponse::GetMimeType will now be empty for non-200 responses.
- Requests using custom schemes can now be handled via CefResourceRequestHandler
  with the same callback behavior as builtin schemes.
- Redirects of custom scheme requests will now be followed as expected.
- Default handling of builtin schemes can now be disabled by setting
  |disable_default_handling| to true in GetResourceRequestHandler.
- Unhandled requests (custom scheme or builtin scheme with default handling
  disabled) will fail with an CefResponse::GetError value of
  ERR_UNKNOWN_URL_SCHEME.
- The CefSchemeHandlerFactory::Create callback will now include cookie headers.

To test:
- Run `cefclient --enable-network-service`. All resources should load
  successfully (this tests the transparent proxy capability).
- All tests pass with NetworkService disabled.
- The following tests pass with NetworkService enabled:
  - CookieTest.*
  - FrameTest.* (excluding .*Nav)
  - NavigationTest.* (excluding .Redirect*)
  - RequestHandlerTest.*
  - RequestContextTest.Basic*
  - RequestContextTest.Popup*
  - RequestTest.*
  - ResourceManagerTest.*
  - ResourceRequestHandlerTest.* (excluding .Filter*)
  - SchemeHandlerTest.*
  - StreamResourceHandlerTest.*
This commit is contained in:
Marshall Greenblatt
2019-04-24 02:50:25 +00:00
parent 019611c764
commit 8f240861e3
141 changed files with 12733 additions and 3927 deletions

View File

@@ -6,8 +6,10 @@
#include <string>
#include "libcef/common/net_service/net_service_util.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "base/strings/string_util.h"
#include "net/http/http_request_headers.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_request.h"
@@ -84,6 +86,17 @@ void CefResponseImpl::SetMimeType(const CefString& mimeType) {
mime_type_ = mimeType;
}
CefString CefResponseImpl::GetCharset() {
base::AutoLock lock_scope(lock_);
return charset_;
}
void CefResponseImpl::SetCharset(const CefString& charset) {
base::AutoLock lock_scope(lock_);
CHECK_READONLY_RETURN_VOID();
charset_ = charset;
}
CefString CefResponseImpl::GetHeader(const CefString& name) {
base::AutoLock lock_scope(lock_);
@@ -121,51 +134,17 @@ void CefResponseImpl::SetHeaderMap(const HeaderMap& headerMap) {
net::HttpResponseHeaders* CefResponseImpl::GetResponseHeaders() {
base::AutoLock lock_scope(lock_);
std::string response;
std::string status_text;
bool has_content_type_header = false;
std::string mime_type = mime_type_;
if (mime_type.empty())
mime_type = "text/html";
if (!status_text_.empty())
status_text = status_text_;
else
status_text = (status_code_ == 200) ? "OK" : "ERROR";
std::multimap<std::string, std::string> extra_headers;
for (const auto& pair : header_map_)
extra_headers.insert(std::make_pair(pair.first, pair.second));
base::SStringPrintf(&response, "HTTP/1.1 %d %s", status_code_,
status_text.c_str());
if (header_map_.size() > 0) {
for (HeaderMap::const_iterator header = header_map_.begin();
header != header_map_.end(); ++header) {
const CefString& key = header->first;
const CefString& value = header->second;
if (!key.empty()) {
// Delimit with "\0" as required by net::HttpResponseHeaders.
std::string key_str(key);
std::string value_str(value);
base::StringAppendF(&response, "%c%s: %s", '\0', key_str.c_str(),
value_str.c_str());
if (!has_content_type_header &&
key_str == net::HttpRequestHeaders::kContentType) {
has_content_type_header = true;
}
}
}
}
if (!has_content_type_header) {
std::string mime_type;
if (!mime_type_.empty())
mime_type = mime_type_;
else
mime_type = "text/html";
base::StringAppendF(&response, "%c%s: %s", '\0',
net::HttpRequestHeaders::kContentType,
mime_type.c_str());
}
return new net::HttpResponseHeaders(response);
return net_service::MakeResponseHeaders(
status_code_, status_text_, mime_type, charset_, -1, extra_headers,
true /* allow_existing_header_override */);
}
void CefResponseImpl::SetResponseHeaders(
@@ -183,11 +162,10 @@ void CefResponseImpl::SetResponseHeaders(
status_code_ = headers.response_code();
status_text_ = headers.GetStatusText();
std::string mime_type;
if (headers.GetMimeType(&mime_type))
mime_type_ = mime_type;
else
mime_type_.clear();
std::string mime_type, charset;
headers.GetMimeTypeAndCharset(&mime_type, &charset);
mime_type_ = mime_type;
charset_ = charset;
}
void CefResponseImpl::Set(const blink::WebURLResponse& response) {