Mercurial Hosting > luan
comparison core/src/luan/modules/LuanUrl.java @ 723:eaf30d5aaf6a
handle url headers
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 03 Jun 2016 18:43:27 -0600 |
parents | 647602e8291a |
children |
comparison
equal
deleted
inserted
replaced
722:647602e8291a | 723:eaf30d5aaf6a |
---|---|
39 this.method = Method.valueOf(methodStr); | 39 this.method = Method.valueOf(methodStr); |
40 } catch(IllegalArgumentException e) { | 40 } catch(IllegalArgumentException e) { |
41 throw new LuanException( "invalid method: "+methodStr ); | 41 throw new LuanException( "invalid method: "+methodStr ); |
42 } | 42 } |
43 } | 43 } |
44 Map headerMap = getMap(luan,map,"headers"); | |
45 if( headerMap != null ) { | |
46 headers = new HashMap(); | |
47 for( Object hack : headerMap.entrySet() ) { | |
48 Map.Entry entry = (Map.Entry)hack; | |
49 String key = (String)entry.getKey(); | |
50 Object val = entry.getValue(); | |
51 String name = toHttpHeaderName(key); | |
52 if( val instanceof String ) { | |
53 headers.put(name,val); | |
54 } else { | |
55 if( !(val instanceof LuanTable) ) | |
56 throw new LuanException( "header '"+key+"' must be string or table" ); | |
57 LuanTable t = (LuanTable)val; | |
58 if( !t.isList() ) | |
59 throw new LuanException( "header '"+key+"' table must be list" ); | |
60 headers.put(name,t.asList()); | |
61 } | |
62 } | |
63 } | |
44 Map auth = getMap(luan,map,"authorization"); | 64 Map auth = getMap(luan,map,"authorization"); |
45 if( auth != null ) { | 65 if( auth != null ) { |
66 if( headers!=null && headers.containsKey("Authorization") ) | |
67 throw new LuanException( "can't define authorization with header 'Authorization' defined" ); | |
46 String user = getString(auth,"user"); | 68 String user = getString(auth,"user"); |
47 String password = getString(auth,"password"); | 69 String password = getString(auth,"password"); |
48 if( !auth.isEmpty() ) | 70 if( !auth.isEmpty() ) |
49 throw new LuanException( "unrecognized authorization options: "+auth ); | 71 throw new LuanException( "unrecognized authorization options: "+auth ); |
50 StringBuilder sb = new StringBuilder(); | 72 StringBuilder sb = new StringBuilder(); |
86 if( this.method==Method.DELETE ) | 108 if( this.method==Method.DELETE ) |
87 throw new LuanException( "the DELETE method cannot take parameters" ); | 109 throw new LuanException( "the DELETE method cannot take parameters" ); |
88 if( this.method==Method.POST ) { | 110 if( this.method==Method.POST ) { |
89 content = sb.toString(); | 111 content = sb.toString(); |
90 } else { // GET | 112 } else { // GET |
91 String urlS = url.toString(); | 113 String urlS = this.url.toString(); |
92 if( urlS.indexOf('?') == -1 ) { | 114 if( urlS.indexOf('?') == -1 ) { |
93 urlS += '?'; | 115 urlS += '?'; |
94 } else { | 116 } else { |
95 urlS += '&'; | 117 urlS += '&'; |
96 } | 118 } |
97 urlS += sb; | 119 urlS += sb; |
98 try { | 120 try { |
99 url = new URL(urlS); | 121 this.url = new URL(urlS); |
100 } catch(IOException e) { | 122 } catch(IOException e) { |
101 throw new RuntimeException(e); | 123 throw new RuntimeException(e); |
102 } | 124 } |
103 } | 125 } |
104 } | 126 } |
105 if( !map.isEmpty() ) | 127 if( !map.isEmpty() ) |
106 throw new LuanException( "unrecognized options: "+map ); | 128 throw new LuanException( "unrecognized options: "+map ); |
107 } | 129 } |
130 } | |
131 | |
132 public static String toHttpHeaderName(String luanName) { | |
133 luanName = luanName.toLowerCase(); | |
134 StringBuilder buf = new StringBuilder(); | |
135 boolean capitalize = true; | |
136 char[] a = luanName.toCharArray(); | |
137 for( int i=0; i<a.length; i++ ) { | |
138 char c = a[i]; | |
139 if( c == '_' || c == '-' ) { | |
140 a[i] = '-'; | |
141 capitalize = true; | |
142 } else if( capitalize ) { | |
143 a[i] = Character.toUpperCase(c); | |
144 capitalize = false; | |
145 } | |
146 } | |
147 return String.valueOf(a); | |
108 } | 148 } |
109 | 149 |
110 private static void and(StringBuilder sb) { | 150 private static void and(StringBuilder sb) { |
111 if( sb.length() > 0 ) | 151 if( sb.length() > 0 ) |
112 sb.append('&'); | 152 sb.append('&'); |