Skip to content

Commit

Permalink
make cloudflare outages non-fatal to uploads
Browse files Browse the repository at this point in the history
if a reverse-proxy starts hijacking requests and replying with HTML,
don't panic when it fails to decode as a handshake json

fix this for most other json-expecting gizmos too,
and take the opportunity to cleanup some text formatting
  • Loading branch information
9001 committed Mar 8, 2024
1 parent 8785d2f commit 7741870
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
19 changes: 14 additions & 5 deletions copyparty/web/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,8 @@ var Ls = {
"md_eshow": "cannot render ",
"md_off": "[📜<em>readme</em>] disabled in [⚙️] -- document hidden",

"badreply": "Failed to parse reply from server",

"xhr403": "403: Access denied\n\ntry pressing F5, maybe you got logged out",
"cf_ok": "sorry about that -- DD" + wah + "oS protection kicked in\n\nthings should resume in about 30 sec\n\nif nothing happens, hit F5 to reload the page",
"tl_xe1": "could not list subfolders:\n\nerror ",
Expand Down Expand Up @@ -887,6 +889,8 @@ var Ls = {
"md_eshow": "viser forenklet ",
"md_off": "[📜<em>readme</em>] er avskrudd i [⚙️] -- dokument skjult",

"badreply": "Ugyldig svar ifra serveren",

"xhr403": "403: Tilgang nektet\n\nkanskje du ble logget ut? prøv å trykk F5",
"cf_ok": "beklager -- liten tilfeldig kontroll, alt OK\n\nting skal fortsette om ca. 30 sekunder\n\nhvis ikkeno skjer, trykk F5 for å laste siden på nytt",
"tl_xe1": "kunne ikke hente undermapper:\n\nfeil ",
Expand Down Expand Up @@ -969,7 +973,7 @@ var Ls = {
"u_emtleakf": 'prøver følgende:\n<ul><li>trykk F5 for å laste siden på nytt</li><li>så skru på <code>🥔</code> ("enkelt UI") i opplasteren</li><li>og forsøk den samme opplastningen igjen</li></ul>\nPS: Firefox <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1790500">fikser forhåpentligvis feilen</a> en eller annen gang',
"u_s404": "ikke funnet på serveren",
"u_expl": "forklar",
"u_maxconn": "de fleste nettlesere tillater ikke mer enn 6, men firefox lar deg øke grensen med <code>connections-per-server</code> in <code>about:config</code>",
"u_maxconn": "de fleste nettlesere tillater ikke mer enn 6, men firefox lar deg øke grensen med <code>connections-per-server</code> i <code>about:config</code>",
"u_tu": '<p class="warn">ADVARSEL: turbo er på, <span>&nbsp;avbrutte opplastninger vil muligens ikke oppdages og gjenopptas; hold musepekeren over turbo-knappen for mer info</span></p>',
"u_ts": '<p class="warn">ADVARSEL: turbo er på, <span>&nbsp;søkeresultater kan være feil; hold musepekeren over turbo-knappen for mer info</span></p>',
"u_turbo_c": "turbo er deaktivert i serverkonfigurasjonen",
Expand Down Expand Up @@ -5554,7 +5558,7 @@ document.onkeydown = function (e) {

function xhr_search_results() {
if (this.status !== 200) {
var msg = unpre(this.responseText);
var msg = hunpre(this.responseText);
srch_msg(true, "http " + this.status + ": " + msg);
search_in_progress = 0;
return;
Expand Down Expand Up @@ -7494,7 +7498,7 @@ var msel = (function () {
xhrchk(this, L.fd_xe1, L.fd_xe2);

if (this.status !== 201) {
sf.textContent = 'error: ' + unpre(this.responseText);
sf.textContent = 'error: ' + hunpre(this.responseText);
return;
}

Expand Down Expand Up @@ -7542,7 +7546,7 @@ var msel = (function () {
xhrchk(this, L.fsm_xe1, L.fsm_xe2);

if (this.status < 200 || this.status > 201) {
sf.textContent = 'error: ' + unpre(this.responseText);
sf.textContent = 'error: ' + hunpre(this.responseText);
return;
}

Expand Down Expand Up @@ -7878,7 +7882,12 @@ var unpost = (function () {
if (!xhrchk(this, L.fu_xe1, L.fu_xe2))
return ebi('op_unpost').innerHTML = L.fu_xe1;

var res = JSON.parse(this.responseText);
try {
var res = JSON.parse(this.responseText);
}
catch (ex) {
return ebi('op_unpost').innerHTML = '<p>' + L.badreply + ':</p>' + unpre(this.responseText);
}
if (res.length) {
if (res.length == 2000)
html.push("<p>" + L.un_max);
Expand Down
6 changes: 3 additions & 3 deletions copyparty/web/md2.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,14 +368,14 @@ function save(e) {

function save_cb() {
if (this.status !== 200)
return toast.err(0, 'Error! The file was NOT saved.\n\n' + this.status + ": " + (this.responseText + '').replace(/^<pre>/, ""));
return toast.err(0, 'Error! The file was NOT saved.\n\nError ' + this.status + ":\n" + unpre(this.responseText));

var r;
try {
r = JSON.parse(this.responseText);
}
catch (ex) {
return toast.err(0, 'Failed to parse reply from server:\n\n' + this.responseText);
return toast.err(0, 'Error! The file was likely NOT saved.\n\nFailed to parse reply from server:\n\n' + unpre(this.responseText));
}

if (!r.ok) {
Expand Down Expand Up @@ -418,7 +418,7 @@ function run_savechk(lastmod, txt, btn, ntry) {

function savechk_cb() {
if (this.status !== 200)
return toast.err(0, 'Error! The file was NOT saved.\n\n' + this.status + ": " + (this.responseText + '').replace(/^<pre>/, ""));
return toast.err(0, 'Error! The file was NOT saved.\n\nError ' + this.status + ":\n" + unpre(this.responseText));

var doc1 = this.txt.replace(/\r\n/g, "\n");
var doc2 = this.responseText.replace(/\r\n/g, "\n");
Expand Down
6 changes: 3 additions & 3 deletions copyparty/web/mde.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,14 @@ function save(mde) {

function save_cb() {
if (this.status !== 200)
return toast.err(0, 'Error! The file was NOT saved.\n\n' + this.status + ": " + (this.responseText + '').replace(/^<pre>/, ""));
return toast.err(0, 'Error! The file was NOT saved.\n\nError ' + this.status + ":\n" + unpre(this.responseText));

var r;
try {
r = JSON.parse(this.responseText);
}
catch (ex) {
return toast.err(0, 'Failed to parse reply from server:\n\n' + this.responseText);
return toast.err(0, 'Error! The file was likely NOT saved.\n\nFailed to parse reply from server:\n\n' + unpre(this.responseText));
}

if (!r.ok) {
Expand Down Expand Up @@ -180,7 +180,7 @@ function save_cb() {

function save_chk() {
if (this.status !== 200)
return toast.err(0, 'Error! The file was NOT saved.\n\n' + this.status + ": " + (this.responseText + '').replace(/^<pre>/, ""));
return toast.err(0, 'Error! The file was NOT saved.\n\nError ' + this.status + ":\n" + unpre(this.responseText));

var doc1 = this.txt.replace(/\r\n/g, "\n");
var doc2 = this.responseText.replace(/\r\n/g, "\n");
Expand Down
13 changes: 12 additions & 1 deletion copyparty/web/up2k.js
Original file line number Diff line number Diff line change
Expand Up @@ -2256,23 +2256,34 @@ function up2k_init(subtle) {
console.log('handshake onerror, retrying', t.name, t);
apop(st.busy.handshake, t);
st.todo.handshake.unshift(t);
t.cooldown = Date.now() + 5000 + Math.floor(Math.random() * 3000);
t.keepalive = keepalive;
};
var orz = function (e) {
if (t.t_busied != me || t.done)
return console.log('zombie handshake onload', t.name, t);

if (xhr.status == 200) {
try {
var response = JSON.parse(xhr.responseText);
}
catch (ex) {
apop(st.busy.handshake, t);
st.todo.handshake.unshift(t);
t.cooldown = Date.now() + 5000 + Math.floor(Math.random() * 3000);
return toast.err(0, 'Handshake error; will retry...\n\n' + L.badreply + ':\n\n' + unpre(xhr.responseText));
}

t.t_handshake = Date.now();
if (keepalive) {
apop(st.busy.handshake, t);
tasker();
return;
}

if (toast.tag === t)
toast.ok(5, L.u_fixed);

var response = JSON.parse(xhr.responseText);
if (!response.name) {
var msg = '',
smsg = '';
Expand Down
9 changes: 7 additions & 2 deletions copyparty/web/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1417,9 +1417,12 @@ function lf2br(txt) {
}


function unpre(txt) {
function hunpre(txt) {
return ('' + txt).replace(/^<pre>/, '');
}
function unpre(txt) {
return esc(hunpre(txt));
}


var toast = (function () {
Expand Down Expand Up @@ -2001,7 +2004,9 @@ function xhrchk(xhr, prefix, e404, lvl, tag) {
is_cf = /[Cc]loud[f]lare|>Just a mo[m]ent|#cf-b[u]bbles|Chec[k]ing your br[o]wser|\/chall[e]nge-platform|"chall[e]nge-error|nable Ja[v]aScript and cook/.test(errtxt);

if (errtxt.startsWith('<pre>'))
suf = '\n\nerror-details: «' + errtxt.slice(5).split('\n')[0].trim() + '»';
suf = '\n\nerror-details: «' + unpre(errtxt).split('\n')[0].trim() + '»';
else
errtxt = esc(errtxt).slice(0, 32768);

if (xhr.status == 403 && !is_cf)
return toast.err(0, prefix + (L && L.xhr403 || "403: access denied\n\ntry pressing F5, maybe you got logged out") + suf, tag);
Expand Down

0 comments on commit 7741870

Please sign in to comment.