| 1 |
# -*- coding: utf-8 -*- |
| 2 |
""" |
| 3 |
head_request.py |
| 4 |
~~~~~~~~~~~~~~~ |
| 5 |
|
| 6 |
A short example that demonstrates a client that makes HEAD requests to certain |
| 7 |
websites. |
| 8 |
|
| 9 |
This example is intended as a reproduction of nghttp2 issue 396, for the |
| 10 |
purposes of compatibility testing. |
| 11 |
""" |
| 12 |
from __future__ import print_function |
| 13 |
|
| 14 |
from twisted.internet import reactor |
| 15 |
from twisted.internet.endpoints import connectProtocol, SSL4ClientEndpoint |
| 16 |
from twisted.internet.protocol import Protocol |
| 17 |
from twisted.internet.ssl import optionsForClientTLS |
| 18 |
from hyperframe.frame import SettingsFrame |
| 19 |
from h2.connection import H2Connection |
| 20 |
from h2.events import ( |
| 21 |
ResponseReceived, DataReceived, StreamEnded, |
| 22 |
StreamReset, SettingsAcknowledged, |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
AUTHORITY = u'nghttp2.org' |
| 27 |
PATH = '/httpbin/' |
| 28 |
SIZE = 4096 |
| 29 |
|
| 30 |
|
| 31 |
class H2Protocol(Protocol): |
| 32 |
def __init__(self): |
| 33 |
self.conn = H2Connection() |
| 34 |
self.known_proto = None |
| 35 |
self.request_made = False |
| 36 |
|
| 37 |
def connectionMade(self): |
| 38 |
self.conn.initiate_connection() |
| 39 |
|
| 40 |
# This reproduces the error in #396, by changing the header table size. |
| 41 |
self.conn.update_settings({SettingsFrame.HEADER_TABLE_SIZE: SIZE}) |
| 42 |
|
| 43 |
self.transport.write(self.conn.data_to_send()) |
| 44 |
|
| 45 |
def dataReceived(self, data): |
| 46 |
if not self.known_proto: |
| 47 |
self.known_proto = self.transport.negotiatedProtocol |
| 48 |
assert self.known_proto == b'h2' |
| 49 |
|
| 50 |
events = self.conn.receive_data(data) |
| 51 |
|
| 52 |
for event in events: |
| 53 |
if isinstance(event, ResponseReceived): |
| 54 |
self.handleResponse(event.headers, event.stream_id) |
| 55 |
elif isinstance(event, DataReceived): |
| 56 |
self.handleData(event.data, event.stream_id) |
| 57 |
elif isinstance(event, StreamEnded): |
| 58 |
self.endStream(event.stream_id) |
| 59 |
elif isinstance(event, SettingsAcknowledged): |
| 60 |
self.settingsAcked(event) |
| 61 |
elif isinstance(event, StreamReset): |
| 62 |
reactor.stop() |
| 63 |
raise RuntimeError("Stream reset: %d" % event.error_code) |
| 64 |
else: |
| 65 |
print(event) |
| 66 |
|
| 67 |
data = self.conn.data_to_send() |
| 68 |
if data: |
| 69 |
self.transport.write(data) |
| 70 |
|
| 71 |
def settingsAcked(self, event): |
| 72 |
# Having received the remote settings change, lets send our request. |
| 73 |
if not self.request_made: |
| 74 |
self.sendRequest() |
| 75 |
|
| 76 |
def handleResponse(self, response_headers, stream_id): |
| 77 |
for name, value in response_headers: |
| 78 |
print("%s: %s" % (name.decode('utf-8'), value.decode('utf-8'))) |
| 79 |
|
| 80 |
print("") |
| 81 |
|
| 82 |
def handleData(self, data, stream_id): |
| 83 |
print(data, end='') |
| 84 |
|
| 85 |
def endStream(self, stream_id): |
| 86 |
self.conn.close_connection() |
| 87 |
self.transport.write(self.conn.data_to_send()) |
| 88 |
self.transport.loseConnection() |
| 89 |
reactor.stop() |
| 90 |
|
| 91 |
def sendRequest(self): |
| 92 |
request_headers = [ |
| 93 |
(':method', 'HEAD'), |
| 94 |
(':authority', AUTHORITY), |
| 95 |
(':scheme', 'https'), |
| 96 |
(':path', PATH), |
| 97 |
] |
| 98 |
self.conn.send_headers(1, request_headers, end_stream=True) |
| 99 |
self.request_made = True |
| 100 |
|
| 101 |
options = optionsForClientTLS( |
| 102 |
hostname=AUTHORITY, |
| 103 |
acceptableProtocols=[b'h2'], |
| 104 |
) |
| 105 |
|
| 106 |
connectProtocol( |
| 107 |
SSL4ClientEndpoint(reactor, AUTHORITY, 443, options), |
| 108 |
H2Protocol() |
| 109 |
) |
| 110 |
reactor.run() |