comparison src/luan/webserver/RequestParser.java @ 1212:220228bf1af9

better urlDecode error handling
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 12 Mar 2018 20:33:02 -0600
parents 1e23b913c327
children b99947af8b79
comparison
equal deleted inserted replaced
1211:f67628bd3582 1212:220228bf1af9
1 package luan.webserver; 1 package luan.webserver;
2 2
3 import java.io.UnsupportedEncodingException;
4 import java.net.URLDecoder;
3 import java.util.List; 5 import java.util.List;
4 import java.util.ArrayList; 6 import java.util.ArrayList;
5 import org.slf4j.Logger; 7 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory; 8 import org.slf4j.LoggerFactory;
7 import luan.lib.parser.Parser; 9 import luan.lib.parser.Parser;
68 private void parsePath() throws ParseException { 70 private void parsePath() throws ParseException {
69 int start = parser.currentIndex(); 71 int start = parser.currentIndex();
70 if( !parser.match('/') ) 72 if( !parser.match('/') )
71 throw new ParseException(parser,"bad path"); 73 throw new ParseException(parser,"bad path");
72 while( safePathChar() || parser.anyOf("&=") ); 74 while( safePathChar() || parser.anyOf("&=") );
73 request.path = Util.urlDecode( parser.textFrom(start) ); 75 request.path = urlDecode( parser.textFrom(start) );
74 } 76 }
75 77
76 private void parseQuery() throws ParseException { 78 private void parseQuery() throws ParseException {
77 do { 79 do {
78 int start = parser.currentIndex(); 80 int start = parser.currentIndex();
79 while( queryChar() ); 81 while( queryChar() );
80 String name = Util.urlDecode( parser.textFrom(start) ); 82 String name = urlDecode( parser.textFrom(start) );
81 String value = null; 83 String value = null;
82 if( parser.match('=') ) { 84 if( parser.match('=') ) {
83 start = parser.currentIndex(); 85 start = parser.currentIndex();
84 while( queryChar() || parser.match('=') ); 86 while( queryChar() || parser.match('=') );
85 value = Util.urlDecode( parser.textFrom(start) ); 87 value = urlDecode( parser.textFrom(start) );
86 } 88 }
87 if( name.length() > 0 || value != null ) { 89 if( name.length() > 0 || value != null ) {
88 if( value==null ) 90 if( value==null )
89 value = ""; 91 value = "";
90 Util.add(request.parameters,name,value); 92 Util.add(request.parameters,name,value);
174 return; 176 return;
175 this.parser = new Parser(text); 177 this.parser = new Parser(text);
176 while(true) { 178 while(true) {
177 int start = parser.currentIndex(); 179 int start = parser.currentIndex();
178 while( parser.noneOf("=;") ); 180 while( parser.noneOf("=;") );
179 String name = Util.urlDecode( parser.textFrom(start) ); 181 String name = urlDecode( parser.textFrom(start) );
180 if( parser.match('=') ) { 182 if( parser.match('=') ) {
181 start = parser.currentIndex(); 183 start = parser.currentIndex();
182 while( parser.noneOf(";") ); 184 while( parser.noneOf(";") );
183 String value = Util.urlDecode( parser.textFrom(start) ); 185 String value = urlDecode( parser.textFrom(start) );
184 request.cookies.put(name,value); 186 request.cookies.put(name,value);
185 } 187 }
186 if( parser.endOfInput() ) 188 if( parser.endOfInput() )
187 return; 189 return;
188 require( parser.match(';') ); 190 require( parser.match(';') );
252 } 254 }
253 } 255 }
254 return sb.toString(); 256 return sb.toString();
255 } 257 }
256 258
259 private String urlDecode(String s) throws ParseException {
260 try {
261 return URLDecoder.decode(s,"UTF-8");
262 } catch(UnsupportedEncodingException e) {
263 parser.rollback();
264 throw new ParseException(parser,e);
265 } catch(IllegalArgumentException e) {
266 parser.rollback();
267 throw new ParseException(parser,e);
268 }
269 }
270
257 } 271 }