Added 2 (commented) failed attempts to throttle the web server.
[quix0rs-apt-p2p.git] / apt_dht / HTTPServer.py
index 266078ec718da4d33ab029015d1ecd9be40478f6..20f94cdfa5e3088193591455b269a1fbd6963b15 100644 (file)
@@ -1,6 +1,10 @@
 
+from urllib import unquote_plus
+
 from twisted.python import log
 from twisted.internet import defer
+#from twisted.protocols import htb
+#from twisted.protocols.policies import ThrottlingFactory
 from twisted.web2 import server, http, resource, channel
 from twisted.web2 import static, http_headers, responsecode
 
@@ -51,6 +55,17 @@ class TopLevel(resource.Resource):
             self.factory = channel.HTTPFactory(server.Site(self),
                                                **{'maxPipeline': 10, 
                                                   'betweenRequestsTimeOut': 60})
+#            serverFilter = htb.HierarchicalBucketFilter()
+#            serverBucket = htb.Bucket()
+#
+#            # Cap total server traffic at 20 kB/s
+#            serverBucket.maxburst = 20000
+#            serverBucket.rate = 20000
+#
+#            serverFilter.buckets[None] = serverBucket
+#
+#            self.factory.protocol = htb.ShapedProtocolFactory(self.factory.protocol, serverFilter)
+#            self.factory = ThrottlingFactory(self.factory, writeLimit = 300*1024)
         return self.factory
 
     def render(self, ctx):
@@ -62,12 +77,13 @@ class TopLevel(resource.Resource):
             <p>TODO: eventually some stats will be shown here.</body></html>""")
 
     def locateChild(self, request, segments):
+        log.msg('Got HTTP request for %s from %s' % (request.uri, request.remoteAddr))
         name = segments[0]
         if name == '~':
             if len(segments) != 2:
                 log.msg('Got a malformed request from %s' % request.remoteAddr)
                 return None, ()
-            hash = segments[1]
+            hash = unquote_plus(segments[1])
             files = self.db.lookupHash(hash)
             if files:
                 log.msg('Sharing %s with %s' % (files[0]['path'].path, request.remoteAddr))