Skip to content

Commit

Permalink
refactor(misc): clean up post-falconry#2246
Browse files Browse the repository at this point in the history
  • Loading branch information
vytas7 committed Aug 27, 2024
1 parent 4f8d639 commit 2813837
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
4 changes: 4 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ listed below by date of first contribution:
* Derk Weijers (derkweijers)
* bssyousefi
* Pavel 宝尔米 (e-io)
* wendy5667
* Dave Tapley (davetapley)
* Agustin Arce (aarcex3)
* Christian Grossmüller (chgad)

(et al.)

Expand Down
6 changes: 5 additions & 1 deletion docs/changes/4.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ now typed, further type annotations may be added throughout the 4.x release cycl
To improve them, we may introduce changes to the typing that do not affect
runtime behavior, but may surface new or different errors with type checkers.

.. note::
.. note::

All type aliases in falcon are considered private, and if used should be
imported inside ``if TYPE_CHECKING:`` blocks to avoid possible import errors
Expand All @@ -45,11 +45,14 @@ Contributors to this Release

Many thanks to all of our talented and stylish contributors for this release!

- `aarcex3 <https://github.com/aarcex3>`__
- `aryaniyaps <https://github.com/aryaniyaps>`__
- `bssyousefi <https://github.com/bssyousefi>`__
- `CaselIT <https://github.com/CaselIT>`__
- `cclauss <https://github.com/cclauss>`__
- `chgad <https://github.com/chgad>`__
- `copalco <https://github.com/copalco>`__
- `davetapley <https://github.com/davetapley>`__
- `derkweijers <https://github.com/derkweijers>`__
- `e-io <https://github.com/e-io>`__
- `euj1n0ng <https://github.com/euj1n0ng>`__
Expand All @@ -69,3 +72,4 @@ Many thanks to all of our talented and stylish contributors for this release!
- `TigreModerata <https://github.com/TigreModerata>`__
- `vgerak <https://github.com/vgerak>`__
- `vytas7 <https://github.com/vytas7>`__
- `wendy5667 <https://github.com/wendy5667>`__
22 changes: 12 additions & 10 deletions falcon/util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@

_UNSAFE_CHARS = re.compile(r'[^a-zA-Z0-9.-]')

_ALLOWED_HTTP_TIMEZONES: Tuple[str, ...] = ('GMT', 'UTC')

_UTC_TIMEZONE = datetime.timezone.utc

# PERF(kgriffs): Avoid superfluous namespace lookups
Expand Down Expand Up @@ -176,20 +174,16 @@ def http_date_to_dt(http_date: str, obs_date: bool = False) -> datetime.datetime
ValueError: http_date doesn't match any of the available time formats
ValueError: http_date doesn't match allowed timezones
"""
date_timezone_str = http_date[-3:]
has_tz_identifier = not date_timezone_str.isdigit()
if date_timezone_str not in _ALLOWED_HTTP_TIMEZONES and has_tz_identifier:
raise ValueError(
'timezone information of time data %r is not allowed' % http_date
)

if not obs_date:
# PERF(kgriffs): This violates DRY, but we do it anyway
# to avoid the overhead of setting up a tuple, looping
# over it, and setting up exception handling blocks each
# time around the loop, in the case that we don't actually
# need to check for multiple formats.
return _strptime(http_date, '%a, %d %b %Y %H:%M:%S %Z').replace(
# NOTE(vytas): According to RFC 9110, Section 5.6.7, the only allowed
# value for the TIMEZONE field [of IMF-fixdate] is %s"GMT", so we
# simply hardcode GMT in the strptime expression.
return _strptime(http_date, '%a, %d %b %Y %H:%M:%S GMT').replace(
tzinfo=_UTC_TIMEZONE
)

Expand All @@ -203,6 +197,14 @@ def http_date_to_dt(http_date: str, obs_date: bool = False) -> datetime.datetime
# Loop through the formats and return the first that matches
for time_format in time_formats:
try:
# NOTE(chgad,vytas): As per now-obsolete RFC 850, Section 2.1.4
# (and later references in newer RFCs) the TIMEZONE field may be
# be one of many abbreviations such as EST, MDT, etc; which are
# not equivalent to UTC.
# However, Python seems unable to parse any such abbreviations
# except GMT and UTC due to a bug/lacking implementation
# (see https://github.com/python/cpython/issues/66571); so we can
# indiscriminately assume UTC after all.
return _strptime(http_date, time_format).replace(tzinfo=_UTC_TIMEZONE)
except ValueError:
continue
Expand Down

0 comments on commit 2813837

Please sign in to comment.