fix response headers modification

This commit is contained in:
Robo 2015-12-04 07:24:01 +05:30
parent c5b5bbbeb2
commit 461ee49988
2 changed files with 35 additions and 19 deletions

View file

@ -44,9 +44,10 @@ bool MatchesFilterCondition(
for (auto& pattern : info.url_patterns) for (auto& pattern : info.url_patterns)
if (pattern.MatchesURL(url)) if (pattern.MatchesURL(url))
return true; return true;
return false;
} }
return false; return true;
} }
base::DictionaryValue* ExtractRequestInfo(net::URLRequest* request) { base::DictionaryValue* ExtractRequestInfo(net::URLRequest* request) {
@ -106,10 +107,20 @@ void OnBeforeSendHeadersResponse(
void OnHeadersReceivedResponse( void OnHeadersReceivedResponse(
const net::CompletionCallback& callback, const net::CompletionCallback& callback,
const net::HttpResponseHeaders* original_response_headers,
scoped_refptr<net::HttpResponseHeaders>* override_response_headers, scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
const AtomNetworkDelegate::BlockingResponse& result) { const AtomNetworkDelegate::BlockingResponse& result) {
if (result.responseHeaders.get()) if (result.responseHeaders.get()) {
*override_response_headers = result.responseHeaders; *override_response_headers = new net::HttpResponseHeaders(
original_response_headers->raw_headers());
void* iter = nullptr;
std::string key;
std::string value;
while (result.responseHeaders->EnumerateHeaderLines(&iter, &key, &value)) {
(*override_response_headers)->RemoveHeader(key);
(*override_response_headers)->AddHeader(key + ": " + value);
}
}
callback.Run(result.Cancel()); callback.Run(result.Cancel());
} }
@ -242,6 +253,7 @@ int AtomNetworkDelegate::OnHeadersReceived(
base::Bind(wrapped_callback, details), base::Bind(wrapped_callback, details),
base::Bind(&OnHeadersReceivedResponse, base::Bind(&OnHeadersReceivedResponse,
callback, callback,
original_response_headers,
override_response_headers)); override_response_headers));
return net::ERR_IO_PENDING; return net::ERR_IO_PENDING;
@ -290,12 +302,14 @@ void AtomNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
details->Set("responseHeaders", details->Set("responseHeaders",
GetResponseHeadersDict(request->response_headers())); GetResponseHeadersDict(request->response_headers()));
details->SetBoolean("fromCache", request->was_cached()); details->SetBoolean("fromCache", request->was_cached());
auto response_headers = request->response_headers();
details->SetInteger("statusCode", details->SetInteger("statusCode",
request->response_headers() ? response_headers ?
request->response_headers()->response_code() : 200); response_headers->response_code() : 200);
details->SetString("statusLine", details->SetString("statusLine",
request->response_headers() ? response_headers ?
request->response_headers()->GetStatusLine() : std::string()); response_headers->GetStatusLine() : std::string());
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(base::IgnoreResult(wrapped_callback), base::Bind(base::IgnoreResult(wrapped_callback),
@ -327,12 +341,14 @@ void AtomNetworkDelegate::OnCompleted(net::URLRequest* request, bool started) {
details->Set("responseHeaders", details->Set("responseHeaders",
GetResponseHeadersDict(request->response_headers())); GetResponseHeadersDict(request->response_headers()));
details->SetBoolean("fromCache", request->was_cached()); details->SetBoolean("fromCache", request->was_cached());
auto response_headers = request->response_headers();
details->SetInteger("statusCode", details->SetInteger("statusCode",
request->response_headers() ? response_headers ?
request->response_headers()->response_code() : 200); response_headers->response_code() : 200);
details->SetString("statusLine", details->SetString("statusLine",
request->response_headers() ? response_headers ?
request->response_headers()->GetStatusLine() : std::string()); response_headers->GetStatusLine() : std::string());
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(base::IgnoreResult(wrapped_callback), base::Bind(base::IgnoreResult(wrapped_callback),

View file

@ -309,7 +309,7 @@ myWindow.webContents.session.webRequest.onBeforeSendHeaders(filter, function(det
* `filter` Object * `filter` Object
* `urls` Array - A list of URLs or URL patterns. Request that cannot match any of the URLs * `urls` Array - A list of URLs or URL patterns. Request that cannot match any of the URLs
will be filtered out. will be filtered out.
* `listener` Function * `listener` Function
* `details` Object * `details` Object
* `id` String - Request id. * `id` String - Request id.
@ -320,7 +320,7 @@ myWindow.webContents.session.webRequest.onBeforeSendHeaders(filter, function(det
* `blockingResponse` Object * `blockingResponse` Object
* `cancel` Boolean - Whether to continue or block the request. * `cancel` Boolean - Whether to continue or block the request.
* `redirectURL` String **optional** - The original request is prevented from being sent or * `redirectURL` String **optional** - The original request is prevented from being sent or
completed, and is instead redirected to the given URL. completed, and is instead redirected to the given URL.
Fired when a request is about to occur. Should return a `blockingResponse`. Fired when a request is about to occur. Should return a `blockingResponse`.
@ -328,7 +328,7 @@ Fired when a request is about to occur. Should return a `blockingResponse`.
* `filter` Object * `filter` Object
* `urls` Array - A list of URLs or URL patterns. Request that cannot match any of the URLs * `urls` Array - A list of URLs or URL patterns. Request that cannot match any of the URLs
will be filtered out. will be filtered out.
* `listener` Function * `listener` Function
* `details` Object * `details` Object
* `id` String - Request id. * `id` String - Request id.
@ -340,7 +340,7 @@ Fired when a request is about to occur. Should return a `blockingResponse`.
* `blockingResponse` Object * `blockingResponse` Object
* `cancel` Boolean - Whether to continue or block the request. * `cancel` Boolean - Whether to continue or block the request.
* `requestHeaders` Object **optional** - When provided, request will be made with these * `requestHeaders` Object **optional** - When provided, request will be made with these
headers. headers.
Fired before sending an HTTP request, once the request headers are available. This may Fired before sending an HTTP request, once the request headers are available. This may
occur after a TCP connection is made to the server, but before any http data is sent. occur after a TCP connection is made to the server, but before any http data is sent.
@ -350,7 +350,7 @@ Should return a `blockingResponse`.
* `filter` Object * `filter` Object
* `urls` Array - A list of URLs or URL patterns. Request that cannot match any of the URLs * `urls` Array - A list of URLs or URL patterns. Request that cannot match any of the URLs
will be filtered out. will be filtered out.
* `listener` Function * `listener` Function
* `details` Object * `details` Object
* `id` String - Request id. * `id` String - Request id.
@ -381,7 +381,7 @@ Fired just before a request is going to be sent to the server, modifications of
* `blockingResponse` Object * `blockingResponse` Object
* `cancel` Boolean - Whether to continue or block the request. * `cancel` Boolean - Whether to continue or block the request.
* `responseHeaders` Object **optional** - When provided, the server is assumed to have * `responseHeaders` Object **optional** - When provided, the server is assumed to have
responded with these headers. responded with these headers.
Fired when HTTP response headers of a request have been received. Should return a Fired when HTTP response headers of a request have been received. Should return a
`blockingResponse`. `blockingResponse`.
@ -430,7 +430,7 @@ Fired when a server initiated redirect is about to occur.
* `filter` Object * `filter` Object
* `urls` Array - A list of URLs or URL patterns. Request that cannot match any of the URLs * `urls` Array - A list of URLs or URL patterns. Request that cannot match any of the URLs
will be filtered out. will be filtered out.
* `listener` Function * `listener` Function
* `details` Object * `details` Object
* `id` String - Request id. * `id` String - Request id.
@ -449,7 +449,7 @@ Fired when a request is completed.
* `filter` Object * `filter` Object
* `urls` Array - A list of URLs or URL patterns. Request that cannot match any of the URLs * `urls` Array - A list of URLs or URL patterns. Request that cannot match any of the URLs
will be filtered out. will be filtered out.
* `listener` Function * `listener` Function
* `details` Object * `details` Object
* `id` String - Request id. * `id` String - Request id.