Mercurial Hosting > luan
comparison src/luan/modules/http/impl/HttpServicer.java @ 1163:fef8f0742da9
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 06 Feb 2018 22:04:47 -0700 |
parents | 4beabb087be6 |
children | 7e6f28c769a1 |
comparison
equal
deleted
inserted
replaced
1162:e2d2354807f3 | 1163:fef8f0742da9 |
---|---|
1 package luan.modules.http.impl; | 1 package luan.modules.http.impl; |
2 | 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; | 3 import org.slf4j.Logger; |
8 import org.slf4j.LoggerFactory; | 4 import org.slf4j.LoggerFactory; |
9 import luan.webserver.Request; | 5 import luan.webserver.Request; |
10 import luan.webserver.Response; | 6 import luan.webserver.Response; |
11 import luan.webserver.Status; | |
12 import luan.Luan; | |
13 import luan.LuanState; | 7 import luan.LuanState; |
14 import luan.LuanFunction; | 8 import luan.LuanFunction; |
15 import luan.LuanException; | 9 import luan.LuanException; |
16 import luan.LuanTable; | 10 import luan.LuanTable; |
17 import luan.LuanCloner; | 11 import luan.LuanCloner; |
46 // request | 40 // request |
47 LuanFunction newRequestFn = (LuanFunction)module.rawGet("new_request"); | 41 LuanFunction newRequestFn = (LuanFunction)module.rawGet("new_request"); |
48 newRequestFn.call( luan, new Object[]{request} ); | 42 newRequestFn.call( luan, new Object[]{request} ); |
49 | 43 |
50 // response | 44 // response |
51 Response response = new Response(); | |
52 LuanFunction newResponseFn = (LuanFunction)module.rawGet("new_response"); | 45 LuanFunction newResponseFn = (LuanFunction)module.rawGet("new_response"); |
53 LuanTable responseTbl = (LuanTable)newResponseFn.call( luan, new Object[]{response} ); | 46 newResponseFn.call(luan); |
54 | 47 |
55 fn.call(luan); | 48 fn.call(luan); |
56 | 49 |
57 response.status = Status.getStatus( Luan.asInteger(responseTbl.rawGet("status")) ); | 50 LuanFunction finishFn = (LuanFunction)module.rawGet("finish"); |
58 LuanTable headersTbl = (LuanTable)responseTbl.rawGet("headers"); | 51 Response response = (Response)finishFn.call(luan); |
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; | 52 return response; |
78 } | 53 } |
79 | 54 |
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 } | 55 } |