diff core/src/luan/modules/IoLuan.java @ 722:647602e8291a

add url options
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 03 Jun 2016 17:51:58 -0600
parents 63cda9eec9a0
children a741a3a33423
line wrap: on
line diff
--- a/core/src/luan/modules/IoLuan.java	Tue May 31 19:31:28 2016 -0600
+++ b/core/src/luan/modules/IoLuan.java	Fri Jun 03 17:51:58 2016 -0600
@@ -27,8 +27,6 @@
 import java.net.InetAddress;
 import java.net.MalformedURLException;
 import java.net.UnknownHostException;
-import java.util.List;
-import java.util.ArrayList;
 import java.util.Map;
 import java.util.zip.ZipOutputStream;
 import java.util.zip.ZipEntry;
@@ -169,38 +167,38 @@
 
 
 	public static abstract class LuanIn {
-		abstract InputStream inputStream() throws IOException;
+		abstract InputStream inputStream() throws IOException, LuanException;
 		public abstract String to_string();
 		public abstract String to_uri_string();
 
-		public Reader reader() throws IOException {
+		public Reader reader() throws IOException, LuanException {
 			return new InputStreamReader(inputStream());
 		}
 
-		public String read_text() throws IOException {
+		public String read_text() throws IOException, LuanException {
 			Reader in = reader();
 			String s = Utils.readAll(in);
 			in.close();
 			return s;
 		}
 
-		public byte[] read_binary() throws IOException {
+		public byte[] read_binary() throws IOException, LuanException {
 			InputStream in = inputStream();
 			byte[] a = Utils.readAll(in);
 			in.close();
 			return a;
 		}
 
-		public LuanFunction read_lines() throws IOException {
+		public LuanFunction read_lines() throws IOException, LuanException {
 			return lines(new BufferedReader(reader()));
 		}
 
-		public LuanFunction read_blocks(Integer blockSize) throws IOException {
+		public LuanFunction read_blocks(Integer blockSize) throws IOException, LuanException {
 			int n = blockSize!=null ? blockSize : Utils.bufSize;
 			return blocks(inputStream(),n);
 		}
 
-		public boolean exists() throws IOException {
+		public boolean exists() throws IOException, LuanException {
 			try {
 				inputStream().close();
 				return true;
@@ -397,42 +395,6 @@
 		}
 	}
 
-	public static final class LuanUrl extends LuanIn {
-		private final URL url;
-
-		private LuanUrl(URL url) {
-			this.url = url;
-		}
-
-		@Override InputStream inputStream() throws IOException {
-			return url.openStream();
-		}
-
-		@Override public String to_string() {
-			return url.toString();
-		}
-
-		@Override public String to_uri_string() {
-			return url.toString();
-		}
-
-		public String post(String postS) throws IOException {
-			return new UrlCall(url).post(postS);
-		}
-
-		@Override public LuanTable table() {
-			LuanTable tbl = super.table();
-			try {
-				tbl.rawPut( "post", new LuanJavaFunction(
-					LuanUrl.class.getMethod( "post", String.class ), this
-				) );
-			} catch(NoSuchMethodException e) {
-				throw new RuntimeException(e);
-			}
-			return tbl;
-		}
-	}
-
 	public static final class LuanFile extends LuanIO {
 		public final File file;
 
@@ -581,21 +543,21 @@
 			}
 		}
 		if( url != null )
-			return new LuanUrl(url).table();
+			return new LuanUrl(luan,url,null).table();
 
 		return null;
 	}
 
-	private static LuanTable url(String url) throws IOException {
-		return new LuanUrl(new URL(url)).table();
+	private static LuanTable url(LuanState luan,String url,LuanTable options) throws IOException, LuanException {
+		return new LuanUrl(luan,new URL(url),options).table();
 	}
 
-	public static LuanTable http(String path) throws IOException {
-		return url("http:"+path);
+	public static LuanTable http(LuanState luan,String path,LuanTable options) throws IOException, LuanException {
+		return url(luan,"http:"+path,options);
 	}
 
-	public static LuanTable https(String path) throws IOException {
-		return url("https:"+path);
+	public static LuanTable https(LuanState luan,String path,LuanTable options) throws IOException, LuanException {
+		return url(luan,"https:"+path,options);
 	}
 
 	public static LuanTable luan(LuanState luan,String path) throws LuanException {
@@ -615,8 +577,8 @@
 			add( schemes, "file", LuanState.class, String.class );
 			add( schemes, "classpath", LuanState.class, String.class );
 			add( schemes, "socket", String.class );
-			add( schemes, "http", String.class );
-			add( schemes, "https", String.class );
+			add( schemes, "http", LuanState.class, String.class, LuanTable.class );
+			add( schemes, "https", LuanState.class, String.class, LuanTable.class );
 			add( schemes, "luan", LuanState.class, String.class );
 			add( schemes, "stdin", LuanState.class );
 		} catch(NoSuchMethodException e) {
@@ -635,7 +597,7 @@
 		return t;
 	}
 
-	public static LuanTable uri(LuanState luan,String name) throws LuanException {
+	public static LuanTable uri(LuanState luan,String name,LuanTable options) throws LuanException {
 		int i = name.indexOf(':');
 		if( i == -1 )
 			throw new LuanException( "invalid Io.uri name '"+name+"', missing scheme" );
@@ -645,7 +607,7 @@
 		LuanFunction opener = (LuanFunction)schemes.get(luan,scheme);
 		if( opener == null )
 			throw new LuanException( "invalid scheme '"+scheme+"' in '"+name+"'" );
-		return (LuanTable)Luan.first(opener.call(luan,new Object[]{location}));
+		return (LuanTable)Luan.first(opener.call(luan,new Object[]{location,options}));
 	}
 
 	public static final class LuanSocket extends LuanIO {
@@ -717,7 +679,7 @@
 
 	// files maps zip name to uri
 	public static void zip(LuanState luan,String zipUri,LuanTable files) throws LuanException, IOException {
-		Object obj = uri(luan,zipUri).rawGet("java");
+		Object obj = uri(luan,zipUri,null).rawGet("java");
 		if( !(obj instanceof LuanIO) )
 			throw new LuanException("invalid uri for zip");
 		LuanIO zipIo = (LuanIO)obj;
@@ -732,7 +694,7 @@
 				throw new LuanException("zip file table values must be strings");
 			String uriStr = (String)obj;
 			out.putNextEntry(new ZipEntry(fileName));
-			obj = uri(luan,uriStr).rawGet("java");
+			obj = uri(luan,uriStr,null).rawGet("java");
 			if( !(obj instanceof LuanIn) )
 				throw new LuanException("invalid uri for zip");
 			LuanIn zipIn = (LuanIn)obj;