From 28d962547bbfd24a6c38653685f65bf3165e8e5d Mon Sep 17 00:00:00 2001 From: Paul Bonser Date: Fri, 18 Oct 2024 13:44:29 -0500 Subject: [PATCH 1/2] Switch to email header parser from deprecated cgi library --- hashin.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/hashin.py b/hashin.py index ecef05f..2223b08 100755 --- a/hashin.py +++ b/hashin.py @@ -7,8 +7,8 @@ from __future__ import print_function import argparse -import cgi import difflib +from email.headerregistry import HeaderRegistry import tempfile import os import re @@ -52,6 +52,9 @@ def _verbose(*args): print("* " + " ".join(args)) +_header_registry = HeaderRegistry() + + def _download(url, binary=False): try: r = urlopen(url) @@ -64,7 +67,7 @@ def _download(url, binary=False): # Note that urlopen will, by default, follow redirects. status_code = r.getcode() if 301 <= status_code < 400: - location, _ = cgi.parse_header(r.headers.get("location", "")) + location = r.headers.get("location", "") if not location: raise PackageError( "No 'Location' header on {0} ({1})".format(url, status_code) @@ -76,8 +79,9 @@ def _download(url, binary=False): raise PackageError("Download error. {0} on {1}".format(status_code, url)) if binary: return r.read() - _, params = cgi.parse_header(r.headers.get("Content-Type", "")) - encoding = params.get("charset", "utf-8") + + content_type = _header_registry("content-type", r.headers.get("Content-Type", "")) + encoding = content_type.params.get("charset", "utf-8") return r.read().decode(encoding) From 062d7d53909f8693fb98f0f65005e4eb996317f0 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Mon, 28 Oct 2024 09:51:31 -0400 Subject: [PATCH 2/2] Update hashin.py Co-authored-by: Sebastian Pipping --- hashin.py | 1 - 1 file changed, 1 deletion(-) diff --git a/hashin.py b/hashin.py index 2223b08..cbe30ab 100755 --- a/hashin.py +++ b/hashin.py @@ -79,7 +79,6 @@ def _download(url, binary=False): raise PackageError("Download error. {0} on {1}".format(status_code, url)) if binary: return r.read() - content_type = _header_registry("content-type", r.headers.get("Content-Type", "")) encoding = content_type.params.get("charset", "utf-8") return r.read().decode(encoding)