Jump to content

POSTing torrent to utorrent webui; base64encoded? General error msg


dabear

Recommended Posts

Hi, i am trying to do a POST request to http://mysite.mine.nu:9000/gui/?action=add-file using gm_xmlhttprequest (greasemonkey extension, provides cross-site ajax) in firefox, but because javascript munges binary content when concatenated with a string, I have no chose but to base64encode the data. Is base64 supported by the utorrent webui?

The reason for me asking, is that although I get a response, I get an error message. The error message isn't that helpful though.

greasemonkey/myscript: fetching url https://someaddress/sometorrent.torrent?passkey=mypasskey
greasemonkey/myscript: Status is:200, attempting to send binary data to url: http://mysite.mine.nu:9000/gui/?action=add-url
greasemonkey/myscript: binary content converted to base64, will now send to http://mysite.mine.nu:9000/gui/?action=add-url
greasemonkey/myscript: Binary sent with base64 encoding. Status is: 200, reponseText is {"build":14458,"error": "Error"}

function sendBinary(binaryContents, url) {
var base64Encoded = Base64.encode(binaryContents);
//var base64Encoded = binaryContents;

var boundary_string = 'whatever';
var boundary = '--' + boundary_string + '--' ;
//try file or form-data as disposition
var data = boundary + '\r\n'+ 'Content-Disposition: form-data; name="torrent_file"; filename="mytorrent.torrent"\r\n'+
'Content-Type: application/x-bittorrent\r\n'
+ 'Content-Transfer-Encoding: base64\r\n'
+ '\r\n'+ base64Encoded + '\r\n'
+ boundary;

console.log("binary content converted to base64, will now send to " + url)
GM_xmlhttpRequest({
method: 'POST',
url: url,
headers: {'Content-Type':'multipart/form-data; boundary="'+boundary_string +'"', 'Content-Length':data.length },
data: data,
onload: function(response){
console.log("Binary sent with base64 encoding. Status is: " + response.status + ", reponseText is " + response.responseText);

},
onerror: function(){
alert("error in sending")
}
});

}

base64.encode is this:

var Base64 = {

// private property
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

// public method for encoding
encode : function (input) {
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;

input = Base64._utf8_encode(input);

while (i < input.length) {

chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);

enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;

if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}

output = output +
this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

}

return output;
},

// public method for decoding
decode : function (input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;

input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

while (i < input.length) {

enc1 = this._keyStr.indexOf(input.charAt(i++));
enc2 = this._keyStr.indexOf(input.charAt(i++));
enc3 = this._keyStr.indexOf(input.charAt(i++));
enc4 = this._keyStr.indexOf(input.charAt(i++));

chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;

output = output + String.fromCharCode(chr1);

if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}

}

output = Base64._utf8_decode(output);

return output;

},

// private method for UTF-8 encoding
_utf8_encode : function (string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";

for (var n = 0; n < string.length; n++) {

var c = string.charCodeAt(n);

if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}

}

return utftext;
},

// private method for UTF-8 decoding
_utf8_decode : function (utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;

while ( i < utftext.length ) {

c = utftext.charCodeAt(i);

if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}

}

return string;
}

}

Link to comment
Share on other sites

Thanks for answering. Yes, I've seen that post and others. However, that post assumes Components.classes is usable, which is not the case in greasemonkey userscripts.

Well, it _is_ available, but not usable. "Permission denied to get property XPCComponents.classes" will be the error when you access Components.classes from within a greasemonkey script.

before firefox 3.0, you could also ask for permissions to access Components.classes, but that's not allowed either, anymore. So I guess I'm stuck with ?add-url :)

Error: A script from "http://example.com" was denied UniversalXPConnect privileges.

Here's the code to test that, from within a greasemonkey userscript:

try {
unsafeWindow.netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
var stream = Components.classes["@mozilla.org/network/file-input-stream;1"]
.createInstance(Components.interfaces.nsIFileInputStream);
alert("worked");
} catch(e){
alert("Didn't work: " + e);
}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...