From 09e1eb8cf31961798601cfff1ba45f2a3c2ece6e Mon Sep 17 00:00:00 2001 From: Joxit Date: Wed, 22 Jun 2016 22:14:48 +0200 Subject: [PATCH] [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 --- catalog.tag | 3 +-- http.js | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ index.html | 1 + taglist.tag | 3 +-- 4 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 http.js diff --git a/catalog.tag b/catalog.tag index fd6d686..a5c1a6f 100644 --- a/catalog.tag +++ b/catalog.tag @@ -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 () { diff --git a/http.js b/http.js new file mode 100644 index 0000000..edc3a69 --- /dev/null +++ b/http.js @@ -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 . + */ +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(); +}; diff --git a/index.html b/index.html index 3c24225..5388065 100644 --- a/index.html +++ b/index.html @@ -67,6 +67,7 @@ + diff --git a/taglist.tag b/taglist.tag index 15b3ff6..cda75cb 100644 --- a/taglist.tag +++ b/taglist.tag @@ -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(); } };