def test_build_status():
    status = utils.build_status(200)
    assert status == "200 OK"

    status = utils.build_status(400)
    assert status == "400 Bad Request"

    status = utils.build_status(404)
    assert status == "404 Not Found"

    status = utils.build_status(500)
    assert status == "500 Internal Server Error"
Пример #2
0
def handle_404(request: RequestT) -> ResponseT:
    url = request.path
    pin = random.randint(1, 999999)

    environ_pairs = "\n".join(
        f"<div class=\"pair {'http' if env_var_name.startswith('HTTP') else ''}\">"
        f"<p>{escape(str(env_var_name))}</p>"
        f"<p>{format_env_var(env_var_name, env_var_value)}</p>"
        f"</div>"
        for env_var_name, env_var_value in sorted(request.headers.items(),
                                                  key=http_first))

    base_html = read_static("_base.html").content.decode()

    html_404 = f"""
    <h1>OOPS!</h1>
        <hr>
        <h2>The path you've looking for does not exist on this server.</h2>
        <p class="url"><span>{url}</span></p>
        <p>Pin: <span class="pin">{pin:>06}</span></p>
        <div class="environ-table">
        {environ_pairs}
        <div>
    """

    document = base_html.format(body=html_404)

    payload = document.encode()
    status = build_status(404)
    headers = {"Content-type": "text/html"}

    return ResponseT(status, headers, payload)
def handle_500(_request: RequestT = None) -> ResponseT:
    traceback.print_exc()

    error_class, error, tb = sys.exc_info()

    filenames = "".join(
        f"""<p>File <a href="{build_absolute_url("/s/{fn}", fn=frame.f_code.co_filename)}">{frame.f_code.co_filename}</a>, line {lineno}</p>"""
        for frame, lineno in traceback.walk_tb(tb))

    document = f"""
        <h1>WASTED</h1>
        <hr>
        <div class="code">
        {filenames}
        <p>
        {error_class.__name__}: {error}
        </p>
        </div>
    """

    base_html = read_static("_base.html").content.decode()

    document = base_html.format(body=document)

    payload = document.encode()
    status = build_status(500)
    headers = {"Content-type": "text/html"}

    return ResponseT(status, headers, payload)
Пример #4
0
def _handle_hello_greet(request: RequestT) -> ResponseT:
    if request.method != "POST":
        raise MethodNotAllowed

    name = request.form_data.get("name", [None])[0]
    address = request.form_data.get("address", [None])[0]

    request.user.name = name
    request.user.address = address

    save_user(request.user)

    status = build_status(302)

    cookie = build_session_header(request.user.id)

    headers = {
        "Location": "/h/",
        "Set-Cookie": cookie,
    }

    response = ResponseT(
        headers=headers,
        status=status,
    )

    return response
Пример #5
0
def _handle_hello_index(request: RequestT) -> ResponseT:
    if request.method != "GET":
        raise MethodNotAllowed

    base = read_static("_base.html")
    base_html = base.content.decode()
    hello_html = read_static("hello.html").content.decode()

    body = hello_html.format(
        address_header=request.user.address or "the middle of f*****g nowhere",
        address_value=request.user.address or "",
        name_header=request.user.name or "anonymous",
        name_value=request.user.name or "",
    )

    document = base_html.format(body=body)

    status = build_status(200)

    headers = {"Content-Type": base.content_type}

    response = ResponseT(
        headers=headers,
        payload=document.encode(),
        status=status,
    )

    return response
Пример #6
0
def hello_reset(_request):
    if USER_DATA_FILE.is_file():
        os.remove(USER_DATA_FILE)
    response = ResponseT(
        status=build_status(302),
        headers={"Location": "/h/"},
    )

    return response
Пример #7
0
def handle_405(request: RequestT) -> ResponseT:
    status = build_status(405)
    payload = f"Resource {request.path} does not support {request.method} requests."
    headers = {"Content-type": "text/plain"}

    return ResponseT(
        status=status,
        headers=headers,
        payload=payload.encode(),
    )
Пример #8
0
def handle_static(request: RequestT) -> ResponseT:
    file_name = request.kwargs["file_name"]

    static = read_static(file_name)
    status = build_status(200)

    return ResponseT(
        headers={"Content-Type": static.content_type},
        payload=static.content,
        status=status,
    )
Пример #9
0
def handle_hello_post(request: RequestT) -> ResponseT:
    assert request.method == "POST"

    form_data = request.form_data

    name = form_data.get("name", [None])[0]
    address = form_data.get("address", [None])[0]

    user_data = UserDataT(name=name, address=address)

    save_user_data(user_data)

    response = ResponseT(
        status=build_status(302),
        headers={"Location": "/h/"},
    )

    return response
Пример #10
0
def handle_index(_request: RequestT) -> ResponseT:
    base = read_static("_base.html")
    base_html = base.content.decode()
    index_html = read_static("index.html").content.decode()

    result = base_html.format(xxx=index_html)
    result = result.encode()

    status = build_status(200)
    headers = {
        "Content-type": base.content_type,
    }

    return ResponseT(
        headers=headers,
        payload=result,
        status=status,
    )
Пример #11
0
def _handle_hello_reset(request: RequestT) -> ResponseT:
    if request.method != "POST":
        raise MethodNotAllowed

    delete_user(request.user)

    status = build_status(302)

    cookie = build_reset_session_header(request.user.id)

    headers = {
        "Location": "/h/",
        "Set-Cookie": cookie,
    }

    response = ResponseT(
        headers=headers,
        status=status,
    )

    return response
Пример #12
0
def handle_hello_get(request: RequestT) -> ResponseT:
    assert request.method == "GET"

    base = read_static("_base.html")
    base_html = base.content.decode()
    hello_html = read_static("hello.html").content.decode()

    user_data = load_user_data(request)

    document = hello_html.format(
        address_header=user_data.address or "nowhere",
        address_value=user_data.address or "",
        name_header=user_data.name or "Bro",
        name_value=user_data.name or "",
    )
    document = base_html.format(xxx=document)

    resp = ResponseT(
        status=build_status(200),
        headers={"Content-Type": base.content_type},
        payload=document.encode(),
    )

    return resp