+ def check_freshness(self, req, url, orig_resp, d):
+ """Send a HEAD to the mirror to check if the response from the cache is still valid.
+
+ @type req: L{twisted.web2.http.Request}
+ @param req: the initial request sent to the HTTP server by apt
+ @param url: the URI of the actual mirror request
+ @type orig_resp: L{twisted.web2.http.Response}
+ @param orig_resp: the response from the cache to be sent to apt
+ @rtype: L{twisted.internet.defer.Deferred}
+ @return: a deferred that will be called back with the correct response
+ """
+ log.msg('Checking if %s is still fresh' % url)
+ modtime = orig_resp.headers.getHeader('Last-Modified')
+ headDefer = self.peers.get(HashObject(), url, method = "HEAD",
+ modtime = modtime)
+ headDefer.addCallbacks(self.check_freshness_done,
+ self.check_freshness_error,
+ callbackArgs = (req, url, orig_resp, d),
+ errbackArgs = (req, url, d))
+
+ def check_freshness_done(self, resp, req, url, orig_resp, d):
+ """Return the fresh response, if stale start to redownload.
+
+ @type resp: L{twisted.web2.http.Response}
+ @param resp: the response from the mirror to the HEAD request
+ @type req: L{twisted.web2.http.Request}
+ @param req: the initial request sent to the HTTP server by apt
+ @param url: the URI of the actual mirror request
+ @type orig_resp: L{twisted.web2.http.Response}
+ @param orig_resp: the response from the cache to be sent to apt
+ """
+ if resp.code == 304:
+ log.msg('Still fresh, returning: %s' % url)
+ d.callback(orig_resp)
+ else:
+ log.msg('Stale, need to redownload: %s' % url)
+ self.startDownload([], req, HashObject(), url, d)
+
+ def check_freshness_error(self, err, req, url, d):
+ """Mirror request failed, continue with download.
+
+ @param err: the response from the mirror to the HEAD request
+ @type req: L{twisted.web2.http.Request}
+ @param req: the initial request sent to the HTTP server by apt
+ @param url: the URI of the actual mirror request
+ """
+ log.err(err)
+ self.startDownload([], req, HashObject(), url, d)
+