comparison src/luan/modules/url/LuanUrl.java @ 1267:9fa8b8389578

add LuanTable.luan; support metatable __gc(); add luan.sql;
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 12 Nov 2018 02:10:41 -0700
parents d1911842c2be
children 781ec0a92bb5
comparison
equal deleted inserted replaced
1266:05934fbf635a 1267:9fa8b8389578
32 private Map headers; 32 private Map headers;
33 private String content = ""; 33 private String content = "";
34 private MultipartClient multipart = null; 34 private MultipartClient multipart = null;
35 private int timeout = 0; 35 private int timeout = 0;
36 36
37 public LuanUrl(LuanState luan,URL url,LuanTable options) throws LuanException { 37 public LuanUrl(URL url,LuanTable options) throws LuanException {
38 this.url = url; 38 this.url = url;
39 if( options != null ) { 39 if( options != null ) {
40 Map map = options.asMap(luan); 40 Map map = options.asMap();
41 String methodStr = getString(map,"method"); 41 String methodStr = getString(map,"method");
42 if( methodStr != null ) { 42 if( methodStr != null ) {
43 methodStr = methodStr.toUpperCase(); 43 methodStr = methodStr.toUpperCase();
44 try { 44 try {
45 this.method = Method.valueOf(methodStr); 45 this.method = Method.valueOf(methodStr);
46 } catch(IllegalArgumentException e) { 46 } catch(IllegalArgumentException e) {
47 throw new LuanException( "invalid method: "+methodStr ); 47 throw new LuanException( "invalid method: "+methodStr );
48 } 48 }
49 } 49 }
50 Map headerMap = getMap(luan,map,"headers"); 50 Map headerMap = getMap(map,"headers");
51 if( headerMap != null ) { 51 if( headerMap != null ) {
52 headers = new HashMap(); 52 headers = new HashMap();
53 for( Object hack : headerMap.entrySet() ) { 53 for( Object hack : headerMap.entrySet() ) {
54 Map.Entry entry = (Map.Entry)hack; 54 Map.Entry entry = (Map.Entry)hack;
55 String name = (String)entry.getKey(); 55 String name = (String)entry.getKey();
64 throw new LuanException( "header '"+name+"' table must be list" ); 64 throw new LuanException( "header '"+name+"' table must be list" );
65 headers.put(name,t.asList()); 65 headers.put(name,t.asList());
66 } 66 }
67 } 67 }
68 } 68 }
69 Map auth = getMap(luan,map,"authorization"); 69 Map auth = getMap(map,"authorization");
70 if( auth != null ) { 70 if( auth != null ) {
71 if( headers!=null && headers.containsKey("authorization") ) 71 if( headers!=null && headers.containsKey("authorization") )
72 throw new LuanException( "can't define authorization with header 'authorization' defined" ); 72 throw new LuanException( "can't define authorization with header 'authorization' defined" );
73 String user = getString(auth,"user"); 73 String user = getString(auth,"user");
74 String password = getString(auth,"password"); 74 String password = getString(auth,"password");
83 String val = "Basic " + Base64.getEncoder().encodeToString(sb.toString().getBytes()); 83 String val = "Basic " + Base64.getEncoder().encodeToString(sb.toString().getBytes());
84 if( headers == null ) 84 if( headers == null )
85 headers = new HashMap(); 85 headers = new HashMap();
86 headers.put("authorization",val); 86 headers.put("authorization",val);
87 } 87 }
88 Map params = getMap(luan,map,"parameters"); 88 Map params = getMap(map,"parameters");
89 String enctype = getString(map,"enctype"); 89 String enctype = getString(map,"enctype");
90 if( enctype != null ) { 90 if( enctype != null ) {
91 if( !enctype.equals("multipart/form-data") ) 91 if( !enctype.equals("multipart/form-data") )
92 throw new LuanException( "unrecognized enctype: "+enctype ); 92 throw new LuanException( "unrecognized enctype: "+enctype );
93 if( this.method!=Method.POST ) 93 if( this.method!=Method.POST )
182 if( val!=null && !(val instanceof LuanTable) ) 182 if( val!=null && !(val instanceof LuanTable) )
183 throw new LuanException( "parameter '"+key+"' must be a table" ); 183 throw new LuanException( "parameter '"+key+"' must be a table" );
184 return (LuanTable)val; 184 return (LuanTable)val;
185 } 185 }
186 186
187 private static Map getMap(LuanState luan,Map map,String key) throws LuanException { 187 private static Map getMap(Map map,String key) throws LuanException {
188 LuanTable t = getTable(map,key); 188 LuanTable t = getTable(map,key);
189 return t==null ? null : t.asMap(luan); 189 return t==null ? null : t.asMap();
190 } 190 }
191 191
192 @Override public InputStream inputStream() throws IOException, LuanException { 192 @Override public InputStream inputStream(LuanState luan) throws IOException, LuanException {
193 URLConnection con = url.openConnection(); 193 URLConnection con = url.openConnection();
194 if( timeout != 0 ) { 194 if( timeout != 0 ) {
195 con.setConnectTimeout(timeout); 195 con.setConnectTimeout(timeout);
196 con.setReadTimeout(timeout); 196 con.setReadTimeout(timeout);
197 } 197 }
217 } 217 }
218 218
219 HttpURLConnection httpCon = (HttpURLConnection)con; 219 HttpURLConnection httpCon = (HttpURLConnection)con;
220 220
221 if( method==Method.GET ) { 221 if( method==Method.GET ) {
222 return getInputStream(httpCon); 222 return getInputStream(luan,httpCon);
223 } 223 }
224 224
225 if( method==Method.DELETE ) { 225 if( method==Method.DELETE ) {
226 httpCon.setRequestMethod("DELETE"); 226 httpCon.setRequestMethod("DELETE");
227 return getInputStream(httpCon); 227 return getInputStream(luan,httpCon);
228 } 228 }
229 229
230 // POST 230 // POST
231 231
232 // httpCon.setRequestProperty("content-type","application/x-www-form-urlencoded"); 232 // httpCon.setRequestProperty("content-type","application/x-www-form-urlencoded");
242 out = httpCon.getOutputStream(); 242 out = httpCon.getOutputStream();
243 out.write(post); 243 out.write(post);
244 } 244 }
245 out.flush(); 245 out.flush();
246 try { 246 try {
247 return getInputStream(httpCon); 247 return getInputStream(luan,httpCon);
248 } finally { 248 } finally {
249 out.close(); 249 out.close();
250 } 250 }
251 } 251 }
252 252
253 private static InputStream getInputStream(HttpURLConnection httpCon) throws IOException, LuanException { 253 private static InputStream getInputStream(LuanState luan,HttpURLConnection httpCon) throws IOException, LuanException {
254 try { 254 try {
255 return httpCon.getInputStream(); 255 return httpCon.getInputStream();
256 } catch(IOException e) { 256 } catch(IOException e) {
257 int responseCode = httpCon.getResponseCode(); 257 int responseCode = httpCon.getResponseCode();
258 String responseMessage = httpCon.getResponseMessage(); 258 String responseMessage = httpCon.getResponseMessage();
261 throw e; 261 throw e;
262 Reader in = new InputStreamReader(is); 262 Reader in = new InputStreamReader(is);
263 String msg = Utils.readAll(in); 263 String msg = Utils.readAll(in);
264 in.close(); 264 in.close();
265 LuanException le = new LuanException(msg,e); 265 LuanException le = new LuanException(msg,e);
266 LuanTable tbl = le.table(); 266 LuanTable tbl = le.table(luan);
267 tbl.rawPut("response_code",responseCode); 267 tbl.rawPut("response_code",responseCode);
268 tbl.rawPut("response_message",responseMessage); 268 tbl.rawPut("response_message",responseMessage);
269 throw le; 269 throw le;
270 } 270 }
271 } 271 }