Created
January 31, 2009 13:25
-
-
Save greut/55543 to your computer and use it in GitHub Desktop.
playing with middlewares: wsgi, rack and jack.
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 characters
| #!/usr/bin/env java org.mozilla.javascript.tools.shell.Main | |
| load("core.js"); | |
| require("lib/jack"); | |
| require("lib/jack/handler/jetty"); | |
| function app(env) { | |
| return [200, | |
| {"Content-Type": "text/plain"}, | |
| "Hello, world!"]; | |
| }; | |
| function rot13(str) { | |
| var new_chars = [], i, r, c; | |
| for (var i=0; i<str.length; i++) { | |
| // 'A' -> 65 | |
| // 'a' -> 97 | |
| // a - A -> 32 | |
| c = str.charCodeAt(i) - 65; | |
| r = c; | |
| if((c > 0 && c < 26) || (c >= 32 && c < 58)) { | |
| r = (c >= 32) ? (r - 32) : r; | |
| r = (r + 13) % 26; | |
| r = (c >= 32) ? (r + 32) : r; | |
| } | |
| new_chars.push(String.fromCharCode(r + 65)); | |
| } | |
| return new_chars.join(""); | |
| } | |
| function Rot13Middleware(app) { | |
| this.app = app; | |
| } | |
| Rot13Middleware.prototype.invoke = function(env) { | |
| var response = this.app.invoke(env) | |
| return [response[0], response[1], rot13(response[2])]; | |
| }; | |
| function rot13decorator(func) { | |
| return function(env) { | |
| var response = func.invoke(env); | |
| return [response[0], response[1], rot13(response[2])]; | |
| } | |
| } | |
| app = rot13decorator(new Rot13Middleware(app)); | |
| Jack.Handler.Jetty.run(app); |
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 characters
| #!/usr/bin/env python | |
| from wsgiref.simple_server import make_server | |
| PORT = 8080 | |
| def app(environ, start_response): | |
| start_response("200 OK", [("Content-Type", "text/plain")]) | |
| return ["Hello, world!"] | |
| class Rot13(object): | |
| def __init__(self, app): | |
| self.app = app | |
| def __call__(self, environ, start_response): | |
| response = self.app(environ, start_response) | |
| return [s.encode("rot13") for s in response] | |
| def rot13(func): | |
| def decorated(environ, start_response): | |
| response = func(environ, start_response) | |
| return [s.encode("rot13") for s in response] | |
| return decorated | |
| if __name__ == "__main__": | |
| app = rot13(Rot13(app)) | |
| server = make_server('', PORT, app) | |
| try: | |
| print "Serving from localhost on port %d" % PORT | |
| server.serve_forever() | |
| except KeyboardInterrupt, ki: | |
| print "bye bye!" |
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 characters
| #!/usr/bin/env rackup | |
| # vim:ft=ruby: | |
| use Rack::ShowExceptions | |
| use Rack::Lint | |
| use Rack::ContentLength | |
| app = lambda do |env| | |
| [200, | |
| {'Content-Type' => 'text/plain'}, | |
| "Hello, world!"] | |
| end | |
| # http://neeraj.name/blog/articles/280-implementing-rot13-in-ruby | |
| class String | |
| def rot13 | |
| self.tr("A-Ma-mN-Zn-z", "N-Zn-zA-Ma-m") | |
| end | |
| end | |
| class Rot13Middleware | |
| def initialize(app) | |
| @app = app | |
| end | |
| def call(env) | |
| status, headers, response = @app.call(env) | |
| [status, headers, response.rot13] | |
| end | |
| end | |
| def rot13decorator(func) | |
| lambda do |env| | |
| status, headers, response = func.call(env) | |
| [status, headers, response.rot13] | |
| end | |
| end | |
| run rot13decorator(Rot13Middleware.new(app)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment