X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=apt_p2p%2FHTTPServer.py;h=d6c12bf3ce402a949700b53887a4fd94c5a06d2a;hb=d63ad7d7b1c9e5567bd28450197ef810dc5c5475;hp=3a9a3a3177ced71374c3d4c4a51b0979d617bd0c;hpb=6d23f1559df1435172f1da25ae0f57ae11a24bde;p=quix0rs-apt-p2p.git diff --git a/apt_p2p/HTTPServer.py b/apt_p2p/HTTPServer.py index 3a9a3a3..d6c12bf 100644 --- a/apt_p2p/HTTPServer.py +++ b/apt_p2p/HTTPServer.py @@ -1,15 +1,18 @@ """Serve local requests from apt and remote requests from peers.""" -from urllib import unquote_plus +from urllib import quote_plus, unquote_plus from binascii import b2a_hex from twisted.python import log from twisted.internet import defer from twisted.web2 import server, http, resource, channel, stream from twisted.web2 import static, http_headers, responsecode +from twisted.trial import unittest +from twisted.python.filepath import FilePath from policies import ThrottlingFactory, ThrottlingProtocol, ProtocolWrapper +from apt_p2p_conf import config from apt_p2p_Khashmir.bencode import bencode class FileDownloader(static.File): @@ -56,7 +59,7 @@ class FileDownloader(static.File): class FileUploaderStream(stream.FileStream): """Modified to make it suitable for streaming to peers. - Streams the file is small chunks to make it easier to throttle the + Streams the file in small chunks to make it easier to throttle the streaming to peers. @ivar CHUNK_SIZE: the size of chunks of data to send at a time @@ -167,7 +170,7 @@ class TopLevel(resource.Resource): addSlash = True - def __init__(self, directory, db, manager, uploadLimit): + def __init__(self, directory, db, manager): """Initialize the instance. @type directory: L{twisted.python.filepath.FilePath} @@ -181,8 +184,8 @@ class TopLevel(resource.Resource): self.db = db self.manager = manager self.uploadLimit = None - if uploadLimit > 0: - self.uploadLimit = int(uploadLimit*1024) + if config.getint('DEFAULT', 'UPLOAD_LIMIT') > 0: + self.uploadLimit = int(config.getint('DEFAULT', 'UPLOAD_LIMIT')*1024) self.factory = None def getHTTPFactory(self): @@ -197,10 +200,16 @@ class TopLevel(resource.Resource): def render(self, ctx): """Render a web page with descriptive statistics.""" - return http.Response( - 200, - {'content-type': http_headers.MimeType('text', 'html')}, - self.manager.getStats()) + if self.manager: + return http.Response( + 200, + {'content-type': http_headers.MimeType('text', 'html')}, + self.manager.getStats()) + else: + return http.Response( + 200, + {'content-type': http_headers.MimeType('text', 'html')}, + '

Some Statistics') def locateChild(self, request, segments): """Process the incoming request.""" @@ -214,7 +223,8 @@ class TopLevel(resource.Resource): return None, () # Find the file in the database - hash = unquote_plus(segments[1]) + # Have to unquote_plus the uri, because the segments are unquoted by twisted + hash = unquote_plus(request.uri[3:]) files = self.db.lookupHash(hash) if files: # If it is a file, return it @@ -226,13 +236,17 @@ class TopLevel(resource.Resource): log.msg('Sending torrent string %s to %s' % (b2a_hex(hash), request.remoteAddr)) return static.Data(bencode({'t': files[0]['pieces']}), 'application/x-bencoded'), () else: - log.msg('Hash could not be found in database: %s' % hash) + log.msg('Hash could not be found in database: %r' % hash) # 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 return FileDownloader(self.directory.path, self.manager), segments[0:] @@ -243,6 +257,101 @@ class TopLevel(resource.Resource): log.msg('Got a malformed request for "%s" from %s' % (request.uri, request.remoteAddr)) return None, () +class TestTopLevel(unittest.TestCase): + """Unit tests for the HTTP Server.""" + + client = None + pending_calls = [] + torrent_hash = '\xca \xb8\x0c\x00\xe7\x07\xf8~])+\x9d\xe5_B\xff\x1a\xc4!' + torrent = 'abcdefghij0123456789\xca\xec\xb8\x0c\x00\xe7\x07\xf8~])\x8f\x9d\xe5_B\xff\x1a\xc4!' + file_hash = '\xf8~])+\x9d\xe5_B\xff\x1a\xc4!\xca \xb8\x0c\x00\xe7\x07' + + def setUp(self): + self.client = TopLevel(FilePath('/boot'), self, None) + + def lookupHash(self, hash): + if hash == self.torrent_hash: + return [{'pieces': self.torrent}] + elif hash == self.file_hash: + return [{'path': FilePath('/boot/grub/stage2')}] + else: + return [] + + def create_request(self, host, path): + req = server.Request(None, 'GET', path, (1,1), 0, http_headers.Headers()) + class addr: + host = '' + port = 0 + req.remoteAddr = addr() + req.remoteAddr.host = host + req.remoteAddr.port = 23456 + server.Request._parseURL(req) + return req + + def test_unauthorized(self): + req = self.create_request('128.0.0.1', '/foo/bar') + self.failUnlessRaises(http.HTTPError, req._getChild, None, self.client, req.postpath) + + def test_Packages_diff(self): + req = self.create_request('127.0.0.1', + '/ftp.us.debian.org/debian/dists/unstable/main/binary-i386/Packages.diff/Index') + self.failUnlessRaises(http.HTTPError, req._getChild, None, self.client, req.postpath) + + def test_Statistics(self): + req = self.create_request('127.0.0.1', '/') + res = req._getChild(None, self.client, req.postpath) + self.failIfEqual(res, None) + df = defer.maybeDeferred(res.renderHTTP, req) + df.addCallback(self.check_resp, 200) + return df + + def test_apt_download(self): + req = self.create_request('127.0.0.1', + '/ftp.us.debian.org/debian/dists/stable/Release') + res = req._getChild(None, self.client, req.postpath) + self.failIfEqual(res, None) + self.failUnless(isinstance(res, FileDownloader)) + df = defer.maybeDeferred(res.renderHTTP, req) + df.addCallback(self.check_resp, 404) + return df + + def test_torrent_upload(self): + req = self.create_request('123.45.67.89', + '/~/' + quote_plus(self.torrent_hash)) + res = req._getChild(None, self.client, req.postpath) + self.failIfEqual(res, None) + self.failUnless(isinstance(res, static.Data)) + df = defer.maybeDeferred(res.renderHTTP, req) + df.addCallback(self.check_resp, 200) + return df + + def test_file_upload(self): + req = self.create_request('123.45.67.89', + '/~/' + quote_plus(self.file_hash)) + res = req._getChild(None, self.client, req.postpath) + self.failIfEqual(res, None) + self.failUnless(isinstance(res, FileUploader)) + df = defer.maybeDeferred(res.renderHTTP, req) + df.addCallback(self.check_resp, 200) + return df + + def test_missing_hash(self): + req = self.create_request('123.45.67.89', + '/~/' + quote_plus('foobar')) + self.failUnlessRaises(http.HTTPError, req._getChild, None, self.client, req.postpath) + + def check_resp(self, resp, code): + self.failUnlessEqual(resp.code, code) + return resp + + def tearDown(self): + for p in self.pending_calls: + if p.active(): + p.cancel() + self.pending_calls = [] + if self.client: + self.client = None + if __name__ == '__builtin__': # Running from twistd -ny HTTPServer.py # Then test with: @@ -258,7 +367,7 @@ if __name__ == '__builtin__': 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) + t = TopLevel(FilePath(os.path.expanduser('~')), DB(), None, 0) factory = t.getHTTPFactory() # Standard twisted application Boilerplate