From 994f53d3c4a1d3b9d020a24d1ffebc5febb8e467 Mon Sep 17 00:00:00 2001 From: Dave Winer Date: Mon, 11 May 2015 12:15:11 -0400 Subject: [PATCH] v0.57 --- README.md | 10 ++++++ lib/utils.js | 4 +-- pagepark.js | 92 ++++++++++++++++++++++++++++++++++------------------ 3 files changed, 73 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 259e3b1..22c808a 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,16 @@ There will always be more work to do here. ;-) #### Updates +##### v0.57 5/11/15 by DW + +PagePark has pre-defined pages, /now, /version and /status, whose values are returned by PagePark itself. It used to be that they took precedence, so if a site defines pages with those names, the internal ones would be served instead. Now we only serve them if the site didn't define it. + +The urlSiteContents feature now transmits search params. It still will only forward GET calls. This needs to be updated in a future version. + +PagePark now supports wildcards. Suppose you want to serve all the names from mydomain.org with a wildcard. Create a sub-folder of the domains folder with the name *.mydomain.org. If a request comes in for a sub-domain of mydomain.org that doesn't have its own folder, we'll route it through that folder. You can combine this feature with the urlSiteContents feature, or script-implemented pages. + +We also set the X-Forwarded-Host and X-Forwarded-For headers on urlSiteContents requests. + ##### v0.56 5/5/15 by DW New prefs and config values that allow you to disable processing of scripts and Markdown files. By setting the values in prefs.json, you control all domains on the server. And by adding the values to config.json, in the folder the site is served from, you can turn them off selectively by site. I needed to turn off script processing for .js files served from River4, to make it possible to serve a full river from PagePark. diff --git a/lib/utils.js b/lib/utils.js index f3b54ca..5fbe0a2 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -376,7 +376,7 @@ function readHttpFile (url, callback, timeoutInMilliseconds) { //5/27/14 by DW }); } function readHttpFileThruProxy (url, type, callback) { //10/25/14 by DW - var urlReadFileApi = "http://pub.fargo.io/httpReadUrl"; + var urlReadFileApi = "http://pub2.fargo.io:5347/httpReadUrl"; if (type === undefined) { type = "text/plain"; } @@ -528,7 +528,7 @@ function innerCaseName (text) { //8/12/14 by DW return (s); } function hitCounter (counterGroup, counterServer) { //8/12/14 by DW - var defaultCounterGroup = "scripting", defaultCounterServer = "http://counter.fargo.io/counter"; + var defaultCounterGroup = "scripting", defaultCounterServer = "http://counter2.fargo.io:5337/counter"; var thispageurl = location.href; if (counterGroup === undefined) { counterGroup = defaultCounterGroup; diff --git a/pagepark.js b/pagepark.js index 6e54656..5de1340 100644 --- a/pagepark.js +++ b/pagepark.js @@ -1,4 +1,4 @@ -var myVersion = "0.56", myProductName = "PagePark"; +var myVersion = "0.57", myProductName = "PagePark"; //The MIT License (MIT) @@ -139,7 +139,7 @@ function checkPathForIllegalChars (path) { return (false); } switch (ch) { - case "/": case "_": case "-": case ".": case " ": + case "/": case "_": case "-": case ".": case " ": case "*": return (false); } return (true); @@ -161,6 +161,26 @@ function everySecond () { } } function handleHttpRequest (httpRequest, httpResponse) { + function getDomainFolder (host, callback) { //5/11/15 by DW + var folder = getFullFilePath (domainsPath); + var domainfolder = folder + host; + fs.exists (domainfolder, function (flExists) { + if (flExists) { + callback (domainfolder, host); + } + else { + if (utils.stringCountFields (host, ".") == 3) { + var firstpart = utils.stringNthField (host, ".", 1); + var wildcardhost = "*" + utils.stringDelete (host, 1, firstpart.length); + domainfolder = folder + wildcardhost; + callback (domainfolder, wildcardhost); + } + else { + callback (domainfolder, host); + } + } + }); + } function getConfigFile (host, callback) { var config = { urlSiteRedirect: undefined, @@ -317,30 +337,12 @@ function handleHttpRequest (httpRequest, httpResponse) { } console.log (now.toLocaleTimeString () + " " + httpRequest.method + " " + host + ":" + port + " " + lowerpath + " " + referrer + " " + client); }); - - switch (lowerpath) { - case "/version": - httpResponse.writeHead (200, {"Content-Type": "text/plain"}); - httpResponse.end (myVersion); - break; - case "/now": - httpResponse.writeHead (200, {"Content-Type": "text/plain"}); - httpResponse.end (now.toString ()); - break; - case "/status": - var status = { - prefs: pageparkPrefs, - status: pageparkStats - } - httpResponse.writeHead (200, {"Content-Type": "text/plain"}); - httpResponse.end (utils.jsonStringify (status)); - break; - default: //see if it's a path in the domains folder, if not 404 - var domainfolder = getFullFilePath (domainsPath) + host; + //handle the request + getDomainFolder (host, function (domainfolder, actualhost) { //might be a wildcard folder var f = domainfolder + parsedUrl.pathname; if (checkPathForIllegalChars (f)) { fsSureFilePath (domainsPath, function () { //make sure domains folder exists - getConfigFile (host, function (config) { //get config.json, if it exists -- 1/18/15 by DW + getConfigFile (actualhost, function (config) { //get config.json, if it exists -- 1/18/15 by DW if (config != undefined) { if (config.urlSiteRedirect != undefined) { var urlRedirect = config.urlSiteRedirect + parsedUrl.pathname; @@ -349,18 +351,47 @@ function handleHttpRequest (httpRequest, httpResponse) { return; } if (config.urlSiteContents != undefined) { //4/26/15 by DW -- v0.55 - var path = parsedUrl.pathname; - if (path == "/") { - path += pageparkPrefs.indexFilename + ".html"; + var theRequest = { + url: config.urlSiteContents + httpRequest.url, + headers: { + "X-Forwarded-Host": host, + "X-Forwarded-For": httpRequest.connection.remoteAddress + } + }; + try { + httpRequest.pipe (request (theRequest)).pipe (httpResponse); } - var url = config.urlSiteContents + path; - httpRequest.pipe (request (url)).pipe (httpResponse); + catch (tryError) { + httpResponse.writeHead (500, {"Content-Type": "text/plain"}); + httpResponse.end (tryError.message); + } + return; } } fs.stat (f, function (err, stats) { if (err) { - return404 (); + switch (lowerpath) { + case "/version": + httpResponse.writeHead (200, {"Content-Type": "text/plain"}); + httpResponse.end (myVersion); + break; + case "/now": + httpResponse.writeHead (200, {"Content-Type": "text/plain"}); + httpResponse.end (now.toString ()); + break; + case "/status": + var status = { + prefs: pageparkPrefs, + status: pageparkStats + } + httpResponse.writeHead (200, {"Content-Type": "text/plain"}); + httpResponse.end (utils.jsonStringify (status)); + break; + default: + return404 (); + break; + } } else { if (stats.isDirectory ()) { @@ -383,8 +414,7 @@ function handleHttpRequest (httpRequest, httpResponse) { httpResponse.writeHead (500, {"Content-Type": "text/plain"}); httpResponse.end ("The file name contains illegal characters."); } - break; - } + }); } catch (tryError) { httpResponse.writeHead (500, {"Content-Type": "text/plain"});