Skip to content

Commit

Permalink
Use email.utils.format_datetime instead of undocumented `wsgiref.ha…
Browse files Browse the repository at this point in the history
…ndlers.format_date_time` (#148)

* Use documented API `email.utils.format_datetime`

instead of undocumented `wsgiref.handlers.format_date_time`
  • Loading branch information
GalaxySnail committed Aug 24, 2022
1 parent 98e3587 commit be52390
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions examples/trio-server.py
Expand Up @@ -76,8 +76,9 @@
# TCP_CORK and suchlike.

import json
import datetime
import email.utils
from itertools import count
from wsgiref.handlers import format_date_time

import trio

Expand All @@ -86,6 +87,22 @@
MAX_RECV = 2**16
TIMEOUT = 10


# We are using email.utils.format_datetime to generate the Date header.
# It may sound weird, but it actually follows the RFC.
# Please see: https://stackoverflow.com/a/59416334/14723771
#
# See also:
# [1] https://www.rfc-editor.org/rfc/rfc9110#section-5.6.7
# [2] https://www.rfc-editor.org/rfc/rfc7231#section-7.1.1.1
# [3] https://www.rfc-editor.org/rfc/rfc5322#section-3.3
def format_date_time(dt=None):
"""Generate a RFC 7231 / RFC 9110 IMF-fixdate string"""
if dt is None:
dt = datetime.datetime.now(datetime.timezone.utc)
return email.utils.format_datetime(dt, usegmt=True)


################################################################
# I/O adapter: h11 <-> trio
################################################################
Expand Down Expand Up @@ -177,7 +194,7 @@ def basic_headers(self):
# HTTP requires these headers in all responses (client would do
# something different here)
return [
("Date", format_date_time(None).encode("ascii")),
("Date", format_date_time().encode("ascii")),
("Server", self.ident),
]

Expand Down

0 comments on commit be52390

Please sign in to comment.