Question

In: Computer Science

The goal of this lab is to write a simple, but functional, web server that is...

The goal of this lab is to write a simple, but functional, web server that is capable of sending files to a web browser on request. The web server must create a listening socket and accept connections. It must implement just enough of the HTTP/1.1 protocol to enable it to read requests for files and to send the requested files. It should also be able to send error responses to the client when appropriate.


It would be useful to see the HTTP protocol in action before beginning your work. Your program will have to read HTTP requests and send HTTP responses

To see what an HTTP request might look like, you can run the program ReadRequest, which can be found in the code directory. To run the program, cd to that directory on the command line, and enter the command java ReadRequest. The program is ready to accept requests from web browsers on port 50505. (It will continue to do so until you terminate the program.) When it receives a request, it simply prints out the request, including all headers. It then closes the connection; it does not send any response back to the browser. To see a request, open a web browser, and enter the URL http://localhost:50505/path/file.txt into the browser's location box. The request will be output to the console window where you are running the ReadRequest program. The first line of the request should be GET /path/file.txt HTTP/1.1". This will be followed by several headers.

To send an HTTP request and see the response, you can use the standard telnet program. Type the following lines carefully in a console window:

telnet google.com 80

Add a blank line, by pressing return twice after typing the last line. This sends a legal HTTP request for the file index.html. The web server on math.hws.edu will respond by sending a status line followed by some headers and a blank line, followed by the contents of the file. You can try to get some error responses, if you want, such as by asking for a non-existent file instead of index.html or by using a different method instead of GET.

Start a new Eclipse project and create your main program class. The basic programming for a server is pretty standard: It just has to create a ServerSocket and use it to accept connection requests. For the main routine in your program, you can just use the main routine from ReadRequest.java. You can copy-and-paste it from here:

public static void main(String[] args) {
ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(LISTENING_PORT);
}
catch (Exception e) {
System.out.println("Failed to create listening socket.");
return;
}
System.out.println("Listening on port " + LISTENING_PORT);
try {
while (true) {
Socket connection = serverSocket.accept();
System.out.println("\nConnection from "
+ connection.getRemoteSocketAddress());
handleConnection(connection);
}
}
catch (Exception e) {
System.out.println("Server socket shut down unexpectedly!");
System.out.println("Error: " + e);
System.out.println("Exiting.");
}
}

The problem is to write the handleConnection() method. This method gets an already connected socket as a parameter. It can use that socket to get an InputStream and an OutputStream for communicating over the connection. It can read the request from the input stream and send a response to the output stream. Finally, it can close the connection. You can use some ideas (and maybe some code) from the handleConnection method in ReadRequest.java, but the method that you are writing will be a good deal more complicated.

It is very important that:

(a) the handleConnection method should catch and handle any exception that occurs so that the exception does not crash the whole server, and

(b) the socket must be closed at the end of the method. Use a try..catch..finally statement to make sure that (a) and (b) are done correctly. See ReadRequest.java for an example.

Your program should be ready to send error responses to the client, as well as fulfilling legitimate requests. The next section asks you to implement error-handling. For now, you can simply return from the handleConnection method when you detect an error.

The first three tokens that you read from the input stream should be "GET", the path to the file that is being requested, and "HTTP/1.1" (or, just possibly, "HTTP/1.0"). If you can't read three tokens, or if they are not of the expected form, you should consider that to be an error.

Assuming that the request has the correct form, you want to try to find the requested file and send it in a response over the output stream. All the files that are available on your server should be in some directory, which is called the root directory of the server. You can use any directory that you want as your root directory, as long as you can read that directory. For example, if you want to serve up files from Professor Corliss's web directory, you can set

String rootDirectory = "/home/mcorliss/www"

Of course, you could also use your own www directory. Assuming that rootDirectory is the root directory of your server and pathToFile is the path to the file as given in the request from the browser, then the full name of the file is rootDirectory + pathToFile, and you can create a File object to represent the file that is being requested as follows:

File file = new File(rootDirectory + pathToFile);

Note that:

the method file.exists() can be used to check whether the requested file actually exists
the method file.isDirectory() tests whether the file is actually a directory rather than a regular file
the method file.canRead() tests whether you can read the file
the method file.length() tells you the length of the file, that is, how many bytes of data it contains.
Once you have found the file and know that it is a regular file and that you can read it, you are ready to send a response to the browser. (If the file is a directory, you can't send it, but a typical server, in this case, will send the contents of a file named index.html in that directory, if it exists. You can think about how to implement this if you want.) Before you send the file itself, you have to send the status line, some headers, and an empty line. You can use a PrintWriter to do this. However, the HTTP protocol specifies that ends-of-line should be indicated by "\r\n" rather than the "\n" that is standard in Linux. Although I have found that it doesn't matter when sending text documents, it does seem to matter when sending images. So, instead of using out.println(x), you should use out.print(x + "\r\n") to send a line of text, and use out.print("\r\n") to send a blank line. The status line to indicate a good response should be:

HTTP/1.1 200 OK

(with "\r\n" at the end). You should send three headers: "Connection", "Content-Length", and "Content-Type". For the Connection header, you can send

Connection: close

which informs the browser that you are going to close the connection after sending the file. The Content-Length header should specify the number of bytes in the file, which you can find with the file.length() method. The Content-Type header tells the browser what kind of data is in the file. It can generally be determined from the extension part of the file name. Here is a method that will return the proper content type for many kinds of files:

private static String getMimeType(String fileName) {
int pos = fileName.lastIndexOf('.');
if (pos < 0) // no file extension in name
return "x-application/x-unknown";
String ext = fileName.substring(pos+1).toLowerCase();
if (ext.equals("txt")) return "text/plain";
else if (ext.equals("html")) return "text/html";
else if (ext.equals("htm")) return "text/html";
else if (ext.equals("css")) return "text/css";
else if (ext.equals("js")) return "text/javascript";
else if (ext.equals("java")) return "text/x-java";
else if (ext.equals("jpeg")) return "image/jpeg";
else if (ext.equals("jpg")) return "image/jpeg";
else if (ext.equals("png")) return "image/png";
else if (ext.equals("gif")) return "image/gif";
else if (ext.equals("ico")) return "image/x-icon";
else if (ext.equals("class")) return "application/java-vm";
else if (ext.equals("jar")) return "application/java-archive";
else if (ext.equals("zip")) return "application/zip";
else if (ext.equals("xml")) return "application/xml";
else if (ext.equals("xhtml")) return"application/xhtml+xml";
else return "x-application/x-unknown";
// Note: x-application/x-unknown is something made up;
// it will probably make the browser offer to save the file.
}

Putting all this together, the beginning of the response might look something like:
HTTP/1.1 200 OK
Connection: close
Content-Type: text/html
Content-Length: 3572

with a blank line at the end. And don't forget to flush the PrintWriter after sending this data.

Finally, it's time to send the data from the file itself! The file is not necessarily text, and in any case it should be sent as a stream of bytes. The following method can be used to copy the content of the file to the socket's output stream:

private static void sendFile(File file, OutputStream socketOut) throws
IOException {
InputStream in = new BufferedInputStream(new FileInputStream(file));
OutputStream out = new BufferedOutputStream(socketOut);
while (true) {
int x = in.read(); // read one byte from file
if (x < 0)
break; // end of file reached
out.write(x); // write the byte to the socket
}
out.flush();
}

At this point, your web server program should work for legal requests for valid files. Note, by the way, that you can try telnetting to your server and sending it a request, to see what response it actually sends.


Error Handling

If something goes wrong with a request, the server should nevertheless send a response. The first line of a response always contains a status code indicating the status of the response, as well as a textual description of the status. For a normal response, the status code is 200, indicating success. Status codes for errors are in the 400s and 500s. For example, the status code 404 is used when the request asks for a file that does not exist on the server. The first line of the response, in this case, is "HTTP/1.1 404 Not

Found". The response should also include, at least, a "Content-type" header and the content of the page that is to be displayed in the browser to inform the user of the error. For example, the complete response for a 404 error might look like:

HTTP/1.1 404 Not Found
Connection: close
Content-Type: text/html

<html><head><title>Error</title></head><body>
<h2>Error: 404 Not Found</h2>
<p>The resource that you requested does not exist on this server.</p>
</body></html>

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

class ReadRequest {
  static int LISTENING_PORT = 50505;
  static String rootDirectory = "/home/mcorliss/www";

  public static void main(String[] args) {
    ServerSocket serverSocket;
    try {
      serverSocket = new ServerSocket(LISTENING_PORT);
    } catch (Exception e) {
      System.out.println("Failed to create listening socket.");
      return;
    }
    System.out.println("Listening on port " + LISTENING_PORT);
    try {
      while (true) {
        Socket connection = serverSocket.accept();
        System.out.println(
          "\nConnection from " + connection.getRemoteSocketAddress()
        );

        ConnectionThread cT = new ConnectionThread(connection);

        cT.start();
      }
    } catch (Exception e) {
      System.out.println("Server socket shut down unexpectedly!");
      System.out.println("Error: " + e);
      System.out.println("Exiting.");
    }
  }

  static void sendErrorResponse(int errorCode, OutputStream socketOut) {
    String header = errorCode + "Not Found";
    header =
      header + "\\n" + "Connection: close" + "\\n" + "Content-Type: text/html";
    header =
      header +
      "\\n" +
      "<html><head><title>Error</title></head><body><h2>Error: 404 Not Found</h2>" +
      "<p>The resource that you requested does not exist on this server.</p>" +
      "</body></html>";
    try {
      socketOut.write(header.getBytes());
      socketOut.flush();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      System.out.println(e.getStackTrace());
      e.printStackTrace();
    }
  }

  static void sendSuccessResponse(String fileType, OutputStream socketOut) {
    String header = "HTTP/1.1 200 Ok";
    header =
      header +
      "\\n" +
      "Connection: close" +
      "\\n" +
      "Content-Type: " +
      fileType;
    try {
      socketOut.write(header.getBytes());
      socketOut.flush();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      System.out.println(e.getStackTrace());
      e.printStackTrace();
    }
  }

  private static String getMimeType(String fileName) {
    int pos = fileName.lastIndexOf('.');
    if (pos < 0) return "x-application/x-unknown"; // no file extension in name
    String ext = fileName.substring(pos + 1).toLowerCase();
    if (ext.equals("txt")) return "text/plain"; else if (
      ext.equals("html")
    ) return "text/html"; else if (
      ext.equals("htm")
    ) return "text/html"; else if (
      ext.equals("css")
    ) return "text/css"; else if (
      ext.equals("js")
    ) return "text/javascript"; else if (
      ext.equals("java")
    ) return "text/x-java"; else if (
      ext.equals("jpeg")
    ) return "image/jpeg"; else if (
      ext.equals("jpg")
    ) return "image/jpeg"; else if (
      ext.equals("png")
    ) return "image/png"; else if (
      ext.equals("gif")
    ) return "image/gif"; else if (
      ext.equals("ico")
    ) return "image/x-icon"; else if (
      ext.equals("class")
    ) return "application/java-vm"; else if (
      ext.equals("jar")
    ) return "application/java-archive"; else if (
      ext.equals("zip")
    ) return "application/zip"; else if (
      ext.equals("xml")
    ) return "application/xml"; else if (
      ext.equals("xhtml")
    ) return "application/xhtml+xml"; else return "x-application/x-unknown";
    // Note: x-application/x-unknown is something made up;
    // it will probably make the browser offer to save the file.
  }

  private static void sendFile(File file, OutputStream socketOut)
    throws IOException {
    InputStream in = new BufferedInputStream(new FileInputStream(file));
    OutputStream out = new BufferedOutputStream(socketOut);
    while (true) {
      int x = in.read(); // read one byte from file
      if (x < 0) break; // end of file reached
      out.write(x); // write the byte to the socket
    }
    out.flush();
  }

  private static class ConnectionThread extends Thread {
    Socket connection;

    ConnectionThread(Socket connection) {
      this.connection = connection;
    }

    public void run() {
      handleConnection(connection);
    }
  }

  private static void handleConnection(Socket connection) {
    try {
      Scanner in = new Scanner(connection.getInputStream());
      while (true) {
        if (!in.hasNextLine()) break;
        String line = in.nextLine();
        if (line.trim().length() == 0) break;
        if (line.startsWith("GET")) {
          System.out.println("Got the request " + line);
          String req = line.split(" ")[1];
          System.out.println("Request file " + req);

          OutputStream outStrm = connection.getOutputStream();
          BufferedOutputStream bout = new BufferedOutputStream(outStrm);

          File reqFile = new File(
            System.getProperty("user.dir") + "\\src\\" + rootDirectory + req
          );
          //System.out.println(ReadRequest.class.getProtectionDomain().getCodeSource().getLocation().getFile());

          System.out.println();
          System.out.println(reqFile.getAbsolutePath());
          if (reqFile.exists()) {
            //System.out.println(reqFile.getAbsolutePath());
            if (reqFile.isDirectory()) {
              for (File file : reqFile.listFiles()) {
                bout.write(file.getName().getBytes());
              }
            } else {
              sendSuccessResponse(getMimeType(reqFile.getName()), bout);
              sendFile(reqFile, bout);
            }
          } else {
            System.out.println(" File not Containing");
            sendErrorResponse(404, bout);
          }

          bout.write(req.getBytes());
          bout.flush();
        }
      }
    } catch (Exception e) {
      System.out.println("Error while communicating with client: " + e);
    } finally { // make SURE connection is closed before returning!
      try {
        connection.close();
      } catch (Exception e) {}
      System.out.println("Connection closed.");
    }
  }
}

Related Solutions

In this assignment, you will develop a simple Web server in Python that is capable of...
In this assignment, you will develop a simple Web server in Python that is capable of processing only one request. Specifically, your Web server will (i) create a connection socket when contacted by a client (browser); (ii) receive the HTTP request from this connection; (iii) parse the request to determine the specific file being requested; (iv) get the requested file from the server’s file system; (v) create an HTTP response message consisting of the requested file preceded by header lines;...
How do I make a simple TCP python web client and web server using only "import...
How do I make a simple TCP python web client and web server using only "import socket"? Basically, the client connects to the server, and sends a HTTP GET request for a specific file (like a text file, HTML page, jpeg, png etc), the server checks for the file and sends a copy of the data to the client along with the response headers (like 404 if not found, or 200 if okay etc). The process would be: You first...
Web Server is the computer that stores Web Server Software and Website. If you are running...
Web Server is the computer that stores Web Server Software and Website. If you are running some service like Food Panda which type of Hosting Server will be used. Answer your question by discussion and comparison of different types of web hosting? If you have low budget so what will be the best possible hosting plan in this situation? Justify your answer by logical reasoning.
Objective: The goal of this lab is to practice (ragged) 2D arrays and simple recursion (in...
Objective: The goal of this lab is to practice (ragged) 2D arrays and simple recursion (in two separate parts). You are not allowed to use any of the built-in classes for this lab. If you find yourself needing to import anything at the top of your class, you are doing it wrong. Part A a) Create a class method called printArray2D that accepts a 2D integer array and prints out the values formatted such that each value occupies 4 spaces...
Discuss the main similarity and difference between a dedicated web server and a co-located web server....
Discuss the main similarity and difference between a dedicated web server and a co-located web server. Group of answer choices Both of them are mainly used for small to medium-size web sites. Both of them are mainly used for large to enterprise-size web sites. Both of them are kept and connected to the Internet at the web host provider's location. One of them is kept and connected to the Internet at the web host provider's location, while the other is...
You need to design a Web Server, Database Server and a Backup server. If you had...
You need to design a Web Server, Database Server and a Backup server. If you had to choose from the following list of resources which ones would you place a priority on and state why you would do so. List these for each server type. Hint: You need to think about the functionality of the server. Based on this information, which resource would you emphasize on the most to increase the performance of the server. CPU utilization and speed Multiprocessing...
Describe the process involving the transmission of a Web page from a Web server to a...
Describe the process involving the transmission of a Web page from a Web server to a user’s computer.
Computer/Network Security How do you implement write access through web server?
Computer/Network Security How do you implement write access through web server?
AWS screenshot of a view of the web browser connection to your web server via the...
AWS screenshot of a view of the web browser connection to your web server via the load balancer (step 5 of this lab document).
How are the web frameworks - Spring, Google Web Toolkit, and Java Server Faves - similar...
How are the web frameworks - Spring, Google Web Toolkit, and Java Server Faves - similar and how are they different?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT