changeset 733:ffbbe25dab09

add http option time_out
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 14 Jun 2016 00:04:08 -0600
parents d7371dc8c2e7
children e44e98fe9de8
files core/src/luan/modules/url/LuanUrl.java
diffstat 1 files changed, 19 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/modules/url/LuanUrl.java	Sun Jun 12 23:05:31 2016 -0600
+++ b/core/src/luan/modules/url/LuanUrl.java	Tue Jun 14 00:04:08 2016 -0600
@@ -14,6 +14,7 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Base64;
+import luan.Luan;
 import luan.LuanState;
 import luan.LuanTable;
 import luan.LuanJavaFunction;
@@ -31,6 +32,7 @@
 	private Map headers;
 	private String content = null;
 	private MultipartClient multipart = null;
+	private int timeout = 0;
 
 	public LuanUrl(LuanState luan,URL url,LuanTable options) throws LuanException {
 		this.url = url;
@@ -140,6 +142,9 @@
 					}
 				}
 			}
+			Integer timeout = getInt(map,"time_out");
+			if( timeout != null )
+				this.timeout = timeout;
 			if( !map.isEmpty() )
 				throw new LuanException( "unrecognized options: "+map );
 		}
@@ -183,6 +188,16 @@
 		return (String)val;
 	}
 
+	private static Integer getInt(Map map,String key) throws LuanException {
+		Object val = map.remove(key);
+		if( val==null )
+			return null;
+		Integer i = Luan.asInteger(val);
+		if( i==null )
+			throw new LuanException( "parameter '"+key+"' must be an integer" );
+		return i;
+	}
+
 	private static LuanTable getTable(Map map,String key) throws LuanException {
 		Object val = map.remove(key);
 		if( val!=null && !(val instanceof LuanTable) )
@@ -197,6 +212,10 @@
 
 	@Override public InputStream inputStream() throws IOException, LuanException {
 		URLConnection con = url.openConnection();
+		if( timeout != 0 ) {
+			con.setConnectTimeout(timeout);
+			con.setReadTimeout(timeout);
+		}
 		if( headers != null ) {
 			for( Object hack : headers.entrySet() ) {
 				Map.Entry entry = (Map.Entry)hack;