From 12a7e03703625dcc155e7c95793414edeae4f59f Mon Sep 17 00:00:00 2001 From: amla70 Date: Wed, 13 Jul 2011 08:45:52 +0000 Subject: [PATCH] FoxToPhone: Remove charset workaround, identify ourselves correctly, add workaround for long urls with goo.gl --- .../firefox_sendtophone/modules/OAuth.js | 6 +-- .../modules/sendtophone.js | 43 +++++++++++++------ 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/third_party/firefox_sendtophone/modules/OAuth.js b/third_party/firefox_sendtophone/modules/OAuth.js index d4f142d..3cb7f87 100644 --- a/third_party/firefox_sendtophone/modules/OAuth.js +++ b/third_party/firefox_sendtophone/modules/OAuth.js @@ -315,11 +315,7 @@ ChromeExOAuth.sendRequest = function(method, url, headers, body, callback) { } } -// The server is expecting an exact content-type header (line 96 of RequestInfo.java) -// but Firefox adds the charset, so let's workaround the problem -// https://bugzilla.mozilla.org/show_bug.cgi?id=416178#c31 -// xhr.send(body); - xhr.sendAsBinary(body); + xhr.send(body); }; /** diff --git a/third_party/firefox_sendtophone/modules/sendtophone.js b/third_party/firefox_sendtophone/modules/sendtophone.js index cdacabe..e7affaf 100644 --- a/third_party/firefox_sendtophone/modules/sendtophone.js +++ b/third_party/firefox_sendtophone/modules/sendtophone.js @@ -61,7 +61,7 @@ var sendtophoneCore = { 'consumer_key' : 'anonymous', 'consumer_secret' : 'anonymous', 'scope' : baseUrl, - 'app_name' : 'Chrome To Phone', + 'app_name' : 'Fox To Phone', 'callback_page': this.returnOAuthUrl }); }, @@ -113,16 +113,20 @@ var sendtophoneCore = { .logStringMessage( text ); }, - processXHR: function(url, method, data, callback) + processXHR: function(url, method, headers, data, callback) { var req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"] .createInstance(Ci.nsIXMLHttpRequest); req.open(method, url, true); - req.setRequestHeader('X-Same-Domain', 'true'); // XSRF protector - if (method=='POST') - req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); + if (headers) { + for (var header in headers) { + if (headers.hasOwnProperty(header)) { + req.setRequestHeader(header, headers[header]); + } + } + } req.onreadystatechange = function() { @@ -139,19 +143,14 @@ var sendtophoneCore = { var redirectUrl = redirectMatch[2].replace(/&/g, '&'); // Do the redirect and use the original callback - sendtophoneCore.processXHR( redirectUrl, 'GET', null, callback); + sendtophoneCore.processXHR( redirectUrl, 'GET', null, null, callback); } else callback.call( sendtophoneCore, req ); } else { - if (req.status==400&&this.retryCount<4){ - this.retryCount++; - this.processXHR(this.sendUrl, 'POST', this.pendingMessage, this.processSentData); - } - else - sendtophoneCore.alert(sendtophoneCore.getString("ErrorOnSend") + ' (status ' + req.status + ')\r\n' + body); + sendtophoneCore.alert(sendtophoneCore.getString("ErrorOnSend") + ' (status ' + req.status + ')\r\n' + body); } } }; @@ -211,6 +210,24 @@ var sendtophoneCore = { url = proxyUrl + encodeURIComponent( url); } + // Seems that the server fails with URLs larger than 990 bytes, so let's shorten it in those cases + // http://code.google.com/p/chrometophone/issues/detail?id=315 + if (url.length>900) { + var self = this; + this.processXHR("https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyDfmYwz1EevIW18Ifda3YeS9dVPhKsDUQo", "POST", {'Content-Type':'application/json'}, + JSON.stringify( {"longUrl": url} ), function(req) { + var body = req.responseText, + response = (body && body[0]=='{' && JSON.parse( body ) ); + // If OK then perform now the action + if (response && response.kind == "urlshortener#url") + self.send(title, response.id, selection); + else + this.alert( body ); + }) + + return + } + var data = 'title=' + encodeURIComponent(title) + '&url=' + encodeURIComponent(url) + '&sel=' + encodeURIComponent(selection); @@ -520,7 +537,7 @@ var Minus = { SendFile : function(nsFile, callback) { // Create a gallery - sendtophoneCore.processXHR( Minus.prefix + 'CreateGallery', 'GET', null, function(req) { + sendtophoneCore.processXHR( Minus.prefix + 'CreateGallery', 'GET', null, null, function(req) { var body = req.responseText; if (body.substring(0, 1) != '{')