mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-11-25 03:34:50 +00:00
* Introduce a new named parameter scheme for commands * Move the SimpleServer class into it's own module * Deprecate the --server command because of the unwieldy syntax * Add a new --listen command using the new syntax For example: tiddlywiki mywiki --listen host:0.0.0.0 port:8090
188 lines
5.3 KiB
JavaScript
188 lines
5.3 KiB
JavaScript
/*\
|
|
title: $:/core/modules/server.js
|
|
type: application/javascript
|
|
module-type: library
|
|
|
|
Serve tiddlers over http
|
|
|
|
\*/
|
|
(function(){
|
|
|
|
/*jslint node: true, browser: true */
|
|
/*global $tw: false */
|
|
"use strict";
|
|
|
|
if($tw.node) {
|
|
var util = require("util"),
|
|
fs = require("fs"),
|
|
url = require("url"),
|
|
path = require("path"),
|
|
http = require("http");
|
|
}
|
|
|
|
/*
|
|
A simple HTTP server with regexp-based routes
|
|
options: variables - optional hashmap of variables to set (a misnomer - they are really constant parameters)
|
|
routes - optional array of routes to use
|
|
wiki - reference to wiki object
|
|
*/
|
|
function Server(options) {
|
|
var self = this;
|
|
this.routes = options.routes || [];
|
|
this.wiki = options.wiki;
|
|
this.variables = $tw.utils.extend({},this.defaultVariables,options.variables);
|
|
// Add route handlers
|
|
$tw.modules.forEachModuleOfType("serverroute", function(title,routeDefinition) {
|
|
// console.log("Loading server route " + title);
|
|
self.addRoute(routeDefinition);
|
|
});
|
|
}
|
|
|
|
Server.prototype.defaultVariables = {
|
|
port: "8080",
|
|
host: "127.0.0.1",
|
|
rootTiddler: "$:/core/save/all",
|
|
renderType: "text/plain",
|
|
serveType: "text/html",
|
|
debugLevel: "none"
|
|
};
|
|
|
|
Server.prototype.set = function(obj) {
|
|
var self = this;
|
|
$tw.utils.each(obj,function(value,name) {
|
|
self.variables[name] = value;
|
|
});
|
|
};
|
|
|
|
Server.prototype.get = function(name) {
|
|
return this.variables[name];
|
|
};
|
|
|
|
Server.prototype.addRoute = function(route) {
|
|
this.routes.push(route);
|
|
};
|
|
|
|
Server.prototype.findMatchingRoute = function(request,state) {
|
|
var pathprefix = this.get("pathprefix") || "";
|
|
for(var t=0; t<this.routes.length; t++) {
|
|
var potentialRoute = this.routes[t],
|
|
pathRegExp = potentialRoute.path,
|
|
pathname = state.urlInfo.pathname,
|
|
match;
|
|
if(pathprefix) {
|
|
if(pathname.substr(0,pathprefix.length) === pathprefix) {
|
|
pathname = pathname.substr(pathprefix.length) || "/";
|
|
match = potentialRoute.path.exec(pathname);
|
|
} else {
|
|
match = false;
|
|
}
|
|
} else {
|
|
match = potentialRoute.path.exec(pathname);
|
|
}
|
|
if(match && request.method === potentialRoute.method) {
|
|
state.params = [];
|
|
for(var p=1; p<match.length; p++) {
|
|
state.params.push(match[p]);
|
|
}
|
|
return potentialRoute;
|
|
}
|
|
}
|
|
return null;
|
|
};
|
|
|
|
Server.prototype.checkCredentials = function(request,incomingUsername,incomingPassword) {
|
|
var header = request.headers.authorization || "",
|
|
token = header.split(/\s+/).pop() || "",
|
|
auth = $tw.utils.base64Decode(token),
|
|
parts = auth.split(/:/),
|
|
username = parts[0],
|
|
password = parts[1];
|
|
if(incomingUsername === username && incomingPassword === password) {
|
|
return "ALLOWED";
|
|
} else {
|
|
return "DENIED";
|
|
}
|
|
};
|
|
|
|
Server.prototype.requestHandler = function(request,response) {
|
|
// Compose the state object
|
|
var self = this;
|
|
var state = {};
|
|
state.wiki = self.wiki;
|
|
state.server = self;
|
|
state.urlInfo = url.parse(request.url);
|
|
// Optionally output debug info
|
|
if(self.get("debugLevel") !== "none") {
|
|
console.log("Request path:",JSON.stringify(state.urlInfo));
|
|
console.log("Request headers:",JSON.stringify(request.headers));
|
|
}
|
|
// Find the route that matches this path
|
|
var route = self.findMatchingRoute(request,state);
|
|
// Check for the username and password if we've got one
|
|
var username = self.get("username"),
|
|
password = self.get("password");
|
|
if(username && password) {
|
|
// Check they match
|
|
if(self.checkCredentials(request,username,password) !== "ALLOWED") {
|
|
var servername = state.wiki.getTiddlerText("$:/SiteTitle") || "TiddlyWiki5";
|
|
response.writeHead(401,"Authentication required",{
|
|
"WWW-Authenticate": 'Basic realm="Please provide your username and password to login to ' + servername + '"'
|
|
});
|
|
response.end();
|
|
return;
|
|
}
|
|
}
|
|
// Return a 404 if we didn't find a route
|
|
if(!route) {
|
|
response.writeHead(404);
|
|
response.end();
|
|
return;
|
|
}
|
|
// Set the encoding for the incoming request
|
|
// TODO: Presumably this would need tweaking if we supported PUTting binary tiddlers
|
|
request.setEncoding("utf8");
|
|
// Dispatch the appropriate method
|
|
switch(request.method) {
|
|
case "GET": // Intentional fall-through
|
|
case "DELETE":
|
|
route.handler(request,response,state);
|
|
break;
|
|
case "PUT":
|
|
var data = "";
|
|
request.on("data",function(chunk) {
|
|
data += chunk.toString();
|
|
});
|
|
request.on("end",function() {
|
|
state.data = data;
|
|
route.handler(request,response,state);
|
|
});
|
|
break;
|
|
}
|
|
};
|
|
|
|
/*
|
|
Listen for requests
|
|
port: optional port number (falls back to value of "port" variable)
|
|
host: optional host address (falls back to value of "hist" variable)
|
|
*/
|
|
Server.prototype.listen = function(port,host) {
|
|
// Handle defaults for port and host
|
|
port = port || this.get("port");
|
|
host = host || this.get("host");
|
|
// Check for the port being a string and look it up as an environment variable
|
|
if(parseInt(port,10).toString() !== port) {
|
|
port = process.env[port] || 8080;
|
|
}
|
|
$tw.utils.log("Serving on " + host + ":" + port,"brown/orange");
|
|
$tw.utils.log("(press ctrl-C to exit)","red");
|
|
// Warn if required plugins are missing
|
|
if(!$tw.wiki.getTiddler("$:/plugins/tiddlywiki/tiddlyweb") || !$tw.wiki.getTiddler("$:/plugins/tiddlywiki/filesystem")) {
|
|
$tw.utils.warning("Warning: Plugins required for client-server operation (\"tiddlywiki/filesystem\" and \"tiddlywiki/tiddlyweb\") are missing from tiddlywiki.info file");
|
|
}
|
|
return http.createServer(this.requestHandler.bind(this)).listen(port,host);
|
|
};
|
|
|
|
exports.Server = Server;
|
|
|
|
})();
|