Skip to content

Commit

Permalink
Merge pull request certbot#2453 from letsencrypt/keinwerkzeug
Browse files Browse the repository at this point in the history
Remove werkzeug by parsing Retry-After ourselves
  • Loading branch information
bmw committed Feb 26, 2016
2 parents 452ee2a + b316649 commit e2f07e1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
23 changes: 15 additions & 8 deletions acme/acme/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""ACME client API."""
import collections
import datetime
from email.utils import parsedate_tz
import heapq
import logging
import time
Expand All @@ -11,7 +12,6 @@
import OpenSSL
import requests
import sys
import werkzeug

from acme import errors
from acme import jose
Expand Down Expand Up @@ -248,6 +248,9 @@ def answer_challenge(self, challb, response):
def retry_after(cls, response, default):
"""Compute next `poll` time based on response ``Retry-After`` header.
Handles integers and various datestring formats per
https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.37
:param requests.Response response: Response from `poll`.
:param int default: Default value (in seconds), used when
``Retry-After`` header is not present or invalid.
Expand All @@ -260,12 +263,16 @@ def retry_after(cls, response, default):
try:
seconds = int(retry_after)
except ValueError:
# pylint: disable=no-member
decoded = werkzeug.parse_date(retry_after) # RFC1123
if decoded is None:
seconds = default
else:
return decoded
# The RFC 2822 parser handles all of RFC 2616's cases in modern
# environments (primarily HTTP 1.1+ but also py27+)
when = parsedate_tz(retry_after)
if when is not None:
try:
tz_secs = datetime.timedelta(when[-1] if when[-1] else 0)
return datetime.datetime(*when[:7]) - tz_secs
except (ValueError, OverflowError):
pass
seconds = default

return datetime.datetime.now() + datetime.timedelta(seconds=seconds)

Expand Down Expand Up @@ -357,7 +364,7 @@ def poll_and_request_issuance(
attempts = collections.defaultdict(int)
exhausted = set()

# priority queue with datetime (based on Retry-After) as key,
# priority queue with datetime.datetime (based on Retry-After) as key,
# and original Authorization Resource as value
waiting = [(datetime.datetime.now(), authzr) for authzr in authzrs]
# mapping between original Authorization Resource and the most
Expand Down
11 changes: 11 additions & 0 deletions acme/acme/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,17 @@ def test_retry_after_invalid(self, dt_mock):
datetime.datetime(2015, 3, 27, 0, 0, 10),
self.client.retry_after(response=self.response, default=10))

@mock.patch('acme.client.datetime')
def test_retry_after_overflow(self, dt_mock):
dt_mock.datetime.now.return_value = datetime.datetime(2015, 3, 27)
dt_mock.timedelta = datetime.timedelta
dt_mock.datetime.side_effect = datetime.datetime

self.response.headers['Retry-After'] = "Tue, 116 Feb 2016 11:50:00 MST"
self.assertEqual(
datetime.datetime(2015, 3, 27, 0, 0, 10),
self.client.retry_after(response=self.response, default=10))

@mock.patch('acme.client.datetime')
def test_retry_after_seconds(self, dt_mock):
dt_mock.datetime.now.return_value = datetime.datetime(2015, 3, 27)
Expand Down
1 change: 0 additions & 1 deletion acme/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
'requests',
'setuptools', # pkg_resources
'six',
'werkzeug',
]

# env markers in extras_require cause problems with older pip: #517
Expand Down
4 changes: 0 additions & 4 deletions letsencrypt-auto-source/letsencrypt-auto
Original file line number Diff line number Diff line change
Expand Up @@ -620,10 +620,6 @@ traceback2==1.4.0
# sha256: IogqDkGMKE4fcYqCKzsCKUTVPS2QjhaQsxmp0-ssBXk
unittest2==1.1.0
# sha256: aUkbUwUVfDxuDwSnAZhNaud_1yn8HJrNJQd_HfOFMms
# sha256: 619wCpv8lkILBVY1r5AC02YuQ9gMP_0x8iTCW8DV9GI
Werkzeug==0.11.3
# sha256: KCwRK1XdjjyGmjVx-GdnwVCrEoSprOK97CJsWSrK-Bo
zope.component==4.2.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,6 @@ traceback2==1.4.0
# sha256: IogqDkGMKE4fcYqCKzsCKUTVPS2QjhaQsxmp0-ssBXk
unittest2==1.1.0

# sha256: aUkbUwUVfDxuDwSnAZhNaud_1yn8HJrNJQd_HfOFMms
# sha256: 619wCpv8lkILBVY1r5AC02YuQ9gMP_0x8iTCW8DV9GI
Werkzeug==0.11.3

# sha256: KCwRK1XdjjyGmjVx-GdnwVCrEoSprOK97CJsWSrK-Bo
zope.component==4.2.2

Expand Down

0 comments on commit e2f07e1

Please sign in to comment.