Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions doc/notebooks/HTTP_2_Tuto.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"First, we need to build an TLS socket to the HTTP server, and to negotiate the HTTP/2 protocol as the next protocol. Doing so requires a fairly recent version of the Python ssl module. We indeed need support of ALPN (https://www.rfc-editor.org/rfc/rfc7301.txt).\n",
"First, we need to build a TLS socket to the HTTP server, and to negotiate the HTTP/2 protocol as the next protocol. Doing so requires a fairly recent version of the Python ssl module. We indeed need support of ALPN (https://www.rfc-editor.org/rfc/rfc7301.txt).\n",
"We build our TCP socket first."
]
},
Expand Down Expand Up @@ -321,7 +321,7 @@
" own_set\n",
"]\n",
"# We verify that the server window is large enough for us to send our frames.\n",
"srv_global_window -= len(str(h2seq))\n",
"srv_global_window -= len(bytes(h2seq))\n",
"assert(srv_global_window >= 0)\n",
"ss.send(h2seq)\n",
"\n",
Expand All @@ -343,7 +343,7 @@
" new_flags = new_frame.getfieldval('flags')\n",
" new_flags.add('A')\n",
" new_frame.flags = new_flags\n",
" srv_global_window -= len(str(new_frame))\n",
" srv_global_window -= len(bytes(new_frame))\n",
" assert(srv_global_window >= 0)\n",
" ss.send(new_frame)\n",
" else:\n",
Expand Down Expand Up @@ -515,7 +515,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
"for i in xrange(max(tblhdr._static_entries.keys()) + 1, max(tblhdr._static_entries.keys()) + 1 + len(tblhdr._dynamic_table)):\n",
"for i in range(max(tblhdr._static_entries.keys()) + 1, max(tblhdr._static_entries.keys()) + 1 + len(tblhdr._dynamic_table)):\n",
" print('Header: {} Value: {}'.format(tblhdr[i].name(), tblhdr[i].value()))"
],
"language": "python",
Expand Down Expand Up @@ -632,7 +632,7 @@
"metadata": {},
"source": [
"You may note that several of the headers that are in common between the two queries (the one for the frontpage and the one for the /favicon.ico) are compressed in the second query. They are only referred to by an index number of the dynamic HPack Header Table.\n",
"We now alter the favicon query to be dependent of the query for the form."
"We now alter the favicon query to be dependent on the query for the form."
]
},
{
Expand Down Expand Up @@ -725,7 +725,7 @@
" qry_frontpage.frames[0],\n",
" real_qry_icon\n",
"]\n",
"srv_global_window -= len(str(h2seq))\n",
"srv_global_window -= len(bytes(h2seq))\n",
"assert(srv_global_window >= 0)\n",
"ss.send(h2seq)"
],
Expand Down Expand Up @@ -1437,10 +1437,11 @@
"cell_type": "code",
"collapsed": false,
"input": [
"import base64\n",
"import zlib\n",
"img = zlib.decompress(stream_data[3][0].data, 16+zlib.MAX_WBITS)\n",
"from IPython.core.display import HTML\n",
"HTML('<img src=\"data:image/x-icon;base64,{}\" />'.format(img.encode('base64')))"
"HTML('<img src=\"data:image/x-icon;base64,{}\" />'.format(base64.b64encode(img).decode('ascii')))"
],
"language": "python",
"metadata": {},
Expand Down Expand Up @@ -1604,7 +1605,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
"data = ''\n",
"data = b''\n",
"for frgmt in stream_data[1]:\n",
" data += frgmt.payload.data\n",
"\n",
Expand Down Expand Up @@ -1652,8 +1653,8 @@
"cell_type": "code",
"collapsed": false,
"input": [
"from io import BytesIO\n",
"sio = BytesIO(stream_txt[1])\n",
"from io import StringIO\n",
"sio = StringIO(stream_txt[1])\n",
"cookie = [val[len('set-cookie: '):].strip() for val in sio if val.startswith('set-cookie: ')]\n",
"print(cookie)"
],
Expand All @@ -1674,9 +1675,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, we build the query by hand. Let's create the Header frame, so that we can later stuff all the header \"lines\" in it. This is a new stream, and we already used the stream ids 1 and 3, so we will use the stream id 5, which is the next available. We will set the \"End Stream\" and \"End Headers\" flags. The End Stream flag means that they are no more frames (except header frames...) after this one in this stream. The End Headers flag means that there are no Continuation frames after this frame. Continuation frames can be used to add more headers than one could fit in previous H2HeaderFrame and Continuation frames...\n",
"Now, we build the query by hand. Let's create the Header frame, so that we can later stuff all the header \"lines\" in it. This is a new stream, and we already used the stream ids 1 and 3, so we will use the stream id 5, which is the next available. We will set the \"End Stream\" and \"End Headers\" flags. The End Stream flag means that there are no more frames (except header frames...) after this one in this stream. The End Headers flag means that there are no Continuation frames after this frame. Continuation frames can be used to add more headers than one could fit in previous H2HeaderFrame and Continuation frames...\n",
"\n",
"Our frame will contain little headers and we set very large limits for this tutorial, so we will skip all the checks, but they should be done! The helpers does them, by the way."
"Our frame will contain few headers and we set very large limits for this tutorial, so we will skip all the checks, but they should be done! The helpers do them, by the way."
]
},
{
Expand Down Expand Up @@ -1747,7 +1748,7 @@
"z_str = h2.HPackZString('/search?q=scapy')\n",
"unz_str = h2.HPackLiteralString('/search?q=scapy')\n",
"\n",
"print(len(str(z_str)), len(str(unz_str)))"
"print(len(bytes(z_str)), len(bytes(unz_str)))"
],
"language": "python",
"metadata": {},
Expand All @@ -1766,9 +1767,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"So the compressed version is smaller. Let's use it, since HTTP/2 is all about performances and compression. \n",
"So the compressed version is smaller. Let's use it, since HTTP/2 is all about performance and compression. \n",
"\n",
"\":path\" is the pseudo-header to define the query path. While we don't want to compress the *value* of the query path, the name can be compressed. As it happens, the \":path\" header name is in the static header table. For this reason, we will search of its index, and then build the header."
"\":path\" is the pseudo-header to define the query path. While we don't want to compress the *value* of the query path, the name can be compressed. As it happens, the \":path\" header name is in the static header table. For this reason, we will search for its index, and then build the header."
]
},
{
Expand Down Expand Up @@ -1902,7 +1903,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We also need to create the header for our new cookie. Cookie are sensitive. We don't want it to be indexed and we don't want any intermediate to index it. As such, we will use a HPackLitHdrFldWithoutIndexing and with the never_index bit set, here as well. The name \"cookie\", though, happens to be in the RFC7541 static headers table, so we will use it."
"We also need to create the header for our new cookie. The cookie is sensitive. We don't want it to be indexed and we don't want any intermediary to index it. As such, we will use a HPackLitHdrFldWithoutIndexing and with the never_index bit set, here as well. The name \"cookie\", though, happens to be in the RFC7541 static headers table, so we will use it."
]
},
{
Expand Down Expand Up @@ -2043,7 +2044,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, we forget a piece in our previous queries regarding privacy: I want to add a Do Not Track header (https://tools.ietf.org/html/draft-mayer-do-not-track-00). Let's add this header into every subsequent queries. For this reason, I want to have it indexed. It is worth noting that the \"DNT\" header is not part of the HPack static table (how curious?). Finally, the value of this header is just 1. We might actually save a few bits by NOT compressing this value."
"Now, we forget a piece in our previous queries regarding privacy: I want to add a Do Not Track header (https://tools.ietf.org/html/draft-mayer-do-not-track-00). Let's add this header into every subsequent query. For this reason, I want to have it indexed. It is worth noting that the \"DNT\" header is not part of the HPack static table (how curious?). Finally, the value of this header is just 1. We might actually save a few bits by NOT compressing this value."
]
},
{
Expand Down Expand Up @@ -2310,19 +2311,19 @@
"cell_type": "code",
"collapsed": false,
"input": [
"srv_global_window -= len(str(hdrs_frm))\n",
"srv_global_window -= len(bytes(hdrs_frm))\n",
"assert(srv_global_window >= 0)\n",
"ss.send(hdrs_frm)\n",
"\n",
"h2seq = h2.H2Seq()\n",
"\n",
"new_frame = None\n",
"while isinstance(new_frame, type(None)) or 'ES' not in new_frame.flags:\n",
" # As previously, if we receive a ping, we ackownledge it.\n",
" # As previously, if we receive a ping, we acknowledge it.\n",
" if not isinstance(new_frame, type(None)) and new_frame.stream_id == 0:\n",
" if new_frame.type == h2.H2PingFrame.type_id:\n",
" new_frame.flags.add('A')\n",
" srv_global_window -= len(str(new_frame))\n",
" srv_global_window -= len(bytes(new_frame))\n",
" assert(srv_global_window >= 0)\n",
" ss.send(new_frame)\n",
" \n",
Expand Down Expand Up @@ -2495,7 +2496,7 @@
"collapsed": false,
"input": [
"stream_txt = srv_tblhdr.gen_txt_repr(h2seq.frames[0])\n",
"data = ''\n",
"data = b''\n",
"for frgmt in h2seq.frames[1:]:\n",
" data += frgmt.payload.data\n",
"print(stream_txt)\n",
Expand Down
Loading
Loading