private void processRequest() throws Exception { while (true) { Socket socket = socketRequestsQueue.take(); DataOutputStream socketOutputStream = new DataOutputStream(socket.getOutputStream()); HtmlRequest htmlRequest = readRequest(socket); // If the request is empty than the socket was closed on the other side if (htmlRequest.equals(null)) { try { socket.close(); } catch (Exception e) { System.out.println("Error on trying to close socket on empty request: " + e.toString()); } continue; } HtmlResponse responseToClient; if (!htmlRequest.isLegalRequest) { // The request format is illegal responseToClient = respond400(htmlRequest); } else if (!legalRequestType(htmlRequest)) { // The request method is unimplemented responseToClient = respond501(htmlRequest); } else if (directRequestToResultPages(htmlRequest)) { responseToClient = respond403(htmlRequest); } else { if (!htmlRequest.type.equals("TRACE") && !htmlRequest.type.equals("POST")) { boolean isFileLegal = false; try { isFileLegal = checkIfRequestedFileLegal(htmlRequest); } catch (IOException e) { responseToClient = respond500(htmlRequest); } if (!isFileLegal) { responseToClient = respond404(htmlRequest); } else { responseToClient = respond200(htmlRequest); } } else { responseToClient = respond200(htmlRequest); } } try { // Send the status line. socketOutputStream.writeBytes(responseToClient.getStatusLine()); // Send the content type line. socketOutputStream.writeBytes(responseToClient.getContentType()); // Send content length. if (!htmlRequest.isChunked) { socketOutputStream.writeBytes(responseToClient.getContentLengthLine()); } if (htmlRequest.isChunked) { socketOutputStream.writeBytes(responseToClient.getTransferEncoding()); } // Send a blank line to indicate the end of the header lines. socketOutputStream.writeBytes(CRLF); } catch (Exception e) { System.out.println("Writing the header caused an error" + e.toString()); } // Send the content of the HTTP. if (!htmlRequest.type.equals("HEAD")) { sendEntityBodyToClient(socketOutputStream, responseToClient, htmlRequest.isChunked); } // Close streams and socket. try { socketOutputStream.close(); socket.close(); } catch (Exception e) { System.out.println("closing the socket caused an error"); } } }