From b1aecb607a05837e401e27660a1b39715a67f520 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Wed, 1 Feb 2012 20:19:10 +0100 Subject: [PATCH] I puked a little in Firefags' mouths. --- 4chan_x.user.js | 24 +++++++++++++++++++++--- changelog | 2 ++ script.coffee | 19 +++++++++++++++++-- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 3510dd9a5..c3625e4f1 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -1823,19 +1823,37 @@ } }, post: function(data) { - var boundary, callbacks, form, name, opts, parts, url, val; + var boundary, callbacks, form, name, opts, parts, toUTF8, url, val; url = "http://sys.4chan.org/" + data.board + "/post"; delete data.board; delete data.qr; if (engine === 'gecko' && data.upfile) { + toUTF8 = function(val) { + var c, n, utf, _ref; + utf = ''; + for (n = 0, _ref = val.length - 1; 0 <= _ref ? n <= _ref : n >= _ref; 0 <= _ref ? n++ : n--) { + c = val.charCodeAt(n); + if (c < 128) { + utf += String.fromCharCode(c); + } else if ((127 < c && c < 2048)) { + utf += String.fromCharCode((c >> 6) | 192); + utf += String.fromCharCode((c & 63) | 128); + } else { + utf += String.fromCharCode((c >> 12) | 224); + utf += String.fromCharCode(((c >> 6) & 63) | 128); + utf += String.fromCharCode((c & 63) | 128); + } + } + return utf; + }; boundary = '-------------SMCD' + Date.now(); parts = []; - parts.push('Content-Disposition: form-data; name="upfile"; filename="' + data.upfile.name + '"\r\n' + 'Content-Type: ' + data.upfile.type + '\r\n\r\n' + data.upfile.buffer + '\r\n'); + parts.push('Content-Disposition: form-data; name="upfile"; filename="' + toUTF8(data.upfile.name) + '"\r\n' + 'Content-Type: ' + data.upfile.type + '\r\n\r\n' + data.upfile.buffer + '\r\n'); delete data.upfile; for (name in data) { val = data[name]; if (val) { - parts.push('Content-Disposition: form-data; name="' + name + '"\r\n\r\n' + val + '\r\n'); + parts.push('Content-Disposition: form-data; name="' + name + '"\r\n\r\n' + toUTF8(val) + '\r\n'); } } form = '--' + boundary + '\r\n' + parts.join('--' + boundary + '\r\n') + '--' + boundary + '--\r\n'; diff --git a/changelog b/changelog index de99dc2e6..89db1b5bd 100644 --- a/changelog +++ b/changelog @@ -1,4 +1,6 @@ master +- Mayhem + Fix image posting on Firefox along with Unicode characters in the form. 2.25.1 - Mayhem diff --git a/script.coffee b/script.coffee index e213ee7e9..4d941ebdc 100644 --- a/script.coffee +++ b/script.coffee @@ -1398,13 +1398,28 @@ qr = # File with filename upload fix from desuwa if engine is 'gecko' and data.upfile + toUTF8 = (val) -> + utf = '' + for n in [0..val.length-1] + c = val.charCodeAt n + if c < 128 + utf += String.fromCharCode c + else if 127 < c < 2048 + utf += String.fromCharCode (c >> 6) | 192 + utf += String.fromCharCode (c & 63) | 128 + else + utf += String.fromCharCode (c >> 12) | 224 + utf += String.fromCharCode ((c >> 6) & 63) | 128 + utf += String.fromCharCode (c & 63) | 128 + utf + boundary = '-------------SMCD' + Date.now(); parts = [] - parts.push 'Content-Disposition: form-data; name="upfile"; filename="' + data.upfile.name + '"\r\n' + 'Content-Type: ' + data.upfile.type + '\r\n\r\n' + data.upfile.buffer + '\r\n' + parts.push 'Content-Disposition: form-data; name="upfile"; filename="' + toUTF8(data.upfile.name) + '"\r\n' + 'Content-Type: ' + data.upfile.type + '\r\n\r\n' + data.upfile.buffer + '\r\n' delete data.upfile for name, val of data - parts.push 'Content-Disposition: form-data; name="' + name + '"\r\n\r\n' + val + '\r\n' if val + parts.push 'Content-Disposition: form-data; name="' + name + '"\r\n\r\n' + toUTF8(val) + '\r\n' if val form = '--' + boundary + '\r\n' + parts.join('--' + boundary + '\r\n') + '--' + boundary + '--\r\n' else