Class: Mizuno::RackHandler

Inherits:
AbstractHandler
  • Object
show all
Defined in:
lib/mizuno/rack_handler.rb

Constant Summary collapse

NEWLINE =

Regex for splitting on newlines.

/\n/

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ RackHandler

Returns a new instance of RackHandler.



17
18
19
20
# File 'lib/mizuno/rack_handler.rb', line 17

def initialize(server)
    @server = server
    super()
end

Instance Method Details

#handle(target, base_request, request, response) ⇒ Object

Takes an incoming request (as a HttpServletRequest) 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.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mizuno/rack_handler.rb', line 49

def handle(target, base_request, request, response)
    handle_exceptions(response) do
        # 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.
        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'] = Proc.new 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
end

#rackup(app) ⇒ Object

Sets the Rack application that handles requests sent to this Jetty handler.



26
27
28
# File 'lib/mizuno/rack_handler.rb', line 26

def rackup(app)
    @app = app
end