Make Zotero Standalone use the browser's user agent string when making requests on its behalf via connectors

In conjunction with a change to the translator, this fixes the issue reported at http://forums.zotero.org/discussion/22409/
This commit is contained in:
Simon Kornblith 2012-08-19 19:06:05 -04:00
parent 9ca142c4f6
commit c81c9478d9
3 changed files with 19 additions and 9 deletions

View file

@ -31,8 +31,9 @@
* @param {String|nsIURI} uri URI of page to manage cookies for (cookies for domains that are not
* subdomains of this URI are ignored)
* @param {String} cookieData Cookies with which to initiate the sandbox
* @param {String} userAgent User agent to use for sandboxed requests
*/
Zotero.CookieSandbox = function(browser, uri, cookieData) {
Zotero.CookieSandbox = function(browser, uri, cookieData, userAgent) {
this._observerService = Components.classes["@mozilla.org/observer-service;1"].
getService(Components.interfaces.nsIObserverService);
@ -54,6 +55,8 @@ Zotero.CookieSandbox = function(browser, uri, cookieData) {
}
}
if(userAgent) this.userAgent = userAgent;
Zotero.CookieSandbox.Observer.register();
if(browser) {
this.attachToBrowser(browser);
@ -206,6 +209,10 @@ Zotero.CookieSandbox.Observer = new function() {
return;
}
if(trackedBy.userAgent) {
channel.setRequestHeader("User-Agent", trackedBy.userAgent, false);
}
// add cookies to be sent to this domain
channel.setRequestHeader("Cookie", trackedBy.cookieString, false);
Zotero.debug("CookieSandbox: Added cookies for request to "+channelURI, 5);

View file

@ -361,9 +361,12 @@ Zotero.Server.DataListener.prototype._processEndpoint = function(method, postDat
// pass to endpoint
if((endpoint.init.length ? endpoint.init.length : endpoint.init.arity) === 3) {
const uaRe = /[\r\n]User-Agent: +([^\r\n]+)/i;
var m = uaRe.exec(this.header);
var url = {
"pathname":this.pathname,
"query":this.query ? Zotero.Server.decodeQueryString(this.query.substr(1)) : {}
"query":this.query ? Zotero.Server.decodeQueryString(this.query.substr(1)) : {},
"userAgent":m && m[1]
};
endpoint.init(url, decodedData, sendResponseCallback);

View file

@ -98,7 +98,7 @@ Zotero.Server.Connector.Detect.prototype = {
* @param {Object} data POST data or GET query string
* @param {Function} sendResponseCallback function to send HTTP response
*/
"init":function(data, sendResponseCallback) {
"init":function(url, data, sendResponseCallback) {
this.sendResponse = sendResponseCallback;
this._parsedPostData = data;
@ -115,7 +115,7 @@ Zotero.Server.Connector.Detect.prototype = {
var pageShowCalled = false;
var me = this;
this._translate.setCookieSandbox(new Zotero.CookieSandbox(this._browser,
this._parsedPostData["uri"], this._parsedPostData["cookie"]));
this._parsedPostData["uri"], this._parsedPostData["cookie"], url.userAgent));
this._browser.addEventListener("DOMContentLoaded", function() {
try {
if(me._browser.contentDocument.location.href == "about:blank") return;
@ -283,7 +283,7 @@ Zotero.Server.Connector.SaveItem.prototype = {
* @param {Object} data POST data or GET query string
* @param {Function} sendResponseCallback function to send HTTP response
*/
"init":function(data, sendResponseCallback) {
"init":function(url, data, sendResponseCallback) {
// figure out where to save
var libraryID = null;
var collectionID = null;
@ -293,8 +293,8 @@ Zotero.Server.Connector.SaveItem.prototype = {
var collection = zp.getSelectedCollection();
} catch(e) {}
var cookieSandbox = data["uri"] && data["cookie"] ? new Zotero.CookieSandbox(null, data["uri"],
data["cookie"]) : null;
var cookieSandbox = data["uri"] ? new Zotero.CookieSandbox(null, data["uri"],
data["cookie"] || "", url.userAgent) : null;
// save items
var itemSaver = new Zotero.Translate.ItemSaver(libraryID,
@ -339,12 +339,12 @@ Zotero.Server.Connector.SaveSnapshot.prototype = {
* @param {String} data POST data or GET query string
* @param {Function} sendResponseCallback function to send HTTP response
*/
"init":function(data, sendResponseCallback) {
"init":function(url, data, sendResponseCallback) {
Zotero.Server.Connector.Data[data["url"]] = "<html>"+data["html"]+"</html>";
var browser = Zotero.Browser.createHiddenBrowser();
var pageShowCalled = false;
var cookieSandbox = new Zotero.CookieSandbox(browser, data["url"], data["cookie"]);
var cookieSandbox = new Zotero.CookieSandbox(browser, data["url"], data["cookie"], url.userAgent);
browser.addEventListener("pageshow", function() {
if(browser.contentDocument.location.href == "about:blank"
|| browser.contentDocument.readyState !== "complete") return;