Mercurial Hosting > luan
comparison src/luan/lib/HttpLib.java @ 138:06159094b802
replace WebRun.java with web_run.luan
git-svn-id: https://luan-java.googlecode.com/svn/trunk@139 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Thu, 12 Jun 2014 08:20:48 +0000 |
parents | 7e160d2f6d9c |
children | 3b384dc5ca91 |
comparison
equal
deleted
inserted
replaced
137:573ce091ae00 | 138:06159094b802 |
---|---|
31 }; | 31 }; |
32 | 32 |
33 public static void service(LuanState luan,HttpServletRequest request,HttpServletResponse response,String modName) | 33 public static void service(LuanState luan,HttpServletRequest request,HttpServletResponse response,String modName) |
34 throws LuanException | 34 throws LuanException |
35 { | 35 { |
36 LuanState newLuan; | 36 LuanFunction fn; |
37 LuanFunction newFn; | |
38 synchronized(luan) { | 37 synchronized(luan) { |
39 Object mod = PackageLib.require(luan,modName); | 38 Object mod = PackageLib.require(luan,modName); |
40 if( !(mod instanceof LuanFunction) ) | 39 if( !(mod instanceof LuanFunction) ) |
41 throw luan.exception( "module '"+modName+"' must return a function" ); | 40 throw luan.exception( "module '"+modName+"' must return a function" ); |
42 LuanFunction fn = (LuanFunction)mod; | 41 fn = (LuanFunction)mod; |
43 DeepCloner cloner = new DeepCloner(); | 42 DeepCloner cloner = new DeepCloner(); |
44 newLuan = cloner.deepClone(luan); | 43 luan = cloner.deepClone(luan); |
45 newFn = cloner.get(fn); | 44 fn = cloner.get(fn); |
46 } | 45 } |
47 | 46 |
48 LuanTable module = (LuanTable)luan.loaded().get(NAME); | 47 LuanTable module = (LuanTable)luan.loaded().get(NAME); |
49 if( module == null ) | 48 if( module == null ) |
50 throw luan.exception( "module 'Http' not defined" ); | 49 throw luan.exception( "module 'Http' not defined" ); |
51 HttpLib lib = new HttpLib(request,response); | 50 HttpLib lib = new HttpLib(request,response); |
52 try { | 51 try { |
53 module.put( "request", lib.requestTable() ); | 52 module.put( "request", lib.requestTable() ); |
54 module.put( "response", lib.responseTable() ); | 53 module.put( "response", lib.responseTable() ); |
54 /* | |
55 module.put( "write", new LuanJavaFunction( | 55 module.put( "write", new LuanJavaFunction( |
56 HttpLib.class.getMethod( "text_write", LuanState.class, new Object[0].getClass() ), lib | 56 HttpLib.class.getMethod( "text_write", LuanState.class, new Object[0].getClass() ), lib |
57 ) ); | 57 ) ); |
58 */ | |
58 } catch(NoSuchMethodException e) { | 59 } catch(NoSuchMethodException e) { |
59 throw new RuntimeException(e); | 60 throw new RuntimeException(e); |
60 } | 61 } |
61 | 62 |
62 newLuan.call(newFn,"<http>"); | 63 luan.call(fn,"<http>"); |
63 } | 64 } |
64 | 65 |
65 | 66 |
66 | 67 |
67 private final HttpServletRequest request; | 68 private final HttpServletRequest request; |
68 private final HttpServletResponse response; | 69 private final HttpServletResponse response; |
69 private PrintWriter writer = null; | 70 // private PrintWriter writer = null; |
70 // private ServletOutputStream sos = null; | 71 // private ServletOutputStream sos = null; |
71 | 72 |
72 private HttpLib(HttpServletRequest request,HttpServletResponse response) { | 73 private HttpLib(HttpServletRequest request,HttpServletResponse response) { |
73 this.request = request; | 74 this.request = request; |
74 this.response = response; | 75 this.response = response; |
96 add( resp, "send_error", Integer.TYPE, String.class ); | 97 add( resp, "send_error", Integer.TYPE, String.class ); |
97 resp.put( "contains_header", new LuanJavaFunction(HttpServletResponse.class.getMethod("containsHeader",String.class),response) ); | 98 resp.put( "contains_header", new LuanJavaFunction(HttpServletResponse.class.getMethod("containsHeader",String.class),response) ); |
98 resp.put( "set_header", new LuanJavaFunction(HttpServletResponse.class.getMethod("setHeader",String.class,String.class),response) ); | 99 resp.put( "set_header", new LuanJavaFunction(HttpServletResponse.class.getMethod("setHeader",String.class,String.class),response) ); |
99 add( resp, "set_cookie", String.class, String.class, Boolean.TYPE, String.class ); | 100 add( resp, "set_cookie", String.class, String.class, Boolean.TYPE, String.class ); |
100 add( resp, "remove_cookie", String.class, String.class ); | 101 add( resp, "remove_cookie", String.class, String.class ); |
102 resp.put( "set_content_type", new LuanJavaFunction(HttpServletResponse.class.getMethod("setContentType",String.class),response) ); | |
103 add( resp, "text_writer" ); | |
101 return resp; | 104 return resp; |
102 } | 105 } |
103 | 106 |
104 private void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { | 107 private void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { |
105 t.put( method, new LuanJavaFunction(HttpLib.class.getMethod(method,parameterTypes),this) ); | 108 t.put( method, new LuanJavaFunction(HttpLib.class.getMethod(method,parameterTypes),this) ); |
106 } | 109 } |
107 | 110 /* |
108 public void text_write(LuanState luan,Object... args) throws LuanException, IOException { | 111 public void text_write(LuanState luan,Object... args) throws LuanException, IOException { |
109 if( writer == null ) | 112 if( writer == null ) |
110 writer = response.getWriter(); | 113 writer = response.getWriter(); |
111 for( Object obj : args ) { | 114 for( Object obj : args ) { |
112 writer.print( luan.toString(obj) ); | 115 writer.print( luan.toString(obj) ); |
113 } | 116 } |
117 } | |
118 */ | |
119 public LuanTable text_writer() throws IOException { | |
120 return IoLib.textWriter(response.getWriter()); | |
114 } | 121 } |
115 | 122 |
116 public String get_cookie_value(String name) { | 123 public String get_cookie_value(String name) { |
117 return getCookieValue(request, name); | 124 return getCookieValue(request, name); |
118 } | 125 } |