Ticket #3903 (closed defect: invalid )

Opened 7 months ago

Last modified 7 months ago

Resource routing can't handle empty paths

Reported by: jpsimons Assigned to: jknight
Type: defect 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().

Attachments

Change History

  2009-07-05 22:01:15+00:00 changed 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.

  2009-07-26 23:09:04+00:00 changed 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)

  2009-07-26 23:09:58+00:00 changed 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)

  2009-07-26 23:15:32+00:00 changed 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.

  2009-07-27 03:20:07+00:00 changed 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.