| 
									
										
										
										
											2022-05-02 21:03:16 -05:00
										 |  |  |         try { | 
					
						
							| 
									
										
										
										
											2022-05-03 13:45:03 +00:00
										 |  |  |             socket = new ServerSocket(sigPlace.PORT); | 
					
						
							|  |  |  |             System.out.println("Listening on port "+sigPlace.PORT+"."); | 
					
						
							| 
									
										
										
										
											2022-05-02 21:03:16 -05:00
										 |  |  |             while (true) { | 
					
						
							|  |  |  |                 try (Socket client = socket.accept()) { | 
					
						
							|  |  |  |                     System.out.println("New client connection detected: "+client.toString()); | 
					
						
							|  |  |  |                     BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); | 
					
						
							|  |  |  |                     String line; | 
					
						
							|  |  |  |                     line=in.readLine(); //Read the first line, this should be our request. | 
					
						
							| 
									
										
										
										
											2022-05-03 14:58:01 +00:00
										 |  |  |                     if (line!=null) { | 
					
						
							|  |  |  |                         String[] splitter = line.split(Pattern.quote(" ")); | 
					
						
							|  |  |  |                         if (splitter.length==3) { | 
					
						
							|  |  |  |                             //This is valid. | 
					
						
							|  |  |  |                             if (splitter[0].equals("GET")) { //This is a GET request. | 
					
						
							|  |  |  |                                 if (splitter[2].equals("HTTP/1.1")||splitter[2].equals("HTTP/2.0")) { | 
					
						
							|  |  |  |                                     String[] requestSplit = splitter[1].split(Pattern.quote("?")); | 
					
						
							|  |  |  |                                     String requestloc = requestSplit[0]; | 
					
						
							|  |  |  |                                     HashMap<String,String> requestParams = new HashMap<>(); | 
					
						
							|  |  |  |                                     if (requestSplit.length>1) { | 
					
						
							|  |  |  |                                         String[] params = requestSplit[1].split(Pattern.quote("&")); | 
					
						
							|  |  |  |                                         for (String s : params) { | 
					
						
							|  |  |  |                                             String key = s.substring(0,s.indexOf('=')); | 
					
						
							|  |  |  |                                             String value = s.substring(s.indexOf('=')+1); | 
					
						
							|  |  |  |                                             requestParams.put(key,value); | 
					
						
							|  |  |  |                                         } | 
					
						
							|  |  |  |                                         System.out.println("  ==Params for this request are: "+requestParams); | 
					
						
							|  |  |  |                                     } | 
					
						
							|  |  |  |                                     if (requestloc.equals("/")) { | 
					
						
							|  |  |  |                                         //Send default directory. | 
					
						
							|  |  |  |                                         CreateRequest(client,"200","OK","testfile.html"); | 
					
						
							|  |  |  |                                     } else { | 
					
						
							| 
									
										
										
										
											2022-05-04 18:46:53 +00:00
										 |  |  |                                         CreateRequest(client,"200","OK",URLDecoder.decode(requestloc.replaceFirst("/",""),StandardCharsets.UTF_8)); | 
					
						
							| 
									
										
										
										
											2022-05-03 12:17:58 +00:00
										 |  |  |                                     } | 
					
						
							| 
									
										
										
										
											2022-05-02 21:03:16 -05:00
										 |  |  |                                 } | 
					
						
							| 
									
										
										
										
											2022-05-03 14:58:01 +00:00
										 |  |  |                             } else { | 
					
						
							|  |  |  |                                 CreateRequest(client,"501","Not Implemented","testfile.html"); | 
					
						
							| 
									
										
										
										
											2022-05-02 21:03:16 -05:00
										 |  |  |                             } | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2022-05-03 14:58:01 +00:00
										 |  |  |                         while (!(line=in.readLine()).isBlank()) { | 
					
						
							| 
									
										
										
										
											2022-05-06 17:40:32 +00:00
										 |  |  |                             System.out.println(line); |