Skip to content

Commit

Permalink
set pathname as mutable string
Browse files Browse the repository at this point in the history
  • Loading branch information
silverbucket committed Dec 29, 2023
1 parent a077399 commit 9a1dcd9
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions lib/armadietto.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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) {
Expand Down

0 comments on commit 9a1dcd9

Please sign in to comment.