diff --git a/lib/armadietto.js b/lib/armadietto.js index 39fc7204..8e28baa4 100644 --- a/lib/armadietto.js +++ b/lib/armadietto.js @@ -139,63 +139,62 @@ class Armadietto { async dispatch (req, res) { const method = req.method.toUpperCase(); - const uri = new url.URL(req.url); + let pathname = new url.URL(req.url, `http://${this._options.http.host}`).pathname; const startBasePath = new RegExp('^/?' + this._basePath + '/?'); let match; req.secure = this.isSecureRequest(req); - - if (!uri.pathname.match(startBasePath)) { + if (!pathname.match(startBasePath)) { res.writeHead(302, { Location: this._basePath }); res.end(); return logRequest(req, '-', 302, 0, '-> ' + this._basePath); } - uri.pathname = uri.pathname.replace(startBasePath, ''); + pathname = pathname.replace(startBasePath, ''); - if (/(^|\/)\.\.(\/|$)/.test(uri.pathname)) { + if (/(^|\/)\.\.(\/|$)/.test(pathname)) { res.writeHead(400, { 'Access-Control-Allow-Origin': req.headers.origin || '*' }); res.end(); return logRequest(req, '-', 400, 0, 'no relative paths'); } if (method === 'GET') { - match = uri.pathname.match(/^assets\/([^/]+)$/); + match = pathname.match(/^assets\/([^/]+)$/); if (match) { return new Assets(this, req, res).serve(match[1]); } - if (uri.pathname === '') { + if (pathname === '') { return new Assets(this, req, res).renderHTML(200, 'index.html', { title: 'Welcome' }); } - match = uri.pathname.match(/^\.well-known\/(host-meta|webfinger)(\.[a-z]+)?$/); + match = pathname.match(/^\.well-known\/(host-meta|webfinger)(\.[a-z]+)?$/); if (match) { return new WebFinger(this, req, res).hostMeta(match[1], match[2]); } - match = uri.pathname.match(/^webfinger\/(jrd|xrd)$/); + match = pathname.match(/^webfinger\/(jrd|xrd)$/); if (match) { return new WebFinger(this, req, res).account(match[1]); } - match = uri.pathname.match(/^oauth\/(.*)$/); + match = pathname.match(/^oauth\/(.*)$/); if (match) { return new OAuth(this, req, res).showForm(decodeURIComponent(match[1])); } } - if (method === 'POST' && uri.pathname === 'oauth') { + if (method === 'POST' && pathname === 'oauth') { return new OAuth(this, req, res).authenticate(); } - if (uri.pathname === 'signup') { + if (pathname === 'signup') { const users = new Users(this, req, res); if (method === 'GET') return users.showForm(); if (method === 'POST') return users.register(); } - match = uri.pathname.match(/^storage\/([^/]+)(.*)$/); + match = pathname.match(/^storage\/([^/]+)(.*)$/); if (match) { const username = decodeURIComponent(match[1]).split('@')[0]; const path = match[2]; @@ -218,7 +217,7 @@ class Armadietto { getLogger().error('Storage Error:', e); } } - new Assets(this, req, res).errorPage(404, uri.pathname + ' Not found'); + new Assets(this, req, res).errorPage(404, pathname + ' Not found'); } isSecureRequest (r) {