From dfcc16691843d35cb754f6b4618f2cd71dc4d78e Mon Sep 17 00:00:00 2001 From: Dennis Wilson Date: Tue, 21 Jul 2015 22:35:51 +0200 Subject: [PATCH 1/3] fix(core): prevent endless reload on cache requests --- gui/scripts/syncthing/app.js | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/gui/scripts/syncthing/app.js b/gui/scripts/syncthing/app.js index b85da427..bcb12288 100644 --- a/gui/scripts/syncthing/app.js +++ b/gui/scripts/syncthing/app.js @@ -22,20 +22,26 @@ syncthing.config(function ($httpProvider, $translateProvider, LocaleServiceProvi $httpProvider.interceptors.push(function () { return { response: function (response) { - var responseVersion = response.headers()['x-syncthing-version']; - if (!guiVersion) { - guiVersion = responseVersion; - } else if (guiVersion != responseVersion) { - document.location.reload(true); - } - if (!deviceId) { - deviceId = response.headers()['x-syncthing-id']; - if (deviceId) { - var deviceIdShort = deviceId.substring(0, 5); - $httpProvider.defaults.xsrfHeaderName = 'X-CSRF-Token-' + deviceIdShort; - $httpProvider.defaults.xsrfCookieName = 'CSRF-Token-' + deviceIdShort; + var headers = response.headers(); + + // angular template cache sends no headers + if(Object.keys(headers).length > 0) { + var responseVersion = headers['x-syncthing-version']; + if (!guiVersion) { + guiVersion = responseVersion; + } else if (guiVersion != responseVersion) { + document.location.reload(true); + } + if (!deviceId) { + deviceId = headers['x-syncthing-id']; + if (deviceId) { + var deviceIdShort = deviceId.substring(0, 5); + $httpProvider.defaults.xsrfHeaderName = 'X-CSRF-Token-' + deviceIdShort; + $httpProvider.defaults.xsrfCookieName = 'CSRF-Token-' + deviceIdShort; + } } } + return response; } }; From e34be16237b19087dfd7afcc94af0d3c80c44e4b Mon Sep 17 00:00:00 2001 From: Dennis Wilson Date: Tue, 21 Jul 2015 23:41:10 +0200 Subject: [PATCH 2/3] style(core): add simple flow, hoisting, stacktrace infos --- gui/scripts/syncthing/app.js | 38 +++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/gui/scripts/syncthing/app.js b/gui/scripts/syncthing/app.js index bcb12288..b689000d 100644 --- a/gui/scripts/syncthing/app.js +++ b/gui/scripts/syncthing/app.js @@ -19,26 +19,32 @@ var guiVersion = null; var deviceId = null; syncthing.config(function ($httpProvider, $translateProvider, LocaleServiceProvider) { - $httpProvider.interceptors.push(function () { + $httpProvider.interceptors.push(function xHeadersResponseInterceptor() { return { - response: function (response) { + response: function onResponse(response) { var headers = response.headers(); + var responseVersion; + var deviceIdShort; // angular template cache sends no headers - if(Object.keys(headers).length > 0) { - var responseVersion = headers['x-syncthing-version']; - if (!guiVersion) { - guiVersion = responseVersion; - } else if (guiVersion != responseVersion) { - document.location.reload(true); - } - if (!deviceId) { - deviceId = headers['x-syncthing-id']; - if (deviceId) { - var deviceIdShort = deviceId.substring(0, 5); - $httpProvider.defaults.xsrfHeaderName = 'X-CSRF-Token-' + deviceIdShort; - $httpProvider.defaults.xsrfCookieName = 'CSRF-Token-' + deviceIdShort; - } + if(Object.keys(headers).length === 0) { + return response; + } + + responseVersion = headers['x-syncthing-version']; + + if (!guiVersion) { + guiVersion = responseVersion; + } else if (guiVersion != responseVersion) { + document.location.reload(true); + } + + if (!deviceId) { + deviceId = headers['x-syncthing-id']; + if (deviceId) { + deviceIdShort = deviceId.substring(0, 5); + $httpProvider.defaults.xsrfHeaderName = 'X-CSRF-Token-' + deviceIdShort; + $httpProvider.defaults.xsrfCookieName = 'CSRF-Token-' + deviceIdShort; } } From a4a46f480da6ea58c71876552ac58b58a90c9aaa Mon Sep 17 00:00:00 2001 From: Dennis Wilson Date: Tue, 21 Jul 2015 23:47:35 +0200 Subject: [PATCH 3/3] refactor(core): eleminate global state of guiVersion and deviceId --- gui/scripts/syncthing/app.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/gui/scripts/syncthing/app.js b/gui/scripts/syncthing/app.js index b689000d..9d260efc 100644 --- a/gui/scripts/syncthing/app.js +++ b/gui/scripts/syncthing/app.js @@ -15,11 +15,12 @@ var syncthing = angular.module('syncthing', [ ]); var urlbase = 'rest'; -var guiVersion = null; -var deviceId = null; syncthing.config(function ($httpProvider, $translateProvider, LocaleServiceProvider) { $httpProvider.interceptors.push(function xHeadersResponseInterceptor() { + var guiVersion = null; + var deviceId = null; + return { response: function onResponse(response) { var headers = response.headers();