from urllib import quote_plus, unquote_plus
from binascii import b2a_hex
+import operator
from twisted.python import log
from twisted.internet import defer
def __init__(self, path, manager, defaultType="text/plain", ignoredExts=(), processors=None, indexNames=None):
self.manager = manager
super(FileDownloader, self).__init__(path, defaultType, ignoredExts, processors, indexNames)
-
+
+ def locateChild(self, req, segments):
+ child, segments = super(FileDownloader, self).locateChild(req, segments)
+ # Make sure we always call renderHTTP()
+ if isinstance(child, FileDownloader):
+ return child, segments
+ else:
+ return self, server.StopTraversal
+
def renderHTTP(self, req):
log.msg('Got request for %s from %s' % (req.uri, req.remoteAddr))
+
+ # Make sure the file is in the DB and unchanged
+ if self.manager and not self.manager.db.isUnchanged(self.fp):
+ if self.fp.exists() and self.fp.isfile():
+ self.fp.remove()
+ return self._renderHTTP_done(http.Response(404,
+ {'content-type': http_headers.MimeType('text', 'html')},
+ '<html><body><p>File found but it has changed.</body></html>'),
+ req)
+
resp = super(FileDownloader, self).renderHTTP(req)
if isinstance(resp, defer.Deferred):
- resp.addCallback(self._renderHTTP_done, req)
+ resp.addCallbacks(self._renderHTTP_done, self._renderHTTP_error,
+ callbackArgs = (req, ), errbackArgs = (req, ))
else:
resp = self._renderHTTP_done(resp, req)
return resp
if self.manager:
path = 'http:/' + req.uri
if resp.code >= 200 and resp.code < 400:
- return self.manager.check_freshness(req, path, resp.headers.getHeader('Last-Modified'), resp)
+ return self.manager.get_resp(req, path, resp)
log.msg('Not found, trying other methods for %s' % req.uri)
return self.manager.get_resp(req, path)
return resp
+ def _renderHTTP_error(self, err, req):
+ log.msg('Failed to render %s: %r' % (req.uri, err))
+ log.err(err)
+
+ if self.manager:
+ path = 'http:/' + req.uri
+ return self.manager.get_resp(req, path)
+
+ return err
+
def createSimilarFile(self, path):
return self.__class__(path, self.manager, self.defaultType, self.ignoredExts,
self.processors, self.indexNames[:])
-class FileUploaderStream(stream.FileStream):
+class UploadStream:
+ """Identifier for streams that are uploaded to peers."""
+
+class FileUploaderStream(stream.FileStream, UploadStream):
"""Modified to make it suitable for streaming to peers.
Streams the file in small chunks to make it easier to throttle the
self.start += bytesRead
return b
+class PiecesUploaderStream(stream.MemoryStream, UploadStream):
+ """Modified to identify it for streaming to peers."""
+
+class PiecesUploader(static.Data):
+ """Modified to identify it for peer requests.
+
+ Uses the modified L{PieceUploaderStream} to stream the pieces for throttling.
+ """
+ def render(self, req):
+ return http.Response(responsecode.OK,
+ http_headers.Headers({'content-type': self.contentType()}),
+ stream=PiecesUploaderStream(self.data))
+
class FileUploader(static.File):
"""Modified to make it suitable for peer requests.
Uploads use L{FileUploaderStream} or L{twisted.web2.stream.MemorySTream},
apt uses L{CacheManager.ProxyFileStream} or L{twisted.web.stream.FileStream}.
"""
+
+ stats = None
def __init__(self, factory, wrappedProtocol):
ThrottlingProtocol.__init__(self, factory, wrappedProtocol)
def write(self, data):
if self.throttle:
ThrottlingProtocol.write(self, data)
+ if self.stats:
+ self.stats.sentBytes(len(data))
else:
ProtocolWrapper.write(self, data)
+ def writeSequence(self, seq):
+ if self.throttle:
+ ThrottlingProtocol.writeSequence(self, seq)
+ if self.stats:
+ self.stats.sentBytes(reduce(operator.add, map(len, seq)))
+ else:
+ ProtocolWrapper.writeSequence(self, seq)
+
def registerProducer(self, producer, streaming):
ThrottlingProtocol.registerProducer(self, producer, streaming)
streamType = getattr(producer, 'stream', None)
- if isinstance(streamType, FileUploaderStream) or isinstance(streamType, stream.MemoryStream):
+ if isinstance(streamType, UploadStream):
self.throttle = True
'betweenRequestsTimeOut': 60})
self.factory = ThrottlingFactory(self.factory, writeLimit = self.uploadLimit)
self.factory.protocol = UploadThrottlingProtocol
+ if self.manager:
+ self.factory.protocol.stats = self.manager.stats
return self.factory
def render(self, ctx):
else:
# It's not for a file, but for a piece string, so return that
log.msg('Sending torrent string %s to %s' % (b2a_hex(hash), request.remoteAddr))
- return static.Data(bencode({'t': files[0]['pieces']}), 'application/x-bencoded'), ()
+ return PiecesUploader(bencode({'t': files[0]['pieces']}), 'application/x-bencoded'), ()
else:
log.msg('Hash could not be found in database: %r' % hash)
+ return None, ()
- # Only local requests (apt) get past this point
- if request.remoteAddr.host != "127.0.0.1":
- log.msg('Blocked illegal access to %s from %s' % (request.uri, request.remoteAddr))
- return None, ()
-
- # Block access to index .diff files (for now)
- if 'Packages.diff' in segments or 'Sources.diff' in segments:
- return None, ()
-
if len(name) > 1:
# It's a request from apt
+
+ # Only local requests (apt) get past this point
+ if request.remoteAddr.host != "127.0.0.1":
+ log.msg('Blocked illegal access to %s from %s' % (request.uri, request.remoteAddr))
+ return None, ()
+
+ # Block access to index .diff files (for now)
+ if 'Packages.diff' in segments or 'Sources.diff' in segments or name == 'favicon.ico':
+ return None, ()
+
return FileDownloader(self.directory.path, self.manager), segments[0:]
else:
# Will render the statistics page
+
+ # Only local requests for stats are allowed
+ if not config.getboolean('DEFAULT', 'REMOTE_STATS') and request.remoteAddr.host != "127.0.0.1":
+ log.msg('Blocked illegal access to %s from %s' % (request.uri, request.remoteAddr))
+ return None, ()
+
return self, ()
log.msg('Got a malformed request for "%s" from %s' % (request.uri, request.remoteAddr))
return [{'pieces': 'abcdefghij0123456789\xca\xec\xb8\x0c\x00\xe7\x07\xf8~])\x8f\x9d\xe5_B\xff\x1a\xc4!'}]
return [{'path': FilePath(os.path.expanduser('~/school/optout'))}]
- t = TopLevel(FilePath(os.path.expanduser('~')), DB(), None, 0)
+ t = TopLevel(FilePath(os.path.expanduser('~')), DB(), None)
factory = t.getHTTPFactory()
# Standard twisted application Boilerplate