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

Use https and http appropriately when connecting to MusicBrainz #450

Merged
merged 2 commits into from
Jan 29, 2020
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
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,17 @@ options:

```INI
[main]
path_filter_fat = True ; replace FAT file system unsafe characters in filenames with _
path_filter_special = False ; replace special characters in filenames with _
path_filter_fat = True ; replace FAT file system unsafe characters in filenames with _
path_filter_special = False ; replace special characters in filenames with _

[musicbrainz]
server = musicbrainz.org:80 ; use MusicBrainz server at host[:port]
server = https://musicbrainz.org ; use MusicBrainz server at host[:port]
# use http as scheme if connecting to a plain http server. Example below:
# server = http://example.com:8080

[drive:HL-20]
defeats_cache = True ; whether the drive is capable of defeating the audio cache
read_offset = 6 ; drive read offset in positive/negative frames (no leading +)
defeats_cache = True ; whether the drive is capable of defeating the audio cache
read_offset = 6 ; drive read offset in positive/negative frames (no leading +)
# do not edit the values 'vendor', 'model', and 'release'; they are used by whipper to match the drive

# command line defaults for `whipper cd rip`
Expand Down
8 changes: 7 additions & 1 deletion whipper/command/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@

def main():
server = config.Config().get_musicbrainz_server()
musicbrainzngs.set_hostname(server)
https_enabled = server['scheme'] == 'https'
try:
musicbrainzngs.set_hostname(server['netloc'], https_enabled)
# Parameter 'use_https' is missing in versions of musicbrainzngs < 0.7
except TypeError as e:
logger.warning(e)
musicbrainzngs.set_hostname(server['netloc'])

# Find whipper's plugins paths (local paths have higher priority)
plugins_p = [directory.data_path('plugins')] # local path (in $HOME)
Expand Down
11 changes: 6 additions & 5 deletions whipper/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ def getboolean(self, section, option):
# musicbrainz section

def get_musicbrainz_server(self):
server = self.get('musicbrainz', 'server') or 'musicbrainz.org'
server_url = urlparse('//' + server)
if server_url.scheme != '' or server_url.path != '':
raise KeyError('Invalid MusicBrainz server: %s' % server)
return server
conf = self.get('musicbrainz', 'server') or 'https://musicbrainz.org'
if not conf.startswith(('http://', 'https://')):
raise KeyError('Invalid MusicBrainz server: unsupported '
'or missing scheme')
scheme, netloc, _, _, _, _ = urlparse(conf)
return {'scheme': scheme, 'netloc': netloc}

# drive sections

Expand Down
4 changes: 2 additions & 2 deletions whipper/image/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def getMusicBrainzDiscId(self):
return disc.id

def getMusicBrainzSubmitURL(self):
host = config.Config().get_musicbrainz_server()
serv = config.Config().get_musicbrainz_server()

discid = self.getMusicBrainzDiscId()
values = self._getMusicBrainzValues()
Expand All @@ -363,7 +363,7 @@ def getMusicBrainzSubmitURL(self):
])

return urlunparse((
'https', host, '/cdtoc/attach', '', query, ''))
serv['scheme'], serv['netloc'], '/cdtoc/attach', '', query, ''))

def getFrameLength(self, data=False):
"""
Expand Down
14 changes: 7 additions & 7 deletions whipper/test/test_common_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,25 @@ def testDefeatsCache(self):

def test_get_musicbrainz_server(self):
self.assertEqual(self._config.get_musicbrainz_server(),
'musicbrainz.org',
{'scheme': 'https', 'netloc': 'musicbrainz.org'},
msg='Default value is correct')

self._config._parser.add_section('musicbrainz')

self._config._parser.set('musicbrainz', 'server',
'192.168.2.141:5000')
'http://192.168.2.141:5000')
self._config.write()
self.assertEqual(self._config.get_musicbrainz_server(),
'192.168.2.141:5000',
{'scheme': 'http', 'netloc': '192.168.2.141:5000'},
msg='Correctly returns user-set value')

self._config._parser.set('musicbrainz', 'server',
'192.168.2.141:5000/hello/world')
# Test for unsupported scheme
self._config._parser.set('musicbrainz', 'server', 'ftp://example.com')
self._config.write()
self.assertRaises(KeyError, self._config.get_musicbrainz_server)

self._config._parser.set('musicbrainz', 'server',
'http://192.168.2.141:5000')
# Test for absent scheme
self._config._parser.set('musicbrainz', 'server', 'example.com')
self._config.write()
self.assertRaises(KeyError, self._config.get_musicbrainz_server)

Expand Down
27 changes: 27 additions & 0 deletions whipper/test/test_image_table.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# -*- Mode: Python; test-case-name: whipper.test.test_image_table -*-
# vi:si:et:sw=4:sts=4:ts=4

from os import environ
from shutil import rmtree
from tempfile import mkdtemp
from whipper.image import table
from whipper.common import config

from whipper.test import common as tcommon

Expand Down Expand Up @@ -58,8 +62,31 @@ def testMusicBrainz(self):
# 177832&tracks=12&id=KnpGsLhvH.lPrNc1PBL21lb9Bg4-
# however, not (yet) in MusicBrainz database

# setup to test if MusicBrainz submit URL is hardcoded to use https
env_original = environ.get('XDG_CONFIG_HOME')
tmp_conf = mkdtemp(suffix='.config')
# HACK: hijack env var to avoid overwriting user's whipper config file
# This works because directory.config_path() builds the location where
# whipper's conf will reside based on the value of env XDG_CONFIG_HOME
environ['XDG_CONFIG_HOME'] = tmp_conf
self.config = config.Config()
self.config._parser.add_section('musicbrainz')
self.config._parser.set('musicbrainz', 'server',
'http://musicbrainz.org')
self.config.write()
self.assertEqual(self.table.getMusicBrainzSubmitURL(),
"http://musicbrainz.org/cdtoc/attach?toc=1+12+1958"
"56+150+15687+31841+51016+66616+81352+99559+116070+13"
"3243+149997+161710+177832&tracks=12&id=KnpGsLhvH.lPr"
"Nc1PBL21lb9Bg4-")
# HACK: continuation - restore original env value (if defined)
if env_original is not None:
environ['XDG_CONFIG_HOME'] = env_original
else:
environ.pop('XDG_CONFIG_HOME', None)
self.assertEqual(self.table.getMusicBrainzDiscId(),
"KnpGsLhvH.lPrNc1PBL21lb9Bg4-")
rmtree(tmp_conf)

def testAccurateRip(self):
self.assertEqual(self.table.accuraterip_ids(), (
Expand Down