Skip to content

Commit

Permalink
Change response string per http://mongrel2.org/info/54b08f901b.
Browse files Browse the repository at this point in the history
  • Loading branch information
perplexes committed Jul 27, 2010
1 parent 29282ba commit 7a530c7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
25 changes: 13 additions & 12 deletions connection.rb
Expand Up @@ -56,21 +56,23 @@ def recv_json

# Raw send to the given connection ID, mostly used
# internally.
def send_resp(conn_id, msg)
# debugger
@resp.send_string(conn_id + ' ' + msg, 0)
def send_resp(uuid, conn_id, msg)
header = "%s %d:%s," % [uuid, conn_id.size, conn_id]
string = header + ' ' + msg
#puts "DEBUG: #{string.inspect}"
@resp.send_string(string, 0)
end

# Does a reply based on the given Request object and message.
# This is easier since the req object contains all the info
# needed to do the proper reply addressing.
def reply(req, msg)
self.send_resp(req.conn_id, msg)
self.send_resp(req.sender, req.conn_id, msg)
end

# Same as reply, but tries to convert data to JSON first.
def reply_json(req, data)
self.send_resp(req.conn_id, JSON.generate(data))
self.send_resp(req.sender, req.conn_id, JSON.generate(data))
end

# Basic HTTP response mechanism which will take your body,
Expand All @@ -85,21 +87,20 @@ def reply_http(req, body, code=200, headers={})
# not exceed, so chunk your targets as needed. Each target
# will receive the message once by Mongrel2, but you don't have
# to loop which cuts down on reply volume.
def deliver(idents, data)
# debugger
@resp.send_string(idents.join(' ') + ' ' + data, 0)
def deliver(uuid, idents, data)
self.send_resp(uuid, idents.join(' '), data)
end

# Same as deliver, but converts to JSON first.
def deliver_json(idents, data)
self.deliver(idents, JSON.generate(data))
def deliver_json(uuid, idents, data)
self.deliver(uuid, idents, JSON.generate(data))
end

# Same as deliver, but builds an HTTP response, which means, yes,
# you can reply to multiple connected clients waiting for an HTTP
# response from one handler. Kinda cool.
def deliver_http(idents, body, code=200, headers={})
self.deliver(idents, http_response(body, code, headers))
def deliver_http(uuid, idents, body, code=200, headers={})
self.deliver(uuid, idents, http_response(body, code, headers))
end

private
Expand Down
4 changes: 2 additions & 2 deletions example/lobster.ru
@@ -1,7 +1,7 @@
require 'rack/lobster'
$: << File.dirname(__FILE__)
$: << ::File.dirname(__FILE__)
require 'rack_handler'

use Rack::ShowExceptions
puts "Lobster at http://localhost:6767/handlertest"
Rack::Handler::Mongrel2Handler.run Rack::Lobster.new
Rack::Handler::Mongrel2.run Rack::Lobster.new
11 changes: 8 additions & 3 deletions example/rack_handler.rb
Expand Up @@ -12,9 +12,9 @@

module Rack
module Handler
class Mongrel2Handler
class Mongrel2
def self.run(app, receive = "tcp://127.0.0.1:9997", send = "tcp://127.0.0.1:9996")
conn = Mongrel2::Connection.new($sender_id, receive, send)
conn = ::Mongrel2::Connection.new($sender_id, receive, send)
@running = true
trap("SIGINT") do
@running = false
Expand All @@ -30,7 +30,9 @@ def self.run(app, receive = "tcp://127.0.0.1:9997", send = "tcp://127.0.0.1:9996
next
end

script_name = ENV["RAILS_RELATIVE_URL_ROOT"] || ""
script_name = ENV["RACK_RELATIVE_URL_ROOT"] ||
# PATTERN is like: /test/(.*.json) or /handlertest
req.headers["PATTERN"].split('(', 2).first.gsub(/\/$/, '')

env = {
"rack.version" => Rack::VERSION,
Expand All @@ -40,6 +42,9 @@ def self.run(app, receive = "tcp://127.0.0.1:9997", send = "tcp://127.0.0.1:9996
"rack.multithread" => true,
"rack.multiprocess" => true,
"rack.run_once" => false,

"mongrel2.pattern" => req.headers["PATTERN"],

"REQUEST_METHOD" => req.headers["METHOD"],
"SCRIPT_NAME" => script_name,
"PATH_INFO" => req.headers["PATH"].gsub(script_name, ''),
Expand Down

0 comments on commit 7a530c7

Please sign in to comment.