Carry over modifications to request object in CefHandler::HandleBeforeResourceLoad() (issue #41).

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@95 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt 2010-08-30 20:26:13 +00:00
parent af70b21705
commit a4776a9dda
5 changed files with 63 additions and 6 deletions

View File

@ -599,7 +599,9 @@ public:
// populate the |redirectUrl| value and return RV_CONTINUE. To specify
// data for the resource return a CefStream object in |resourceStream|, set
// |mimeType| to the resource stream's mime type, and return RV_CONTINUE.
// To cancel loading of the resource return RV_HANDLED.
// To cancel loading of the resource return RV_HANDLED. Any modifications
// to |request| will be observed. If the URL in |request| is changed and
// |redirectUrl| is also set, the URL in |request| will be used.
/*--cef()--*/
virtual RetVal HandleBeforeResourceLoad(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefRequest> request,

View File

@ -433,7 +433,9 @@ typedef struct _cef_handler_t
// the |redirectUrl| value and return RV_CONTINUE. To specify data for the
// resource return a CefStream object in |resourceStream|, set |mimeType| to
// the resource stream's mime type, and return RV_CONTINUE. To cancel loading
// of the resource return RV_HANDLED.
// of the resource return RV_HANDLED. Any modifications to |request| will be
// observed. If the URL in |request| is changed and |redirectUrl| is also
// set, the URL in |request| will be used.
enum cef_retval_t (CEF_CALLBACK *handle_before_resource_load)(
struct _cef_handler_t* self, struct _cef_browser_t* browser,
struct _cef_request_t* request, cef_string_t* redirectUrl,

View File

@ -207,12 +207,13 @@ class RequestProxy : public URLRequest::Delegate,
CefRefPtr<CefRequest> request(new CefRequestImpl());
CefRequestImpl* requestimpl = static_cast<CefRequestImpl*>(request.get());
requestimpl->SetURL(UTF8ToWide(params->url.spec()));
const std::wstring originalUrl = UTF8ToWide(params->url.spec());
requestimpl->SetURL(originalUrl);
requestimpl->SetMethod(UTF8ToWide(params->method));
// Transfer request headers
CefRequest::HeaderMap headerMap;
CefRequestImpl::GetHeaderMap(params->headers, headerMap);
CefRequestImpl::ParseHeaders(params->headers, headerMap);
headerMap.insert(
std::make_pair(L"Referrer", UTF8ToWide(params->referrer.spec())));
requestimpl->SetHeaderMap(headerMap);
@ -234,6 +235,35 @@ class RequestProxy : public URLRequest::Delegate,
CefHandler::RetVal rv = handler->HandleBeforeResourceLoad(
browser_, request, redirectUrl, resourceStream, mimeType, loadFlags);
// Observe URL from request.
const std::wstring requestUrl = request->GetURL();
if(requestUrl != originalUrl) {
params->url = GURL(WideToUTF8(requestUrl));
redirectUrl.clear(); // Request URL trumps redirect URL
}
// Observe method from request.
params->method = WideToUTF8(request->GetMethod());
// Observe headers from request.
request->GetHeaderMap(headerMap);
CefRequest::HeaderMap::iterator referrer = headerMap.find(L"Referrer");
if(referrer == headerMap.end()) {
params->referrer = GURL();
} else {
params->referrer = GURL(WideToUTF8(referrer->second));
headerMap.erase(referrer);
}
params->headers = CefRequestImpl::GenerateHeaders(headerMap);
// Observe post data from request.
CefRefPtr<CefPostData> postData = request->GetPostData();
if(postData.get()) {
params->upload = new net::UploadData();
static_cast<CefPostDataImpl*>(postData.get())->Get(*params->upload);
}
if(rv == RV_HANDLED) {
// cancel the resource load
handled = true;

View File

@ -161,7 +161,29 @@ void CefRequestImpl::SetHeaderMap(const HeaderMap& map,
}
}
void CefRequestImpl::GetHeaderMap(const std::string& header_str, HeaderMap& map)
std::string CefRequestImpl::GenerateHeaders(const HeaderMap& map)
{
std::string headers;
for(HeaderMap::const_iterator header = map.begin();
header != map.end();
++header) {
const std::wstring& key = header->first;
const std::wstring& value = header->second;
if(!key.empty()) {
// Delimit with "\r\n".
if(!headers.empty())
headers += "\r\n";
headers += WideToUTF8(key) + ": " + WideToUTF8(value);
}
}
return headers;
}
void CefRequestImpl::ParseHeaders(const std::string& header_str, HeaderMap& map)
{
// Parse the request header values
std::string headerStr = "HTTP/1.1 200 OK\n";

View File

@ -42,7 +42,8 @@ public:
static void SetHeaderMap(const HeaderMap& map,
WebKit::WebURLRequest& request);
static void GetHeaderMap(const std::string& header_str, HeaderMap& map);
static std::string GenerateHeaders(const HeaderMap& map);
static void ParseHeaders(const std::string& header_str, HeaderMap& map);
protected:
std::wstring url_;