diff src/luan/modules/http/HttpServicer.java @ 1171:794ddcfbee20

remove http/impl
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 11 Feb 2018 02:41:23 -0700
parents src/luan/modules/http/impl/HttpServicer.java@7e6f28c769a1
children 354e661dee7f
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/modules/http/HttpServicer.java	Sun Feb 11 02:41:23 2018 -0700
@@ -0,0 +1,52 @@
+package luan.modules.http;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import luan.webserver.Request;
+import luan.webserver.Response;
+import luan.LuanState;
+import luan.LuanFunction;
+import luan.LuanException;
+import luan.LuanTable;
+import luan.LuanCloner;
+import luan.modules.PackageLuan;
+
+
+public final class HttpServicer {
+	private static final Logger logger = LoggerFactory.getLogger(HttpServicer.class);
+
+	public static Response service(LuanState luan,Request request,String modName)
+		throws LuanException
+	{
+		LuanFunction fn;
+		synchronized(luan) {
+			PackageLuan.enableLoad(luan,"luan:http/Http.luan",modName);
+			LuanTable module = (LuanTable)PackageLuan.require(luan,"luan:http/Http.luan");
+			Object mod = PackageLuan.load(luan,modName);
+			if( mod.equals(Boolean.FALSE) )
+				return null;
+			if( !(mod instanceof LuanFunction) )
+				throw new LuanException( "module '"+modName+"' must return a function" );
+			LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL);
+			luan = (LuanState)cloner.clone(luan);
+			fn = (LuanFunction)cloner.get(mod);
+		}
+
+		LuanTable module = (LuanTable)PackageLuan.require(luan,"luan:http/Http.luan");
+
+		// request
+		LuanFunction newRequestFn = (LuanFunction)module.rawGet("new_request");
+		newRequestFn.call( luan, new Object[]{request} );
+
+		// response
+		LuanFunction newResponseFn = (LuanFunction)module.rawGet("new_response");
+		newResponseFn.call(luan);
+
+		fn.call(luan);
+
+		LuanFunction finishFn = (LuanFunction)module.rawGet("finish");
+		Response response = (Response)finishFn.call(luan);
+		return response;
+	}
+
+}