~nhanb/mcross

1f361980b5c3a01162a6f4a996162b27a1608f1f — Bùi Thành Nhân 11 months ago 526da28 0.5.11
show request time status, add request timeout
3 files changed, 21 insertions(+), 9 deletions(-)

M pyproject.toml
M src/mcross/gui/controller.py
M src/mcross/transport.py
M pyproject.toml => pyproject.toml +1 -1
@@ 1,6 1,6 @@
[tool.poetry]
name = "mcross"
version = "0.5.10"
version = "0.5.11"
description = "Do you remember www?"
authors = ["nhanb <hi@imnhan.com>"]
license = "MIT"

M src/mcross/gui/controller.py => src/mcross/gui/controller.py +18 -7
@@ 1,5 1,6 @@
import logging
import threading
import time
import traceback
from ssl import SSLCertVerificationError
from tkinter import READABLE, Tk, messagebox


@@ 7,6 8,7 @@ from tkinter import READABLE, Tk, messagebox
import curio

from ..transport import (
    MAX_REQUEST_SECONDS,
    GeminiUrl,
    NonAbsoluteUrlWithoutContextError,
    UnsupportedProtocolError,


@@ 122,6 124,16 @@ class Controller:
            self.model.history.visit(resp.url)
            await self.put_gui_op(self.view.render_page)

        except curio.errors.TaskTimeout:
            await self.put_gui_op(
                statusbar_logger.info, f"Request timed out: {MAX_REQUEST_SECONDS}s",
            )
            await self.put_gui_op(
                messagebox.showwarning,
                "Request timed out",
                f"Request to {url.without_protocol()} took longer than {MAX_REQUEST_SECONDS}s",
            )

        except (ConnectionError, OSError) as e:
            await self.put_gui_op(statusbar_logger.info, str(e))
            raise


@@ 138,14 150,13 @@ class Controller:

    async def load_page(self, url: GeminiUrl):
        await self.put_gui_op(statusbar_logger.info, f"Requesting {url}...")
        start = time.time()
        resp = await get(url)
        await self.put_gui_op(statusbar_logger.info, f"{resp.status} {resp.meta}")

        async def clear_status_bar_later():
            await curio.sleep(2)
            await self.put_gui_op(statusbar_logger.info, "")

        await curio.spawn(clear_status_bar_later(), daemon=True)
        request_time = time.time() - start
        await self.put_gui_op(
            statusbar_logger.info,
            f"{resp.status} {resp.meta} (took {request_time:.2f}s)",
        )

        if resp.status.startswith("2"):
            await self.put_gui_op(self.model.update_content, resp.body.decode())

M src/mcross/transport.py => src/mcross/transport.py +2 -1
@@ 7,6 7,7 @@ import curio
MAX_RESP_HEADER_BYTES = 2 + 1 + 1024 + 2  # <STATUS><whitespace><META><CR><LF>
MAX_RESP_BODY_BYTES = 1024 * 1024 * 5
MAX_REDIRECTS = 3
MAX_REQUEST_SECONDS = 30


class Response:


@@ 142,7 143,7 @@ async def raw_get(url: GeminiUrl):


async def get(url: GeminiUrl, redirect_count=0):
    resp = await raw_get(url)
    resp = await curio.timeout_after(MAX_REQUEST_SECONDS, raw_get, url)
    if resp.status.startswith("3") and redirect_count < MAX_REDIRECTS:
        redirect_count += 1
        new_url = GeminiUrl.parse_absolute_url(resp.meta)