Programming Assignment: Development of a Proxy Cache



Similar documents
Building a Multi-Threaded Web Server

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Mail User Agent Project

Lab 5: HTTP Web Proxy Server

Network/Socket Programming in Java. Rajkumar Buyya

Creating a Simple, Multithreaded Chat System with Java

Division of Informatics, University of Edinburgh

B.Sc (Honours) - Software Development

Learning Outcomes. Networking. Sockets. TCP/IP Networks. Hostnames and DNS TCP/IP

Lesson: All About Sockets

Intel Retail Client Manager Audience Analytics

CS506 Web Design and Development Solved Online Quiz No. 01

Java Network Programming. The java.net package contains the Socket class. This class speaks TCP (connection-oriented protocol).

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

!"# $ %!&' # &! ())*!$

String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivepacket.getaddress(); int port = receivepacket.

Internet and Intranet Protocols and Applications

JAVA Program For Processing SMS Messages

Application Development with TCP/IP. Brian S. Mitchell Drexel University

CS170 Lab 11 Abstract Data Types & Objects

The Java I/O System. Binary I/O streams (ascii, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

NETWORK PROGRAMMING IN JAVA USING SOCKETS

Network Communication

Sample CSE8A midterm Multiple Choice (circle one)

Assignment 4 Solutions

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

An Overview of Java. overview-1

Advanced Network Programming Lab using Java. Angelos Stavrou

Introduction to Java. Module 12: Networking (Java Sockets) Prepared by Costantinos Costa for EPL 233. ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 1

The HTTP Plug-in. Table of contents

Java Interview Questions and Answers

Network Technologies

Load Balancing BEA WebLogic Servers with F5 Networks BIG-IP v9

Example of a Java program

TP1 : Correction. Rappels : Stream, Thread et Socket TCP

Project Report on Implementation and Testing of an HTTP/1.0 Webserver

Explain the relationship between a class and an object. Which is general and which is specific?

Java Network. Slides prepared by : Farzana Rahman

Socket Programming in Java

J a v a Quiz (Unit 3, Test 0 Practice)

Continuous Integration Part 2

13 File Output and Input

public static void main(string[] args) { System.out.println("hello, world"); } }

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

INPUT AND OUTPUT STREAMS

Chapter 2 Introduction to Java programming

VIRTUAL LABORATORY: MULTI-STYLE CODE EDITOR

CS105, Spring 2016 Proxy Lab: Writing a Caching Web Proxy See course webpage for due dates

What is an I/O Stream?

CS 111 Classes I 1. Software Organization View to this point:

CISC 4700 L01 Network & Client- Server Programming Spring Harold, Chapter 8: Sockets for Clients

Manual. Programmer's Guide for Java API

Lecture 2-ter. 2. A communication example Managing a HTTP v1.0 connection. G.Bianchi, G.Neglia, V.Mancuso

Introduction to Java

Transport layer protocols. Message destination: Socket +Port. Asynchronous vs. Synchronous. Operations of Request-Reply. Sockets

AVRO - SERIALIZATION

Java Programming Fundamentals

WRITING DATA TO A BINARY FILE

AP Computer Science Java Subset

FTP client Selection and Programming

Introducing the Microsoft IIS deployment guide

HTTP. Internet Engineering. Fall Bahador Bakhshi CE & IT Department, Amirkabir University of Technology

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Socket programming. Complement for the programming assignment INFO-0010

The Web: some jargon. User agent for Web is called a browser: Web page: Most Web pages consist of: Server for Web is called Web server:

CSS 543 Program 3: Online Tic-Tac-Toe Game Professor: Munehiro Fukuda Due date: see the syllabus

Programming Languages CIS 443

Introduktion til distribuerede systemer uge 37 - fil og webserver

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Data Communication & Networks G

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Form Validation. Server-side Web Development and Programming. What to Validate. Error Prevention. Lecture 7: Input Validation and Error Handling

Zebra and MapReduce. Table of contents. 1 Overview Hadoop MapReduce APIs Zebra MapReduce APIs Zebra MapReduce Examples...

5.2 Q2 The control variable of a counter-controlled loop should be declared as: a.int. b.float. c.double. d.any of the above. ANS: a. int.

ExempleRMI.java. // Fichier de defintion des droits et proprietes // System.setProperty("java.security.policy","../server.java.

Tutorial Reference Manual. Java WireFusion 4.1

Communicating with a Barco projector over network. Technical note

Servlets. Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun

CS 213, Fall 2000 Lab Assignment L5: Logging Web Proxy Assigned: Nov. 28, Due: Mon. Dec. 11, 11:59PM

1945: 1989: ! Tim Berners-Lee (CERN) writes internal proposal to develop a. 1990:! Tim BL writes a graphical browser for Next machines.

Socket-based Network Communication in J2SE and J2ME

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

WIRIS quizzes web services Getting started with PHP and Java

TFTP Trivial File Transfer Protocol. TFTP Usage and Design. TFTP Usage and Design (cont.) References: RFC 783, Transfer files between processes.

Integrating the F5 BigIP with Blackboard

Code and Usage Description. A.Konstantinov

JAVA - FILES AND I/O

Java Program Coding Standards Programming for Information Technology

DEVELOPING CONTRACT - DRIVEN WEB SERVICES USING JDEVELOPER. The purpose of this tutorial is to develop a java web service using a top-down approach.

Domain Name System (DNS) Omer F. Rana. Networks and Data Communications 1

THE PROXY SERVER 1 1 PURPOSE 3 2 USAGE EXAMPLES 4 3 STARTING THE PROXY SERVER 5 4 READING THE LOG 6

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

ResellerPlus - Bulk Http API Specification. (This Document gives details on how to send messages via the Bulk HTTP API for the RouteSms SMPP System)

1 Recommended Readings. 2 Resources Required. 3 Compiling and Running on Linux

CHAPTER 7. ing with CGI

Lab 4: Socket Programming: netcat part

Agenda. Network Programming and Java Sockets. Introduction. Internet Applications Serving Local and Remote Users

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Building a Java chat server

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Transcription:

Programming Assignment: Development of a Proxy Cache La prova avrà una valutazione da 0 a 3 punti da aggiungere alla media dell esito delle due prove di esonero scritte. La gestione dei thread non è da considerarsi obbligatoria. In this lab you will develop a small web proxy server which is also able to cache web pages. This is a very simple proxy server which only understands simple GET-requests, but is able to handle all kinds of objects, not just HTML pages, but also images. Code The code is divided into three classes as follows: ProxyCache holds the start-up code for the proxy and code for handling the requests. HttpRequest contains the routines for parsing and processing the incoming requests from clients. HttpResponse takes care of reading the replies from servers and processing them. Your work will be to complete the proxy so that it is able to receive requests, forward them, read replies, and return those to the clients. You will need to complete the classes ProxyCache, HttpRequest, and HttpResponse. The places where you need to fill in code are marked with / Fill in /. Each place may require one or more lines of code. NOTE: As explained below, the proxy uses DataInputStreams for processing the replies from servers. This is because the replies are a mixture of textual and binary data and the only input streams in Java which allow treating both at the same time are DataInputStreams. To get the code to compile, you must use the -deprecation argument for the compiler as follows: javac -deprecation.java If you do not use the -deprecation flag, the compiler will refuse to compile your code! Running the Proxy Running the proxy is as follows: java ProxyCache port where port is the port number on which you want the proxy to listen for incoming connections from clients. Configuring Your Browser

You will also need to configure your web browser to use your proxy. This depends on your browser. In Internet Explorer, you can set the proxy in "Internet Options" in the Connections tab under LAN Settings. In Netscape (and derived browsers, such as Mozilla), you can set the proxy in Edit->Preferences and then select Advanced and Proxies. In both cases you need to give the address of the proxy and the port number which you gave when you started the proxy. You can run the proxy and browser on the same computer without any problems. Proxy Functionality The proxy works as follows. 1. The proxy listens for requests from clients 2. When there is a request, the proxy spawns a new thread for handling the request and creates an HttpRequest-object which contains the request. 3. The new thread sends the request to the server and reads the server's reply into an HttpResponse-object. 4. The thread sends the response back to the requesting client. Your task is to complete the code which handles the above process. Most of the error handling in the proxy is very simple and it does not inform the client about errors. When there are errors, the proxy will simply stop processing the request and the client will eventually get a timeout. Some browsers also send their requests one at a time, without using parallel connections. Especially in pages with lot of inlined images, this may cause the page to load very slowly. Caching The basic functionality of caching goes as follows. 1. When the proxy gets a request, it checks if the requested object is cached, and if yes, then returns the object from the cache, without contacting the server. 2. If the object is not cached, the proxy retrieves the object from the server, returns it to the client, and caches a copy for future requests. In practice, the proxy must verify that the cached responses are still valid and that they are the correct response to the client's request. You can read more about caching and how it is handled in HTTP in RFC 2068. For this lab, it is sufficient to implement the above simple policy. Programming Hints Most of the code you need to write relates to processing HTTP requests and responses as well as handling Java sockets. One point worth noting is the processing of replies from the server. In an HTTP response, the headers are sent as ASCII lines, separated by CRLF character sequences. The headers are followed by an empty line and the response body, which can be binary data in the case of images, for example. Java separates the input streams according to whether they are text-based or binary, which presents a small problem in this case. Only DataInputStreams are able to handle both text and binary data

simultaneously; all other streams are either pure text (e.g., BufferedReader), or pure binary (e.g., BufferedInputStream), and mixing them on the same socket does not generally work. The DataInputStream has a small gotcha, because it is not able to guarantee that the data it reads can be correctly converted to the correct characters on every platform (DataInputStream.readLine() function). In the case of this lab, the conversion usually works, but the compiler will flag the DataInputStream.readLine()-method as deprecated and will refuse to compile without the - deprecation flag. It is highly recommended that you use the DataInputStream for reading the response. Code for proxy cache lab This page contains the code for the proxy cache lab. You can copy the code into the correct files. ProxyCache.java / ProxyCache.java - Simple caching proxy / import java.net.; import java.io.; import java.util.; public class ProxyCache { / Port for the proxy / private static int port; / Socket for client connections / private static ServerSocket socket; / Create the ProxyCache object and the socket / public static void init(int p) { port = p; socket = / Fill in /; catch (IOException e) { System.out.println("Error creating socket: " + e); System.exit(-1); public static void handle(socket client) { Socket server = null; HttpRequest request = null; HttpResponse response = null; / Process request. If there are any exceptions, then simply return and end this request. This unfortunately means the client will hang for a while, until it timeouts. / / Read request / BufferedReader fromclient = / Fill in /; request = / Fill in /; catch (IOException e) {

System.out.println("Error reading request from client: " + e); / Send request to server / / Open socket and write request to socket / server = / Fill in /; DataOutputStream toserver = / Fill in /; / Fill in / catch (UnknownHostException e) { System.out.println("Unknown host: " + request.gethost()); System.out.println(e); catch (IOException e) { System.out.println("Error writing request to server: " + e); / Read response and forward it to client / DataInputStream fromserver = / Fill in /; response = / Fill in /; DataOutputStream toclient = / Fill in /; / Fill in / / Write response to client. First headers, then body / client.close(); server.close(); / Insert object into the cache / / Fill in / catch (IOException e) { System.out.println("Error writing response to client: " + e); / Read command line arguments and start proxy / public static void main(string args[]) { int myport = 0; myport = Integer.parseInt(args[0]); catch (ArrayIndexOutOfBoundsException e) { System.out.println("Need port number as argument"); System.exit(-1); catch (NumberFormatException e) { System.out.println("Please give port number as integer."); System.exit(-1); init(myport); / Main loop. Listen for incoming connections and spawn a new thread for handling them / Socket client = null; while (true) { client = / Fill in /; handle(client); catch (IOException e) { System.out.println("Error reading request from client: " + e); / Definitely cannot continue processing this request, so skip to next iteration of while loop. / continue;

HttpRequest.java / HttpRequest - HTTP request container and parser $Id: HttpRequest.java,v 1.2 2003/11/26 18:11:53 kangasha Exp $ / import java.io.; import java.net.; import java.util.; public class HttpRequest { / Help variables / final static String CRLF = "\r\n"; final static int HTTP_PORT = 80; / Store the request parameters / String method; String URI; String version; String headers = ""; / Server and port / private String host; private int port; / Create HttpRequest by reading it from the client socket / public HttpRequest(BufferedReader from) { String firstline = ""; firstline = from.readline(); catch (IOException e) { System.out.println("Error reading request line: " + e); String[] tmp = firstline.split(" "); method = / Fill in /; URI = / Fill in /; version = / Fill in /; System.out.println("URI is: " + URI); if (!method.equals("get")) { System.out.println("Error: Method not GET"); String line = from.readline(); while (line.length()!= 0) { headers += line + CRLF; / We need to find host header to know which server to contact in case the request URI is not complete. / if (line.startswith("host:")) { tmp = line.split(" "); if (tmp[1].indexof(':') > 0) { String[] tmp2 = tmp[1].split(":");

host = tmp2[0]; port = Integer.parseInt(tmp2[1]); else { host = tmp[1]; port = HTTP_PORT; line = from.readline(); catch (IOException e) { System.out.println("Error reading from socket: " + e); System.out.println("Host to contact is: " + host + " at port " + port); / Return host for which this request is intended / public String gethost() { return host; / Return port for server / public int getport() { return port; / Convert request into a string for easy re-sending. / public String tostring() { String req = ""; req = method + " " + URI + " " + version + CRLF; req += headers; / This proxy does not support persistent connections / req += "Connection: close" + CRLF; req += CRLF; return req; HttpResponse.java / HttpResponse - Handle HTTP replies $Id: HttpResponse.java,v 1.2 2003/11/26 18:12:42 kangasha Exp $ / import java.io.; import java.net.; import java.util.; public class HttpResponse { final static String CRLF = "\r\n"; / How big is the buffer used for reading the object / final static int BUF_SIZE = 8192; / Maximum size of objects that this proxy can handle. For the moment set to 100 KB. You can adjust this as needed. /

final static int MAX_OBJECT_SIZE = 100000; / Reply status and headers / String version; int status; String statusline = ""; String headers = ""; / Body of reply / byte[] body = new byte[max_object_size]; / Read response from server. / public HttpResponse(DataInputStream fromserver) { / Length of the object / int length = -1; boolean gotstatusline = false; / First read status line and response headers / String line = / Fill in /; while (line.length()!= 0) { if (!gotstatusline) { statusline = line; gotstatusline = true; else { headers += line + CRLF; / Get length of content as indicated by Content-Length header. Unfortunately this is not present in every response. Some servers return the header "Content-Length", others return "Content-length". You need to check for both here. / if (line.startswith(/ Fill in /) line.startswith(/ Fill in /)) { String[] tmp = line.split(" "); length = Integer.parseInt(tmp[1]); line = fromserver.readline(); catch (IOException e) { System.out.println("Error reading headers from server: " + e); int bytesread = 0; byte buf[] = new byte[buf_size]; boolean loop = false; / If we didn't get Content-Length header, just loop until the connection is closed. / if (length == -1) { loop = true; / Read the body in chunks of BUF_SIZE and copy the chunk into body. Usually replies come back in smaller chunks than BUF_SIZE. The while-loop ends when either we have read Content-Length bytes or when the connection is closed (when there is no Connection-Length in the response. / while (bytesread < length loop) { / Read it in as binary data /

int res = / Fill in /; if (res == -1) { break; / Copy the bytes into body. Make sure we don't exceed the maximum object size. / for (int i = 0; i < res && (i + bytesread) < MAX_OBJECT_SIZE; i++) { / Fill in / bytesread += res; catch (IOException e) { System.out.println("Error reading response body: " + e); / Convert response into a string for easy re-sending. Only converts the response headers, body is not converted to a string. / public String tostring() { String res = ""; res = statusline + CRLF; res += headers; res += CRLF; return res;