Added ability to use .json to get JSON from self URL. Also added some logging.

This commit is contained in:
Nathan Byrd 2023-01-06 13:15:29 -06:00
parent 9f33c8b21d
commit 344d4716ce
2 changed files with 55 additions and 33 deletions

View File

@ -5,7 +5,7 @@
const Log = require('../../logger.js').log;
const ServerModule = require('../../server_module.js').ServerModule;
const Config = require('../../config.js').get;
const { Errors, ErrorReasons } = require('../../enig_error.js');
const { Errors } = require('../../enig_error.js');
const { loadModulesForCategory, moduleCategories } = require('../../module_util');
const WebHandlerModule = require('../../web_handler_module');
@ -49,18 +49,18 @@ class Route {
isValid() {
return (
(this.pathRegExp instanceof RegExp &&
-1 !==
[
'GET',
'HEAD',
'POST',
'PUT',
'DELETE',
'CONNECT',
'OPTIONS',
'TRACE',
].indexOf(this.method)) ||
!_.isFunction(this.handler)
-1 !==
[
'GET',
'HEAD',
'POST',
'PUT',
'DELETE',
'CONNECT',
'OPTIONS',
'TRACE',
].indexOf(this.method)) ||
!_.isFunction(this.handler)
);
}
@ -96,13 +96,13 @@ exports.getModule = class WebServerModule extends ServerModule {
}
buildUrl(pathAndQuery) {
//
// Create a URL such as
// https://l33t.codes:44512/ + |pathAndQuery|
//
// Prefer HTTPS over HTTP. Be explicit about the port
// only if non-standard. Allow users to override full prefix in config.
//
//
// Create a URL such as
// https://l33t.codes:44512/ + |pathAndQuery|
//
// Prefer HTTPS over HTTP. Be explicit about the port
// only if non-standard. Allow users to override full prefix in config.
//
const config = Config();
if (_.isString(config.contentServers.web.overrideUrlPrefix)) {
return `${config.contentServers.web.overrideUrlPrefix}${pathAndQuery}`;
@ -113,15 +113,15 @@ exports.getModule = class WebServerModule extends ServerModule {
if (config.contentServers.web.https.enabled) {
schema = 'https://';
port =
443 === config.contentServers.web.https.port
? ''
: `:${config.contentServers.web.https.port}`;
443 === config.contentServers.web.https.port
? ''
: `:${config.contentServers.web.https.port}`;
} else {
schema = 'http://';
port =
80 === config.contentServers.web.http.port
? ''
: `:${config.contentServers.web.http.port}`;
80 === config.contentServers.web.http.port
? ''
: `:${config.contentServers.web.http.port}`;
}
return `${schema}${config.contentServers.web.domain}${port}${pathAndQuery}`;
@ -168,9 +168,12 @@ exports.getModule = class WebServerModule extends ServerModule {
try {
const normalizedName = _.camelCase(module.moduleInfo.name);
if (!WebHandlerModule.isEnabled(normalizedName)) {
Log.info({ moduleName: normalizedName }, 'Skipping web handler module - not enabled.');
return nextModule(null);
}
Log.info({ moduleName: normalizedName }, 'Initializing web handler module.');
moduleInst.init(err => {
return nextModule(err);
});
@ -326,8 +329,8 @@ exports.getModule = class WebServerModule extends ServerModule {
const headers = {
'Content-Type':
mimeTypes.contentType(paths.basename(filePath)) ||
mimeTypes.contentType('.bin'),
mimeTypes.contentType(paths.basename(filePath)) ||
mimeTypes.contentType('.bin'),
'Content-Length': stats.size,
};
@ -359,8 +362,8 @@ exports.getModule = class WebServerModule extends ServerModule {
const headers = {
'Content-Type':
mimeTypes.contentType(paths.basename(filePath)) ||
mimeTypes.contentType('.bin'),
mimeTypes.contentType(paths.basename(filePath)) ||
mimeTypes.contentType('.bin'),
'Content-Length': stats.size,
};

View File

@ -10,6 +10,7 @@ const {
const UserProps = require('../../../user_property');
const { Errors } = require('../../../enig_error');
const Config = require('../../../config').get;
const Log = require('../../../logger').log;
// deps
const _ = require('lodash');
@ -31,6 +32,7 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule {
if (!this.webServer) {
return cb(Errors.UnexpectedState('Cannot access web server!'));
}
Log.debug('Adding route for ActivityPub');
this.webServer.addRoute({
method: 'GET',
@ -42,20 +44,37 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule {
}
_selfUrlRequestHandler(req, resp) {
Log.debug({ url: req.url }, 'Received request for self url');
const url = new URL(req.url, `https://${req.headers.host}`);
const accountName = url.pathname.substring(url.pathname.lastIndexOf('/') + 1);
let accountName = url.pathname.substring(url.pathname.lastIndexOf('/') + 1);
let sendActor = false;
// Like Mastodon, if .json is appended onto URL then return the JSON content
if (accountName.endsWith('.json')) {
sendActor = true;
accountName = accountName.slice(0, -5);
}
Log.debug({ accountName: accountName }, 'Retrieving self url for account.');
userFromAccount(accountName, (err, user) => {
if (err) {
Log.info({ accountName: accountName }, 'Unable to find user from account retrieving self url.');
return this._notFound(resp);
}
const accept = req.headers['accept'] || '*/*';
if (accept === 'application/activity+json') {
return this._selfAsActorHandler(user, req, resp);
sendActor = true;
}
return this._standardSelfHandler(user, req, resp);
Log.debug({ sendActor: sendActor }, 'Sending actor JSON');
if (sendActor) {
return this._selfAsActorHandler(user, req, resp);
}
else {
return this._standardSelfHandler(user, req, resp);
}
});
}