19.9 Simple Proxy Server

Team-FLY

This section describes a modification of the tunnelserver program of Section 19.6 so that it acts like a proxy rather than a tunnel. A proxy must parse the initial request line (unless the proxy happens to be using a proxy, too).

Example 19.25

When a proxy server receives the following GET line, it knows that it is to act as a proxy because the absolute URI form of the request is given.

 GET http://www.usp.cs.utsa.edu/usp/simple.html HTTP/1.0 

The proxy knows that the origin server is www.usp.cs.utsa.edu and replaces the initial line with the following initial line.

 GET /usp/simple.html HTTP/1.0 

The proxy then makes a connection to port 80 of www.usp.cs.utsa.edu .

Make a new directory with a copy of the files for tunnelserver of Section 19.6. Rename tunnelserver to proxyserver . The proxyserver program takes a single command-line argument, the port number at which it listens for requests . The proxyserver program does not need the destination web server as a command-line argument because it parses the initial HTTP request from the client, as in Example 19.25. Write a processproxy function that has the following prototype.

 int processproxy(int clientfd); 

The clientfd parameter is the file descriptor returned when the server accepts the client's connection request.

The processproxy function reads in the first line from clientfd and calls parse to parse the initial request. If parse is successful and the line contains an absolute URI (the server pointer is not NULL ), processproxy establishes a connection to the destination server. Then processproxy writes to the destination server an initial line containing a command with an absolute path and calls the tunnel function to continue the communication. If the port parameter of parse is not NULL , use the indicated port. Otherwise use port 80.

If successful, processproxy returns the total number of bytes transferred, which is the return value from tunnel plus the length of the initial line read from the client and the corresponding line sent to the server. If unsuccessful , processproxy returns “1 and sets errno .

Assume a maximum line length of 4096 bytes for the initial command from the client so that you need not do dynamic memory allocation. This means that a longer request is considered invalid, but you must not let a long request overflow the buffer. To read the first line from the client, you must read one byte at a time until you get a newline.

If parse returns an error, processproxy should treat the connection request as an error. In this case, processproxy writes the following message on clientfd , closes the connection, and returns “1 with errno set.

 HTTP/1.0 <SP> 400 <SP> Bad <SP> Request <CRLF> <CRLF> 

The proxyserver program listens for connection requests on the given port, and for each request it forks a child that calls processproxy and prints the number of bytes transferred.

Copy your servertester.c into proxytester.c and modify the request to contain an absolute URI instead of an absolute path. Use proxytester to test proxyserver .

Exercise 19.26

How would you test proxyserver through your browser?

Answer:

Set your browser to use proxyserver as its proxy. Suppose that proxyserver is running on machine os1.cs.utsa.edu using port 15000. Set your browser proxy to be os1.cs.utsa.edu on port number 15000. You should be able to use your browser with no noticeable difference.

Team-FLY


Unix Systems Programming
UNIX Systems Programming: Communication, Concurrency and Threads
ISBN: 0130424110
EAN: 2147483647
Year: 2003
Pages: 274

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net