Implementing an HTTP proxy
-
12-06-2021 - |
Question
I would like to implement and HTTP proxy server to get a deeper understanding of some of the finer points of the protocol and learn some socket programming along the way but I don't want to implement a full-blown HTTP server. What are the relevant RFC pages and sections in those pages that I should be focusing on if I just want to implement a proxy?
The plan is to start with a regular HTTP proxy and then potentially extend it to an HTTPS proxy.
Solution
I would take a look at HTTP 1.0 (RFC 1945) and HTTP 1.1 (RFC 2616). You can also take a look at:
- RFC 822 - structure of Internet text messages, including header fields
- RFC 2396 - definition of URL/URI syntax (replaces RFC 1738 and RFC 1808)
- RFC 1521 - definition of MIME and of MIME types
There are probably lots of example code that you can find with Google!
OTHER TIPS
I'm currently working on it too. The basic implementation is simple. Listen on a socket port, find the host, connect to remote server, send http header, recv from server, then send back to client. The difficult part is consistent connection and pipelining.
As to HTTPs, its header is like this:
CONNECT addons.mozilla.org:443 HTTP/1.1
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0
Host: addons.mozilla.org
Proxy should connect to server, then reply to client a 200 message, then redirect back and forth as a redirector.
Check this: http://tools.ietf.org/html/draft-luotonen-ssl-tunneling-03
You might also find the work of the HTTPbis group in the IETF helpful; we're re-writing the specs to make them more clear and easier to implement.
See http://trac.tools.ietf.org/wg/httpbis/trac/wiki for more information.