www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

commit 7f7fe90ad5fc21502fb1ab2e84f793628cd9892d
parent abe266d74b46955b958ffe9561cc3e88b44aa7dd
Author: Simon Kornblith <simon@simonster.com>
Date:   Mon,  8 Apr 2013 18:29:17 -0400

Allow attachments to be saved using relative URLs

See https://groups.google.com/forum/?fromgroups=#!topic/zotero-dev/bBB8lyXy9V8
and zotero/translators#569

Diffstat:
Mchrome/content/zotero/xpcom/translation/translate.js | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
Mchrome/content/zotero/xpcom/utilities_translate.js | 76++++++++++++++--------------------------------------------------------------
2 files changed, 70 insertions(+), 70 deletions(-)

diff --git a/chrome/content/zotero/xpcom/translation/translate.js b/chrome/content/zotero/xpcom/translation/translate.js @@ -630,14 +630,11 @@ Zotero.Translate.Sandbox = { } } - // Remap attachment (but not link) URLs - var properToProxy = translate.translator[0].properToProxy; - if(properToProxy && item.attachments) { - for(var i=0; i<item.attachments.length; i++) { - var attachment = item.attachments[i]; - if(attachment.snapshot !== false && attachment.url) { - attachment.url = properToProxy(attachment.url); - } + for(var i=0; i<item.attachments.length; i++) { + var attachment = item.attachments[i]; + if(attachment.url) { + // Remap attachment (but not link) URLs + attachment.url = translate.resolveURL(attachment.url, attachment.snapshot === false); } } } @@ -1185,6 +1182,57 @@ Zotero.Translate.Base.prototype = { * Return the progress of the import operation, or null if progress cannot be determined */ "getProgress":function() { return null }, + + /** + * Translate a URL to a form that goes through the appropriate proxy, or + * convert a relative URL to an absolute one + * + * @param {String} url + * @param {Boolean} dontUseProxy If true, don't convert URLs to variants + * that use the proxy + * @type String + * @private + */ + "resolveURL":function(url, dontUseProxy) { + const hostPortRe = /^((?:http|https|ftp):)\/\/([^\/]+)/i; + // resolve local URL + var resolved = ""; + + // convert proxy to proper if applicable + if(hostPortRe.test(url)) { + if(this.translator && this.translator[0] + && this.translator[0].properToProxy && !dontUseProxy) { + resolved = this.translator[0].properToProxy(url); + } else { + resolved = url; + } + } else if(Zotero.isFx) { + resolved = Components.classes["@mozilla.org/network/io-service;1"]. + getService(Components.interfaces.nsIIOService). + newURI(this.location, "", null).resolve(url); + } else if(Zotero.isNode) { + resolved = require('url').resolve(this.location, url); + } else { + var a = document.createElement('a'); + a.href = url; + resolved = a.href; + } + + /*var m = hostPortRe.exec(resolved); + if(!m) { + throw new Error("Invalid URL supplied for HTTP request: "+url); + } else if(this._translate.document && this._translate.document.location) { + var loc = this._translate.document.location; + if(this._translate._currentState !== "translate" && loc + && (m[1].toLowerCase() !== loc.protocol.toLowerCase() + || m[2].toLowerCase() !== loc.host.toLowerCase())) { + throw new Error("Attempt to access "+m[1]+"//"+m[2]+" from "+loc.protocol+"//"+loc.host + +" blocked: Cross-site requests are only allowed during translation"); + } + }*/ + + return resolved; + }, /** * Executed on translator completion, either automatically from a synchronous scraper or as diff --git a/chrome/content/zotero/xpcom/utilities_translate.js b/chrome/content/zotero/xpcom/utilities_translate.js @@ -195,17 +195,18 @@ Zotero.Utilities.Translate.prototype.loadDocument = function(url, succeeded, fai * @ignore */ Zotero.Utilities.Translate.prototype.processDocuments = function(urls, processor, done, exception) { + var translate = this._translate; + if(typeof(urls) == "string") { - urls = [this._convertURL(urls)]; + urls = [translate.resolveURL(urls)]; } else { for(var i in urls) { - urls[i] = this._convertURL(urls[i]); + urls[i] = translate.resolveURL(urls[i]); } } // Unless the translator has proposed some way to handle an error, handle it // by throwing a "scraping error" message - var translate = this._translate; if(exception) { var myException = function(e) { var browserDeleted; @@ -300,7 +301,9 @@ Zotero.Utilities.Translate.prototype.processDocuments = function(urls, processor * @return {Boolean} True if the request was sent, or false if the browser is offline */ Zotero.Utilities.Translate.prototype.doGet = function(urls, processor, done, responseCharset) { - var callAgain = false; + var callAgain = false, + me = this, + translate = this._translate; if(typeof(urls) == "string") { var url = urls; @@ -309,11 +312,9 @@ Zotero.Utilities.Translate.prototype.doGet = function(urls, processor, done, res var url = urls.shift(); } - url = this._convertURL(url); + url = translate.resolveURL(url); - var me = this; - - this._translate.incrementAsyncProcesses("Zotero.Utilities.Translate#doGet"); + translate.incrementAsyncProcesses("Zotero.Utilities.Translate#doGet"); var xmlhttp = Zotero.HTTP.doGet(url, function(xmlhttp) { try { if(processor) { @@ -327,9 +328,9 @@ Zotero.Utilities.Translate.prototype.doGet = function(urls, processor, done, res done(); } } - me._translate.decrementAsyncProcesses("Zotero.Utilities.Translate#doGet"); + translate.decrementAsyncProcesses("Zotero.Utilities.Translate#doGet"); } catch(e) { - me._translate.complete(false, e); + translate.complete(false, e); } }, responseCharset, this._translate.cookieSandbox); } @@ -339,10 +340,10 @@ Zotero.Utilities.Translate.prototype.doGet = function(urls, processor, done, res * @ignore */ Zotero.Utilities.Translate.prototype.doPost = function(url, body, onDone, headers, responseCharset) { - url = this._convertURL(url); - var translate = this._translate; - this._translate.incrementAsyncProcesses("Zotero.Utilities.Translate#doPost"); + url = translate.resolveURL(url); + + translate.incrementAsyncProcesses("Zotero.Utilities.Translate#doPost"); var xmlhttp = Zotero.HTTP.doPost(url, body, function(xmlhttp) { try { onDone(xmlhttp.responseText, xmlhttp); @@ -353,55 +354,6 @@ Zotero.Utilities.Translate.prototype.doPost = function(url, body, onDone, header }, headers, responseCharset, translate.cookieSandbox ? translate.cookieSandbox : undefined); } -/** - * Translate a URL to a form that goes through the appropriate proxy, or convert a relative URL to - * an absolute one - * - * @param {String} url - * @type String - * @private - */ -Zotero.Utilities.Translate.prototype._convertURL = function(url) { - const hostPortRe = /^((?:http|https|ftp):)\/\/([^\/]+)/i; - // resolve local URL - var resolved = ""; - - // convert proxy to proper if applicable - if(hostPortRe.test(url)) { - if(this._translate.translator && this._translate.translator[0] - && this._translate.translator[0].properToProxy) { - resolved = this._translate.translator[0].properToProxy(url); - } else { - resolved = url; - } - } else if(Zotero.isFx) { - resolved = Components.classes["@mozilla.org/network/io-service;1"]. - getService(Components.interfaces.nsIIOService). - newURI(this._translate.location, "", null).resolve(url); - } else if(Zotero.isNode) { - resolved = require('url').resolve(this._translate.location, url); - } else { - var a = document.createElement('a'); - a.href = url; - resolved = a.href; - } - - /*var m = hostPortRe.exec(resolved); - if(!m) { - throw new Error("Invalid URL supplied for HTTP request: "+url); - } else if(this._translate.document && this._translate.document.location) { - var loc = this._translate.document.location; - if(this._translate._currentState !== "translate" && loc - && (m[1].toLowerCase() !== loc.protocol.toLowerCase() - || m[2].toLowerCase() !== loc.host.toLowerCase())) { - throw new Error("Attempt to access "+m[1]+"//"+m[2]+" from "+loc.protocol+"//"+loc.host - +" blocked: Cross-site requests are only allowed during translation"); - } - }*/ - - return resolved; -} - Zotero.Utilities.Translate.prototype.__exposedProps__ = {"HTTP":"r"}; for(var j in Zotero.Utilities.Translate.prototype) { if(typeof Zotero.Utilities.Translate.prototype[j] === "function" && j[0] !== "_" && j != "Translate") {