Esempio n. 1
0
async def test_http_to_https_redirect_middleware_http() -> None:
    app = HTTPToHTTPSRedirectMiddleware(empty_framework, "localhost")
    sent_events = []

    async def send(message: dict) -> None:
        nonlocal sent_events
        sent_events.append(message)

    scope = {
        "type": "http",
        "scheme": "http",
        "path": "/abc",
        "query_string": b"a=b"
    }
    await app(scope, None, send)

    assert sent_events == [
        {
            "type": "http.response.start",
            "status": 307,
            "headers": [(b"location", b"https://localhost/abc?a=b")],
        },
        {
            "type": "http.response.body"
        },
    ]
Esempio n. 2
0
async def test_http_to_https_redirect_middleware_websocket_http2() -> None:
    app = HTTPToHTTPSRedirectMiddleware(empty_framework, "localhost")
    sent_events = []

    async def send(message: dict) -> None:
        nonlocal sent_events
        sent_events.append(message)

    scope: WebsocketScope = {
        "type": "websocket",
        "asgi": {},
        "http_version": "2",
        "scheme": "ws",
        "path": "/abc",
        "raw_path": b"/abc",
        "query_string": b"a=b",
        "root_path": "",
        "headers": [],
        "client": None,
        "server": None,
        "subprotocols": [],
        "extensions": {"websocket.http.response": {}},
    }
    await app(scope, None, send)

    assert sent_events == [
        {
            "type": "websocket.http.response.start",
            "status": 307,
            "headers": [(b"location", b"https://localhost/abc?a=b")],
        },
        {"type": "websocket.http.response.body"},
    ]
Esempio n. 3
0
async def test_http_to_https_redirect_middleware_websocket(
        raw_path: bytes) -> None:
    app = HTTPToHTTPSRedirectMiddleware(empty_framework, "localhost")
    sent_events = []

    async def send(message: dict) -> None:
        nonlocal sent_events
        sent_events.append(message)

    scope = {
        "type": "websocket",
        "scheme": "ws",
        "raw_path": raw_path,
        "query_string": b"a=b",
        "extensions": {
            "websocket.http.response": {}
        },
    }
    await app(scope, None, send)

    assert sent_events == [
        {
            "type": "websocket.http.response.start",
            "status": 307,
            "headers": [(b"location", b"wss://localhost%s?a=b" % raw_path)],
        },
        {
            "type": "websocket.http.response.body"
        },
    ]
Esempio n. 4
0
async def test_http_to_https_redirect_middleware_websocket_no_rejection() -> None:
    app = HTTPToHTTPSRedirectMiddleware(empty_framework, "localhost")
    sent_events = []

    async def send(message: dict) -> None:
        nonlocal sent_events
        sent_events.append(message)

    scope: WebsocketScope = {
        "type": "websocket",
        "asgi": {},
        "http_version": "2",
        "scheme": "ws",
        "path": "/abc",
        "raw_path": b"/abc",
        "query_string": b"a=b",
        "root_path": "",
        "headers": [],
        "client": None,
        "server": None,
        "subprotocols": [],
        "extensions": {},
    }
    await app(scope, None, send)

    assert sent_events == [{"type": "websocket.close"}]
Esempio n. 5
0
async def test_http_to_https_redirect_middleware_http(raw_path: bytes) -> None:
    app = HTTPToHTTPSRedirectMiddleware(empty_framework, "localhost")
    sent_events = []

    async def send(message: dict) -> None:
        nonlocal sent_events
        sent_events.append(message)

    scope: HTTPScope = {
        "type": "http",
        "asgi": {},
        "http_version": "2",
        "method": "GET",
        "scheme": "http",
        "path": raw_path.decode(),
        "raw_path": raw_path,
        "query_string": b"a=b",
        "root_path": "",
        "headers": [],
        "client": ("127.0.0.1", 80),
        "server": None,
        "extensions": {},
    }

    await app(scope, None, send)

    assert sent_events == [
        {
            "type": "http.response.start",
            "status": 307,
            "headers": [(b"location", b"https://localhost%s?a=b" % raw_path)],
        },
        {"type": "http.response.body"},
    ]
Esempio n. 6
0
def test_http_to_https_redirect_new_url_header() -> None:
    app = HTTPToHTTPSRedirectMiddleware(empty_framework, None)
    new_url = app._new_url(
        "https",
        {
            "http_version": "1.1",
            "asgi": {},
            "method": "GET",
            "headers": [(b"host", b"localhost")],
            "path": "/",
            "root_path": "",
            "query_string": b"",
            "raw_path": b"/",
            "scheme": "http",
            "type": "http",
            "client": None,
            "server": None,
            "extensions": {},
        },
    )
    assert new_url == "https://localhost/"
Esempio n. 7
0
async def test_http_to_https_redirect_middleware_websocket_no_rejection() -> None:
    app = HTTPToHTTPSRedirectMiddleware(empty_framework, "localhost")
    sent_events = []

    async def send(message: dict) -> None:
        nonlocal sent_events
        sent_events.append(message)

    scope = {
        "type": "websocket",
        "http_version": "2",
        "scheme": "ws",
        "path": "/abc",
        "query_string": b"a=b",
    }
    await app(scope, None, send)

    assert sent_events == [{"type": "websocket.close"}]
Esempio n. 8
0
        -dNOPAUSE -dQUIET -dBATCH -sOutputFile={out_pdf_name} {in_pdf.name}
    """.split())
    with open(out_pdf_name, 'rb') as out_pdf:
        compressed = out_pdf.read()
    os.remove(in_pdf.name)
    os.remove(out_pdf_name)
    kb = lambda bytes: f'{len(bytes)//1000}kb'
    print(f'Compressed pdf {kb(bytes)} -> {kb(compressed)}')
    return compressed if len(compressed) < len(bytes) else bytes


# Serve React App ######################################################################################################


@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
async def serve(path):
    if path and (Path(app.static_folder) / path).exists():
        return await send_from_directory(app.static_folder, path)
    else:
        return await send_from_directory(app.static_folder, 'index.html')


# Run ##################################################################################################################

if 'HOST' in os.environ:
    from hypercorn.middleware import HTTPToHTTPSRedirectMiddleware
    redirected_app = HTTPToHTTPSRedirectMiddleware(app, os.environ['HOST'])

if __name__ == '__main__':
    app.run(host="0.0.0.0", debug=True)