File tree Expand file tree Collapse file tree 1 file changed +19
-2
lines changed Expand file tree Collapse file tree 1 file changed +19
-2
lines changed Original file line number Diff line number Diff line change 7676# TCP_CORK and suchlike.
7777
7878import json
79+ import datetime
80+ import email .utils
7981from itertools import count
80- from wsgiref .handlers import format_date_time
8182
8283import trio
8384
8687MAX_RECV = 2 ** 16
8788TIMEOUT = 10
8889
90+
91+ # We are using email.utils.format_datetime to generate the Date header.
92+ # It may sound weird, but it actually follows the RFC.
93+ # Please see: https://stackoverflow.com/a/59416334/14723771
94+ #
95+ # See also:
96+ # [1] https://www.rfc-editor.org/rfc/rfc9110#section-5.6.7
97+ # [2] https://www.rfc-editor.org/rfc/rfc7231#section-7.1.1.1
98+ # [3] https://www.rfc-editor.org/rfc/rfc5322#section-3.3
99+ def format_date_time (dt = None ):
100+ """Generate a RFC 7231 / RFC 9110 IMF-fixdate string"""
101+ if dt is None :
102+ dt = datetime .datetime .now (datetime .timezone .utc )
103+ return email .utils .format_datetime (dt , usegmt = True )
104+
105+
89106################################################################
90107# I/O adapter: h11 <-> trio
91108################################################################
@@ -177,7 +194,7 @@ def basic_headers(self):
177194 # HTTP requires these headers in all responses (client would do
178195 # something different here)
179196 return [
180- ("Date" , format_date_time (None ).encode ("ascii" )),
197+ ("Date" , format_date_time ().encode ("ascii" )),
181198 ("Server" , self .ident ),
182199 ]
183200
You can’t perform that action at this time.
0 commit comments