logo

Parsing HTTP Requests and Responses

   

Added on  2019-09-23

7 Pages1720 Words105 Views
 | 
 | 
 | 
Code for proxy cache AssignmentThis page contains the code for the proxy cache assignment level 1 implementation. You can copy the code into the appropriate java files. ProxyCache.javaimport 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;try { 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 */try { BufferedReader fromClient = /* Fill in */; request = /* Fill in */;} catch (IOException e) { System.out.println("Error reading request from client: " + e); return;}/* Send request to server */try { /* 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());
Parsing HTTP Requests and Responses_1

System.out.println(e); return;} catch (IOException e) { System.out.println("Error writing request to server: " + e); return;}/* Read response and forward it to client */try { 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();} 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;try { 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) { try {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; }}
Parsing HTTP Requests and Responses_2

}}
Parsing HTTP Requests and Responses_3

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
Program Description | Document
|15
|1603
|24

Client and Server Application | Report
|16
|1570
|21

Client Server System for Socket Programming | Desklib
|18
|1451
|253

Simple Calculator Server
|3
|469
|309

R programming for analyzing flight dataset and flight scheduling
|26
|1439
|140

CS4348 - Operating Systems Concepts
|5
|1324
|177