Commit 52cec359 authored by Tatsuhiro Tsujikawa's avatar Tatsuhiro Tsujikawa

python: Fix NameError if asyncio is not available

parent 13cc3f2f
...@@ -250,7 +250,7 @@ try: ...@@ -250,7 +250,7 @@ try:
import datetime import datetime
import time import time
except ImportError: except ImportError:
pass asyncio = None
cdef _get_stream_user_data(cnghttp2.nghttp2_session *session, cdef _get_stream_user_data(cnghttp2.nghttp2_session *session,
int32_t stream_id): int32_t stream_id):
...@@ -659,7 +659,9 @@ cdef class _HTTP2SessionCore: ...@@ -659,7 +659,9 @@ cdef class _HTTP2SessionCore:
datestr, method, path, handler.status, datestr, method, path, handler.status,
'P' if handler.pushed else '-')) 'P' if handler.pushed else '-'))
class BaseRequestHandler: if asyncio:
class BaseRequestHandler:
"""HTTP/2 request (stream) handler base class. """HTTP/2 request (stream) handler base class.
...@@ -737,8 +739,8 @@ class BaseRequestHandler: ...@@ -737,8 +739,8 @@ class BaseRequestHandler:
def on_data(self, data): def on_data(self, data):
'''Called when a chunk of request body is arrived. This method will be '''Called when a chunk of request body is arrived. This method
called multiple times until all data are received. will be called multiple times until all data are received.
''' '''
pass pass
...@@ -786,12 +788,14 @@ class BaseRequestHandler: ...@@ -786,12 +788,14 @@ class BaseRequestHandler:
def push(self, path, method='GET', request_headers=None, def push(self, path, method='GET', request_headers=None,
status=200, headers=None, body=None): status=200, headers=None, body=None):
'''Push a resource. The path is a path portion of request URI for this '''Push a resource. The path is a path portion of request URI
resource. The method is a method to access this resource. The for this
request_headers is additional request headers to access this resource. The method is a method to access this
resource. The :scheme, :method, :authority and :path are resource. The request_headers is additional request
appended by the library. The :scheme and :authority are headers to access this resource. The :scheme, :method,
inherited from the request (not request_headers parameter). :authority and :path are appended by the library. The
:scheme and :authority are inherited from the request (not
request_headers parameter).
The status is HTTP status code. The headers is additional The status is HTTP status code. The headers is additional
response headers. The :status header field is appended by the response headers. The :status header field is appended by the
...@@ -845,7 +849,8 @@ class BaseRequestHandler: ...@@ -845,7 +849,8 @@ class BaseRequestHandler:
headers = [] headers = []
self.response_headers = _encode_headers(headers) self.response_headers = _encode_headers(headers)
self.response_headers.append((b':status', str(status).encode('utf-8'))) self.response_headers.append((b':status', str(status)\
.encode('utf-8')))
self.response_body = body self.response_body = body
...@@ -859,15 +864,15 @@ class BaseRequestHandler: ...@@ -859,15 +864,15 @@ class BaseRequestHandler:
elif isinstance(body, io.IOBase): elif isinstance(body, io.IOBase):
return body return body
else: else:
raise Exception(('body must be None or instance of str or bytes ' raise Exception(('body must be None or instance of str or '
'or io.IOBase')) 'bytes or io.IOBase'))
def _encode_headers(headers): def _encode_headers(headers):
return [(k if isinstance(k, bytes) else k.encode('utf-8'), return [(k if isinstance(k, bytes) else k.encode('utf-8'),
v if isinstance(v, bytes) else v.encode('utf-8')) \ v if isinstance(v, bytes) else v.encode('utf-8')) \
for k, v in headers] for k, v in headers]
class _HTTP2Session(asyncio.Protocol): class _HTTP2Session(asyncio.Protocol):
def __init__(self, RequestHandlerClass): def __init__(self, RequestHandlerClass):
asyncio.Protocol.__init__(self) asyncio.Protocol.__init__(self)
...@@ -897,7 +902,8 @@ class _HTTP2Session(asyncio.Protocol): ...@@ -897,7 +902,8 @@ class _HTTP2Session(asyncio.Protocol):
self.connection_header = self.connection_header[nread:] self.connection_header = self.connection_header[nread:]
if len(self.connection_header) == 0: if len(self.connection_header) == 0:
try: try:
self.http2 = _HTTP2SessionCore(self.transport, self.http2 = _HTTP2SessionCore\
(self.transport,
self.RequestHandlerClass) self.RequestHandlerClass)
except Exception as err: except Exception as err:
sys.stderr.write(traceback.format_exc()) sys.stderr.write(traceback.format_exc())
...@@ -926,7 +932,7 @@ class _HTTP2Session(asyncio.Protocol): ...@@ -926,7 +932,7 @@ class _HTTP2Session(asyncio.Protocol):
self.transport.close() self.transport.close()
return return
class HTTP2Server: class HTTP2Server:
'''HTTP/2 server. '''HTTP/2 server.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment