Added ability to use .json to get JSON from self URL. Also added some logging.
This commit is contained in:
parent
9f33c8b21d
commit
344d4716ce
|
@ -5,7 +5,7 @@
|
||||||
const Log = require('../../logger.js').log;
|
const Log = require('../../logger.js').log;
|
||||||
const ServerModule = require('../../server_module.js').ServerModule;
|
const ServerModule = require('../../server_module.js').ServerModule;
|
||||||
const Config = require('../../config.js').get;
|
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 { loadModulesForCategory, moduleCategories } = require('../../module_util');
|
||||||
const WebHandlerModule = require('../../web_handler_module');
|
const WebHandlerModule = require('../../web_handler_module');
|
||||||
|
|
||||||
|
@ -49,18 +49,18 @@ class Route {
|
||||||
isValid() {
|
isValid() {
|
||||||
return (
|
return (
|
||||||
(this.pathRegExp instanceof RegExp &&
|
(this.pathRegExp instanceof RegExp &&
|
||||||
-1 !==
|
-1 !==
|
||||||
[
|
[
|
||||||
'GET',
|
'GET',
|
||||||
'HEAD',
|
'HEAD',
|
||||||
'POST',
|
'POST',
|
||||||
'PUT',
|
'PUT',
|
||||||
'DELETE',
|
'DELETE',
|
||||||
'CONNECT',
|
'CONNECT',
|
||||||
'OPTIONS',
|
'OPTIONS',
|
||||||
'TRACE',
|
'TRACE',
|
||||||
].indexOf(this.method)) ||
|
].indexOf(this.method)) ||
|
||||||
!_.isFunction(this.handler)
|
!_.isFunction(this.handler)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,13 +96,13 @@ exports.getModule = class WebServerModule extends ServerModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
buildUrl(pathAndQuery) {
|
buildUrl(pathAndQuery) {
|
||||||
//
|
//
|
||||||
// Create a URL such as
|
// Create a URL such as
|
||||||
// https://l33t.codes:44512/ + |pathAndQuery|
|
// https://l33t.codes:44512/ + |pathAndQuery|
|
||||||
//
|
//
|
||||||
// Prefer HTTPS over HTTP. Be explicit about the port
|
// Prefer HTTPS over HTTP. Be explicit about the port
|
||||||
// only if non-standard. Allow users to override full prefix in config.
|
// only if non-standard. Allow users to override full prefix in config.
|
||||||
//
|
//
|
||||||
const config = Config();
|
const config = Config();
|
||||||
if (_.isString(config.contentServers.web.overrideUrlPrefix)) {
|
if (_.isString(config.contentServers.web.overrideUrlPrefix)) {
|
||||||
return `${config.contentServers.web.overrideUrlPrefix}${pathAndQuery}`;
|
return `${config.contentServers.web.overrideUrlPrefix}${pathAndQuery}`;
|
||||||
|
@ -113,15 +113,15 @@ exports.getModule = class WebServerModule extends ServerModule {
|
||||||
if (config.contentServers.web.https.enabled) {
|
if (config.contentServers.web.https.enabled) {
|
||||||
schema = 'https://';
|
schema = 'https://';
|
||||||
port =
|
port =
|
||||||
443 === config.contentServers.web.https.port
|
443 === config.contentServers.web.https.port
|
||||||
? ''
|
? ''
|
||||||
: `:${config.contentServers.web.https.port}`;
|
: `:${config.contentServers.web.https.port}`;
|
||||||
} else {
|
} else {
|
||||||
schema = 'http://';
|
schema = 'http://';
|
||||||
port =
|
port =
|
||||||
80 === config.contentServers.web.http.port
|
80 === config.contentServers.web.http.port
|
||||||
? ''
|
? ''
|
||||||
: `:${config.contentServers.web.http.port}`;
|
: `:${config.contentServers.web.http.port}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return `${schema}${config.contentServers.web.domain}${port}${pathAndQuery}`;
|
return `${schema}${config.contentServers.web.domain}${port}${pathAndQuery}`;
|
||||||
|
@ -168,9 +168,12 @@ exports.getModule = class WebServerModule extends ServerModule {
|
||||||
try {
|
try {
|
||||||
const normalizedName = _.camelCase(module.moduleInfo.name);
|
const normalizedName = _.camelCase(module.moduleInfo.name);
|
||||||
if (!WebHandlerModule.isEnabled(normalizedName)) {
|
if (!WebHandlerModule.isEnabled(normalizedName)) {
|
||||||
|
Log.info({ moduleName: normalizedName }, 'Skipping web handler module - not enabled.');
|
||||||
return nextModule(null);
|
return nextModule(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Log.info({ moduleName: normalizedName }, 'Initializing web handler module.');
|
||||||
|
|
||||||
moduleInst.init(err => {
|
moduleInst.init(err => {
|
||||||
return nextModule(err);
|
return nextModule(err);
|
||||||
});
|
});
|
||||||
|
@ -326,8 +329,8 @@ exports.getModule = class WebServerModule extends ServerModule {
|
||||||
|
|
||||||
const headers = {
|
const headers = {
|
||||||
'Content-Type':
|
'Content-Type':
|
||||||
mimeTypes.contentType(paths.basename(filePath)) ||
|
mimeTypes.contentType(paths.basename(filePath)) ||
|
||||||
mimeTypes.contentType('.bin'),
|
mimeTypes.contentType('.bin'),
|
||||||
'Content-Length': stats.size,
|
'Content-Length': stats.size,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -359,8 +362,8 @@ exports.getModule = class WebServerModule extends ServerModule {
|
||||||
|
|
||||||
const headers = {
|
const headers = {
|
||||||
'Content-Type':
|
'Content-Type':
|
||||||
mimeTypes.contentType(paths.basename(filePath)) ||
|
mimeTypes.contentType(paths.basename(filePath)) ||
|
||||||
mimeTypes.contentType('.bin'),
|
mimeTypes.contentType('.bin'),
|
||||||
'Content-Length': stats.size,
|
'Content-Length': stats.size,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ const {
|
||||||
const UserProps = require('../../../user_property');
|
const UserProps = require('../../../user_property');
|
||||||
const { Errors } = require('../../../enig_error');
|
const { Errors } = require('../../../enig_error');
|
||||||
const Config = require('../../../config').get;
|
const Config = require('../../../config').get;
|
||||||
|
const Log = require('../../../logger').log;
|
||||||
|
|
||||||
// deps
|
// deps
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
@ -31,6 +32,7 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule {
|
||||||
if (!this.webServer) {
|
if (!this.webServer) {
|
||||||
return cb(Errors.UnexpectedState('Cannot access web server!'));
|
return cb(Errors.UnexpectedState('Cannot access web server!'));
|
||||||
}
|
}
|
||||||
|
Log.debug('Adding route for ActivityPub');
|
||||||
|
|
||||||
this.webServer.addRoute({
|
this.webServer.addRoute({
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
|
@ -42,20 +44,37 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
_selfUrlRequestHandler(req, resp) {
|
_selfUrlRequestHandler(req, resp) {
|
||||||
|
Log.debug({ url: req.url }, 'Received request for self url');
|
||||||
const url = new URL(req.url, `https://${req.headers.host}`);
|
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) => {
|
userFromAccount(accountName, (err, user) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
Log.info({ accountName: accountName }, 'Unable to find user from account retrieving self url.');
|
||||||
return this._notFound(resp);
|
return this._notFound(resp);
|
||||||
}
|
}
|
||||||
|
|
||||||
const accept = req.headers['accept'] || '*/*';
|
const accept = req.headers['accept'] || '*/*';
|
||||||
if (accept === 'application/activity+json') {
|
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);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue