changeset 1148:49fb4e83484f

webserver - change headers to lower case
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 04 Feb 2018 17:11:06 -0700
parents 30d87b7d1d62
children 1b7c20e20ca7
files src/luan/webserver/Connection.java src/luan/webserver/RequestParser.java src/luan/webserver/Response.java src/luan/webserver/examples/Cookies.java src/luan/webserver/examples/Example.java src/luan/webserver/examples/Headers.java src/luan/webserver/examples/Params.java src/luan/webserver/handlers/ContentTypeHandler.java src/luan/webserver/handlers/FileHandler.java src/luan/webserver/handlers/SafeHandler.java
diffstat 10 files changed, 20 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/webserver/Connection.java	Thu Feb 01 22:06:37 2018 -0700
+++ b/src/luan/webserver/Connection.java	Sun Feb 04 17:11:06 2018 -0700
@@ -63,7 +63,7 @@
 				RequestParser parser = new RequestParser(request);
 				parser.parseHead();
 	
-				String lenStr = (String)request.headers.get("Content-Length");
+				String lenStr = (String)request.headers.get("content-length");
 				if( lenStr != null ) {
 					int len = Integer.parseInt(lenStr);
 					byte[] body = new byte[len];
@@ -80,7 +80,7 @@
 //System.out.println(new String(request.body));
 				}
 
-				String contentType = (String)request.headers.get("Content-Type");
+				String contentType = (String)request.headers.get("content-type");
 				if( contentType != null ) {
 					if( request.body == null ) {
 						logger.error("body is null");
@@ -97,8 +97,8 @@
 			}
 
 			Response response = server.handler.handle(request);
-			response.headers.put("Connection","close");
-			response.headers.put("Content-Length",Long.toString(response.body.length));
+			response.headers.put("connection","close");
+			response.headers.put("content-length",Long.toString(response.body.length));
 			byte[] header = response.toHeaderString().getBytes();
 	
 			OutputStream out = socket.getOutputStream();
--- a/src/luan/webserver/RequestParser.java	Thu Feb 01 22:06:37 2018 -0700
+++ b/src/luan/webserver/RequestParser.java	Sun Feb 04 17:11:06 2018 -0700
@@ -123,28 +123,16 @@
 	}
 
 	private String parseName() throws ParseException {
-		StringBuilder buf = new StringBuilder();
-		boolean cap = true;
+		int start = parser.currentIndex();
 		require( tokenChar() );
-		do {
-			char c = parser.lastChar();
-			if( c == '-' ) {
-				cap = true;
-			} else if( cap ) {
-				c = Character.toUpperCase(c);
-				cap = false;
-			} else {
-				c = Character.toLowerCase(c);
-			}
-			buf.append(c);
-		} while( tokenChar() );
-		return buf.toString();
+		while( tokenChar() );
+		return parser.textFrom(start).toLowerCase();
 	}
 
-	private String parseValue() {
+	private String parseValue() throws ParseException {
 		int start = parser.currentIndex();
 		while( !testEndOfValue() )
-			parser.anyChar();
+			require( parser.anyChar() );
 		return parser.textFrom(start);
 	}
 
@@ -175,7 +163,7 @@
 
 
 	private void parseCookies() throws ParseException {
-		String text = (String)request.headers.get("Cookie");
+		String text = (String)request.headers.get("cookie");
 		if( text == null )
 			return;
 		this.parser = new Parser(text);
@@ -199,7 +187,7 @@
 	private static final String contentTypeStart = "multipart/form-data; boundary=";
 
 	void parseMultipart() throws ParseException {
-		String contentType = (String)request.headers.get("Content-Type");
+		String contentType = (String)request.headers.get("content-type");
 		if( !contentType.startsWith(contentTypeStart) )
 			throw new RuntimeException(contentType);
 		String boundary = "--"+contentType.substring(contentTypeStart.length());
--- a/src/luan/webserver/Response.java	Thu Feb 01 22:06:37 2018 -0700
+++ b/src/luan/webserver/Response.java	Sun Feb 04 17:11:06 2018 -0700
@@ -12,7 +12,7 @@
 	public volatile Status status = Status.OK;
 	public final Map<String,Object> headers = Collections.synchronizedMap(new LinkedHashMap<String,Object>());
 	{
-		headers.put("Server","ThreeBody");
+		headers.put("server","Luan");
 	}
 	public volatile Body body;
 
--- a/src/luan/webserver/examples/Cookies.java	Thu Feb 01 22:06:37 2018 -0700
+++ b/src/luan/webserver/examples/Cookies.java	Sun Feb 04 17:11:06 2018 -0700
@@ -26,7 +26,7 @@
 				response.setCookie(name,"delete",attributes);
 			}
 		}
-		response.headers.put( "Content-Type", "text/plain; charset=UTF-8" );
+		response.headers.put( "content-type", "text/plain; charset=UTF-8" );
 		try {
 			Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) );
 			for( Map.Entry<String,String> entry : request.cookies.entrySet() ) {
--- a/src/luan/webserver/examples/Example.java	Thu Feb 01 22:06:37 2018 -0700
+++ b/src/luan/webserver/examples/Example.java	Sun Feb 04 17:11:06 2018 -0700
@@ -25,7 +25,7 @@
 
 	public Response handle(Request request) {
 		Response response = new Response();
-		response.headers.put( "Content-Type", "text/plain; charset=UTF-8" );
+		response.headers.put( "content-type", "text/plain; charset=UTF-8" );
 		try {
 			Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) );
 			writer.write("Hello World\n");
--- a/src/luan/webserver/examples/Headers.java	Thu Feb 01 22:06:37 2018 -0700
+++ b/src/luan/webserver/examples/Headers.java	Sun Feb 04 17:11:06 2018 -0700
@@ -14,7 +14,7 @@
 
 	public Response handle(Request request) {
 		Response response = new Response();
-		response.headers.put( "Content-Type", "text/plain; charset=UTF-8" );
+		response.headers.put( "content-type", "text/plain; charset=UTF-8" );
 		try {
 			Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) );
 			for( Map.Entry<String,Object> entry : request.headers.entrySet() ) {
--- a/src/luan/webserver/examples/Params.java	Thu Feb 01 22:06:37 2018 -0700
+++ b/src/luan/webserver/examples/Params.java	Sun Feb 04 17:11:06 2018 -0700
@@ -14,7 +14,7 @@
 
 	public Response handle(Request request) {
 		Response response = new Response();
-		response.headers.put( "Content-Type", "text/plain; charset=UTF-8" );
+		response.headers.put( "content-type", "text/plain; charset=UTF-8" );
 		try {
 			Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) );
 			for( Map.Entry<String,Object> entry : request.parameters.entrySet() ) {
--- a/src/luan/webserver/handlers/ContentTypeHandler.java	Thu Feb 01 22:06:37 2018 -0700
+++ b/src/luan/webserver/handlers/ContentTypeHandler.java	Sun Feb 04 17:11:06 2018 -0700
@@ -8,7 +8,7 @@
 
 
 public class ContentTypeHandler implements Handler {
-	public final static String CONTENT_TYPE = "Content-Type";
+	public final static String CONTENT_TYPE = "content-type";
 
 	private final Handler handler;
 
--- a/src/luan/webserver/handlers/FileHandler.java	Thu Feb 01 22:06:37 2018 -0700
+++ b/src/luan/webserver/handlers/FileHandler.java	Sun Feb 04 17:11:06 2018 -0700
@@ -43,7 +43,7 @@
 			if( request.path.endsWith("/") && file.isDirectory() && showDirs ) {
 				DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz");
 				Response response = new Response();
-				response.headers.put( "Content-Type", "text/html; charset=UTF-8" );
+				response.headers.put( "content-type", "text/html; charset=UTF-8" );
 				Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) );
 				writer.write( "<!doctype html><html>" );
 				writer.write( "<head><style>td{padding: 2px 8px}</style></head>" );
--- a/src/luan/webserver/handlers/SafeHandler.java	Thu Feb 01 22:06:37 2018 -0700
+++ b/src/luan/webserver/handlers/SafeHandler.java	Sun Feb 04 17:11:06 2018 -0700
@@ -31,7 +31,7 @@
 			logger.error("",e);
 			Response response = new Response();
 			response.status = Status.INTERNAL_SERVER_ERROR;
-			response.headers.put( "Content-Type", "text/plain; charset=UTF-8" );
+			response.headers.put( "content-type", "text/plain; charset=UTF-8" );
 			PrintWriter writer = new PrintWriter( new ResponseOutputStream(response) );
 			writer.write( "Internel Server Error\n\n" );
 			e.printStackTrace(writer);
@@ -41,7 +41,7 @@
 
 		Response response = new Response();
 		response.status = Status.NOT_FOUND;
-		response.headers.put( "Content-Type", "text/plain; charset=UTF-8" );
+		response.headers.put( "content-type", "text/plain; charset=UTF-8" );
 		try {
 			Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) );
 			writer.write( request.path+" not found\n" );