pull/2/head
gorhill 9 years ago
parent 4f2ecc710d
commit 8fcecdbde8

@ -1059,6 +1059,7 @@ var httpObserver = {
register: function() { register: function() {
// https://developer.mozilla.org/en/docs/Observer_Notifications#HTTP_requests // https://developer.mozilla.org/en/docs/Observer_Notifications#HTTP_requests
Services.obs.addObserver(this, 'http-on-opening-request', true); Services.obs.addObserver(this, 'http-on-opening-request', true);
Services.obs.addObserver(this, 'http-on-modify-request', true);
Services.obs.addObserver(this, 'http-on-examine-response', true); Services.obs.addObserver(this, 'http-on-examine-response', true);
Services.obs.addObserver(this, 'http-on-examine-cached-response', true); Services.obs.addObserver(this, 'http-on-examine-cached-response', true);
@ -1088,6 +1089,7 @@ var httpObserver = {
unregister: function() { unregister: function() {
Services.obs.removeObserver(this, 'http-on-opening-request'); Services.obs.removeObserver(this, 'http-on-opening-request');
Services.obs.removeObserver(this, 'http-on-modify-request');
Services.obs.removeObserver(this, 'http-on-examine-response'); Services.obs.removeObserver(this, 'http-on-examine-response');
Services.obs.removeObserver(this, 'http-on-examine-cached-response'); Services.obs.removeObserver(this, 'http-on-examine-cached-response');
@ -1099,49 +1101,56 @@ var httpObserver = {
); );
}, },
handleRequest: function(channel, URI, details) { handleRequest: function(channel, URI, tabId, rawtype) {
var type = this.typeMap[details.type] || 'other'; var type = this.typeMap[rawtype] || 'other';
var result; var onBeforeRequest = vAPI.net.onBeforeRequest;
var callbackDetails = { if ( onBeforeRequest.types && onBeforeRequest.types.has(type) === false ) {
return false;
}
var result = onBeforeRequest.callback({
hostname: URI.asciiHost, hostname: URI.asciiHost,
parentFrameId: type === 'main_frame' ? -1 : 0, parentFrameId: type === 'main_frame' ? -1 : 0,
tabId: details.tabId, tabId: tabId,
type: type, type: type,
url: URI.asciiSpec url: URI.asciiSpec
}; });
var onBeforeRequest = vAPI.net.onBeforeRequest;
if ( !onBeforeRequest.types || onBeforeRequest.types.has(type) ) {
result = onBeforeRequest.callback(callbackDetails);
if ( typeof result === 'object' && result.cancel === true ) {
channel.cancel(this.ABORT);
return true;
}
// For the time being, will block instead of redirecting if ( typeof result !== 'object' ) {
// TODO: figure a better way of blocking embedded frames. return false;
// Maybe blocking network requests, then having a content script
// revisit the DOM to replace blocked frame with something more
// friendly. Will see.
if ( typeof result === 'object' && result.redirectUrl ) {
channel.cancel(this.ABORT);
return true;
}
} }
channel.cancel(this.ABORT);
return true;
},
handleRequestHeaders: function(channel, URI, tabId, rawtype) {
var type = this.typeMap[rawtype] || 'other';
var onBeforeSendHeaders = vAPI.net.onBeforeSendHeaders; var onBeforeSendHeaders = vAPI.net.onBeforeSendHeaders;
if ( !onBeforeSendHeaders.types || onBeforeSendHeaders.types.has(type) ) { if ( onBeforeSendHeaders.types && onBeforeSendHeaders.types.has(type) === false ) {
callbackDetails.requestHeaders = httpRequestHeadersFactory(channel); return;
result = onBeforeSendHeaders.callback(callbackDetails);
callbackDetails.requestHeaders.dispose();
if ( typeof result === 'object' && result.cancel === true ) {
channel.cancel(this.ABORT);
return true;
}
} }
var requestHeaders = httpRequestHeadersFactory(channel);
onBeforeSendHeaders.callback({
hostname: URI.asciiHost,
parentFrameId: type === 'main_frame' ? -1 : 0,
requestHeaders: requestHeaders,
tabId: tabId,
type: type,
url: URI.asciiSpec
});
requestHeaders.dispose();
},
return false; channelDataFromChannel: function(channel) {
if ( !(channel instanceof Ci.nsIWritablePropertyBag) ) {
return null;
}
try {
return channel.getProperty(this.REQDATAKEY);
} catch (ex) {
}
return null;
}, },
observe: function(channel, topic) { observe: function(channel, topic) {
@ -1150,33 +1159,25 @@ var httpObserver = {
} }
var URI = channel.URI; var URI = channel.URI;
var channelData, type, result; var channelData;
if ( if (
topic === 'http-on-examine-response' || topic === 'http-on-examine-response' ||
topic === 'http-on-examine-cached-response' topic === 'http-on-examine-cached-response'
) { ) {
if ( !(channel instanceof Ci.nsIWritablePropertyBag) ) { channelData = this.channelDataFromChannel(channel);
return; if ( channelData === null ) {
}
try {
channelData = channel.getProperty(this.REQDATAKEY);
} catch (ex) {
return;
}
if ( !channelData ) {
return; return;
} }
type = this.frameTypeMap[channelData[1]]; var type = this.frameTypeMap[channelData[1]];
if ( !type ) { if ( !type ) {
return; return;
} }
topic = 'Content-Security-Policy'; topic = 'Content-Security-Policy';
var result;
try { try {
result = channel.getResponseHeader(topic); result = channel.getResponseHeader(topic);
} catch (ex) { } catch (ex) {
@ -1203,6 +1204,17 @@ var httpObserver = {
return; return;
} }
if ( topic === 'http-on-modify-request' ) {
channelData = this.channelDataFromChannel(channel);
if ( channelData === null ) {
return;
}
this.handleRequestHeaders(channel, URI, channelData[0], channelData[1]);
return;
}
// http-on-opening-request // http-on-opening-request
// https://github.com/gorhill/uMatrix/issues/165 // https://github.com/gorhill/uMatrix/issues/165
@ -1221,6 +1233,7 @@ var httpObserver = {
} catch (ex) { } catch (ex) {
} }
} }
if ( !aWindow && channel.loadGroup && channel.loadGroup.notificationCallbacks ) { if ( !aWindow && channel.loadGroup && channel.loadGroup.notificationCallbacks ) {
try { try {
aWindow = channel aWindow = channel
@ -1230,6 +1243,7 @@ var httpObserver = {
} catch (ex) { } catch (ex) {
} }
} }
if ( aWindow ) { if ( aWindow ) {
try { try {
tabId = vAPI.tabs.getTabId(aWindow tabId = vAPI.tabs.getTabId(aWindow
@ -1246,10 +1260,9 @@ var httpObserver = {
} }
} }
type = channel.loadInfo && channel.loadInfo.contentPolicyType || 1; var rawtype = channel.loadInfo && channel.loadInfo.contentPolicyType || 1;
result = this.handleRequest(channel, URI, { tabId: tabId, type: type });
if ( result === true ) { if ( this.handleRequest(channel, URI, tabId, rawtype) === true ) {
return; return;
} }
@ -1258,7 +1271,7 @@ var httpObserver = {
} }
// Carry data for behind-the-scene redirects // Carry data for behind-the-scene redirects
channel.setProperty(this.REQDATAKEY, [tabId, type]); channel.setProperty(this.REQDATAKEY, [tabId, rawtype]);
}, },
// contentPolicy.shouldLoad doesn't detect redirects, this needs to be used // contentPolicy.shouldLoad doesn't detect redirects, this needs to be used
@ -1279,12 +1292,7 @@ var httpObserver = {
var channelData = oldChannel.getProperty(this.REQDATAKEY); var channelData = oldChannel.getProperty(this.REQDATAKEY);
var details = { if ( this.handleRequest(newChannel, URI, channelData[0], channelData[1]) ) {
tabId: channelData[0],
type: channelData[1]
};
if ( this.handleRequest(newChannel, URI, details) ) {
result = this.ABORT; result = this.ABORT;
return; return;
} }

Loading…
Cancel
Save