5 require File
.join(rails_root
, 'config/boot')
6 require RAILS_ROOT
+ "/config/environment"
16 $logger = Logger
.new(RAILS_ROOT
+ "/log/fuzed.#{Process.pid}.log")
34 log
"Waiting for connections" if LOG
38 receive(IO
.new(3), IO
.new(4)) do |f
|
39 f
.when(:request, Array
) do |request
|
41 log('------------MATCH------------')
43 res
= me
.service(request
)
48 # [:header, "Content-Type", "text/plain; charset=utf-8"],
49 # [:header, "Cache-Control", "no-cache"]]],
53 total_delta
= Time
.now
- $t
54 $total_avg[0] += total_delta
56 log(">> Total in #{total_delta} (#{$total_avg[0] / $total_avg[1]} avg) sec\n") if LOG
66 log('------------NO-MATCH------------') if LOG
67 log any
.inspect
if LOG
74 request
= vars
.inject({}) { |a
, x
| a
[x
[0]] = x
[1]; a
}
76 method
= request
[:method]
77 version = request
[:http_version] # => e.g. [1, 1]
78 path
= request
[:querypath]
79 query
= request
[:querydata]
80 server
= request
[:servername]
81 headers
= request
[:headers]
82 cookies
= request
[:cookies]
83 postdata
= request
[:postdata] == :undefined ? '' : request
[:postdata]
85 translate
= {:content_type => 'CONTENT_TYPE',
86 :content_length => 'CONTENT_LENGTH',
87 :accept => 'HTTP_ACCEPT',
88 :'Accept-Charset' => 'HTTP_ACCEPT_CHARSET',
89 :'Accept-Encoding' => 'HTTP_ACCEPT_ENCODING',
90 :'Accept-Language' => 'HTTP_ACCEPT_LANGUAGE',
91 :connection => 'HTTP_CONNECTION',
92 :keep_alive => 'HTTP_KEEP_ALIVE',
94 :referer => 'HTTP_REFERER',
95 :user_agent => 'HTTP_USER_AGENT',
96 'X-Prototype-Version' => 'HTTP_X_PROTOTYPE_VERSION',
97 'X-Requested-With' => 'HTTP_X_REQUESTED_WITH'}
100 env['REQUEST_METHOD'] = method
.to_s
101 env['QUERY_STRING'] = query
102 env["PATH_INFO"] = path
== '/' ? '' : path
103 env = headers
.inject(env) { |a
, x
| a
[translate
[x
[0]] || x
[0].to_s
] = x
[1]; a
}
104 env.delete_if
{ |k
, v
| v
.nil? }
106 env.update({"rack.version" => [0, 1],
107 "rack.input" => StringIO
.new(postdata
),
108 "rack.errors" => STDERR,
110 "rack.multithread" => true,
111 "rack.multiprocess" => false,
112 "rack.run_once" => false,
114 "rack.url_scheme" => ["yes", "on", "1"].include?(ENV["HTTPS"]) ? "https" : "http"
117 env['SERVER_NAME'] = server
.split(':')[0]
118 env['SERVER_PORT'] = server
.split(':')[1]
119 env['HTTP_VERSION'] = version.join('.')
121 env["HTTP_VERSION"] ||= env["SERVER_PROTOCOL"]
122 env["QUERY_STRING"] ||= ""
123 env["REQUEST_PATH"] ||= "/"
124 env.delete
"PATH_INFO" if env["PATH_INFO"] == ""
126 cookies
.each
do |cookie
|
127 env["HTTP_COOKIE"] = cookie
.to_s
130 log('------------IN------------') if LOG
131 log(env.inspect
) if LOG
140 status
, headers
, body
= @app.call(env)
142 rails_delta
= Time
.now
- t1
143 $rails_avg[0] += rails_delta
145 log(">> Rails in #{rails_delta} (#{$rails_avg[0] / $rails_avg[1]} avg) sec") if LOG
152 headers
['Server'] = 'YAWS + Fuzed 0.0.1'
153 headers
['Connection'] = 'close'
155 cookies
= headers
.delete('cookie')
156 #cookies.map! {|c| c.include?('path=') ? c : c + "; path=/"}
157 headers
['Set-Cookie'] = cookies
if cookies
161 status
= (headers
["Status"].split(" ").first
rescue nil) || status
162 headers
.delete("Status") if headers
["Status"]
166 [[:status, status
.to_i
],
167 [:allheaders, headers
.inject([]) { |a
, x
| a
+= x
[1].map
{ |y
| [:header, x
[0], y
] } }],
174 [:header, "Content-Type", "text/plain; charset=utf-8"],
175 [:header, "Cache-Control", "no-cache"]]],
176 [:html, "500 Internal Error\n\n#{e}\n\n#{e.backtrace}"]]]
179 log('-----------OUT------------') if LOG
180 log(res
.inspect
) if LOG
188 ###############################################################################
190 unless defined? RAILS_ROOT
191 raise "Rails' environment has to be loaded before using Rack::Adapter::Rails"
194 require "rack/request"
195 require "rack/response"
202 request
= Request
.new(env)
203 response
= Response
.new
205 cgi
= CGIStub
.new(request
, response
)
207 Dispatcher
.dispatch(cgi
, ActionController
::CgiRequest::DEFAULT_SESSION_OPTIONS, response
)
214 class CGIStub
< ::CGI
216 def initialize(request
, response
, *args
)
220 @input = request
.body
224 IGNORED_HEADERS
= [ "Status" ]
226 def header(options
= "text/html")
227 # puts 'header---------------'
229 # puts '---------------------'
231 if options
.instance_of
?(String
)
232 @response['Content-Type'] = options
unless @response['Content-Type']
234 @response['Content-Length'] = options
.delete('Content-Length').to_s
if options
['Content-Length']
236 @response['Content-Type'] = options
.delete('type') || "text/html"
237 @response['Content-Type'] += "; charset=" + options
.delete('charset') if options
['charset']
239 @response['Status'] = options
.delete('Status') if options
['Status']
240 @response['Content-Language'] = options
.delete('language') if options
['language']
241 @response['Expires'] = options
.delete('expires') if options
['expires']
243 IGNORED_HEADERS
.each
{|k
| options
.delete(k
) }
245 options
.each
{|k
,v
| @response[k
] = v
}
247 # convert 'cookie' header to 'Set-Cookie' headers
248 if cookie
= @response['cookie']
251 cookie
.each
{|c
| @response['Set-Cookie'] = c
.to_s
}
253 cookie
.each_value
{|c
| @response['Set-Cookie'] = c
.to_s
}
255 @response['Set-Cookie'] = options
['cookie'].to_s
258 @output_cookies.each
{ |c
| @response['Set-Cookie'] = c
.to_s
} if @output_cookies
266 @params ||= @request.params
274 @request.query_string
277 # Used to wrap the normal args variable used inside CGI.
282 # Used to wrap the normal env_table variable used inside CGI.
287 # Used to wrap the normal stdinput variable used inside CGI.
293 STDERR.puts
"stdoutput should not be used."
301 ###############################################################################
304 require 'rack/cascade'
305 require 'rack/showexceptions'
306 # Rack::Handler::Fuzed.run \
307 # Rack::ShowExceptions.new(Rack::Lint.new(Rack::Adapter::Rails.new))
309 if ARGV.first
!= 'test'
310 Rack
::Handler::Fuzed.run(Rack
::Adapter::Rails.new
)
313 [[:method, :POST], [:http_version, [1, 1]], [:querypath, "/main/go"], [:querydata, ""], [:servername, "testing:8002"], [:headers, [[:connection, "keep-alive"], [:accept, "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"], [:host, "localhost:8002"], [:referer, "http://localhost:8002/main/ready"], [:user_agent, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3"], [:keep_alive, "300"], [:content_length, "7"], [:content_type, "application/x-www-form-urlencoded"], [:"Cache-Control", "max-age=0"], [:"Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"], [:"Accept-Encoding", "gzip,deflate"], [:"Accept-Language", "en-us,en;q=0.5"]]], [:cookies, ["_helloworld_session_id=d3eae987aab3230377abc433b7a8d7c1"]], [:pathinfo, "/Users/tom/dev/fuzed/helloworld/public"], [:postdata, "val=foo"]]
315 # [[:method, :GET], [:http_version, [1, 1]], [:querypath, "/main/say"], [:querydata, ""], [:servername, "testing:8002"], [:headers, [[:connection, "keep-alive"], [:accept, "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"], [:host, "localhost:8002"], [:user_agent, "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3"], [:keep_alive, "300"], [:"Cache-Control", "max-age=0"], [:"Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"], [:"Accept-Encoding", "gzip,deflate"], [:"Accept-Language", "en-us,en;q=0.5"]]], [:cookies, ["_helloworld_session_id=166098a3c3f702698d0529c6148c6164"]], [:pathinfo, "/Users/tom/dev/fuzed/helloworld/public"], [:postdata, :undefined]]
317 p Rack
::Handler::Fuzed.new(Rack
::Adapter::Rails.new
).service(req
)