Skip to content

Commit

Permalink
fix: more HTTP error detection/status reporting
Browse files Browse the repository at this point in the history
Signed-off-by: Josh <[email protected]>
  • Loading branch information
joshtrichards authored Nov 30, 2024
1 parent e73aa39 commit fc8a38c
Showing 1 changed file with 76 additions and 73 deletions.
149 changes: 76 additions & 73 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -1828,14 +1828,23 @@ function escapeHTML(s) {
return s.toString().split('&').join('&amp;').split('<').join('&lt;').split('>').join('&gt;').split('"').join('&quot;').split('\'').join('&#039;');
}

function errorResponse(response) {
var text = escapeHTML(response['response']);
if (response['detailedResponseText'] !== '') { // Include additional details (raw response) when available
text += '<br><details><summary>Show detailed response</summary><pre><code>' +
escapeHTML(response['detailedResponseText']) + '</code></pre></details>';
}
return text;
}

var done = false;
var started = false;
var updaterStepStart = parseInt(document.getElementById('updater-step-start').value);
var elementId =false;
function addStepText(id, text) {
var el = document.getElementById(id);
var output = el.getElementsByClassName('output')[0];
if(typeof text === 'object') {
if (typeof text === 'object') {
text = JSON.stringify(text);
}
output.innerHTML = output.innerHTML + text;
Expand Down Expand Up @@ -1890,32 +1899,47 @@ function waitingStep(id) {

function performStep(number, callback) {
started = true;
var httpRequest = new XMLHttpRequest();
httpRequest.open('POST', window.location.href);
const url = window.location.href;
const httpRequest = new XMLHttpRequest();
httpRequest.open('POST', url);
httpRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
httpRequest.setRequestHeader('X-Updater-Auth', document.getElementById('updater-access-key').value);
httpRequest.onerror = function () {
const response = {
proceed: false,
response: 'Error accessing ' + url + ' (' + (httpRequest.statusText || 'Network error') + ')',
detailedResponseText: httpRequest.responseText, // Probably empty but won't hurt
};
callback(response);
};
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState != 4) { // 4 - request done
if (httpRequest.readyState === 4) { // Request done
if (httpRequest.status === 200) { // Ensure step was successful
try { // Ensure response contains a valid JSON object
const data = JSON.parse(httpRequest.ResponseText);
if (typeof data.response === 'object') {
callback(data); // Valid
}
} catch (error) { // Invalid JSON
// probably SyntaxError; handled by next line
}
const response = { // SyntaxError / unexpected non-object response
proceed: false,
response: 'Syntax error or unexpected response from server. See details for clues.',
detailedResponseText: httpRequest.responseText,
};
callback(response);
} else { // Request not successful (i.e. got something other than HTTP 200 OK)
const response = {
proceed: false,
response: 'Invalid HTTP code received from server ( ' + httpRequest.status + ' ' + httpRequest.statusText + '.',
detailedResponseText: httpRequest.responseText,
};
callback(response);
}
} else { // Request not done
return;
}

if (httpRequest.status != 200) {
// failure
}

if(httpRequest.responseText.substr(0,1) !== '{') {
// it seems that this is not a JSON object
var response = {
processed: false,
response: 'Parsing response failed.',
detailedResponseText: httpRequest.responseText,
};
callback(response);
} else {
// parse JSON
callback(JSON.parse(httpRequest.responseText));
}

};
httpRequest.send("step="+number);
}
Expand All @@ -1927,7 +1951,7 @@ function performStep(number, callback) {
performStep(1, performStepCallbacks[1]);
},
1: function(response) {
if(response.proceed === true) {
if (response.proceed === true) {
successStep('step-check-files');
currentStep('step-check-permissions');
performStep(2, performStepCallbacks[2]);
Expand All @@ -1936,9 +1960,7 @@ function performStep(number, callback) {

var text = '';
if (typeof response['response'] === 'string') {
text = escapeHTML(response['response']);
text += '<br><details><summary>Show detailed response</summary><pre><code>' +
escapeHTML(response['detailedResponseText']) + '</code></pre></details>';
text = errorResponse(response);
} else {
text = 'Unknown files detected within the installation folder. This can be fixed by manually removing (or moving) these files. The following extra files have been found:<ul>';
response['response'].forEach(function(file) {
Expand All @@ -1950,7 +1972,7 @@ function performStep(number, callback) {
}
},
2: function(response) {
if(response.proceed === true) {
if (response.proceed === true) {
successStep('step-check-permissions');
currentStep('step-backup');
performStep(3, performStepCallbacks[3]);
Expand All @@ -1959,9 +1981,7 @@ function performStep(number, callback) {

var text = '';
if (typeof response['response'] === 'string') {
text = escapeHTML(response['response']);
text += '<br><details><summary>Show detailed response</summary><pre><code>' +
escapeHTML(response['detailedResponseText']) + '</code></pre></details>';
text = errorResponse(response);
} else {
text = 'The following places can not be written to:<ul>';
response['response'].forEach(function(file) {
Expand All @@ -1980,10 +2000,8 @@ function performStep(number, callback) {
} else {
errorStep('step-backup', 3);

if(response.response) {
var text = escapeHTML(response.response);
text += '<br><details><summary>Show detailed response</summary><pre><code>' +
escapeHTML(response.detailedResponseText) + '</code></pre></details>';
if (response.response) {
var text = errorResponse(response);
addStepText('step-backup', text);
}
}
Expand All @@ -1996,10 +2014,8 @@ function performStep(number, callback) {
} else {
errorStep('step-download', 4);

if(response.response) {
var text = escapeHTML(response.response);
text += '<br><details><summary>Show detailed response</summary><pre><code>' +
escapeHTML(response.detailedResponseText) + '</code></pre></details>';
if (response.response) {
var text = errorResponse(response);
addStepText('step-download', text);
}
}
Expand All @@ -2012,10 +2028,8 @@ function performStep(number, callback) {
} else {
errorStep('step-verify-integrity', 5);

if(response.response) {
var text = escapeHTML(response.response);
text += '<br><details><summary>Show detailed response</summary><pre><code>' +
escapeHTML(response.detailedResponseText) + '</code></pre></details>';
if (response.response) {
var text = errorResponse(response);
addStepText('step-verify-integrity', text);
}
}
Expand All @@ -2028,10 +2042,8 @@ function performStep(number, callback) {
} else {
errorStep('step-extract', 6);

if(response.response) {
var text = escapeHTML(response.response);
text += '<br><details><summary>Show detailed response</summary><pre><code>' +
escapeHTML(response.detailedResponseText) + '</code></pre></details>';
if (response.response) {
var text = errorResponse(response);
addStepText('step-extract', text);
}
}
Expand All @@ -2044,10 +2056,8 @@ function performStep(number, callback) {
} else {
errorStep('step-enable-maintenance', 7);

if(response.response) {
var text = escapeHTML(response.response);
text += '<br><details><summary>Show detailed response</summary><pre><code>' +
escapeHTML(response.detailedResponseText) + '</code></pre></details>';
if (response.response) {
var text = errorResponse(response);
addStepText('step-enable-maintenance', text);
}
}
Expand All @@ -2060,10 +2070,8 @@ function performStep(number, callback) {
} else {
errorStep('step-entrypoints', 8);

if(response.response) {
var text = escapeHTML(response.response);
text += '<br><details><summary>Show detailed response</summary><pre><code>' +
escapeHTML(response.detailedResponseText) + '</code></pre></details>';
if (response.response) {
var text = errorResponse(response);
addStepText('step-entrypoints', text);
}
}
Expand All @@ -2076,10 +2084,8 @@ function performStep(number, callback) {
} else {
errorStep('step-delete', 9);

if(response.response) {
var text = escapeHTML(response.response);
text += '<br><details><summary>Show detailed response</summary><pre><code>' +
escapeHTML(response.detailedResponseText) + '</code></pre></details>';
if (response.response) {
var text = errorResponse(response);
addStepText('step-delete', text);
}
}
Expand All @@ -2096,10 +2102,8 @@ function performStep(number, callback) {
} else {
errorStep('step-move', 10);

if(response.response) {
var text = escapeHTML(response.response);
text += '<br><details><summary>Show detailed response</summary><pre><code>' +
escapeHTML(response.detailedResponseText) + '</code></pre></details>';
if (response.response) {
var text = errorResponse(response);
addStepText('step-move', text);
}
}
Expand All @@ -2112,10 +2116,8 @@ function performStep(number, callback) {
} else {
errorStep('step-maintenance-mode', 11);

if(response.response) {
var text = escapeHTML(response.response);
text += '<br><details><summary>Show detailed response</summary><pre><code>' +
escapeHTML(response.detailedResponseText) + '</code></pre></details>';
if (response.response) {
var text = errorResponse(response);
addStepText('step-maintenance-mode', text);
}
}
Expand All @@ -2135,10 +2137,11 @@ function performStep(number, callback) {
window.location.href = nextcloudUrl;
} else {
errorStep('step-done', 12);
var text = escapeHTML(response.response);
text += '<br><details><summary>Show detailed response</summary><pre><code>' +
escapeHTML(response.detailedResponseText) + '</code></pre></details>';
addStepText('step-done', text);

if (response.response) {
var text = errorResponse(response);
addStepText('step-done', text);
}
}
},
};
Expand Down Expand Up @@ -2178,20 +2181,20 @@ function askForMaintenance() {
performStep(11, performStepCallbacks[11]);
}

if(document.getElementById('startUpdateButton')) {
if (document.getElementById('startUpdateButton')) {
document.getElementById('startUpdateButton').onclick = function (e) {
e.preventDefault();
this.classList.add('hidden');
startUpdate();
};
}
if(document.getElementById('retryUpdateButton')) {
if (document.getElementById('retryUpdateButton')) {
document.getElementById('retryUpdateButton').onclick = function (e) {
e.preventDefault();
retryUpdate();
};
}
if(document.getElementById('maintenance-disable')) {
if (document.getElementById('maintenance-disable')) {
document.getElementById('maintenance-disable').onclick = function (e) {
e.preventDefault();
askForMaintenance();
Expand Down

0 comments on commit fc8a38c

Please sign in to comment.