Created
January 25, 2010 23:15
-
-
Save deldotdr/286376 to your computer and use it in GitHub Desktop.
Revisions
-
deldotdr revised this gist
Jan 26, 2010 . 2 changed files with 158 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,50 @@ """ run python web.rewrite.example.py and then open a browser to localhost:8080/data/foo/bar The two segments of the path below /data are transformed into query arguments using the rewrite function pathToArgs. The root resource is wrapped with the rewrite resource The second arg to RewriterResource is the rewrite function. The rewrite function can be thought of as a rule + an action: If the request satisfies the rule, the request is rewritten. This rewrite happens before the web framework passes the request object to any resource in root (including root itself). It is possible to give more than one rewrite function; they will all have a chance to rewrite the request object. If you look at pathToArgs, everything below the if statement is the rewrite action. Part of the action is self explanatory: - request.args is added (now the handling resources have access to request.args) - request.path is explicitly set to '/data' The intent of this rewrite implies /data is a leaf resource. And part of it is a little confusing - request.postpath -- this is not as straight forward. When the Request object is first created, the raw HTTP headers of the actual request from your browser are processed. Part of that involves splitting the PATH on '/' and storing that in request.postpath. Then, for every hop down a resource tree, the head element of postpath is popped and appended to prepath. request.path represents the full requested path. """ from twisted.web import resource from twisted.web import rewrite from twisted.web import server from twisted.internet import reactor class GenericResource(resource.Resource): def render(self, request): return "dummy resource page" class DataResource(resource.Resource): def render(self, request): @@ -27,15 +69,17 @@ def pathToArgs(request): -> localhost:8008/data?sta=109C&orid=39495 """ if len(request.postpath) == 3 and request.postpath[0] == 'data': sta = request.postpath[1] orid = request.postpath[2] request.args = {'sta':[sta], 'orid':[orid]} request.postpath = ['data'] request.path = '/data' root = GenericResource() root.putChild('', root) root.putChild('test', GenericResource()) root.putChild('data', DataResource()) rewrite_root = rewrite.RewriterResource(root, pathToArgs) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,112 @@ """ run python web.rewrite.example.py and then open a browser to localhost:8080/data/foo/bar The two segments of the path below /data are transformed into query arguments using the rewrite function pathToArgs. The root resource is wrapped with the rewrite resource The second arg to RewriterResource is the rewrite function. The rewrite function can be thought of as a rule + an action: If the request satisfies the rule, the request is rewritten. This rewrite happens before the web framework passes the request object to any resource in root (including root itself). It is possible to give more than one rewrite function; they will all have a chance to rewrite the request object. If you look at pathToArgs, everything below the if statement is the rewrite action. Part of the action is self explanatory: - request.args is added (now the handling resources have access to request.args) - request.path is explicitly set to '/data' The intent of this rewrite implies /data is a leaf resource. And part of it is a little confusing - request.postpath -- this is not as straight forward. When the Request object is first created, the raw HTTP headers of the actual request from your browser are processed. Part of that involves splitting the PATH on '/' and storing the resulting list in request.postpath. Then, for every step down a resource tree, the head element of postpath is popped and appended to prepath. request.path represents the full requested path. In this second example, there are two rewrite rules. test2Data looks for /test/data/variable When this rule matches, it rewrites the request to a form that matches the rule in pathToArgs: /data/variable/constant The RewriterResource wrapper has three arguments now: the resource tree to be wrapped two rewrite functions The order of the last two arguemnts matters. In this case, the test2Data rewrite only makes sense if it is applied before the pathToArgs rewrite. If you switch the order of those two arguments, it wont work the same way; a request to the path /test/data/something will never be handled by the DataResource. """ from twisted.web import resource from twisted.web import rewrite from twisted.web import server from twisted.internet import reactor class GenericResource(resource.Resource): def render(self, request): return "dummy resource page" class DataResource(resource.Resource): def render(self, request): """request.args are where GET request args are stored. (From docs) For example, for a URI with 'foo=bar&foo=baz&quux=spam' for its query part, args will be {'foo': ['bar', 'baz'], 'quux': ['spam']} """ print request.args request.write(str(request.args)) return "" def pathToArgs(request): """assume segments of path following "data" are values for the keys: - sta - orid localhost:8008/data/109C/39495/ -> localhost:8008/data?sta=109C&orid=39495 """ if len(request.postpath) == 3 and request.postpath[0] == 'data': sta = request.postpath[1] orid = request.postpath[2] request.args = {'sta':[sta], 'orid':[orid]} request.postpath = ['data'] request.path = '/data' def test2Data(request): if request.postpath[0] == 'test': if len(request.postpath) > 2 and request.postpath[1] == 'data': if request.postpath: param =request.postpath[2] request.postpath = ['data', param,'constant'] request.path = '/data' root = GenericResource() root.putChild('', root) root.putChild('test', GenericResource()) root.putChild('data', DataResource()) rewrite_root = rewrite.RewriterResource(root, test2Data, pathToArgs) site = server.Site(rewrite_root) reactor.listenTCP(8080, site) reactor.run() -
deldotdr renamed this gist
Jan 26, 2010 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
deldotdr created this gist
Jan 25, 2010 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,47 @@ from twisted.web import resource from twisted.web import rewrite from twisted.web import server from twisted.internet import reactor class DataResource(resource.Resource): def render(self, request): """request.args are where GET request args are stored. (From docs) For example, for a URI with 'foo=bar&foo=baz&quux=spam' for its query part, args will be {'foo': ['bar', 'baz'], 'quux': ['spam']} """ print request.args request.write(str(request.args)) return "" def pathToArgs(request): """assume segments of path following "data" are values for the keys: - sta - orid localhost:8008/data/109C/39495/ -> localhost:8008/data?sta=109C&orid=39495 """ if request.postpath and request.postpath[0] == 'data': sta = request.postpath[1] orid = request.postpath[2] request.args = {'sta':[sta], 'orid':[orid]} request.postpath = ['data'] request.path = '/data' root = resource.Resource() root.putChild('data', DataResource()) rewrite_root = rewrite.RewriterResource(root, pathToArgs) site = server.Site(rewrite_root) reactor.listenTCP(8080, site) reactor.run()