Skip to content

Commit

Permalink
Fix: Make remaining latin1 workarounds conditional
Browse files Browse the repository at this point in the history
This fixes #701 and fixes #710.
  • Loading branch information
tilboerner committed Feb 4, 2018
1 parent 32f9eca commit 7aaa4fd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Changelog

0.41.2 (2018-02-04)
- FIXED: '--setup' now works for CherryPy 12 and upwards (thanks to TangoSierraAlfaVI)
- FIXED: UnicodeError when transcoding non-ASCII URLs
- IMPROVEMENT: Added Bandcamp album cover fetching (thanks to its-wednesday)

0.41.1 (2017-10-10)
Expand Down
9 changes: 5 additions & 4 deletions cherrymusicserver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,10 +466,11 @@ def start_server(self, httphandler):
basedirpath = codecs.encode(basedirpath, 'utf-8')
scriptname = codecs.encode(config['server.rootpath'], 'utf-8')
else:
# fix cherrypy unicode issue (only for Python3)
# see patch to cherrypy.lib.static.serve_file way above and
# https://bitbucket.org/cherrypy/cherrypy/issue/1148/wrong-encoding-for-urls-containing-utf-8
basedirpath = codecs.decode(codecs.encode(basedirpath, 'utf-8'), 'latin-1')
if needs_serve_file_utf8_fix:
# fix cherrypy unicode issue (only for Python3)
# see patch to cherrypy.lib.static.serve_file way above and
# https://bitbucket.org/cherrypy/cherrypy/issue/1148/wrong-encoding-for-urls-containing-utf-8
basedirpath = codecs.decode(codecs.encode(basedirpath, 'utf-8'), 'latin-1')
scriptname = config['server.rootpath']
cherrypy.tree.mount(
httphandler, scriptname,
Expand Down
2 changes: 1 addition & 1 deletion cherrymusicserver/httphandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def trans(self, newformat, *path, **params):
path = os.path.sep.join(path)
if sys.version_info < (3, 0): # workaround for #327 (cherrypy issue)
path = path.decode('utf-8') # make it work with non-ascii
else:
elif cherry.needs_serve_file_utf8_fix:
path = codecs.decode(codecs.encode(path, 'latin1'), 'utf-8')
fullpath = os.path.join(cherry.config['media.basedir'], path)

Expand Down
40 changes: 28 additions & 12 deletions cherrymusicserver/test/test_httphandler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# CherryMusic - a standalone music server
# Copyright (c) 2012 - 2014 Tom Wallroth & Tilman Boerner
Expand Down Expand Up @@ -44,7 +45,6 @@
from cherrymusicserver import service
from cherrymusicserver.cherrymodel import CherryModel, MusicEntry

from cherrymusicserver import log

class MockAction(Exception):
pass
Expand Down Expand Up @@ -321,19 +321,35 @@ def test_api_userchangepassword(self):
session is used to authenticate the http request."""
self.assertRaises(AttributeError, self.http.api, 'userchangepassword')

def test_trans(self):
import os
config = {'media.basedir': 'BASEDIR', 'media.transcode': True}
with mock_auth():
with patch('cherrymusicserver.httphandler.cherry.config', config):
with patch('cherrymusicserver.httphandler.cherrypy'):
with patch('cherrymusicserver.httphandler.audiotranscode.AudioTranscode') as transcoder:
transcoder.return_value = transcoder
expectPath = os.path.join(config['media.basedir'], 'path')

httphandler.HTTPHandler(config).trans('newformat', 'path', bitrate=111)
def test_trans():
import sys
if sys.version_info < (3, 0):
test_paths = ['path', ('p\xc3\xb6th', u'pöth')]
else:
if cherry.needs_serve_file_utf8_fix:
test_paths = ['path',
('pöth'.encode('utf-8').decode('latin-1'), 'pöth')]
else:
test_paths = ['path', 'pöth']
for path in test_paths:
yield check_trans, path


def check_trans(path):
import os
path, expectPath = (path, path) if isinstance(path, str) else path
config = {'media.basedir': 'BASEDIR', 'media.transcode': True}
with mock_auth():
with patch('cherrymusicserver.httphandler.cherry.config', config):
with patch('cherrymusicserver.httphandler.cherrypy'):
with patch('cherrymusicserver.httphandler.audiotranscode.AudioTranscode') as transcoder:
transcoder.return_value = transcoder
expectPath = os.path.join(config['media.basedir'], expectPath)

httphandler.HTTPHandler(config).trans('newformat', path, bitrate=111)

transcoder.transcode_stream.assert_called_with(expectPath, 'newformat', bitrate=111, starttime=0)
transcoder.transcode_stream.assert_called_with(expectPath, 'newformat', bitrate=111, starttime=0)


if __name__ == "__main__":
Expand Down

0 comments on commit 7aaa4fd

Please sign in to comment.