Mercurial Hosting > luan
changeset 723:eaf30d5aaf6a
handle url headers
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 03 Jun 2016 18:43:27 -0600 |
parents | 647602e8291a |
children | 4f8e30a3ffd0 |
files | core/src/luan/modules/LuanUrl.java http/src/luan/modules/http/HttpServicer.java |
diffstat | 2 files changed, 46 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
diff -r 647602e8291a -r eaf30d5aaf6a core/src/luan/modules/LuanUrl.java --- a/core/src/luan/modules/LuanUrl.java Fri Jun 03 17:51:58 2016 -0600 +++ b/core/src/luan/modules/LuanUrl.java Fri Jun 03 18:43:27 2016 -0600 @@ -41,8 +41,30 @@ throw new LuanException( "invalid method: "+methodStr ); } } + Map headerMap = getMap(luan,map,"headers"); + if( headerMap != null ) { + headers = new HashMap(); + for( Object hack : headerMap.entrySet() ) { + Map.Entry entry = (Map.Entry)hack; + String key = (String)entry.getKey(); + Object val = entry.getValue(); + String name = toHttpHeaderName(key); + if( val instanceof String ) { + headers.put(name,val); + } else { + if( !(val instanceof LuanTable) ) + throw new LuanException( "header '"+key+"' must be string or table" ); + LuanTable t = (LuanTable)val; + if( !t.isList() ) + throw new LuanException( "header '"+key+"' table must be list" ); + headers.put(name,t.asList()); + } + } + } Map auth = getMap(luan,map,"authorization"); if( auth != null ) { + if( headers!=null && headers.containsKey("Authorization") ) + throw new LuanException( "can't define authorization with header 'Authorization' defined" ); String user = getString(auth,"user"); String password = getString(auth,"password"); if( !auth.isEmpty() ) @@ -88,7 +110,7 @@ if( this.method==Method.POST ) { content = sb.toString(); } else { // GET - String urlS = url.toString(); + String urlS = this.url.toString(); if( urlS.indexOf('?') == -1 ) { urlS += '?'; } else { @@ -96,7 +118,7 @@ } urlS += sb; try { - url = new URL(urlS); + this.url = new URL(urlS); } catch(IOException e) { throw new RuntimeException(e); } @@ -107,6 +129,24 @@ } } + public static String toHttpHeaderName(String luanName) { + luanName = luanName.toLowerCase(); + 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 == '_' || c == '-' ) { + a[i] = '-'; + capitalize = true; + } else if( capitalize ) { + a[i] = Character.toUpperCase(c); + capitalize = false; + } + } + return String.valueOf(a); + } + private static void and(StringBuilder sb) { if( sb.length() > 0 ) sb.append('&');
diff -r 647602e8291a -r eaf30d5aaf6a http/src/luan/modules/http/HttpServicer.java --- a/http/src/luan/modules/http/HttpServicer.java Fri Jun 03 17:51:58 2016 -0600 +++ b/http/src/luan/modules/http/HttpServicer.java Fri Jun 03 18:43:27 2016 -0600 @@ -33,6 +33,7 @@ import luan.DeepCloner; import luan.modules.PackageLuan; import luan.modules.IoLuan; +import luan.modules.LuanUrl; import luan.modules.TableLuan; import luan.modules.Utils; @@ -193,6 +194,7 @@ } public static String toHttpHeaderName(String luanName) { +/* StringBuilder buf = new StringBuilder(); boolean capitalize = true; char[] a = luanName.toCharArray(); @@ -207,6 +209,8 @@ } } return String.valueOf(a); +*/ + return LuanUrl.toHttpHeaderName(luanName); } private static String escape(String value) {