changeset 283:b669cdaf54b7

add URL post; add Http.request.query_string; add web_run.form(); git-svn-id: https://luan-java.googlecode.com/svn/trunk@284 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 02 Dec 2014 03:34:04 +0000
parents 38bd29e59a6e
children 8870840251ea
files core/src/luan/modules/IoLuan.java core/src/luan/modules/Time.luan core/src/luan/modules/UrlCall.java web/src/luan/modules/web/HttpServicer.java web/src/luan/modules/web/web_run.luan
diffstat 5 files changed, 120 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/modules/IoLuan.java	Wed Nov 26 22:25:09 2014 +0000
+++ b/core/src/luan/modules/IoLuan.java	Tue Dec 02 03:34:04 2014 +0000
@@ -343,6 +343,22 @@
 		@Override public String to_string() {
 			return url.toString();
 		}
+
+		public String post(String postS) throws IOException {
+			return new UrlCall(url).post(postS);
+		}
+
+		@Override LuanTable table() {
+			LuanTable tbl = super.table();
+			try {
+				tbl.put( "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 {
--- a/core/src/luan/modules/Time.luan	Wed Nov 26 22:25:09 2014 +0000
+++ b/core/src/luan/modules/Time.luan	Tue Dec 02 03:34:04 2014 +0000
@@ -52,4 +52,3 @@
 function parse( pattern, source )
 	return SimpleDateFormat.new(pattern).parse(source).getTime()
 end
-	
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/src/luan/modules/UrlCall.java	Tue Dec 02 03:34:04 2014 +0000
@@ -0,0 +1,81 @@
+package luan.modules;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.IOException;
+import java.net.URLConnection;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.Map;
+import java.util.HashMap;
+
+
+public final class UrlCall {
+	public final URLConnection connection;
+
+	public UrlCall(String url) throws IOException {
+		this(new URL(url));
+	}
+
+	public UrlCall(URL url) throws IOException {
+		connection = url.openConnection();
+	}
+
+	public void acceptJson() {
+		connection.setRequestProperty("accept","application/json");
+	}
+
+	public String get() throws IOException {
+		Reader in = new InputStreamReader(connection.getInputStream());
+		String rtn = Utils.readAll(in);
+		in.close();
+		return rtn;
+	}
+
+	public String post(String content,String contentType) throws IOException {
+		HttpURLConnection connection = (HttpURLConnection)this.connection;
+
+		connection.setRequestProperty("Content-type",contentType);
+		connection.setDoOutput(true);
+		connection.setRequestMethod("POST");
+
+		byte[] post = content.getBytes();
+		connection.setRequestProperty("Content-Length",Integer.toString(post.length));
+		OutputStream out = connection.getOutputStream();
+		out.write(post);
+		out.flush();
+
+		Reader in;
+		try {
+			in = new InputStreamReader(connection.getInputStream());
+		} catch(IOException e) {
+			InputStream is = connection.getErrorStream();
+			if( is == null )
+				throw e;
+			in = new InputStreamReader(is);
+			String msg = Utils.readAll(in);
+			in.close();
+			throw new UrlCallException(msg,e);
+		}
+		String rtn = Utils.readAll(in);
+		in.close();
+		out.close();
+		return rtn;
+	}
+
+	public String post(String content) throws IOException {
+		return post(content,"application/x-www-form-urlencoded");
+	}
+
+	public String postJson(String content) throws IOException {
+		return post(content,"application/json");
+	}
+
+	public static final class UrlCallException extends IOException {
+		UrlCallException(String msg,IOException e) {
+			super(msg,e);
+		}
+	}
+}
--- a/web/src/luan/modules/web/HttpServicer.java	Wed Nov 26 22:25:09 2014 +0000
+++ b/web/src/luan/modules/web/HttpServicer.java	Tue Dec 02 03:34:04 2014 +0000
@@ -147,6 +147,9 @@
 		tbl.put( "current_url", new LuanProperty() { public Object get() {
 			return getCurrentURL(request);
 		} } );
+		tbl.put( "query_string", new LuanProperty() { public Object get() {
+			return getQueryString(request);
+		} } );
 		tbl.put( "remote_address", new LuanProperty() { public Object get() {
 			return request.getRemoteAddr();
 		} } );
--- a/web/src/luan/modules/web/web_run.luan	Wed Nov 26 22:25:09 2014 +0000
+++ b/web/src/luan/modules/web/web_run.luan	Tue Dec 02 03:34:04 2014 +0000
@@ -18,13 +18,31 @@
 	end
 end
 
+local function form() %>
+<html>
+<body>
+<form action="run" method="post">
+<input type="hidden" name="content_type" value="text/plain" />
+<input type="submit" value="Execute Luan code below">
+<br />
+<textarea name="code" rows="30" cols="90" wrap="off">
+</textarea>
+</form>
+</body>
+</html>
+<% end
+
 function service()
+	Io.stdout = Http.response.text_writer()
+	local code = Http.request.parameters.code
+	if code == nil then
+		form()
+		return
+	end
 	local content_type = Http.request.parameters.content_type
 	if content_type ~= nil then
 		Http.response.content_type = content_type
 	end
-	Io.stdout = Http.response.text_writer()
-	local code = Http.request.parameters.code
 	local env = {
 		request = Http.request;
 		response = Http.response;