From 1c9380835c6d8d89a69ee058e5356d3b847da233 Mon Sep 17 00:00:00 2001 From: Lionel Dricot Date: Wed, 30 Nov 2022 00:11:44 +0100 Subject: [PATCH] removing dependency on cgi and implementing our own mime parser --- CHANGELOG | 1 + offpunk.py | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 97e838c..f61e040 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,6 +5,7 @@ - "set accept_bad_ssl_certificates True" allows to lower HTTPS SSL requirements - Accept "localhost" as a valid URL - Better feedback when --sync an URL which is streaming +- Removed cgi dependency (soon deprecated) - Fix a crash when ls on empty page (thanks Marty Oehme) - Fix: A variable was not initialised without python-cryptography - Fix: "cp raw" was not accessing the temp_file correctly diff --git a/offpunk.py b/offpunk.py index 61c51fe..ffad5b7 100755 --- a/offpunk.py +++ b/offpunk.py @@ -16,7 +16,6 @@ _VERSION = "1.7.1" import argparse import cmd -import cgi import codecs import datetime import fnmatch @@ -154,6 +153,19 @@ def terminal_image(img_file): cmd = cmd + " \"%s\""%img_file run(cmd,direct_output=True) +def parse_mime(mime): + options = {} + if mime: + if ";" in mime: + splited = mime.split(";",maxsplit=1) + mime = splited[0] + if len(splited) >= 1: + options_list = splited[1].split() + for o in options_list: + spl = o.split("=",maxsplit=1) + if len(spl) > 0: + options[spl[0]] = spl[1] + return mime, options _HAS_XSEL = shutil.which('xsel') _HAS_XDGOPEN = shutil.which('xdg-open') @@ -1659,8 +1671,7 @@ class GeminiItem(): ## Write_body() also create the cache ! # DEFAULT GEMINI MIME self.body = body - if mime: - self.mime, mime_options = cgi.parse_header(mime) + self.mime, options = parse_mime(mime) if not self.local: if self.mime and self.mime.startswith("text/"): mode = "w" @@ -2467,7 +2478,7 @@ class GeminiClient(cmd.Cmd): # DEFAULT GEMINI MIME if mime == "": mime = "text/gemini; charset=utf-8" - shortmime, mime_options = cgi.parse_header(mime) + shortmime, mime_options = parse_mime(mime) if "charset" in mime_options: try: codecs.lookup(mime_options["charset"])