Deal gracefully with invalid Accept-Languages header

At the moment, calibre-web will raise an internal 500 error if an
invalid locale identifier is passed in through the `Accept-Language`
HTTP header (for a non-logged-in user).

This breaks some crappily-built OPDS feed readers, and there's no reason
to error out so severely. This commit just gracefully falls back to `en`
if it can't parse the locale identifier.

Fixes #743 among others.
pull/744/head
Adrian Petrescu 6 years ago
parent 9cfe1dce20
commit 5341910ea5
No known key found for this signature in database
GPG Key ID: 0F724D1D5E882972

@ -194,7 +194,11 @@ def get_locale():
if user.nickname != 'Guest': # if the account is the guest account bypass the config lang settings if user.nickname != 'Guest': # if the account is the guest account bypass the config lang settings
return user.locale return user.locale
translations = [str(item) for item in babel.list_translations()] + ['en'] translations = [str(item) for item in babel.list_translations()] + ['en']
preferred = [str(LC.parse(x.replace('-','_').replace('*','en'))) for x in request.accept_languages.values()] try:
preferred = [str(LC.parse(x.replace('-','_').replace('*','en'))) for x in request.accept_languages.values()]
except (UnknownLocaleError, ValueError) as e:
app.logger.debug("Could not parse locale:", e)
preferred = ['en']
return negotiate_locale(preferred, translations) return negotiate_locale(preferred, translations)

Loading…
Cancel
Save