Skip to content

Commit

Permalink
Fixup examples to remove deprecated code
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasa committed Mar 24, 2017
1 parent 51ec572 commit ec3a7a0
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 14 deletions.
4 changes: 3 additions & 1 deletion examples/asyncio/asyncio-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import collections
from typing import List, Tuple

from h2.config import H2Configuration
from h2.connection import H2Connection
from h2.events import (
ConnectionTerminated, DataReceived, RequestReceived, StreamEnded
Expand All @@ -33,7 +34,8 @@

class H2Protocol(asyncio.Protocol):
def __init__(self):
self.conn = H2Connection(client_side=False)
config = H2Configuration(client_side=False, header_encoding='utf-8')
self.conn = H2Connection(config=config)
self.transport = None
self.stream_data = {}

Expand Down
5 changes: 4 additions & 1 deletion examples/asyncio/wsgi-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import sys
import threading

from h2.config import H2Configuration
from h2.connection import H2Connection
from h2.events import (
DataReceived, RequestReceived, WindowUpdated, StreamEnded, StreamReset
Expand All @@ -86,8 +87,10 @@

class H2Protocol(asyncio.Protocol):
def __init__(self):
config = H2Configuration(client_side=False, header_encoding='utf-8')

# Our server-side state machine.
self.conn = H2Connection(client_side=False)
self.conn = H2Connection(config=config)

# The backing transport.
self.transport = None
Expand Down
6 changes: 5 additions & 1 deletion examples/curio/curio-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from curio import Kernel, Event, spawn, socket, ssl

import h2.config
import h2.connection
import h2.events

Expand Down Expand Up @@ -64,8 +65,11 @@ class H2Server:
HTTP/1.1.
"""
def __init__(self, sock, root):
config = h2.config.H2Configuration(
client_side=False, header_encoding='utf-8'
)
self.sock = sock
self.conn = h2.connection.H2Connection(client_side=False)
self.conn = h2.connection.H2Connection(config=config)
self.root = root
self.flow_control_events = {}

Expand Down
4 changes: 3 additions & 1 deletion examples/eventlet/eventlet-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import eventlet

from eventlet.green.OpenSSL import SSL, crypto
from h2.config import H2Configuration
from h2.connection import H2Connection
from h2.events import RequestReceived, DataReceived

Expand All @@ -20,8 +21,9 @@ class ConnectionManager(object):
An object that manages a single HTTP/2 connection.
"""
def __init__(self, sock):
config = H2Configuration(client_side=False)
self.sock = sock
self.conn = H2Connection(client_side=False)
self.conn = H2Connection(config=config)

def run_forever(self):
self.conn.initiate_connection()
Expand Down
4 changes: 2 additions & 2 deletions examples/fragments/client_https_setup_fragment.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ def main():
# Step 3: Wrap the connection in TLS and validate that we negotiated HTTP/2
tls_connection = negotiate_tls(connection, context)

# Step 4: Create a server-side H2 connection.
http2_connection = h2.connection.H2Connection(client_side=True)
# Step 4: Create a client-side H2 connection.
http2_connection = h2.connection.H2Connection()

# Step 5: Initiate the connection
http2_connection.initiate_connection()
Expand Down
4 changes: 3 additions & 1 deletion examples/fragments/server_https_setup_fragment.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
This code requires Python 3.5 or later.
"""
import h2.config
import h2.connection
import socket
import ssl
Expand Down Expand Up @@ -100,7 +101,8 @@ def main():
tls_connection = negotiate_tls(connection, context)

# Step 4: Create a server-side H2 connection.
http2_connection = h2.connection.H2Connection(client_side=False)
config = h2.config.H2Configuration(client_side=False)
http2_connection = h2.connection.H2Connection(config=config)

# Step 5: Initiate the connection
http2_connection.initiate_connection()
Expand Down
4 changes: 3 additions & 1 deletion examples/fragments/server_upgrade_fragment.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
This code requires Python 3.5 or later.
"""
import h2.config
import h2.connection
import re
import socket
Expand Down Expand Up @@ -81,7 +82,8 @@ def main():

# Step 3: Create a H2Connection object in server mode, and pass it the
# value of the HTTP2-Settings header field.
h2_connection = h2.connection.H2Connection(client_side=False)
config = h2.config.H2Configuration(client_side=False)
h2_connection = h2.connection.H2Connection(config=config)
h2_connection.initiate_upgrade_connection(
settings_header=settings_header_value
)
Expand Down
5 changes: 4 additions & 1 deletion examples/tornado/tornado-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import tornado.iostream
import tornado.tcpserver

from h2.config import H2Configuration
from h2.connection import H2Connection
from h2.events import RequestReceived, DataReceived

Expand Down Expand Up @@ -42,7 +43,9 @@ class EchoHeadersHandler(object):

def __init__(self, stream):
self.stream = stream
self.conn = H2Connection(client_side=False)

config = H2Configuration(client_side=False)
self.conn = H2Connection(config=config)

@tornado.gen.coroutine
def handle(self):
Expand Down
2 changes: 1 addition & 1 deletion examples/twisted/head_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def settingsAcked(self, event):

def handleResponse(self, response_headers, stream_id):
for name, value in response_headers:
print("%s: %s" % (name, value))
print("%s: %s" % (name.decode('utf-8'), value.decode('utf-8')))

print("")

Expand Down
2 changes: 1 addition & 1 deletion examples/twisted/post_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def handleResponse(self, response_headers):
Handle the response by printing the response headers.
"""
for name, value in response_headers:
print("%s: %s" % (name, value))
print("%s: %s" % (name.decode('utf-8'), value.decode('utf-8')))

print("")

Expand Down
8 changes: 5 additions & 3 deletions examples/twisted/twisted-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from twisted.internet.defer import Deferred, inlineCallbacks
from twisted.internet.protocol import Protocol, Factory
from twisted.internet import endpoints, reactor, ssl
from h2.config import H2Configuration
from h2.connection import H2Connection
from h2.events import (
RequestReceived, DataReceived, WindowUpdated
Expand All @@ -30,7 +31,8 @@ def close_file(file, d):

class H2Protocol(Protocol):
def __init__(self, root):
self.conn = H2Connection(client_side=False)
config = H2Configuration(client_side=False)
self.conn = H2Connection(config=config)
self.known_proto = None
self.root = root

Expand Down Expand Up @@ -58,9 +60,9 @@ def dataReceived(self, data):

def requestReceived(self, headers, stream_id):
headers = dict(headers) # Invalid conversion, fix later.
assert headers[':method'] == 'GET'
assert headers[b':method'] == b'GET'

path = headers[':path'].lstrip('/')
path = headers[b':path'].lstrip(b'/')
full_path = os.path.join(self.root, path)

if not os.path.exists(full_path):
Expand Down

0 comments on commit ec3a7a0

Please sign in to comment.