changeset 702:87970832a3c3

improve Http_test
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 04 May 2016 20:45:40 -0600
parents d0280c7fdc3a
children 6e6e9e73abaa
files core/src/luan/modules/StringLuan.java http/src/luan/modules/http/Http_test.luan website/src/manual.html.luan
diffstat 3 files changed, 32 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/modules/StringLuan.java	Wed May 04 17:49:41 2016 -0600
+++ b/core/src/luan/modules/StringLuan.java	Wed May 04 20:45:40 2016 -0600
@@ -233,7 +233,8 @@
 		return null;
 	}
 
-	public static boolean matches(String s,String pattern) {
+	public static boolean matches(String s,String pattern) throws LuanException {
+		Utils.checkNotNull(s);
 		return Pattern.compile(pattern).matcher(s).find();
 	}
 
--- a/http/src/luan/modules/http/Http_test.luan	Wed May 04 17:49:41 2016 -0600
+++ b/http/src/luan/modules/http/Http_test.luan	Wed May 04 20:45:40 2016 -0600
@@ -1,6 +1,8 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
 local set_metatable = Luan.set_metatable or error()
+local try = Luan.try or error()
+local Package = require "luan:Package.luan"
 local Io = require "luan:Io.luan"
 local String = require "luan:String.luan"
 local matches = String.matches or error()
@@ -13,14 +15,27 @@
 M.cookie = {}
 
 function M.get_page(path)
+	Http.request.path = path
 	if M.welcome_file ~= nil and matches(path,"/$") then
 		path = path .. M.welcome_file
 	end
 	local old_out = Io.stdout
-	local mod = require("site:"..path..".luan")
-	mod()
-	M.text_writer.close()
-	Io.stdout = old_out
+	try {
+		function()
+			local mod = Package.load("site:"..path..".luan")
+			if mod ~= nil then
+				mod()
+			else
+				local not_found = Package.load("site:/not_found.luan")
+				not_found or error(path.." not found")
+				not_found()
+			end
+			M.text_writer.close()
+		end
+		finally = function()
+			Io.stdout = old_out
+		end
+	}
 	return M.result.read_text()
 end
 
@@ -35,19 +50,23 @@
 			M.result = Io.uri "string:"
 			M.text_writer = M.result.text_writer()
 			return M.text_writer
-		end;
+		end
 
 		set_cookie = function(name,value)
 			M.cookie[name] = value
-		end;
+		end
 
 		remove_cookie = function(name)
 			M.cookie[name] = nil
-		end;
+		end
 
 		send_redirect = function(url)
 			Http.response.redirect = url
-		end;
+		end
+
+		send_error = function(code)
+			error("sent error "..code)
+		end
 
 	}
 
--- a/website/src/manual.html.luan	Wed May 04 17:49:41 2016 -0600
+++ b/website/src/manual.html.luan	Wed May 04 20:45:40 2016 -0600
@@ -2167,13 +2167,13 @@
 try {
 	function()
 		a_dangerous_fn()
-	end;
+	end
 	catch = function(e)
 		-- handle error
-	end;
+	end
 	finally = function()
 		-- clean up
-	end;
+	end
 }
 </pre>