Overview

For this assignment you will write a proxy server that services client requests for web pages originating from origin servers. In particular, you are being asked to write essentially a forward proxy server:

An ordinary forward proxy is an intermediate server that sits between the client and the origin server. In order to get content from the origin server, the client sends a request to the proxy naming the origin server as the target and the proxy then requests the content from the origin server and returns it to the client. The client must be specially configured to use the forward proxy to access other sites. [From Apache documentation]

Details

Your proxy server operates as follows:

  • Listen to port 8080.
  • Services each request in a separate thread.

Requests are made to your proxy server as follows (for example, this document):

http://localhost:8080/www.people.westminstercollege.edu/faculty/ggagne/fall2019/cmpt352/hand outs/hw3/index.html
  • The name of the proxy server in the URL appears in blue. (Which in this case is running on the localhost on port 8080.)
  • The origin host where the actual resource exists appears in red.
  • The resource being requested appears in green.

Design of the Proxy Server

Your proxy server must be designed as follows:

  • Read this request and parse it to obtain the origin host and resource.
  • Open a socket connection to the origin host.
  • Make a HTTP 1.1 request to the origin host for the resource.
  • Read the response from the origin host.
  • Write the response back to the requesting client.

Flow: see image.

After writing the response back to the client, the proxy server closes the socket connection to the client.

As an example, for the URL

http://localhost:8080/www.people.westminstercollege.edu/faculty/ggagne/fall2019/cmpt352/hand outs/hw3/index.html

your proxy server will first open a socket connection to the origin server on port 80. Followed by writing the following HTTP GET to the origin server:

GET /faculty/ggagne/fall2019/cmpt352/handouts/hw3/index.html HTTP/1.1
Host:​ www.people.westminstercollege.edu
Connection:​ close
  • The first two lines must be followed by a newline (\r\n)
  • The final line must be followed by two newlines (\r\n\r\n)

(The newlines are part of the HTTP protocol and must be specified.)

Furthermore, your proxy server does not need to support persistent connections so you can specify that non-persistent connections can be used with the Connection: close command.

Parsing

This assignment is largely based around parsing the requests the client browser sends to your proxy server so you can obtain the origin host and the resource that resides on the origin host that you will have to request.

Default Documents

There are some special circumstances you need to check when parsing. For example, if the following query is passed to your proxy server:

http://localhost:8080/www.amazon.com

where no resource appears after www.amazon.com, this means your request to the origin server is for the default document . In this instance, the HTTP command will be GET / where the / refers to requesting the default document.

When requesting the default document from an opigin server, the query your proxy server must construct appears as:

GET / HTTP/1.1
Host:​ amazon.com
Connection:​ close

Handling 404

Also be sure to test your proxy server so that it correctly handles 404 (resource not found) errors. (This should not require any special handling on your part, just make sure that it functions properly.)

How to Proceed

  • Create a multithreaded server that listens to port 8080
  • Initially focus on what the client browser sends your proxy server - this corresponds to step 1 of the above figure. The HttpMirror.java program may be useful when reading what the client browser sends a server.
  • Determine how you will parse the request from the client.
  • Once you have determined how to parse the request, complete steps 2 and 3 from the above figure.

The remaining work will simply involve reading what the origin server sends back to your proxy server, and writing that back to the client.

Socket I/O

This assignment will involve reading and writing between two different socket connections:

  • Reading from the client socket;
  • writing to the origin host socket;
  • reading from the origin host socket;
  • writing to the client socket.

It is suggested you use the following network API's for reading from and writing to sockets:

  • BufferedReader for reading from client.
  • DataOutputStream for writing to origin server. (Look at solution to Homework #2 for example usage.)`
  • BufferedInputStream for reading from origin server, and BufferedOutputStream for writing to the client.

Tests

Using the Google Chrome browser, I will test your proxy server against the following URLs:

http://localhost:8080/www.people.westminstercollege.edu/facultyggagne/fall201
9/cmpt352/handouts/hw3/index.html
http://localhost:8080/www.amazon.com
http://localhost:8080/www.xkcd.com
http://localhost:8080/people.westminstercollege.edu/faculty/ggagne/fall2019/c
mpt352/chapters/chapter2/photos.html

In addition, I will check the following to ensure your proxy server correctly handles 404 errors:

http://localhost:8080/www.apple.com/windowsrules.html

(Again, since you are just handing requests and responses between the client and origin host, you shouldn't have to do anything special to manage handling a 404 error.)

More good news - your proxy server does not need to worry about invalid hosts (i.e. non-existent domain names) as this would require your server generating an HTTP response message back to the client and this is material that will be covered when you design your web server. You can assume that all origin hosts are legitimate IP names.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.