changeset 1212:220228bf1af9

better urlDecode error handling
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 12 Mar 2018 20:33:02 -0600
parents f67628bd3582
children d5d5d29d7592
files src/luan/lib/parser/Parser.java src/luan/webserver/RequestParser.java src/luan/webserver/Util.java
diffstat 3 files changed, 20 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/lib/parser/Parser.java	Mon Mar 12 16:32:55 2018 -0600
+++ b/src/luan/lib/parser/Parser.java	Mon Mar 12 20:33:02 2018 -0600
@@ -35,7 +35,7 @@
 	}
 
 	public void rollback() {
-		stack[frame] = stack[frame-1];
+		stack[frame] = frame==0 ? 0 : stack[frame-1];
 	}
 
 	public <T> T success(T t) {
--- a/src/luan/webserver/RequestParser.java	Mon Mar 12 16:32:55 2018 -0600
+++ b/src/luan/webserver/RequestParser.java	Mon Mar 12 20:33:02 2018 -0600
@@ -1,5 +1,7 @@
 package luan.webserver;
 
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
 import java.util.List;
 import java.util.ArrayList;
 import org.slf4j.Logger;
@@ -70,19 +72,19 @@
 		if( !parser.match('/') )
 			throw new ParseException(parser,"bad path");
 		while( safePathChar() || parser.anyOf("&=") );
-		request.path = Util.urlDecode( parser.textFrom(start) );
+		request.path = urlDecode( parser.textFrom(start) );
 	}
 
 	private void parseQuery() throws ParseException {
 		do {
 			int start = parser.currentIndex();
 			while( queryChar() );
-			String name = Util.urlDecode( parser.textFrom(start) );
+			String name = urlDecode( parser.textFrom(start) );
 			String value = null;
 			if( parser.match('=') ) {
 				start = parser.currentIndex();
 				while( queryChar() || parser.match('=') );
-				value = Util.urlDecode( parser.textFrom(start) );
+				value = urlDecode( parser.textFrom(start) );
 			}
 			if( name.length() > 0 || value != null ) {
 				if( value==null )
@@ -176,11 +178,11 @@
 		while(true) {
 			int start = parser.currentIndex();
 			while( parser.noneOf("=;") );
-			String name = Util.urlDecode( parser.textFrom(start) );
+			String name = urlDecode( parser.textFrom(start) );
 			if( parser.match('=') ) {
 				start = parser.currentIndex();
 				while( parser.noneOf(";") );
-				String value = Util.urlDecode( parser.textFrom(start) );
+				String value = urlDecode( parser.textFrom(start) );
 				request.cookies.put(name,value);
 			}
 			if( parser.endOfInput() )
@@ -254,4 +256,16 @@
 		return sb.toString();
 	}
 
+	private String urlDecode(String s) throws ParseException {
+		try {
+			return URLDecoder.decode(s,"UTF-8");
+		} catch(UnsupportedEncodingException e) {
+			parser.rollback();
+			throw new ParseException(parser,e);
+		} catch(IllegalArgumentException e) {
+			parser.rollback();
+			throw new ParseException(parser,e);
+		}
+	}
+
 }
--- a/src/luan/webserver/Util.java	Mon Mar 12 16:32:55 2018 -0600
+++ b/src/luan/webserver/Util.java	Mon Mar 12 20:33:02 2018 -0600
@@ -1,7 +1,6 @@
 package luan.webserver;
 
 import java.io.UnsupportedEncodingException;
-import java.net.URLDecoder;
 import java.net.URLEncoder;
 import java.util.Map;
 import java.util.List;
@@ -10,14 +9,6 @@
 
 final class Util {
 
-	static String urlDecode(String s) {
-		try {
-			return URLDecoder.decode(s,"UTF-8");
-		} catch(UnsupportedEncodingException e) {
-			throw new RuntimeException(e);
-		}
-	}
-
 	static String urlEncode(String s) {
 		try {
 			return URLEncoder.encode(s,"UTF-8");