import java.net.*; import java.io.*; import java.util.*; public final class HttpRequest implements Runnable { // DATA final static String CRLF = "\r\n"; // carriage return Socket clientcon; // reference to socket // CONSTRUCTOR public HttpRequest(Socket clientcon) { this.clientcon = clientcon; } // METHODS public void run() { // Can't throw exception so we have to use try-catch's try { processRequest(); } catch (Exception e) { System.out.println(e); } } private void processRequest() throws Exception { DataInputStream is = new DataInputStream(clientcon.getInputStream()); DataOutputStream os = new DataOutputStream(clientcon.getOutputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(is)); // Get the HTTP request line String requestLine = in.readLine(); System.out.println("HTTP Request: \t" + requestLine); // Process the request StringTokenizer tokens = new StringTokenizer(requestLine); tokens.nextToken(); String fileName = tokens.nextToken(); String httpVersion = tokens.nextToken(); boolean versionSupported = (httpVersion.equals("HTTP/1.1")); // Prefix with a dot (as the browser precedes with slash) fileName = "." + fileName; // FILE HANDLING FileInputStream fis = null; boolean fileExists = true; try { fis = new FileInputStream(fileName); } catch (FileNotFoundException e) { fileExists = false; } // RESPONSE MESSAGE String statusLine = null; String contentTypeLine = null; String entityBody = null; if (fileExists) { statusLine = "HTTP/1.0 200 OK"; contentTypeLine = "Content-type: " + contentType(fileName) + CRLF; } else { statusLine = "HTTP/1.0 404 Not Found"; contentTypeLine = "Content-Type: text/html"; entityBody = "" + "