Mercurial Hosting > luan
changeset 499:fa4af530697f
add http/dump
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 18 May 2015 14:24:50 -0600 |
parents | ee55be414a34 |
children | ab9c2afefb47 |
files | http/src/luan/modules/http/Http.luan http/src/luan/modules/http/HttpServicer.java http/src/luan/modules/http/dump.luan |
diffstat | 3 files changed, 79 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/http/src/luan/modules/http/Http.luan Mon May 18 00:25:35 2015 -0600 +++ b/http/src/luan/modules/http/Http.luan Mon May 18 14:24:50 2015 -0600 @@ -10,6 +10,9 @@ local IoLuan = require "java:luan.modules.IoLuan" +to_header_name = HttpServicer.toHeaderName + + local singular_metatable = {} function singular_metatable.__index(table,key) @@ -50,23 +53,30 @@ set_metatable(this.parameter,singular_metatable) this.cookie = {} - function this.url() + function this.query_string() local string_uri = Io.uri "string:" local out = string_uri.text_writer() - - out.write( this.scheme, "://", this.header.host, this.path ) - if this.method ~= "POST" then - local and_char = "?" - for name, values in pairs(this.parameters) do - for _, value in ipairs(values) do - out.write( and_char, url_encode(name), "=", url_encode(value) ) - and_char = "&" - end + local and_char = "" + for name, values in pairs(this.parameters) do + for _, value in ipairs(values) do + out.write( and_char, url_encode(name), "=", url_encode(value) ) + and_char = "&" end end + out.close() + local s = string_uri.read_text() + return s == "" and nil or s + end - out.close() - return string_uri.read_text() + function this.url() + local url = this.scheme.."://"..this.header.host..this.path + if this.method ~= "POST" then + local query = this.query_string() + if query ~= nil then + url = url.."?"..query + end + end + return url end return this @@ -100,6 +110,7 @@ end -- request = new_request{} -- filled in by HttpServicer +-- response = new_response{} -- filled in by HttpServicer
--- a/http/src/luan/modules/http/HttpServicer.java Mon May 18 00:25:35 2015 -0600 +++ b/http/src/luan/modules/http/HttpServicer.java Mon May 18 14:24:50 2015 -0600 @@ -87,11 +87,11 @@ LuanTable headersTbl = (LuanTable)requestTbl.rawGet("headers"); for( Enumeration<String> enKeys = request.getHeaderNames(); enKeys.hasMoreElements(); ) { String key = enKeys.nextElement(); - key = key.toLowerCase().replace('-','_'); LuanTable values = new LuanTable(); for( Enumeration<String> en = request.getHeaders(key); en.hasMoreElements(); ) { values.rawPut(values.rawLength()+1,en.nextElement()); } + key = key.toLowerCase().replace('-','_'); headersTbl.rawPut(key,values); } @@ -180,7 +180,7 @@ LuanTable responseHeaders = (LuanTable)responseTbl.rawGet("headers"); for( Map.Entry<Object,Object> entry : responseHeaders.rawIterable() ) { String name = (String)entry.getKey(); - name = name.replace('_','-'); + name = toHeaderName(name); LuanTable values = (LuanTable)entry.getValue(); for( Object value : values.asList() ) { if( value instanceof String ) { @@ -204,6 +204,23 @@ // static utils + public static String toHeaderName(String luanName) { + StringBuilder buf = new StringBuilder(); + boolean capitalize = true; + char[] a = luanName.toCharArray(); + for( int i=0; i<a.length; i++ ) { + char c = a[i]; + if( c == '_' ) { + a[i] = '-'; + capitalize = true; + } else if( capitalize ) { + a[i] = Character.toUpperCase(c); + capitalize = false; + } + } + return String.valueOf(a); + } + private static String escape(String value) { return value.replaceAll(";", "%3B"); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/http/src/luan/modules/http/dump.luan Mon May 18 14:24:50 2015 -0600 @@ -0,0 +1,37 @@ +local Luan = require "luan:Luan" +local pairs = Luan.pairs +local ipairs = Luan.ipairs +local Io = require "luan:Io" +local Http = require "luan:http/Http" + + +function respond() + Http.response.header.content_type = "text/plain" + Io.stdout = Http.response.text_writer() + + local method = Http.request.method + local path = Http.request.path + local query = Http.request.query_string() + if method ~= "POST" and query ~= nil then + path = path.."?"..query + end +%> +<%=method%> <%=path%> <%=Http.request.protocol%> +<% + for name, values in pairs(Http.request.headers) do + local header_name = Http.to_header_name(name) + for _, value in ipairs(values) do +%> +<%=header_name%>: <%=value%> +<% + end + end +%> + +<% + if method == "POST" and query ~= nil then +%> +<%=query%> +<% + end +end