package test.http;

import java.io.PrintWriter;

import com.firefly.codec.http2.model.HttpHeader;
import com.firefly.codec.http2.model.HttpStatus;
import com.firefly.codec.http2.model.MimeTypes;
import com.firefly.server.http2.SimpleHTTPServer;
import com.firefly.server.http2.SimpleResponse;

public class ServerDemo3 {

	public static void main(String[] args) {
		SimpleHTTPServer server = new SimpleHTTPServer();
		server.headerComplete(request -> {
			request.messageComplete(req -> {
				SimpleResponse response = req.getResponse();
				String path = req.getRequest().getURI().getPath();

				response.getResponse().getFields().put(HttpHeader.CONTENT_TYPE, MimeTypes.Type.TEXT_PLAIN.asString());
				switch (path) {
				case "/index":
					response.getResponse().getFields().put(HttpHeader.CONTENT_LENGTH, "11");
					try (PrintWriter writer = response.getPrintWriter()) {
						writer.print("hello index");
					}
					break;
				case "/testPost":
					System.out.println(req.getRequest().toString());
					System.out.println(req.getRequest().getFields());
					System.out.println(req.getStringBody());
					try (PrintWriter writer = response.getPrintWriter()) {
						writer.print("receive post -> " + req.getStringBody());
					}
					break;
				default:
					response.getResponse().setStatus(HttpStatus.NOT_FOUND_404);
					try (PrintWriter writer = response.getPrintWriter()) {
						writer.print("resource not found");
					}
					break;
				}
			});
		}).listen("localhost", 3322);
	}

}

@@ -17,37 +17,8 @@
 p;%0A%0A
-import java.io.PrintWriter;%0A%0A
 impo
@@ -258,16 +258,45 @@
 ponse;%0A%0A
+import java.io.PrintWriter;%0A%0A
 public c
@@ -315,17 +315,20 @@
 emo3 %7B%0A%0A
-%09
+    
 public s
@@ -360,18 +360,24 @@
 args) %7B%0A
-%09%09
+        
 SimpleHT
@@ -418,18 +418,24 @@
 rver();%0A
-%09%09
+        
 server.h
@@ -459,21 +459,16 @@
 uest -%3E 
-%7B%0A%09%09%09
 request.
@@ -492,20 +492,28 @@
 eq -%3E %7B%0A
-%09%09%09%09
+            
 SimpleRe
@@ -549,20 +549,28 @@
 onse();%0A
-%09%09%09%09
+            
 String p
@@ -613,20 +613,28 @@
 ath();%0A%0A
-%09%09%09%09
+            
 response
@@ -728,20 +728,28 @@
 ing());%0A
-%09%09%09%09
+            
 switch (
@@ -760,32 +760,59 @@
 ) %7B%0A
-%09%09%09%09case %22/index%22:%0A%09%09%09%09%09
+                case %22/index%22:%0A                    
 resp
@@ -880,21 +880,36 @@
  %2211%22);%0A
-%09%09%09%09%09
+                    
 try (Pri
@@ -947,38 +947,56 @@
 rintWriter()) %7B%0A
-%09%09%09%09%09%09
+                        
 writer.print(%22he
@@ -1012,54 +1012,111 @@
 %22);%0A
-%09%09%09%09%09%7D%0A%09%09%09%09%09break;%0A%09%09%09%09case %22/testPost%22:%0A%09%09%09%09%09
+                    %7D%0A                    break;%0A                case %22/testPost%22:%0A                    
 Syst
@@ -1156,29 +1156,44 @@
 oString());%0A
-%09%09%09%09%09
+                    
 System.out.p
@@ -1230,21 +1230,36 @@
 lds());%0A
-%09%09%09%09%09
+                    
 System.o
@@ -1291,21 +1291,36 @@
 ody());%0A
-%09%09%09%09%09
+                    
 try (Pri
@@ -1358,38 +1358,56 @@
 rintWriter()) %7B%0A
-%09%09%09%09%09%09
+                        
 writer.print(%22re
@@ -1450,45 +1450,102 @@
 ));%0A
-%09%09%09%09%09%7D%0A%09%09%09%09%09break;%0A%09%09%09%09default:%0A%09%09%09%09%09
+                    %7D%0A                    break;%0A                default:%0A                    
 resp
@@ -1600,21 +1600,36 @@
 D_404);%0A
-%09%09%09%09%09
+                    
 try (Pri
@@ -1679,14 +1679,32 @@
 ) %7B%0A
-%09%09%09%09%09%09
+                        
 writ
@@ -1739,43 +1739,81 @@
 %22);%0A
-%09%09%09%09%09%7D%0A%09%09%09%09%09break;%0A%09%09%09%09%7D%0A%09%09%09%7D);%0A%09%09%7D
+                    %7D%0A                    break;%0A            %7D%0A        %7D)
 ).li
@@ -1841,9 +1841,12 @@
 2);%0A
-%09
+    
 %7D%0A%0A%7D

clean up code