view src/luan/host/WebHandler.java @ 1336:7483108154bb

minor logging
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 14 Feb 2019 05:22:22 -0700
parents e0cf0d108a77
children 8b61c8c4e07a
line wrap: on
line source

package luan.host;

import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import luan.webserver.Handler;
import luan.webserver.Request;
import luan.webserver.Response;
import luan.webserver.handlers.DomainHandler;
import luan.Luan;
import luan.LuanException;
import luan.LuanTable;
import luan.LuanFunction;
import luan.LuanClosure;
import luan.LuanRuntimeException;
import luan.modules.BasicLuan;
import luan.modules.http.LuanHandler;
import luan.modules.logging.Log4j;


public class WebHandler implements Handler {
	private static final Logger logger = LoggerFactory.getLogger(WebHandler.class);

	private static final DomainHandler.Factory factory = new DomainHandler.Factory() {
		public Handler newHandler(String domain) {
			File dir = new File(sitesDir,domain);
			if( !dir.exists() /* && !recover(dir) */ )
				return null;
			String dirStr = dir.toString();

			String logDir = dirStr + "/site/private/local/logs/web";
			new File(logDir).mkdirs();

			Luan luan = new Luan();
			Log4j.newLoggerRepository(luan);
			initLuan(luan,dirStr,domain,true);
			return new LuanHandler(luan);
		}
	};

	public static String securityPassword = "password";  // change for security
	private static final DomainHandler domainHandler = new DomainHandler(factory);
	private static String sitesDir = null;

	public static boolean isServing() {
		return sitesDir != null;
	}

	public WebHandler(String dir) {
		if( sitesDir != null )
			throw new RuntimeException("already set");
		if( !new File(dir).exists() )
			throw new RuntimeException();
		sitesDir = dir;
	}

	@Override public Response handle(Request request) {
		return domainHandler.handle(request);
	}

	public static Object runLuan(String domain,String sourceText,String sourceName) throws LuanException {
		LuanHandler luanHandler = (LuanHandler)domainHandler.getHandler(domain);
		return luanHandler.runLuan(sourceText,sourceName);
	}

	public static Object callSite(String domain,String fnName,Object... args) throws LuanException {
		LuanHandler luanHandler = (LuanHandler)domainHandler.getHandler(domain);
		return luanHandler.call_rpc(fnName,args);
	}

/*
	private static boolean recover(File dir) {
		File backups = new File(dir.getParentFile().getParentFile(),"backups");
		if( !backups.exists() )
			return false;
		String name = dir.getName();
		File from = null;
		for( File backup : backups.listFiles() ) {
			File d = new File(backup,"current/"+name);
			if( d.exists() && (from==null || from.lastModified() < d.lastModified()) )
				from = d;
		}
		if( from == null )
			return false;
		if( !from.renameTo(dir) )
			throw new RuntimeException("couldn't rename "+from+" to "+dir);
		logger.info("recovered "+name+" from "+from);
		return true;
	}
*/
	static void initLuan(Luan luan,String dir,String domain,boolean logging) {
		security(luan,dir);
		try {
			LuanFunction fn = BasicLuan.load_file(luan,"classpath:luan/host/init.luan");
			fn.call(dir,domain,logging);
		} catch(LuanException e) {
			throw new LuanRuntimeException(e);
		}
	}

	public static void removeHandler(String domain) {
		domainHandler.removeHandler(domain);
	}

	public static void loadHandler(String domain) throws LuanException {
		try {
			domainHandler.getHandler(domain);
		} catch(LuanRuntimeException e) {
			throw (LuanException)e.getCause();
		}
	}

	private static final void security(Luan luan,String dir) {
		final String siteUri = "file:" + dir + "/site";
		Luan.Security security = new Luan.Security() {
			public void check(Luan luan,LuanClosure closure,String op,Object... args)
				throws LuanException
			{
				if( op.equals("uri") ) {
					String name = (String)args[0];
					if( name.startsWith("file:") ) {
						if( name.contains("..") )
							throw new LuanException("Security violation - '"+name+"' contains '..'");
						if( !(name.equals(siteUri) || name.startsWith(siteUri+"/")) )
							throw new LuanException("Security violation - '"+name+"' outside of site dir");
					}
					else if( name.startsWith("classpath:luan/host/") ) {
						throw new LuanException("Security violation");
					}
					else if( name.startsWith("os:") || name.startsWith("bash:") ) {
						throw new LuanException("Security violation");
					}
				} else {
					String name = closure.sourceName;
					if( !(
						name.startsWith("luan:")
						|| name.startsWith("classpath:")
						|| name.matches("^file:[^/]+$")
					) )
						throw new LuanException("Security violation - only luan:* modules can load Java");
					if( name.equals("luan:logging/Logging") )
						throw new LuanException("Security violation - cannot reload Logging");
				}
			}
		};
		Luan.setSecurity(luan,security);
	}

}