changeset 1736:a02a75e3daa8

webserver error handling
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 16 Oct 2022 11:44:23 -0600
parents 4b56eff90040
children 6c9aea554691
files src/goodjava/webserver/Connection.java src/goodjava/webserver/Request.java src/goodjava/webserver/RequestParser.java src/goodjava/webserver/handlers/ContentTypeHandler.java
diffstat 4 files changed, 12 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/goodjava/webserver/Connection.java	Sun Oct 02 17:32:00 2022 -0600
+++ b/src/goodjava/webserver/Connection.java	Sun Oct 16 11:44:23 2022 -0600
@@ -26,6 +26,7 @@
 	}
 
 	private void handle() {
+		String rawHead = "";
 		try {
 			Request request = new Request();
 			Response response;
@@ -61,7 +62,7 @@
 							left = a.length - size;
 						}
 					}
-					String rawHead = new String(a,0,endOfHeader);
+					rawHead = new String(a,0,endOfHeader);
 					//System.out.println(rawHead);
 					request.rawHead = rawHead;
 					RequestParser parser = new RequestParser(request);
@@ -124,7 +125,10 @@
 			out.close();
 			socket.close();
 		} catch(IOException e) {
-			logger.info("",e);
+			logger.info(rawHead.trim()+"\n",e);
+		} catch(RuntimeException e) {
+			logger.error(rawHead.trim()+"\n",e);
+			throw e;
 		}
 	}
 
--- a/src/goodjava/webserver/Request.java	Sun Oct 02 17:32:00 2022 -0600
+++ b/src/goodjava/webserver/Request.java	Sun Oct 16 11:44:23 2022 -0600
@@ -19,6 +19,10 @@
 	public final Map<String,String> cookies = Collections.synchronizedMap(new LinkedHashMap<String,String>());
 	public volatile byte[] body;
 
+	public String url() {
+		return scheme + "://" + headers.get("Host") + rawPath;
+	}
+
 	public static final class MultipartFile {
 		public final String filename;
 		public final String contentType;
--- a/src/goodjava/webserver/RequestParser.java	Sun Oct 02 17:32:00 2022 -0600
+++ b/src/goodjava/webserver/RequestParser.java	Sun Oct 16 11:44:23 2022 -0600
@@ -177,7 +177,7 @@
 				while( parser.noneOf(";") );
 				String value = parser.textFrom(start);
 				int len = value.length();
-				if( value.charAt(0)=='"' && value.charAt(len-1)=='"' )
+				if( len > 0 && value.charAt(0)=='"' && value.charAt(len-1)=='"' )
 					value = value.substring(1,len-1);
 				value = urlDecode(value);
 				request.cookies.put(name,value);
--- a/src/goodjava/webserver/handlers/ContentTypeHandler.java	Sun Oct 02 17:32:00 2022 -0600
+++ b/src/goodjava/webserver/handlers/ContentTypeHandler.java	Sun Oct 16 11:44:23 2022 -0600
@@ -59,7 +59,7 @@
 				if( type != null )
 					response.headers.put("Content-Type",type);
 				else
-					logger.info("no type defined for extension: "+extension);
+					logger.info(request.url()+": no type defined for extension: "+extension);
 			}
 		}
 		return response;