The proxy sits between HTTP clients and HTTP servers. The client sends a HTTP request to the proxy. The proxy then forwards this request to the server, and receives the reply. The proxy will send the reply back to the client while also caching it for a certain amount of time for future requests.
- make
- ./webproxy
- Configure a web-browser to use the HTTP web-proxy
- Go to an HTTP website like http://netsys.cs.colorado.edu/
- Web proxy listens for incoming connections on socket;
- Each incoming request is assigned a thread that'll be running the handleRequest function.
- In handleRequest, the client's request is loaded onto a buffer, which is then parsed out into a Request struct object.
- The request is checked for a valid method (only GET supported), valid website (IP address cached in resolvedIPs.txt), and blacklist status. HTTP Error responses are sent out if the request fails any of these checks.
- The cache directory is searched for a file with the filename md5sum(request.URL).
- If that cache file is found, the timestamp of when it was last updated (found in the first line) is checked against the cache-timeout-value. If the cache file isn't stale, all the contents of the file past the first line is sent to the client.
- If the cache file isn't found, or it's stale, a socket connection to the webserver of the request is made. A copy of the client's HTTP request is forwarded to the webserver, and the response is both sent to the client, and cached.
The most relevant sections of the code are in the handleRequest, respondByCache, and respondByServer methods. Everything else is mostly helper methods.