Merge pull request #5030 from deepak1556/webrequest_response_headers_patch
session: webRequest.OnHeadersReceived should accept status line.
This commit is contained in:
commit
5659ee5c0b
3 changed files with 63 additions and 7 deletions
|
@ -5,6 +5,7 @@
|
||||||
#include "atom/browser/net/atom_network_delegate.h"
|
#include "atom/browser/net/atom_network_delegate.h"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
#include "atom/common/native_mate_converters/net_converter.h"
|
#include "atom/common/native_mate_converters/net_converter.h"
|
||||||
#include "base/stl_util.h"
|
#include "base/stl_util.h"
|
||||||
|
@ -19,6 +20,9 @@ namespace atom {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
using ResponseHeadersContainer =
|
||||||
|
std::pair<scoped_refptr<net::HttpResponseHeaders>*, const std::string&>;
|
||||||
|
|
||||||
const char* ResourceTypeToString(content::ResourceType type) {
|
const char* ResourceTypeToString(content::ResourceType type) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case content::RESOURCE_TYPE_MAIN_FRAME:
|
case content::RESOURCE_TYPE_MAIN_FRAME:
|
||||||
|
@ -170,10 +174,15 @@ void ReadFromResponseObject(const base::DictionaryValue& response,
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReadFromResponseObject(const base::DictionaryValue& response,
|
void ReadFromResponseObject(const base::DictionaryValue& response,
|
||||||
scoped_refptr<net::HttpResponseHeaders>* headers) {
|
const ResponseHeadersContainer& container) {
|
||||||
const base::DictionaryValue* dict;
|
const base::DictionaryValue* dict;
|
||||||
|
std::string status_line;
|
||||||
|
if (!response.GetString("statusLine", &status_line))
|
||||||
|
status_line = container.second;
|
||||||
if (response.GetDictionary("responseHeaders", &dict)) {
|
if (response.GetDictionary("responseHeaders", &dict)) {
|
||||||
|
auto headers = container.first;
|
||||||
*headers = new net::HttpResponseHeaders("");
|
*headers = new net::HttpResponseHeaders("");
|
||||||
|
(*headers)->ReplaceStatusLine(status_line);
|
||||||
for (base::DictionaryValue::Iterator it(*dict);
|
for (base::DictionaryValue::Iterator it(*dict);
|
||||||
!it.IsAtEnd();
|
!it.IsAtEnd();
|
||||||
it.Advance()) {
|
it.Advance()) {
|
||||||
|
@ -263,7 +272,8 @@ int AtomNetworkDelegate::OnHeadersReceived(
|
||||||
request, callback, original, override, allowed);
|
request, callback, original, override, allowed);
|
||||||
|
|
||||||
return HandleResponseEvent(
|
return HandleResponseEvent(
|
||||||
kOnHeadersReceived, request, callback, override, original);
|
kOnHeadersReceived, request, callback,
|
||||||
|
std::make_pair(override, original->GetStatusLine()), original);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AtomNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
|
void AtomNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
|
||||||
|
|
|
@ -446,6 +446,8 @@ The `callback` has to be called with an `response` object:
|
||||||
* `cancel` Boolean
|
* `cancel` Boolean
|
||||||
* `responseHeaders` Object (optional) - When provided, the server is assumed
|
* `responseHeaders` Object (optional) - When provided, the server is assumed
|
||||||
to have responded with these headers.
|
to have responded with these headers.
|
||||||
|
* `statusLine` String (optional) - Should be provided when overriding `responseHeaders`
|
||||||
|
to change header status otherwise original response header's status will be used.
|
||||||
|
|
||||||
#### `ses.webRequest.onResponseStarted([filter, ]listener)`
|
#### `ses.webRequest.onResponseStarted([filter, ]listener)`
|
||||||
|
|
||||||
|
|
|
@ -7,12 +7,18 @@ const session = remote.session
|
||||||
describe('webRequest module', function () {
|
describe('webRequest module', function () {
|
||||||
var ses = session.defaultSession
|
var ses = session.defaultSession
|
||||||
var server = http.createServer(function (req, res) {
|
var server = http.createServer(function (req, res) {
|
||||||
res.setHeader('Custom', ['Header'])
|
if (req.url == '/serverRedirect') {
|
||||||
var content = req.url
|
res.statusCode = 301
|
||||||
if (req.headers.accept === '*/*;test/header') {
|
res.setHeader('Location', 'http://' + req.rawHeaders[1])
|
||||||
content += 'header/received'
|
res.end()
|
||||||
|
} else {
|
||||||
|
res.setHeader('Custom', ['Header'])
|
||||||
|
var content = req.url
|
||||||
|
if (req.headers.accept === '*/*;test/header') {
|
||||||
|
content += 'header/received'
|
||||||
|
}
|
||||||
|
res.end(content)
|
||||||
}
|
}
|
||||||
res.end(content)
|
|
||||||
})
|
})
|
||||||
var defaultURL = null
|
var defaultURL = null
|
||||||
|
|
||||||
|
@ -297,6 +303,44 @@ describe('webRequest module', function () {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('follows server redirect', function (done) {
|
||||||
|
ses.webRequest.onHeadersReceived(function (details, callback) {
|
||||||
|
var responseHeaders = details.responseHeaders
|
||||||
|
callback({
|
||||||
|
responseHeaders: responseHeaders,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
$.ajax({
|
||||||
|
url: defaultURL + 'serverRedirect',
|
||||||
|
success: function (data, status, xhr) {
|
||||||
|
assert.equal(xhr.getResponseHeader('Custom'), 'Header')
|
||||||
|
done()
|
||||||
|
},
|
||||||
|
error: function (xhr, errorType) {
|
||||||
|
done(errorType)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('can change the header status', function (done) {
|
||||||
|
ses.webRequest.onHeadersReceived(function (details, callback) {
|
||||||
|
var responseHeaders = details.responseHeaders
|
||||||
|
callback({
|
||||||
|
responseHeaders: responseHeaders,
|
||||||
|
statusLine: "HTTP/1.1 404 Not Found"
|
||||||
|
})
|
||||||
|
})
|
||||||
|
$.ajax({
|
||||||
|
url: defaultURL,
|
||||||
|
success: function (data, status, xhr) {
|
||||||
|
},
|
||||||
|
error: function (xhr, errorType) {
|
||||||
|
assert.equal(xhr.getResponseHeader('Custom'), 'Header')
|
||||||
|
done()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('webRequest.onResponseStarted', function () {
|
describe('webRequest.onResponseStarted', function () {
|
||||||
|
|
Loading…
Reference in a new issue