this fixes #301: many silly bugs broke local mirroring

This commit is contained in:
gorhill 2014-10-11 10:53:09 -04:00
parent c2b6ad1fad
commit f6ef39627d
3 changed files with 33 additions and 7 deletions

View File

@ -1,3 +1,6 @@
# As of now 2014-10-11:
# If no regex capture: use whole URL
# If regex capture: use capture index 1
ajax.googleapis.com
^ajax\.googleapis\.com\/ajax\/libs\/
fonts.googleapis.com

View File

@ -53,7 +53,7 @@ var nullFunc = function() {};
// control over what significant part(s) of a URL is to be used as key.
var mirrorCandidates = Object.create(null);
var magicId = 'nmewkrsniruu';
var magicId = 'yawqboypxuhs';
var metadataPersistTimer = null;
var bytesInUseMercy = 1 * 1024 * 1024;
@ -190,7 +190,9 @@ var toUrlKey = function(url) {
if ( matches === null ) {
continue;
}
return matches.length === 1 ? matches[0] : matches[1];
// https://github.com/gorhill/uBlock/issues/301
// Use whole URL as key when no regex capture
return matches.length === 1 ? url : matches[1];
}
return '';
};
@ -360,9 +362,10 @@ var cacheAsset = function(url) {
return;
}
var mimeType = extractMimeType(this.getResponseHeader('Content-Type'));
var uint8Buffer = new Uint8Array(this.response);
var yamd5 = new YaMD5();
yamd5.appendAsciiStr(mimeType);
yamd5.appendAsciiStr(this.response);
yamd5.appendByteArray(uint8Buffer);
var hash = yamd5.end();
addMetadata(urlKey, hash);
if ( contentExists(hash) ) {
@ -374,7 +377,7 @@ var cacheAsset = function(url) {
// as the result is somewhat more compact I believe
var dataUrl = null;
try {
dataUrl = 'data:' + mimeType + ';base64,' + btoaSafe(new Uint8Array(this.response));
dataUrl = 'data:' + mimeType + ';base64,' + btoaSafe(uint8Buffer);
} catch (e) {
//console.debug('"%s":', url, e);
}
@ -486,10 +489,9 @@ var load = function() {
var onMetadataReady = function(bin) {
//console.debug('mirrors.load(): loaded metadata');
metadata = bin.mirrors_metadata;
var mustReset = metadata.magicId !== magicId;
var u2hmap = metadata.urlKeyToHashMap = bin.mirrors_metadata.urlKeyToHashMap;
var mustReset = bin.mirrors_metadata.magicId !== magicId;
var toRemove = [];
var u2hmap = metadata.urlKeyToHashMap;
var hash;
for ( var urlKey in u2hmap ) {
if ( u2hmap.hasOwnProperty(urlKey) === false ) {

View File

@ -269,6 +269,27 @@ THE SOFTWARE.
return this;
};
MD5.prototype.appendByteArray = function(input) {
var buf8 = this._buffer8;
var buf32 = this._buffer32;
var bufLen = this._bufferLength;
var i, j = 0;
for (;;) {
i = Math.min(input.length-j, 64-bufLen);
while ( i-- ) {
buf8[bufLen++] = input[j++];
}
if ( bufLen < 64 ) {
break;
}
this._dataLength += 64;
md5cycle(this._state, buf32);
bufLen = 0;
}
this._bufferLength = bufLen;
return this;
};
MD5.prototype.start = function() {
this._dataLength = 0;
this._bufferLength = 0;