Mercurial Hosting > luan
comparison src/luan/modules/url/LuanUrl.java @ 1150:0842b9b570f8
change http headers interface
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 04 Feb 2018 18:03:37 -0700 |
parents | cb4c20fce7d0 |
children | dbb3cb906482 |
comparison
equal
deleted
inserted
replaced
1149:1b7c20e20ca7 | 1150:0842b9b570f8 |
---|---|
50 Map headerMap = getMap(luan,map,"headers"); | 50 Map headerMap = getMap(luan,map,"headers"); |
51 if( headerMap != null ) { | 51 if( headerMap != null ) { |
52 headers = new HashMap(); | 52 headers = new HashMap(); |
53 for( Object hack : headerMap.entrySet() ) { | 53 for( Object hack : headerMap.entrySet() ) { |
54 Map.Entry entry = (Map.Entry)hack; | 54 Map.Entry entry = (Map.Entry)hack; |
55 String key = (String)entry.getKey(); | 55 String name = (String)entry.getKey(); |
56 Object val = entry.getValue(); | 56 Object val = entry.getValue(); |
57 String name = toHttpHeaderName(key); | |
58 if( val instanceof String ) { | 57 if( val instanceof String ) { |
59 headers.put(name,val); | 58 headers.put(name,val); |
60 } else { | 59 } else { |
61 if( !(val instanceof LuanTable) ) | 60 if( !(val instanceof LuanTable) ) |
62 throw new LuanException( "header '"+key+"' must be string or table" ); | 61 throw new LuanException( "header '"+name+"' must be string or table" ); |
63 LuanTable t = (LuanTable)val; | 62 LuanTable t = (LuanTable)val; |
64 if( !t.isList() ) | 63 if( !t.isList() ) |
65 throw new LuanException( "header '"+key+"' table must be list" ); | 64 throw new LuanException( "header '"+name+"' table must be list" ); |
66 headers.put(name,t.asList()); | 65 headers.put(name,t.asList()); |
67 } | 66 } |
68 } | 67 } |
69 } | 68 } |
70 Map auth = getMap(luan,map,"authorization"); | 69 Map auth = getMap(luan,map,"authorization"); |
146 if( !map.isEmpty() ) | 145 if( !map.isEmpty() ) |
147 throw new LuanException( "unrecognized options: "+map ); | 146 throw new LuanException( "unrecognized options: "+map ); |
148 } | 147 } |
149 } | 148 } |
150 | 149 |
151 public static String toHttpHeaderName(String luanName) { | |
152 luanName = luanName.toLowerCase(); | |
153 StringBuilder buf = new StringBuilder(); | |
154 boolean capitalize = true; | |
155 char[] a = luanName.toCharArray(); | |
156 for( int i=0; i<a.length; i++ ) { | |
157 char c = a[i]; | |
158 if( c == '_' || c == '-' ) { | |
159 a[i] = '-'; | |
160 capitalize = true; | |
161 } else if( capitalize ) { | |
162 a[i] = Character.toUpperCase(c); | |
163 capitalize = false; | |
164 } | |
165 } | |
166 return String.valueOf(a); | |
167 } | |
168 | |
169 private static void and(StringBuilder sb) { | 150 private static void and(StringBuilder sb) { |
170 if( sb.length() > 0 ) | 151 if( sb.length() > 0 ) |
171 sb.append('&'); | 152 sb.append('&'); |
172 } | 153 } |
173 | 154 |