lib/firehose/rack.rb in firehose-1.1.1 vs lib/firehose/rack.rb in firehose-1.2.0

- old
+ new

@@ -1,56 +1,29 @@ module Firehose module Rack - autoload :ConsumerApp, 'firehose/rack/consumer_app' - autoload :PublisherApp, 'firehose/rack/publisher_app' - autoload :PingApp, 'firehose/rack/ping_app' + autoload :Consumer, 'firehose/rack/consumer' + autoload :Publisher, 'firehose/rack/publisher' + autoload :Ping, 'firehose/rack/ping' + autoload :App, 'firehose/rack/app' - # Evented web servers recognize this as a response deferral. + # Evented web servers recognize the -1 HTTP code as a response deferral, which + # is needed to stream responses via WebSockets or HTTP long polling. ASYNC_RESPONSE = [-1, {}, []].freeze # Normally we'd want to use a custom header to reduce the likelihood of some # HTTP middleware clobbering the value. But Safari seems to ignore our CORS # header instructions, so we are using 'pragma' because it is always allowed. LAST_MESSAGE_SEQUENCE_HEADER = 'Pragma' + + # Rack wants the header to start with HTTP, so we deal with that here. RACK_LAST_MESSAGE_SEQUENCE_HEADER = "HTTP_#{LAST_MESSAGE_SEQUENCE_HEADER.upcase.gsub('-', '_')}" - # Don't cache in development mode + + # Disable CORS preflight caches for requests in development mode. CORS_OPTIONS_MAX_AGE = ENV['RACK_ENV'] == 'development' ? '1' : '1728000' - # Allows the publisher and consumer to be mounted on the same port. - class App - def call(env) - # Cache the parsed request so we don't need to re-parse it when we pass - # control onto another app. - req = env['parsed_request'] ||= ::Rack::Request.new(env) - method = req.request_method - - case method - when 'PUT' - publisher.call(env) - when 'HEAD' - ping.call(env) - else - consumer.call(env) - end - end - - - private - def publisher - @publisher ||= PublisherApp.new - end - - def consumer - @consumer ||= ConsumerApp.new - end - - def ping - @ping ||= PingApp.new - end - end - module Helpers - # Calculates the content length for you + # Calculates the content of a message body for the response so that HTTP Keep-Alive + # connections work. def response(status, body='', headers={}) headers = {'Content-Length' => body.size.to_s}.merge(headers) [status, headers, [body]] end end