~sotirisp/qute-gemini

79a3ae5b9c71fda7d7b34bfd2be5b9c215b68403 — Sotiris Papatheodorou 9 months ago 677acae
Implement a redirect limit
1 files changed, 10 insertions(+), 1 deletions(-)

M qute-gemini
M qute-gemini => qute-gemini +10 -1
@@ 19,6 19,8 @@ import urllib.parse
from typing import Tuple


_max_redirects = 5

_error_page_template = '''<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>


@@ 35,11 37,13 @@ CSS
</html>
'''


_status_code_desc = {
    "1":  "Gemini status code 1 Input. This is not implemented in qute-gemini.",
    "10": "Gemini status code 10 Input. This is not implemented in qute-gemini.",
    "11": "Gemini status code 11 Sensitive Input. This is not implemented in qute-gemini.",
    "3":  "Gemini status code 3 Redirect. Stopped after " + str(_max_redirects) + " redirects.",
    "30": "Gemini status code 30 Temporary Redirect. Stopped after " + str(_max_redirects) + " redirects.",
    "31": "Gemini status code 31 Permanent Redirect. Stopped after " + str(_max_redirects) + " redirects.",
    "4":  "Gemini status code 4 Temporary Failure. Server message: META",
    "40": "Gemini status code 40 Temporary Failure. Server message: META",
    "41": "Gemini status code 41 Server Unavailable. The server is unavailable due to overload or maintenance. Server message: META",


@@ 109,6 113,7 @@ def gemini_fetch_url(url: str) -> Tuple[str, str]:
    else:
        useport = 1965
    # Do the Gemini transaction
    redirects = 0
    while True:
        s = socket.create_connection((parsed_url.hostname, useport))
        context = ssl.SSLContext(ssl.PROTOCOL_TLS)


@@ 124,6 129,10 @@ def gemini_fetch_url(url: str) -> Tuple[str, str]:
        if status.startswith("3"):
            url = gemini_absolutise_url(url, meta)
            parsed_url = urllib.parse.urlparse(url)
            redirects += 1
            if redirects > _max_redirects:
                # Too many redirects
                break
        # Otherwise we're done
        else:
            break