uninvent a square wheel

This commit is contained in:
ed 2022-11-22 19:12:41 +00:00
parent d6e09c3880
commit 5a3e504ec4
2 changed files with 9 additions and 19 deletions

View file

@ -17,6 +17,7 @@ import threading # typechk
import time import time
import uuid import uuid
from datetime import datetime from datetime import datetime
from email.utils import formatdate, parsedate
from operator import itemgetter from operator import itemgetter
import jinja2 # typechk import jinja2 # typechk
@ -33,7 +34,6 @@ from .star import StreamTar
from .sutil import StreamArc # typechk from .sutil import StreamArc # typechk
from .szip import StreamZip from .szip import StreamZip
from .util import ( from .util import (
HTTP_TS_FMT,
HTTPCODE, HTTPCODE,
META_NOBOTS, META_NOBOTS,
MultipartParser, MultipartParser,
@ -54,7 +54,6 @@ from .util import (
hashcopy, hashcopy,
html_bescape, html_bescape,
html_escape, html_escape,
http_ts,
humansize, humansize,
ipnorm, ipnorm,
min_ex, min_ex,
@ -515,7 +514,7 @@ class HttpCli(object):
# close if unknown length, otherwise take client's preference # close if unknown length, otherwise take client's preference
response.append("Connection: " + ("Keep-Alive" if self.keepalive else "Close")) response.append("Connection: " + ("Keep-Alive" if self.keepalive else "Close"))
response.append("Date: " + datetime.utcnow().strftime(HTTP_TS_FMT)) response.append("Date: " + formatdate(usegmt=True))
# headers{} overrides anything set previously # headers{} overrides anything set previously
if headers: if headers:
@ -831,9 +830,7 @@ class HttpCli(object):
pvs: dict[str, str] = { pvs: dict[str, str] = {
"displayname": html_escape(vp.split("/")[-1]), "displayname": html_escape(vp.split("/")[-1]),
"getlastmodified": datetime.utcfromtimestamp(st.st_mtime).strftime( "getlastmodified": formatdate(st.st_mtime, usegmt=True),
HTTP_TS_FMT
),
"resourcetype": '<D:collection xmlns:D="DAV:"/>' if isdir else "", "resourcetype": '<D:collection xmlns:D="DAV:"/>' if isdir else "",
"supportedlock": '<D:lockentry xmlns:D="DAV:"><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry>', "supportedlock": '<D:lockentry xmlns:D="DAV:"><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry>',
} }
@ -2195,13 +2192,14 @@ class HttpCli(object):
return True return True
def _chk_lastmod(self, file_ts: int) -> tuple[str, bool]: def _chk_lastmod(self, file_ts: int) -> tuple[str, bool]:
file_lastmod = http_ts(file_ts) file_lastmod = formatdate(file_ts, usegmt=True)
cli_lastmod = self.headers.get("if-modified-since") cli_lastmod = self.headers.get("if-modified-since")
if cli_lastmod: if cli_lastmod:
try: try:
# some browser append "; length=573" # some browser append "; length=573"
cli_lastmod = cli_lastmod.split(";")[0].strip() cli_lastmod = cli_lastmod.split(";")[0].strip()
cli_dt = time.strptime(cli_lastmod, HTTP_TS_FMT) cli_dt = parsedate(cli_lastmod)
assert cli_dt
cli_ts = calendar.timegm(cli_dt) cli_ts = calendar.timegm(cli_dt)
return file_lastmod, int(file_ts) > int(cli_ts) return file_lastmod, int(file_ts) > int(cli_ts)
except Exception as ex: except Exception as ex:
@ -2479,8 +2477,7 @@ class HttpCli(object):
chrome = " rv:" not in self.ua chrome = " rv:" not in self.ua
mime, ico = self.ico.get(ext, not exact, chrome) mime, ico = self.ico.get(ext, not exact, chrome)
dt = datetime.utcfromtimestamp(self.E.t0) lm = formatdate(self.E.t0, usegmt=True)
lm = dt.strftime(HTTP_TS_FMT)
self.reply(ico, mime=mime, headers={"Last-Modified": lm}) self.reply(ico, mime=mime, headers={"Last-Modified": lm})
return True return True

View file

@ -24,6 +24,7 @@ import time
import traceback import traceback
from collections import Counter from collections import Counter
from datetime import datetime from datetime import datetime
from email.utils import formatdate
from ipaddress import IPv6Address from ipaddress import IPv6Address
from queue import Queue from queue import Queue
@ -137,8 +138,6 @@ else:
SYMTIME = sys.version_info > (3, 6) and os.utime in os.supports_follow_symlinks SYMTIME = sys.version_info > (3, 6) and os.utime in os.supports_follow_symlinks
HTTP_TS_FMT = "%a, %d %b %Y %H:%M:%S GMT"
META_NOBOTS = '<meta name="robots" content="noindex, nofollow">' META_NOBOTS = '<meta name="robots" content="noindex, nofollow">'
HTTPCODE = { HTTPCODE = {
@ -1476,8 +1475,7 @@ def gen_filekey_dbg(
def gencookie(k: str, v: str, dur: Optional[int]) -> str: def gencookie(k: str, v: str, dur: Optional[int]) -> str:
v = v.replace(";", "") v = v.replace(";", "")
if dur: if dur:
dt = datetime.utcfromtimestamp(time.time() + dur) exp = formatdate(time.time() + dur, usegmt=True)
exp = dt.strftime(HTTP_TS_FMT)
else: else:
exp = "Fri, 15 Aug 1997 01:00:00 GMT" exp = "Fri, 15 Aug 1997 01:00:00 GMT"
@ -1637,11 +1635,6 @@ def ipnorm(ip: str) -> str:
return ip return ip
def http_ts(ts: int) -> str:
file_dt = datetime.utcfromtimestamp(ts)
return file_dt.strftime(HTTP_TS_FMT)
def html_escape(s: str, quot: bool = False, crlf: bool = False) -> str: def html_escape(s: str, quot: bool = False, crlf: bool = False) -> str:
"""html.escape but also newlines""" """html.escape but also newlines"""
s = s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;") s = s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")