Ticket #3903 (closed defect: invalid)

Opened 14 months ago

Last modified 13 months ago

Resource routing can't handle empty paths

Reported by: jpsimons Owned by: jknight
Priority: normal Milestone:
Component: web Keywords:
Cc: Branch:
Author: Launchpad Bug:

Description

class HelloWorld(resource.Resource):
    def render_GET(self, request):
        return "<html>Hello World</html>"
site = server.Site(HelloWorld())

This simple example just doesn't work, it returns "Resource not found" if you go to localhost:8080. You can work around it by setting isLeaf = True, but then no subdirectories show up. The fix is this, in twisted/web/server.py, on line 147:

-        self.postpath = map(unquote, string.split(self.path[1:], '/'))

+        self.postpath = []
+        if len(self.path[1:]):
+            self.postpath = map(unquote, string.split(self.path[1:], '/'))

Because in Python, "".split("/") returns [""] instead of [] like one might expect. You'd want the empty array in this case so web.resource.getChildForRequest() will do the right thing and just return the original resource instead of falling back to getChild().

Change History

Changed 14 months ago by radix

  • status changed from new to closed
  • resolution set to invalid

This is expected behavior. If you want a resource to handle rendering a request for "/", then respond to rootResource.getChild("", request) with the resource to be rendered.

Changed 13 months ago by jpsimons

Nice, that's better than the workaround I was using:

class RootResource(resource.Resource):

def getChildWithDefault(self, path, request):

if not path: return self return resource.Resource.getChildWithDefault(self, path, request)

Changed 13 months ago by jpsimons

That is to say,

class RootResource(resource.Resource):
    def getChildWithDefault(self, path, request):
        if not path: return self
        return resource.Resource.getChildWithDefault(self, path, request)

Changed 13 months ago by jpsimons

Hey just out of curiosity, who maintains Twisted? Does it have corporate backing? I've started using it for all my latest projects, (even writing a cool throttling proxy server with it), and I think it's RAD.

Changed 13 months ago by exarkun

TwistedMatrixLaboratories sort of answers that question. TwistedSoftwareFoundation and TSF/FoundingSponsors give another (more recent) perspective.

Note: See TracTickets for help on using tickets.