def _create_client_side(self, ip, port): if self.http2: print("Creating HTTP/2 proxy") # setup ssl ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23) ctx.options = ssl.OP_ALL | ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3 ctx.load_cert_chain( './web/cert/server.crt', './web/cert/server.key', password="******") server = nghttp2.HTTP2Server((ip,port), NGHTTP2Handler, ctx) else: print("Creating HTTP/1.1 proxy") coro = self.loop.create_server(HTTP11Protcol, ip, port) server = self.loop.run_until_complete(coro) return
# other way for now # TODO disallow yielding/returning before start_response is called response_chunks.extend(self.app(environ, start_response)) response_body = b''.join(response_chunks) # TODO automatically set content-length if not provided self.send_response( status=response_status[0], headers=response_headers[0], body=response_body, ) def wsgi_app(app): return lambda *args, **kwargs: WSGIContainer(app, *args, **kwargs) if __name__ == '__main__': import ssl from werkzeug.testapp import test_app ssl_ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23) ssl_ctx.options = ssl.OP_ALL | ssl.OP_NO_SSLv2 ssl_ctx.load_cert_chain('server.crt', 'server.key') server = nghttp2.HTTP2Server(('127.0.0.1', 8443), wsgi_app(test_app), ssl=ssl_ctx) server.serve_forever()
# This is sample test server for testing HTTP2 client. # TODO expand implementation as client features are added. class Handler(nghttp2.BaseRequestHandler): def on_headers(self): self.push(path='/css/bootstrap.css', request_headers=[('content-length', '3')], status=200, body='foo') self.push(path='/js/bootstrap.js', method='GET', request_headers=[('content-length', '10')], status=200, body='foobarbuzz') self.send_response(status=200, headers=[('content-type', 'text/plain')], body=io.BytesIO(b'nghttp2-python FTW')) # ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23) # ctx.options = ssl.OP_ALL | ssl.OP_NO_SSLv2 # ctx.load_cert_chain('server.crt', 'server.key') ctx = None # give None to ssl to make the server non-SSL/TLS server = nghttp2.HTTP2Server(('0.0.0.0', 8000), Handler, ssl=ctx) server.serve_forever()
handler.resume() class Body: def __init__(self, handler): self.handler = handler self.handler.eof = False self.handler.buf = io.BytesIO() def generate(self, n): buf = self.handler.buf data = buf.read1(n) if not data and not self.handler.eof: return None, nghttp2.DATA_DEFERRED return data, nghttp2.DATA_EOF if self.handler.eof else nghttp2.DATA_OK class Handler(nghttp2.BaseRequestHandler): def on_headers(self): body = Body(self) asyncio. async (get_http_header( self, 'http://localhost' + self.path.decode('utf-8'))) self.send_response(status=200, body=body.generate) ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23) ctx.options = ssl.OP_ALL | ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3 ctx.load_cert_chain('../cert/server.crt', '../cert/server.key') server = nghttp2.HTTP2Server(('localhost', 8080), Handler, ssl=ctx) server.serve_forever()