Mercurial Hosting > luan
comparison src/luan/modules/http/impl/HttpServicer.java @ 1160:4beabb087be6
add http/impl
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 05 Feb 2018 22:33:59 -0700 |
parents | src/luan/modules/http/jetty/HttpServicer.java@3ef883468fd0 |
children | fef8f0742da9 |
comparison
equal
deleted
inserted
replaced
1159:3ef883468fd0 | 1160:4beabb087be6 |
---|---|
1 package luan.modules.http.impl; | |
2 | |
3 import java.io.Closeable; | |
4 import java.io.IOException; | |
5 import java.util.Map; | |
6 import java.util.HashMap; | |
7 import org.slf4j.Logger; | |
8 import org.slf4j.LoggerFactory; | |
9 import luan.webserver.Request; | |
10 import luan.webserver.Response; | |
11 import luan.webserver.Status; | |
12 import luan.Luan; | |
13 import luan.LuanState; | |
14 import luan.LuanFunction; | |
15 import luan.LuanException; | |
16 import luan.LuanTable; | |
17 import luan.LuanCloner; | |
18 import luan.modules.PackageLuan; | |
19 | |
20 | |
21 public final class HttpServicer { | |
22 private static final Logger logger = LoggerFactory.getLogger(HttpServicer.class); | |
23 | |
24 public static Response service(LuanState luan,Request request) | |
25 throws LuanException | |
26 { | |
27 if( request.path.endsWith("/") ) | |
28 return null; | |
29 LuanFunction fn; | |
30 synchronized(luan) { | |
31 String modName = "site:" + request.path +".luan"; | |
32 PackageLuan.enableLoad(luan,"luan:http/Http.luan",modName); | |
33 LuanTable module = (LuanTable)PackageLuan.require(luan,"luan:http/Http.luan"); | |
34 Object mod = PackageLuan.load(luan,modName); | |
35 if( mod.equals(Boolean.FALSE) ) | |
36 return null; | |
37 if( !(mod instanceof LuanFunction) ) | |
38 throw new LuanException( "module '"+modName+"' must return a function" ); | |
39 LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL); | |
40 luan = (LuanState)cloner.clone(luan); | |
41 fn = (LuanFunction)cloner.get(mod); | |
42 } | |
43 | |
44 LuanTable module = (LuanTable)PackageLuan.require(luan,"luan:http/Http.luan"); | |
45 | |
46 // request | |
47 LuanFunction newRequestFn = (LuanFunction)module.rawGet("new_request"); | |
48 newRequestFn.call( luan, new Object[]{request} ); | |
49 | |
50 // response | |
51 Response response = new Response(); | |
52 LuanFunction newResponseFn = (LuanFunction)module.rawGet("new_response"); | |
53 LuanTable responseTbl = (LuanTable)newResponseFn.call( luan, new Object[]{response} ); | |
54 | |
55 fn.call(luan); | |
56 | |
57 response.status = Status.getStatus( Luan.asInteger(responseTbl.rawGet("status")) ); | |
58 LuanTable headersTbl = (LuanTable)responseTbl.rawGet("headers"); | |
59 if( !headersTbl.rawIsEmpty() ) { | |
60 Map headers = (Map)Luan.toJava(headersTbl); | |
61 for( Object obj : headers.entrySet() ) { | |
62 Map.Entry entry = (Map.Entry)obj; | |
63 String name = (String)entry.getKey(); | |
64 Object value = entry.getValue(); | |
65 response.headers.put(name,value); | |
66 } | |
67 } | |
68 Closeable writer = (Closeable)responseTbl.rawGet("writer"); | |
69 if( writer != null ) { | |
70 try { | |
71 ((Closeable)writer).close(); | |
72 } catch(IOException e) { | |
73 throw new RuntimeException(e); | |
74 } | |
75 } | |
76 | |
77 return response; | |
78 } | |
79 | |
80 public static void setCookie(LuanState luan,Response response,String name,String value,LuanTable attributesTbl) | |
81 throws LuanException | |
82 { | |
83 Map<String,String> attributes = new HashMap<String,String>(); | |
84 if( attributesTbl != null ) { | |
85 for( Map.Entry entry : attributesTbl.iterable(luan) ) { | |
86 String key = (String)entry.getKey(); | |
87 if( !(key instanceof String) ) | |
88 throw new LuanException("cookie attribute name must be string"); | |
89 String val = (String)entry.getValue(); | |
90 if( !(val instanceof String) ) | |
91 throw new LuanException("cookie attribute value must be string"); | |
92 attributes.put(key,val); | |
93 } | |
94 } | |
95 response.setCookie(name,value,attributes); | |
96 } | |
97 | |
98 } |