diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/README b/README new file mode 100644 index 0000000..c0cdd53 --- /dev/null +++ b/README @@ -0,0 +1,41 @@ + is a high-performance Rack adapter for JRuby apps, +based on the latest version of the Jetty webserver +(http://jetty.codehaus.org/jetty/) + +To use rack-handler-jetty with your Rack app: + + gem install rack-handler-jetty + cd /path/to/my/app + jetty + +We get our performance by implementing a direct servlet-to-rack bridge +via JRuby's Java integration layer. + +The numbers speak for themselves: + + ruby 1.9.1p243 + + Mongrel + Webrick + Thin + + JRuby 1.5.0RC1 on Java SE Server VM 1.6.0_17-b04 + + Mongrel + Webrick + rack-handler-jetty + Jetty+Warbler + Tomcat+Warbler + +rack-handler-jetty also provides access to the underlaying servlet +context, allowing for runtime additions of additional servlets and +filters. We plan on using this to tie in the cometd servlet +as a piece of Rack middleware, which will provide a simple way to +add Comet funtionality, via the Bayeux protocol, to any Rack +application. + +Note that rack-handler-jetty is NOT a direct replacement for jruby-rack +or Warbler, as we don't produce WAR files or make any attempt to package +a Rack application for installation in a Java web container. Rather, we +just make it really easy to use Jetty as a webserver for your Rack +applications. diff --git a/bin/mizuno b/bin/mizuno new file mode 100755 index 0000000..e7a26a0 --- /dev/null +++ b/bin/mizuno @@ -0,0 +1,12 @@ +#!/usr/bin/env ruby + +# Make sure we're on JRuby. +raise("Rack::Handler::Mizuno only runs on JRuby.") \ + unless (RUBY_PLATFORM =~ /java/) + +require 'rack' +require 'rack/handler/mizuno' + +server = Rack::Server.new +server.options[:server] = 'mizuno' +server.start diff --git a/lib/rack/handler/mizuno.rb b/lib/rack/handler/mizuno.rb new file mode 100644 index 0000000..7a180d2 --- /dev/null +++ b/lib/rack/handler/mizuno.rb @@ -0,0 +1 @@ +require 'rack/handler/mizuno/http_server' diff --git a/lib/rack/handler/mizuno/http_server.rb b/lib/rack/handler/mizuno/http_server.rb new file mode 100644 index 0000000..faeb73d --- /dev/null +++ b/lib/rack/handler/mizuno/http_server.rb @@ -0,0 +1,66 @@ +# +# A Rack handler for Jetty 7. +# +# Written by Don Werve +# + +require 'java' +require 'rack' + +# Load Jetty JARs. +jars = File.join(File.dirname(__FILE__), '..', '..', '..', 'java', '*.jar') +Dir[jars].each { |j| require j } + +# Load the Rack/Servlet bridge. +require 'rack/handler/mizuno/rack_servlet' + +# We don't want to mix our logs in with Solr. +# FIXME: Implement a custom logger. +java.lang.System.setProperty("org.eclipse.jetty.util.log.class", + "org.eclipse.jetty.util.log.StdErrLog") + +module Rack::Handler::Mizuno + class HttpServer + # Include various Jetty classes so we can use the short names. + include_class 'org.eclipse.jetty.server.Server' + include_class 'org.eclipse.jetty.servlet.ServletContextHandler' + include_class 'org.eclipse.jetty.servlet.ServletHolder' + include_class 'org.eclipse.jetty.server.nio.SelectChannelConnector' + include_class 'org.eclipse.jetty.util.thread.QueuedThreadPool' + + def self.run(app, options = {}) + # The Jetty server + server = Server.new + + # Thread pool + thread_pool = QueuedThreadPool.new + thread_pool.min_threads = 5 + thread_pool.max_threads = 50 + server.set_thread_pool(thread_pool) + + # Connector + connector = SelectChannelConnector.new + connector.setPort(options[:Port].to_i) + connector.setHost(options[:Host]) + server.addConnector(connector) + + # Servlet context. + context = ServletContextHandler.new(nil, "/", + ServletContextHandler::NO_SESSIONS) + + # The servlet itself. + servlet = RackServlet.new + servlet.rackup(app) + holder = ServletHolder.new(servlet) + context.addServlet(holder, "/") + + # Add the context to the server and start. + server.set_handler(context) + puts "Started Jetty on #{connector.getHost}:#{connector.getPort}" + server.start + end + end +end + +# Register ourselves with Rack when this file gets loaded. +Rack::Handler.register 'mizuno', 'Rack::Handler::Mizuno::Server' diff --git a/lib/rack/handler/mizuno/rack_servlet.rb b/lib/rack/handler/mizuno/rack_servlet.rb new file mode 100644 index 0000000..596cc42 --- /dev/null +++ b/lib/rack/handler/mizuno/rack_servlet.rb @@ -0,0 +1,219 @@ +# +# Wraps a Rack application in a Java servlet. +# +# http://rack.rubyforge.org/doc/SPEC.html +# http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServlet.html +# +module Rack::Handler::Mizuno + include_class javax.servlet.http.HttpServlet + + class RackServlet < HttpServlet + include_class java.io.FileInputStream + include_class org.eclipse.jetty.continuation.ContinuationSupport + + # + # Sets the Rack application that handles requests sent to this + # servlet container. + # + def rackup(app) + @app = app + end + + # + # Takes an incoming request (as a Java Servlet) and dispatches it to + # the rack application setup via [rackup]. All this really involves + # is translating the various bits of the Servlet API into the Rack + # API on the way in, and translating the response back on the way + # out. + # + # Also, we implement a common extension to the Rack api for + # asynchronous request processing. We supply an 'async.callback' + # parameter in env to the Rack application. If we catch an + # :async symbol thrown by the app, we initiate a Jetty continuation. + # + # When 'async.callback' gets a response with empty headers and an + # empty body, we declare the async response finished. + # + def service(request, response) + # Turn the ServletRequest into a Rack env hash + env = servlet_to_rack(request) + + # Handle asynchronous responses via Servlet continuations. + continuation = ContinuationSupport.getContinuation(request) + + # If this is an expired connection, do nothing. + # FIXME: Is this the best way to handle things? + return if continuation.isExpired + + # We should never be re-dispatched. + raise("Request re-dispatched.") unless continuation.isInitial + + # Add our own special bits to the rack environment so that + # Rack middleware can have access to the Java internals. + env['rack.java.servlet'] = true + env['rack.java.servlet.request'] = request + env['rack.java.servlet.response'] = response + env['rack.java.servlet.continuation'] = continuation + + # Add an callback that can be used to add results to the + # response asynchronously. + env['async.callback'] = lambda do |rack_response| + servlet_response = continuation.getServletResponse + rack_to_servlet(rack_response, servlet_response) \ + and continuation.complete + end + + # Execute the Rack request. + catch(:async) do + rack_response = @app.call(env) + + # For apps that don't throw :async. + unless(rack_response[0] == -1) + # Nope, nothing asynchronous here. + rack_to_servlet(rack_response, response) + return + end + end + + # If we got here, this is a continuation. + continuation.suspend(response) + end + + private + + # + # Turns a Servlet request into a Rack request hash. + # + def servlet_to_rack(request) + # The Rack request that we will pass on. + env = Hash.new + + # Map Servlet bits to Rack bits. + env['REQUEST_METHOD'] = request.getMethod + env['QUERY_STRING'] = request.getQueryString.to_s + env['SERVER_NAME'] = request.getServerName + env['SERVER_PORT'] = request.getServerPort.to_s + env['rack.version'] = Rack::VERSION + env['rack.url_scheme'] = request.getScheme + env['HTTP_VERSION'] = request.getProtocol + env["SERVER_PROTOCOL"] = request.getProtocol + env['REMOTE_ADDR'] = request.getRemoteAddr + env['REMOTE_HOST'] = request.getRemoteHost + + # request.getPathInfo seems to be blank, so we're using the URI. + env['REQUEST_PATH'] = request.getRequestURI + env['PATH_INFO'] = request.getRequestURI + env['SCRIPT_NAME'] = "" + + # Rack says URI, but it hands off a URL. + env['REQUEST_URI'] = request.getRequestURL.toString + + # Java chops off the query string, but a Rack application will + # expect it, so we'll add it back if present + env['REQUEST_URI'] << "?#{env['QUERY_STRING']}" \ + if env['QUERY_STRING'] + + # FIXME + # It's a given that we're single-process on JRuby, because we + # can't fork, but this should probably be user-settable. + env['rack.multiprocess'] = false + env['rack.multithread'] = true + env['rack.run_once'] = false + + # Populate the HTTP headers. + request.getHeaderNames.each do |header_name| + header = header_name.upcase.tr('-', '_') + env["HTTP_#{header}"] = request.getHeader(header_name) + end + + # Rack Weirdness: HTTP_CONTENT_TYPE and HTTP_CONTENT_LENGTH + # both need to have the HTTP_ part dropped. + env["CONTENT_TYPE"] = env.delete("HTTP_CONTENT_TYPE") \ + if env["HTTP_CONTENT_TYPE"] + env["CONTENT_LENGTH"] = env.delete("HTTP_CONTENT_LENGTH") \ + if env["HTTP_CONTENT_LENGTH"] + + # The input stream is a wrapper around the Java InputStream. + env['rack.input'] = request.getInputStream.to_io + + # The output stream defaults to stderr. + env['rack.errors'] ||= $stderr + + # All done, hand back the Rack request. + return(env) + end + + # + # Turns a Rack response into a Servlet response; can be called + # multiple times. Returns true if this is the full request (either + # a synchronous request or the last part of an async request), + # false otherwise. + # + # Note that keep-alive *only* happens if we get either a pathname + # (because we can find the length ourselves), or if we get a + # Content-Length header as part of the response. While we can + # readily buffer the response object to figure out how long it is, + # we have no guarantee that we aren't going to be buffering + # something *huge*. + # + # http://docstore.mik.ua/orelly/java-ent/servlet/ch05_03.htm + # + def rack_to_servlet(rack_response, response) + # Split apart the Rack response. + status, headers, body = rack_response + + # We assume the request is finished if we got empty headers, + # an empty body, and we have a committed response. + finished = (headers.empty? and \ + body.respond_to?(:empty?) and body.empty?) + return(true) if (finished and response.isCommitted) + + # No need to send headers again if we've already shipped + # data out on an async request. + unless(response.isCommitted) + # Set the HTTP status code. + response.setStatus(status) + + # Did we get a Content-Length header? + content_length = headers.delete('Content-Length') + response.setContentLength(content_length.to_i) \ + if content_length + + # Add all the result headers. + headers.each { |h, v| response.addHeader(h, v) } + end + + # How else would we write output? + output = response.getOutputStream + + # Turn the body into something nice and Java-y. + if(body.respond_to?(:to_path)) + # We've been told to serve a file; use FileInputStream to + # stream the file directly to the servlet, because this + # is a lot faster than doing it with Ruby. + file = java.io.File.new(body.to_path) + + # We set the content-length so we can use Keep-Alive, + # unless this is an async request. + response.setContentLength(file.length) \ + unless content_length + + # Stream the file directly. + buffer = Java::byte[4096].new + input_stream = FileInputStream.new(file) + while((count = input_stream.read(buffer)) != -1) + output.write(buffer, 0, count) + end + input_stream.close + else + body.each { |l| output.write(l.to_java_bytes) } + end + + # Close the body if we're supposed to. + body.close if body.respond_to?(:close) + + # All done. + output.flush + end + end +end diff --git a/lib/testrequest.rb b/lib/testrequest.rb new file mode 100644 index 0000000..d1f21ec --- /dev/null +++ b/lib/testrequest.rb @@ -0,0 +1,73 @@ +require 'yaml' +require 'net/http' + +class TestApp + def call(env) + status = env["QUERY_STRING"] =~ /secret/ ? 403 : 200 + env["test.postdata"] = env["rack.input"].read + body = env.to_yaml + size = body.respond_to?(:bytesize) ? body.bytesize : body.size + [status, {"Content-Type" => "text/yaml", + "Content-Length" => size.to_s}, [body]] + end + + module Helpers + attr_reader :status, :response + + ROOT = File.expand_path(File.dirname(__FILE__) + "/..") + ENV["RUBYOPT"] = "-I#{ROOT}/lib -rubygems" + + def root + ROOT + end + + def rackup + "#{ROOT}/bin/rackup" + end + + def GET(path, header={}) + Net::HTTP.start(@options[:Host], @options[:Port]) { |http| + user = header.delete(:user) + passwd = header.delete(:passwd) + + get = Net::HTTP::Get.new(path, header) + get.basic_auth user, passwd if user && passwd + http.request(get) { |response| + @status = response.code.to_i + begin + @response = YAML.load(response.body) + rescue ArgumentError + @response = nil + end + } + } + end + + def POST(path, formdata={}, header={}) + Net::HTTP.start(@options[:Host], @options[:Port]) { |http| + user = header.delete(:user) + passwd = header.delete(:passwd) + + post = Net::HTTP::Post.new(path, header) + post.form_data = formdata + post.basic_auth user, passwd if user && passwd + http.request(post) { |response| + @status = response.code.to_i + @response = YAML.load(response.body) + } + } + end + end +end + +class StreamingRequest + def self.call(env) + [200, {"Content-Type" => "text/plain"}, new] + end + + def each + yield "hello there!\n" + sleep 5 + yield "that is all.\n" + end +end diff --git a/test/rack.rb b/test/rack.rb new file mode 100644 index 0000000..5a78b7e --- /dev/null +++ b/test/rack.rb @@ -0,0 +1,86 @@ +#require 'test/spec' + +begin +require 'rack/handler/mizuno' +require 'rack/urlmap' +require 'rack/lint' +require 'testrequest' + +Thread.abort_on_exception = true + +context "Rack::Handler::Mizuno" do + include TestApp::Helpers + + before(:all) do + @app = Rack::Lint.new(TestApp.new) + @options = { :Host => '0.0.0.0', :Port => 9201 } + @server = Rack::Handler::Mizuno::HttpServer.run(@app, @options) + end + + specify "should respond" do + lambda { GET("/test") }.should_not raise_error + end + + specify "should be using Jetty" do + GET("/test") + status.should == 200 + response['rack.java.servlet'].should_not be_nil + response["HTTP_VERSION"].should == "HTTP/1.1" + response["SERVER_PROTOCOL"].should == "HTTP/1.1" + response["SERVER_PORT"].should == "9201" + response["SERVER_NAME"].should == "0.0.0.0" + end + + specify "should have rack headers" do + GET("/test") + response["rack.version"].should == [1,1] + response["rack.multithread"].should be_true + response["rack.multiprocess"].should be_false + response["rack.run_once"].should be_false + end + + specify "should have CGI headers on GET" do + GET("/test") + response["REQUEST_METHOD"].should == "GET" + response["REQUEST_PATH"].should == "/test" + response["PATH_INFO"].should == "/test" + response["QUERY_STRING"].should == "" + response["test.postdata"].should == "" + + GET("/test/foo?quux=1") + response["REQUEST_METHOD"].should == "GET" + response["REQUEST_PATH"].should == "/test/foo" + response["PATH_INFO"].should == "/test/foo" + response["QUERY_STRING"].should == "quux=1" + end + + specify "should have CGI headers on POST" do + POST("/test", {"rack-form-data" => "23"}, {'X-test-header' => '42'}) + status.should == 200 + response["REQUEST_METHOD"].should == "POST" + response["REQUEST_PATH"].should == "/test" + response["QUERY_STRING"].should == "" + response["HTTP_X_TEST_HEADER"].should == "42" + response["test.postdata"].should == "rack-form-data=23" + end + + specify "should support HTTP auth" do + GET("/test", {:user => "ruth", :passwd => "secret"}) + response["HTTP_AUTHORIZATION"].should == "Basic cnV0aDpzZWNyZXQ=" + end + + specify "should set status" do + GET("/test?secret") + status.should == 403 + response["rack.url_scheme"].should == "http" + end + +# teardown do +# end +end + +rescue LoadError => e + $stderr.puts e + $stderr.puts e.backtrace + $stderr.puts "Skipping Rack::Handler::Mizuno tests (Mizuno is required). `gem install mizuno` and try again." +end