|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# HttpParser\n", |
| 8 | + "\n", |
| 9 | + "`HttpParser` class is at the heart of everything related to HTTP. It is used by Web server and Proxy server core and their plugin eco-system. As the name suggests, it is capable of parsing both HTTP request and response packets. It can also parse HTTP look-a-like protocols like ICAP, SIP etc. Most importantly, remember that `HttpParser` was originally written to handle HTTP packets arriving in the context of a proxy server and till date its default behavior favors the same flavor.\n", |
| 10 | + "\n", |
| 11 | + "Let's start by parsing a HTTP web request using `HttpParser`" |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "code", |
| 16 | + "execution_count": 1, |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [ |
| 19 | + { |
| 20 | + "name": "stdout", |
| 21 | + "output_type": "stream", |
| 22 | + "text": [ |
| 23 | + "b'GET / HTTP/1.1\\r\\nHost: jaxl.com\\r\\n\\r\\n'\n" |
| 24 | + ] |
| 25 | + } |
| 26 | + ], |
| 27 | + "source": [ |
| 28 | + "from proxy.http.methods import httpMethods\n", |
| 29 | + "from proxy.http.parser import HttpParser, httpParserTypes, httpParserStates\n", |
| 30 | + "from proxy.common.constants import HTTP_1_1\n", |
| 31 | + "\n", |
| 32 | + "get_request = HttpParser(httpParserTypes.REQUEST_PARSER)\n", |
| 33 | + "get_request.parse(b'GET / HTTP/1.1\\r\\nHost: jaxl.com\\r\\n\\r\\n')\n", |
| 34 | + "\n", |
| 35 | + "print(get_request.build())\n", |
| 36 | + "\n", |
| 37 | + "assert get_request.is_complete\n", |
| 38 | + "assert get_request.method == httpMethods.GET\n", |
| 39 | + "assert get_request.version == HTTP_1_1\n", |
| 40 | + "assert get_request.host == None\n", |
| 41 | + "assert get_request.port == 80\n", |
| 42 | + "assert get_request._url != None\n", |
| 43 | + "assert get_request._url.remainder == b'/'\n", |
| 44 | + "assert get_request.has_header(b'host')\n", |
| 45 | + "assert get_request.header(b'host') == b'jaxl.com'\n", |
| 46 | + "assert len(get_request.headers) == 1" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "markdown", |
| 51 | + "metadata": {}, |
| 52 | + "source": [ |
| 53 | + "Next, let's parse a HTTP proxy request using `HttpParser`" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": 2, |
| 59 | + "metadata": {}, |
| 60 | + "outputs": [ |
| 61 | + { |
| 62 | + "name": "stdout", |
| 63 | + "output_type": "stream", |
| 64 | + "text": [ |
| 65 | + "b'GET / HTTP/1.1\\r\\nHost: jaxl.com\\r\\n\\r\\n'\n", |
| 66 | + "b'GET http://jaxl.com:80/ HTTP/1.1\\r\\nHost: jaxl.com\\r\\n\\r\\n'\n" |
| 67 | + ] |
| 68 | + } |
| 69 | + ], |
| 70 | + "source": [ |
| 71 | + "proxy_request = HttpParser(httpParserTypes.REQUEST_PARSER)\n", |
| 72 | + "proxy_request.parse(b'GET http://jaxl.com/ HTTP/1.1\\r\\nHost: jaxl.com\\r\\n\\r\\n')\n", |
| 73 | + "\n", |
| 74 | + "print(proxy_request.build())\n", |
| 75 | + "print(proxy_request.build(for_proxy=True))\n", |
| 76 | + "\n", |
| 77 | + "assert proxy_request.is_complete\n", |
| 78 | + "assert proxy_request.method == httpMethods.GET\n", |
| 79 | + "assert proxy_request.version == HTTP_1_1\n", |
| 80 | + "assert proxy_request.host == b'jaxl.com'\n", |
| 81 | + "assert proxy_request.port == 80\n", |
| 82 | + "assert proxy_request._url != None\n", |
| 83 | + "assert proxy_request._url.remainder == b'/'\n", |
| 84 | + "assert proxy_request.has_header(b'host')\n", |
| 85 | + "assert proxy_request.header(b'host') == b'jaxl.com'\n", |
| 86 | + "assert len(proxy_request.headers) == 1" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "markdown", |
| 91 | + "metadata": {}, |
| 92 | + "source": [ |
| 93 | + "Notice how `proxy_request.build()` and `proxy_request.build(for_proxy=True)` behave. Also, notice how `proxy_request.host` field is populated for a HTTP proxy packet but not for the prior HTTP web request packet example.\n", |
| 94 | + "\n", |
| 95 | + "To conclude, let's parse a HTTPS proxy request" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "code", |
| 100 | + "execution_count": 4, |
| 101 | + "metadata": {}, |
| 102 | + "outputs": [ |
| 103 | + { |
| 104 | + "name": "stdout", |
| 105 | + "output_type": "stream", |
| 106 | + "text": [ |
| 107 | + "b'CONNECT / HTTP/1.1\\r\\nHost: jaxl.com:443\\r\\n\\r\\n'\n", |
| 108 | + "b'CONNECT jaxl.com:443 HTTP/1.1\\r\\nHost: jaxl.com:443\\r\\n\\r\\n'\n" |
| 109 | + ] |
| 110 | + } |
| 111 | + ], |
| 112 | + "source": [ |
| 113 | + "connect_request = HttpParser(httpParserTypes.REQUEST_PARSER)\n", |
| 114 | + "connect_request.parse(b'CONNECT jaxl.com:443 HTTP/1.1\\r\\nHost: jaxl.com:443\\r\\n\\r\\n')\n", |
| 115 | + "\n", |
| 116 | + "print(connect_request.build())\n", |
| 117 | + "print(connect_request.build(for_proxy=True))\n", |
| 118 | + "\n", |
| 119 | + "assert connect_request.is_complete\n", |
| 120 | + "assert connect_request.is_https_tunnel\n", |
| 121 | + "assert connect_request.version == HTTP_1_1\n", |
| 122 | + "assert connect_request.host == b'jaxl.com'\n", |
| 123 | + "assert connect_request.port == 443\n", |
| 124 | + "assert connect_request._url != None\n", |
| 125 | + "assert connect_request._url.remainder == None\n", |
| 126 | + "assert connect_request.has_header(b'host')\n", |
| 127 | + "assert connect_request.header(b'host') == b'jaxl.com:443'\n", |
| 128 | + "assert len(connect_request.headers) == 1" |
| 129 | + ] |
| 130 | + }, |
| 131 | + { |
| 132 | + "cell_type": "markdown", |
| 133 | + "metadata": {}, |
| 134 | + "source": [ |
| 135 | + "### Take Away\n", |
| 136 | + "\n", |
| 137 | + "- `host` and `port` attributes represent the `host:port` present in the HTTP packet request line. For comparison purposes, below are all the three request lines again. Notice how there is no `host:port` available only for the web server get request\n", |
| 138 | + " | Request Type | Request Line |\n", |
| 139 | + " | ------------------| ---------------- |\n", |
| 140 | + " | `get_request` | `GET / HTTP/1.1` |\n", |
| 141 | + " | `proxy_request` | `GET http://jaxl.com HTTP/1.1` |\n", |
| 142 | + " | `connect_request` | `CONNECT jaxl.com:443 HTTP/1.1` |\n", |
| 143 | + "- `_url` attribute is an instance of `Url` parser and contains parsed information about the URL found in the request line\n", |
| 144 | + "\n", |
| 145 | + "Few of the other handy properties within `HttpParser` are:\n", |
| 146 | + "\n", |
| 147 | + "- `is_complete`\n", |
| 148 | + "- `is_http_1_1_keep_alive`\n", |
| 149 | + "- `is_connection_upgrade`\n", |
| 150 | + "- `is_https_tunnel`\n", |
| 151 | + "- `is_chunked_encoded`\n", |
| 152 | + "- `content_expected`\n", |
| 153 | + "- `body_expected`" |
| 154 | + ] |
| 155 | + } |
| 156 | + ], |
| 157 | + "metadata": { |
| 158 | + "interpreter": { |
| 159 | + "hash": "da9d6927d62b2b95bde149eedfbd5367cb7f465aad65a736f49c99ee3db39df7" |
| 160 | + }, |
| 161 | + "kernelspec": { |
| 162 | + "display_name": "Python 3.10.0 64-bit ('venv310': venv)", |
| 163 | + "language": "python", |
| 164 | + "name": "python3" |
| 165 | + }, |
| 166 | + "language_info": { |
| 167 | + "codemirror_mode": { |
| 168 | + "name": "ipython", |
| 169 | + "version": 3 |
| 170 | + }, |
| 171 | + "file_extension": ".py", |
| 172 | + "mimetype": "text/x-python", |
| 173 | + "name": "python", |
| 174 | + "nbconvert_exporter": "python", |
| 175 | + "pygments_lexer": "ipython3", |
| 176 | + "version": "3.10.0" |
| 177 | + }, |
| 178 | + "orig_nbformat": 4 |
| 179 | + }, |
| 180 | + "nbformat": 4, |
| 181 | + "nbformat_minor": 2 |
| 182 | +} |
0 commit comments