[http] Create slim proxy for http requests.

This will send a normal request and if we receive an 401 status, we send another request with withCredentials = true
This commit is contained in:
Joxit 2016-06-22 22:14:48 +02:00
parent e50843009f
commit 09e1eb8cf3
4 changed files with 64 additions and 4 deletions

View file

@ -41,7 +41,7 @@
registryUI.catalog.instance = this;
this.mixin('rg.router');
registryUI.catalog.display = function () {
var oReq = new XMLHttpRequest();
var oReq = new Http();
registryUI.catalog.createSnackbar = function (msg) {
var snackbar = document.querySelector('#error-snackbar');
registryUI.catalog.error = msg;
@ -72,7 +72,6 @@
registryUI.catalog.instance.update();
});
oReq.open('GET', registryUI.url() + '/v2/_catalog');
oReq.withCredentials = true;
oReq.send();
};
this.on('updated', function () {

61
http.js Normal file
View file

@ -0,0 +1,61 @@
/*
* Copyright (C) 2016 Jones Magloire @Joxit
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
function Http() {
this.oReq = new XMLHttpRequest();
this._events = {};
}
Http.prototype.addEventListener = function(e, f) {
this._events[e] = f;
switch (e) {
case 'loadend':
{
this.oReq.addEventListener('loadend', function() {
if (this.status == 401) {
var req = new XMLHttpRequest();
for (key in this.http._events) {
req.addEventListener(key, this.http._events[key]);
}
req.withCredentials = true;
req.open(this.http._method, this.http._url);
req.send();
} else {
f.bind(this.oReq);
}
});
break;
}
default:
{
this.oReq.addEventListener(e, function() {
f.bind(this.oReq);
});
break;
}
}
};
Http.prototype.open = function(m, u) {
this._method = m;
this._url = u;
this.oReq.open(m, u);
};
Http.prototype.send = function() {
this.oReq.http = this;
this.oReq.send();
};

View file

@ -67,6 +67,7 @@
<script src="node_modules/dialog-polyfill/dialog-polyfill.js"></script>
<script src="node_modules/material-design-lite/dist/material.min.js"></script>
<script src="script.js"></script>
<script src="http.js"></script>
</body>
</html>

View file

@ -53,7 +53,7 @@
name = rg.router.current.params.repository + (rg.router.current.params.image
? '/' + rg.router.current.params.image
: '');
var oReq = new XMLHttpRequest();
var oReq = new Http();
registryUI.taglist.name = name;
registryUI.taglist.createSnackbar = function (msg) {
var snackbar = document.querySelector('#error-snackbar');
@ -85,7 +85,6 @@
registryUI.taglist.instance.update();
});
oReq.open('GET', registryUI.url() + '/v2/' + name + '/tags/list');
oReq.withCredentials = true;
oReq.send();
}
};