mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Issue #747320: Use email.utils.formatdate() to avoid code duplication
in BaseHTTPRequestHandler Initial patch by karlcow.
This commit is contained in:
parent
0647ef05eb
commit
04bc5b9e48
2 changed files with 16 additions and 6 deletions
|
@ -87,6 +87,7 @@ __all__ = [
|
||||||
"SimpleHTTPRequestHandler", "CGIHTTPRequestHandler",
|
"SimpleHTTPRequestHandler", "CGIHTTPRequestHandler",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
import email.utils
|
||||||
import html
|
import html
|
||||||
import http.client
|
import http.client
|
||||||
import io
|
import io
|
||||||
|
@ -566,12 +567,7 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
|
||||||
"""Return the current date and time formatted for a message header."""
|
"""Return the current date and time formatted for a message header."""
|
||||||
if timestamp is None:
|
if timestamp is None:
|
||||||
timestamp = time.time()
|
timestamp = time.time()
|
||||||
year, month, day, hh, mm, ss, wd, y, z = time.gmtime(timestamp)
|
return email.utils.formatdate(timestamp, usegmt=True)
|
||||||
s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
|
|
||||||
self.weekdayname[wd],
|
|
||||||
day, self.monthname[month], year,
|
|
||||||
hh, mm, ss)
|
|
||||||
return s
|
|
||||||
|
|
||||||
def log_date_time_string(self):
|
def log_date_time_string(self):
|
||||||
"""Return the current time formatted for logging."""
|
"""Return the current time formatted for logging."""
|
||||||
|
|
|
@ -17,6 +17,7 @@ import urllib.parse
|
||||||
import html
|
import html
|
||||||
import http.client
|
import http.client
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import time
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
|
@ -873,6 +874,19 @@ class BaseHTTPRequestHandlerTestCase(unittest.TestCase):
|
||||||
self.handler.handle()
|
self.handler.handle()
|
||||||
self.assertRaises(StopIteration, next, close_values)
|
self.assertRaises(StopIteration, next, close_values)
|
||||||
|
|
||||||
|
def test_date_time_string(self):
|
||||||
|
now = time.time()
|
||||||
|
# this is the old code that formats the timestamp
|
||||||
|
year, month, day, hh, mm, ss, wd, y, z = time.gmtime(now)
|
||||||
|
expected = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
|
||||||
|
self.handler.weekdayname[wd],
|
||||||
|
day,
|
||||||
|
self.handler.monthname[month],
|
||||||
|
year, hh, mm, ss
|
||||||
|
)
|
||||||
|
self.assertEqual(self.handler.date_time_string(timestamp=now), expected)
|
||||||
|
|
||||||
|
|
||||||
class SimpleHTTPRequestHandlerTestCase(unittest.TestCase):
|
class SimpleHTTPRequestHandlerTestCase(unittest.TestCase):
|
||||||
""" Test url parsing """
|
""" Test url parsing """
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue