1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-25 14:53:15 +00:00

Put request handler on SimpleServer.prototype (#2627)

The request handler may be used by ExpressJS apps directly and can do most of the heavy lifting without any modification. Note that the self variable must be assignee using `[Function].bind(null,SimpleServer instance)`.
This commit is contained in:
Arlen22 2016-12-17 10:06:10 -05:00 committed by Jeremy Ruston
parent a2fe101848
commit 1530b3e2d8

View File

@ -91,57 +91,59 @@ SimpleServer.prototype.checkCredentials = function(request,incomingUsername,inco
} }
}; };
SimpleServer.prototype.listen = function(port,host) { SimpleServer.prototype.requestHandler = function(request,response) {
// Compose the state object
var self = this; var self = this;
http.createServer(function(request,response) { var state = {};
// Compose the state object state.wiki = self.wiki;
var state = {}; state.server = self;
state.wiki = self.wiki; state.urlInfo = url.parse(request.url);
state.server = self; // Find the route that matches this path
state.urlInfo = url.parse(request.url); var route = self.findMatchingRoute(request,state);
// Find the route that matches this path // Check for the username and password if we've got one
var route = self.findMatchingRoute(request,state); var username = self.get("username"),
// Check for the username and password if we've got one password = self.get("password");
var username = self.get("username"), if(username && password) {
password = self.get("password"); // Check they match
if(username && password) { if(self.checkCredentials(request,username,password) !== "ALLOWED") {
// Check they match var servername = state.wiki.getTiddlerText("$:/SiteTitle") || "TiddlyWiki5";
if(self.checkCredentials(request,username,password) !== "ALLOWED") { response.writeHead(401,"Authentication required",{
var servername = state.wiki.getTiddlerText("$:/SiteTitle") || "TiddlyWiki5"; "WWW-Authenticate": 'Basic realm="Please provide your username and password to login to ' + servername + '"'
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(); response.end();
return; return;
} }
// Set the encoding for the incoming request }
// TODO: Presumably this would need tweaking if we supported PUTting binary tiddlers // Return a 404 if we didn't find a route
request.setEncoding("utf8"); if(!route) {
// Dispatch the appropriate method response.writeHead(404);
switch(request.method) { response.end();
case "GET": // Intentional fall-through return;
case "DELETE": }
// 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); route.handler(request,response,state);
break; });
case "PUT": break;
var data = ""; }
request.on("data",function(chunk) { };
data += chunk.toString();
}); SimpleServer.prototype.listen = function(port,host) {
request.on("end",function() { http.createServer(this.requestHandler.bind(this)).listen(port,host);
state.data = data;
route.handler(request,response,state);
});
break;
}
}).listen(port,host);
}; };
var Command = function(params,commander,callback) { var Command = function(params,commander,callback) {