mirror of
https://github.com/9001/copyparty.git
synced 2025-08-17 09:02:15 -06:00
http 304: strip Content-Length and Content-Type
these response headers are usually not included in 304 replies, and their presence are suspected to confuse some clients (#110) also strip `out_headerlist` (primarily cookie assignments)
This commit is contained in:
parent
159f51b12b
commit
912402364a
|
@ -125,6 +125,9 @@ _ = (argparse, threading)
|
||||||
|
|
||||||
NO_CACHE = {"Cache-Control": "no-cache"}
|
NO_CACHE = {"Cache-Control": "no-cache"}
|
||||||
|
|
||||||
|
H_CONN_KEEPALIVE = "Connection: Keep-Alive"
|
||||||
|
H_CONN_CLOSE = "Connection: Close"
|
||||||
|
|
||||||
LOGUES = [[0, ".prologue.html"], [1, ".epilogue.html"]]
|
LOGUES = [[0, ".prologue.html"], [1, ".epilogue.html"]]
|
||||||
|
|
||||||
READMES = [[0, ["preadme.md", "PREADME.md"]], [1, ["readme.md", "README.md"]]]
|
READMES = [[0, ["preadme.md", "PREADME.md"]], [1, ["readme.md", "README.md"]]]
|
||||||
|
@ -829,25 +832,28 @@ class HttpCli(object):
|
||||||
) -> None:
|
) -> None:
|
||||||
response = ["%s %s %s" % (self.http_ver, status, HTTPCODE[status])]
|
response = ["%s %s %s" % (self.http_ver, status, HTTPCODE[status])]
|
||||||
|
|
||||||
if length is not None:
|
|
||||||
response.append("Content-Length: " + unicode(length))
|
|
||||||
|
|
||||||
if status == 304 and self.k304():
|
|
||||||
self.keepalive = False
|
|
||||||
|
|
||||||
# close if unknown length, otherwise take client's preference
|
|
||||||
response.append("Connection: " + ("Keep-Alive" if self.keepalive else "Close"))
|
|
||||||
response.append("Date: " + formatdate())
|
|
||||||
|
|
||||||
# headers{} overrides anything set previously
|
# headers{} overrides anything set previously
|
||||||
if headers:
|
if headers:
|
||||||
self.out_headers.update(headers)
|
self.out_headers.update(headers)
|
||||||
|
|
||||||
# default to utf8 html if no content-type is set
|
if status == 304:
|
||||||
if not mime:
|
self.out_headers.pop("Content-Length", None)
|
||||||
mime = self.out_headers.get("Content-Type") or "text/html; charset=utf-8"
|
self.out_headers.pop("Content-Type", None)
|
||||||
|
self.out_headerlist.clear()
|
||||||
|
if self.k304():
|
||||||
|
self.keepalive = False
|
||||||
|
else:
|
||||||
|
if length is not None:
|
||||||
|
response.append("Content-Length: " + unicode(length))
|
||||||
|
|
||||||
self.out_headers["Content-Type"] = mime
|
if mime:
|
||||||
|
self.out_headers["Content-Type"] = mime
|
||||||
|
elif "Content-Type" not in self.out_headers:
|
||||||
|
self.out_headers["Content-Type"] = "text/html; charset=utf-8"
|
||||||
|
|
||||||
|
# close if unknown length, otherwise take client's preference
|
||||||
|
response.append(H_CONN_KEEPALIVE if self.keepalive else H_CONN_CLOSE)
|
||||||
|
response.append("Date: " + formatdate())
|
||||||
|
|
||||||
for k, zs in list(self.out_headers.items()) + self.out_headerlist:
|
for k, zs in list(self.out_headers.items()) + self.out_headerlist:
|
||||||
response.append("%s: %s" % (k, zs))
|
response.append("%s: %s" % (k, zs))
|
||||||
|
@ -953,13 +959,14 @@ class HttpCli(object):
|
||||||
|
|
||||||
lines = [
|
lines = [
|
||||||
"%s %s %s" % (self.http_ver or "HTTP/1.1", status, HTTPCODE[status]),
|
"%s %s %s" % (self.http_ver or "HTTP/1.1", status, HTTPCODE[status]),
|
||||||
"Connection: Close",
|
H_CONN_CLOSE,
|
||||||
]
|
]
|
||||||
|
|
||||||
if body:
|
if body:
|
||||||
lines.append("Content-Length: " + unicode(len(body)))
|
lines.append("Content-Length: " + unicode(len(body)))
|
||||||
|
|
||||||
self.s.sendall("\r\n".join(lines).encode("utf-8") + b"\r\n\r\n" + body)
|
lines.append("\r\n")
|
||||||
|
self.s.sendall("\r\n".join(lines).encode("utf-8") + body)
|
||||||
|
|
||||||
def urlq(self, add: dict[str, str], rm: list[str]) -> str:
|
def urlq(self, add: dict[str, str], rm: list[str]) -> str:
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in a new issue