From d592c0370c5eda49913ab0190b981082424175ee Mon Sep 17 00:00:00 2001 From: John Garland Date: Wed, 1 Jul 2009 08:45:09 +0000 Subject: [PATCH] HTTPDownloader now "raises" a PageRedirect error when it encounters a temporary redirect or permanent move. This allows the callee to download the file from the new location. --- deluge/httpdownloader.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/deluge/httpdownloader.py b/deluge/httpdownloader.py index 064c52b1c..1e0f40565 100644 --- a/deluge/httpdownloader.py +++ b/deluge/httpdownloader.py @@ -32,7 +32,9 @@ # statement from all source files in the program, then also delete it here. # -from twisted.web import client +from twisted.web import client, http +from twisted.web.error import PageRedirect +from twisted.python.failure import Failure from twisted.internet import reactor class HTTPDownloader(client.HTTPDownloader): @@ -50,17 +52,25 @@ class HTTPDownloader(client.HTTPDownloader): self.current_length = 0 client.HTTPDownloader.__init__(self, url, filename) + def gotStatus(self, version, status, message): + self.code = int(status) + client.HTTPDownloader.gotStatus(self, version, status, message) + def gotHeaders(self, headers): - if self.status == "200": + if self.code == http.OK: if "content-length" in headers: self.total_length = int(headers["content-length"][0]) else: self.total_length = 0 + elif self.code in (http.TEMPORARY_REDIRECT, http.MOVED_PERMANENTLY): + location = headers["location"][0] + error = PageRedirect(self.code, location=location) + self.noPage(Failure(error)) return client.HTTPDownloader.gotHeaders(self, headers) def pagePart(self, data): - if self.status == "200": + if self.code == http.OK: self.current_length += len(data) if self.__part_callback: self.__part_callback(data, self.current_length, self.total_length)