Commit 6583275b authored by CompileNix's avatar CompileNix

Close #16 full-text search for posts and content

Fix cache should only be written if response code equals 1xx or 2xx
parent 3441eda6
......@@ -54,6 +54,7 @@ function Init() {
handle.push({ match: /^\/edit$|^\/edit\/$/, callback: requestHandlers.Edit, cache: false });
handle.push({ match: /^\/code\/?.+$/, callback: requestHandlers.Code, cache: false });
handle.push({ match: /^\/page\/?.+$/, callback: requestHandlers.Page, cache: true });
handle.push({ match: /^(\/find\/?.+)|(\/search\/?.+)|(\/\?q=)|(\/\?search=)$/, callback: requestHandlers.Find, cache: true });
handle.push({ match: /^\/$/, callback: requestHandlers.Index, cache: true });
server.Start(router.Route);
......
......@@ -5,3 +5,4 @@ exports.Page = require("./requestHandlers/Page.js").Page;
exports.Ajax = require("./requestHandlers/Ajax.js").Ajax;
exports.Edit = require("./requestHandlers/Edit.js").Edit;
exports.Static = require("./requestHandlers/Static.js").Static;
exports.Find = require("./requestHandlers/Find.js").Find;
var querystring = require("querystring");
var url = require("url");
var FileHeader = Config.post.FileHeader || "header.html";
var FileFooter = Config.post.FileFooter || "footer.html";
var DirectoryPosts = Config.post.DirectoryPosts || "posts";
function Find(request) {
const response = {
type: "error",
code: 404,
content: "nothing found!",
mimetype: "text/plain"
};
let query;
{
query = querystring.parse(url.parse(request.url).query);
let tmp = null;
Object.keys(query).forEach(function(member) {
if (!(query[member] == " ")) {
tmp = query[member];
}
});
query = unescape(tmp);
}
if (query == "null") query = unescape(request.url.split("/").pop());
let postsData = [];
let postIds = [];
let matched = [];
let deps = [FileHeader, FileFooter];
let content = "<ul>\n";
if (!query) return response;
if (!query.match(/[\w\d\s\-]+$/)) return response;
let regex = new RegExp(query, "igm");
const posts = Helper.getPosts(false);
for (let index = 0; index < posts.length; index++) {
let postId = posts[index];
postsData.push(Helper.getPost(posts[index]));
postIds.push(postId);
}
for (let index = 0; index < postsData.length; index++) {
let postData = postsData[index];
let postId = posts[index];
let match = postData.title.match(regex);
if (match) {
deps.push(DirectoryPosts + Helper.GetFsDelimiter() + postId + ".json");
matched.push(index);
continue;
}
match = postData.contents.match(regex);
if (match) {
deps.push(DirectoryPosts + Helper.GetFsDelimiter() + postId + ".json");
matched.push(index);
continue;
}
}
if (matched.length < 1) return response;
for (let index = 0; index < matched.length; index++) {
let match = matched[index];
let postData = postsData[match];
let postId = posts[match];
content += "<li>";
content += "[<a href=\"/post/" + postId + "\">" + postData.title + "</a>] <br><br>";
content += postData.contents;
content += "</li>\n";
}
content += "</ul>\n\n";
return {
type: "content",
code: 200,
content: Helper.getPage(content, "Finding: " + query),
mimetype: "text/html"
};
}
exports.Find = Find;
{"title":"aa (old)","contents":"aa (old)"}
\ No newline at end of file
{"title":"aa (old)","contents":"aa (old)"}
\ No newline at end of file
{"title":"aa (new)","contents":"aa (new)"}
\ No newline at end of file
{"title":"aa (new)","contents":"aa (new)"}
\ No newline at end of file
......@@ -7,10 +7,6 @@ global.zlib = require("zlib");
function GetFsDelimiter() {
switch (os.platform()) {
case "darvin":
case "freebsd":
case "linux":
case "sunos":
default:
return "/";
case "win32":
......@@ -224,7 +220,7 @@ tests.push({
pass &= test_equal(data.length, 2, "getPosts (reverse) - return array length is 2");
pass &= test_equal(data[0], "56cb9c69", "getPosts (reverse) - return [0] equals \"56cb9c69\"");
pass &= test_equal(data[1], "56cb9c4a", "getPosts (reverse) - return [1] equals \"56cb9c4a\"");
pass &= test_equal(helper.getTitle(data[0]), "aa (new)", "getTitle (reverse) - equals \"aa (new)\"");
pass &= test_equal(helper.getTitle(data[0]), "aa (new)", "getTitle (reverse) - equals \"aa (new)\"");
pass &= test_equal(helper.getTitle(data[1]), "aa (old)", "getTitle (reverse) - equals \"aa (old)\"");
data = helper.getPosts(true);
......@@ -233,19 +229,19 @@ tests.push({
pass &= test_equal(data[0], "56cb9c4a", "getPosts - return [0] equals \"56cb9c4a\"");
pass &= test_equal(data[1], "56cb9c69", "getPosts - return [1] equals \"56cb9c69\"");
pass &= test_equal(helper.getTitle(data[0]), "aa (old)", "getTitle - equals \"aa (old)\"");
pass &= test_equal(helper.getTitle(data[1]), "aa (new)", "getTitle - equals \"aa (new)\"");
pass &= test_equal(helper.getTitle(data[1]), "aa (new)", "getTitle - equals \"aa (new)\"");
data = helper.getTitles(false);
pass &= test_true(Array.isArray(data), "getTitles (reverse) - return type is an array");
pass &= test_equal(data.length, 2, "getTitles (reverse) - return array length is 2");
pass &= test_equal(data[0], "aa (new)", "getTitles (reverse) - return [0] equals \"aa (new)\"");
pass &= test_equal(data[0], "aa (new)", "getTitles (reverse) - return [0] equals \"aa (new)\"");
pass &= test_equal(data[1], "aa (old)", "getTitles (reverse) - return [1] equals \"aa (old)\"");
data = helper.getTitles(true);
pass &= test_true(Array.isArray(data), "getTitles - return type is an array");
pass &= test_equal(data.length, 2, "getTitles - return array length is 2");
pass &= test_equal(data[0], "aa (old)", "getTitles - return [0] equals \"aa (old)\"");
pass &= test_equal(data[1], "aa (new)", "getTitles - return [1] equals \"aa (new)\"");
pass &= test_equal(data[1], "aa (new)", "getTitles - return [1] equals \"aa (new)\"");
helper.writePost("testpost", "testcontent", "testtitle");
data = helper.getPost("testpost");
......@@ -258,6 +254,144 @@ tests.push({
}
});
tests.push({
test: function() {
log("testing request handler: Find", NOTICE);
var pass = true;
var find = require(".." + GetFsDelimiter() + "requestHandlers" + GetFsDelimiter() + "Find.js");
const responseWrapper = require(".." + GetFsDelimiter() + "ResponseWrapper.js").ResponseWrapper;
global.Cache = true;
const dummy_response = new responseWrapper();
dummy_response.send = function () { };
var dummy_request = { url: "/find/" };
var data = find.Find(dummy_request);
pass &= test_equal(data.code, 404, "/find/ - should not be found");
dummy_request.url = "/find/aa";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 200, "/find/aa - should be found");
dummy_request.url = "/find/aaa";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 404, "/find/aaa - should not be found");
dummy_request.url = "/find/////////////";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 404, "/find///////////// - should not be found");
dummy_request.url = "/find/\\\\\\\\\\\\\\";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 404, "/find/\\\\\\\\\\\\\\ - should not be found");
dummy_request.url = "/find/.";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 404, "/find/. - should not be found");
dummy_request.url = "/find/.......";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 404, "/find/....... - should not be found");
dummy_request.url = "/find/+";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 404, "/find/+ - should not be found");
dummy_request.url = "/find/++";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 404, "/find/++ - should not be found");
dummy_request.url = "/find/*";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 404, "/find/* - should not be found");
dummy_request.url = "/find/old";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 200, "/find/old - should be found");
dummy_request.url = "/find/new";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 200, "/find/new - should be found");
dummy_request.url = "/find/newold";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 404, "/find/newold - should not be found");
dummy_request.url = "/find/<script>alert('hello')</script>";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 404, "/find/<script>alert('hello')</script> - should not be found");
dummy_request.url = "/find/<u>test</u>";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 404, "/find/<u>test</u> - should not be found");
dummy_request.url = "/?q=";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 404, "/?q= - should not be found");
dummy_request.url = "/?q=aa";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 200, "/?q=aa - should be found");
dummy_request.url = "/?q=aaa";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 404, "/?q=aaa - should not be found");
dummy_request.url = "/?q=////////////";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 404, "/?q=//////////// - should not be found");
dummy_request.url = "/?q=\\\\\\\\\\\\\\";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 404, "/?q=\\\\\\\\\\\\\\ - should not be found");
dummy_request.url = "/?q=.";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 404, "/?q=. - should not be found");
dummy_request.url = "/?q=.......";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 404, "/?q=....... - should not be found");
dummy_request.url = "/?q=+";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 404, "/?q=+ - should not be found");
dummy_request.url = "/?q=++";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 200, "/?q=++ - should be found");
dummy_request.url = "/?q=*";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 404, "/?q=* - should not be found");
dummy_request.url = "/?q=old";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 200, "/?q=old - should be found");
dummy_request.url = "/?q=new";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 200, "/?q=new - should be found");
dummy_request.url = "/?q=newold";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 404, "/?q=newold - should not be found");
dummy_request.url = "/?q=<script>alert('hello')</script>";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 404, "/?q=<script>alert('hello')</script> - should not be found");
dummy_request.url = "/?q=<u>test</u>";
data = find.Find(dummy_request);
pass &= test_equal(data.code, 404, "/?q=<u>test</u> - should not be found");
return pass;
}
});
// TODO more tests!
for (var i = 0; i < tests.length; i++) {
......
......@@ -72,15 +72,15 @@ function Start(route) {
}
function process_request(request, response, route) {
const pathname = url.parse(request.url).pathname;
const query = url.parse(request.url).path;
if (Config.DevMode) {
console.log("process_request: " + request.url);
}
if (!router.RouteExists(pathname)) {
if (!router.RouteExists(query)) {
if (Config.DevMode) {
console.log("404: " + (pathname == undefined ? "undefined" : pathname));
console.log("404: " + (query == undefined ? "undefined" : query));
}
response.setResponseCode(404);
ResponseCodeMessage.ResponseCodeMessage(response);
......@@ -88,11 +88,11 @@ function process_request(request, response, route) {
return false;
}
if (router.GetRoute(pathname).callback === requestHandlers.Static && (request.headers["cache-control"] !== "no-cache")) {
if (router.GetRoute(query).callback === requestHandlers.Static && (request.headers["cache-control"] !== "no-cache")) {
const path = Helper.replaceAll("/", Helper.GetFsDelimiter(), Config.staticContentPath);
let lastModified = undefined;
if (pathname === "/favicon.ico") {
if (query === "/favicon.ico") {
lastModified = new Date(fs.statSync(path + Helper.GetFsDelimiter() + "favicon.ico").mtime).toUTCString();
} else {
lastModified = new Date(fs.statSync(path + Helper.GetFsDelimiter() + querystring.parse(url.parse(request.url).query)["f"]).mtime).toUTCString(); // TODO fix this
......@@ -113,13 +113,13 @@ function process_request(request, response, route) {
response.send();
} else {
const deliverCache = !(HandleClientCacheControl && request.headers["cache-control"] === "no-cache");
const writeCache = router.RouteGetCacheEnabled(pathname);
const writeCache = router.RouteGetCacheEnabled(query);
if (deliverCache && cacheHasRequest) {
Cache.send(request, response);
return false;
}
var data = route(pathname, request);
var data = route(query, request);
if (request.method === "POST") {
var body = "";
......@@ -187,7 +187,7 @@ function sendData(data, request, response, writeCache) {
ResponseCodeMessage.ResponseCodeMessage(response);
if (writeCache) {
if (writeCache && (data.code > 99 && data.code < 300)) {
Cache.add(request, data.content, data.mimetype, data.code);
response.setLastModified(Cache.getLastModified(request));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment