Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decompress content while loading gzipped playlists #375

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions m3u8/httpclient.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import gzip
import ssl
import urllib.request

from urllib.parse import urljoin


Expand All @@ -15,9 +15,15 @@ def download(self, uri, timeout=None, headers={}, verify_ssl=True):
opener.addheaders = headers.items()
resource = opener.open(uri, timeout=timeout)
base_uri = urljoin(resource.geturl(), ".")
content = resource.read().decode(
resource.headers.get_content_charset(failobj="utf-8")
)

if resource.info().get("Content-Encoding") == "gzip":
content = gzip.decompress(resource.read()).decode(
resource.headers.get_content_charset(failobj="utf-8")
)
else:
content = resource.read().decode(
resource.headers.get_content_charset(failobj="utf-8")
)
return content, base_uri


Expand Down
63 changes: 63 additions & 0 deletions tests/test_http_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import gzip
import unittest
from http.client import HTTPResponse
from unittest.mock import Mock, patch

from m3u8.httpclient import DefaultHTTPClient


class MockHeaders:
def __init__(self, encoding=None):
self.encoding = encoding

def get_content_charset(self, failobj="utf-8"):
return self.encoding or failobj


class TestDefaultHTTPClient(unittest.TestCase):
@patch("urllib.request.OpenerDirector.open")
def test_download_normal_content(self, mock_open):
client = DefaultHTTPClient()
mock_response = Mock(spec=HTTPResponse)
mock_response.read.return_value = b"playlist content"
mock_response.info.return_value = {}
mock_response.geturl.return_value = "http://example.com/index.m3u8"
mock_response.headers = MockHeaders()
mock_open.return_value = mock_response

content, base_uri = client.download("http://example.com/index.m3u8")

self.assertEqual(content, "playlist content")
self.assertEqual(base_uri, "http://example.com/")

@patch("urllib.request.OpenerDirector.open")
def test_download_gzipped_content(self, mock_open):
client = DefaultHTTPClient()
original_content = "playlist gzipped content"
gzipped_content = gzip.compress(original_content.encode("utf-8"))
mock_response = Mock(spec=HTTPResponse)
mock_response.read.return_value = gzipped_content
mock_response.info.return_value = {"Content-Encoding": "gzip"}
mock_response.geturl.return_value = "http://example.com/index.m3u8"
mock_response.headers = MockHeaders("utf-8")
mock_open.return_value = mock_response

content, base_uri = client.download("http://example.com/index.m3u8")

self.assertEqual(content, original_content)
self.assertEqual(base_uri, "http://example.com/")

@patch("urllib.request.OpenerDirector.open")
def test_download_with_proxy(self, mock_open):
client = DefaultHTTPClient(proxies={"http": "http://proxy.example.com"})
mock_response = Mock(spec=HTTPResponse)
mock_response.read.return_value = b"playlist proxied content"
mock_response.info.return_value = {}
mock_response.geturl.return_value = "http://example.com/index.m3u8"
mock_response.headers = MockHeaders()
mock_open.return_value = mock_response

content, base_uri = client.download("http://example.com/index.m3u8")

self.assertEqual(content, "playlist proxied content")
self.assertEqual(base_uri, "http://example.com/")
Loading