MCPcopy Create free account
hub / github.com/psf/cachecontrol / update_headers

Method update_headers

cachecontrol/heuristics.py:124–154  ·  view source on GitHub ↗
(self, resp: HTTPResponse)

Source from the content-addressed store, hash-verified

122 }
123
124 def update_headers(self, resp: HTTPResponse) -> dict[str, str]:
125 headers: Mapping[str, str] = resp.headers
126
127 if "expires" in headers:
128 return {}
129
130 if "cache-control" in headers and headers["cache-control"] != "public":
131 return {}
132
133 if resp.status not in self.cacheable_by_default_statuses:
134 return {}
135
136 if "date" not in headers or "last-modified" not in headers:
137 return {}
138
139 time_tuple = parsedate_tz(headers["date"])
140 assert time_tuple is not None
141 date = calendar.timegm(time_tuple[:6])
142 last_modified = parsedate(headers["last-modified"])
143 if last_modified is None:
144 return {}
145
146 now = time.time()
147 current_age = max(0, now - date)
148 delta = date - calendar.timegm(last_modified)
149 freshness_lifetime = max(0, min(delta / 10, 24 * 3600))
150 if freshness_lifetime <= current_age:
151 return {}
152
153 expires = date + freshness_lifetime
154 return {"expires": time.strftime(TIME_FMT, time.gmtime(expires))}
155
156 def warning(self, resp: HTTPResponse) -> str | None:
157 return None

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected