Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE READ CAREFULLY: There is a lot missing from this method. For instance, nothing is returned for GIF or JPEG files. You may want to

PLEASE READ CAREFULLY:

There is a lot missing from this method. For instance, nothing is returned for GIF or JPEG files. You may want to add the missing file types yourself, so that the components of your home page are sent with the content type correctly specified in the content type header line. For GIFs the MIME type is image/gif and for JPEGs it is image/jpeg.

This completes the code for the second phase of development of your Web server. Try running the server from the directory where your home page is located, and try viewing your home page files with a browser. Remember to include a port specifier in the URL of your home page, so that your browser doesn't try to connect to the default port 80. When you connect to the running web server with the browser, examine the GET message requests that the web server receives from the browser.

I need help with filling in the rest of my code where the ... and ?s are.

Here is my code so far:

import java.io.* ; import java.net.* ; import java.util.* ; public final class WebServer { public static void main(String argv[]) throws Exception { . . . } } final class HttpRequest implements Runnable {

. . . }

public static void main(String argv[]) throws Exception { // Set the port number. int port = 6789; . . . }

// Establish the listen socket. ? // Process HTTP service requests in an infinite loop. while (true) { // Listen for a TCP connection request. ? . . . }

// Construct an object to process the HTTP request message. HttpRequest request = new HttpRequest( ? ); // Create a new thread to process the request. Thread thread = new Thread(request); // Start the thread. thread.start();

final class HttpRequest implements Runnable { final static String CRLF = " "; Socket socket; // Constructor public HttpRequest(Socket socket) throws Exception { this.socket = socket; } // Implement the run() method of the Runnable interface. public void run() { . . . } private void processRequest() throws Exception { . . . } }

// Implement the run() method of the Runnable interface. public void run() { try { processRequest(); } catch (Exception e) { System.out.println(e); } }

private void processRequest() throws Exception { // Get a reference to the socket's input and output streams. InputStream is = ?; DataOutputStream os = ?; // Set up input stream filters. ? BufferedReader br = ?; . . . }

// Get the request line of the HTTP request message. String requestLine = ?; // Display the request line. System.out.println(); System.out.println(requestLine);

// Get and display the header lines. String headerLine = null; while ((headerLine = br.readLine()).length() != 0) { System.out.println(headerLine); }

(headerLine = br.readLine()).length()

// Close streams and socket. os.close(); br.close(); socket.close();

/*After your program successfully compiles, run it with an available port number, and try contacting it from a browser. To do this, you should enter into the browser's address text box the IP address of your running server. For example, if your machine name is host.someschool.edu, and you ran the server with port number 6789, then you would specify the following URL:

http://host.someschool.edu:6789/ */

// Extract the filename from the request line. StringTokenizer tokens = new StringTokenizer(requestLine); tokens.nextToken(); // skip over the method, which should be "GET" String fileName = tokens.nextToken(); // Prepend a "." so that file request is within the current directory. fileName = "." + fileName;

// Open the requested file. FileInputStream fis = null; boolean fileExists = true;

try { fis = new FileInputStream(fileName); } catch (FileNotFoundException e) { fileExists = false; }

// Construct the response message. String statusLine = null; String contentTypeLine = null; String entityBody = null; if (fileExists) { statusLine = ?; contentTypeLine = "Content-type: " + contentType( fileName ) + CRLF; } else { statusLine = ?; contentTypeLine = ?; entityBody = "" + "Not Found" + "Not Found"; }

// Send the status line. os.writeBytes(statusLine); // Send the content type line. os.writeBytes(?); // Send a blank line to indicate the end of the header lines. os.writeBytes(CRLF);

// Send the entity body. if (fileExists) { sendBytes(fis, os); fis.close(); } else { os.writeBytes(?); }

private static void sendBytes(FileInputStream fis, OutputStream os) throws Exception { // Construct a 1K buffer to hold bytes on their way to the socket. byte[] buffer = new byte[1024]; int bytes = 0; // Copy requested file into the socket's output stream. while((bytes = fis.read(buffer)) != -1 ) { os.write(buffer, 0, bytes); } }

private static String contentType(String fileName) { if(fileName.endsWith(".htm") || fileName.endsWith(".html")) { return "text/html"; } if(?) { ?; } if(?) { ?; } return "application/octet-stream"; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

How does selection differ from recruitment ?

Answered: 1 week ago