Merged updates from trunk into stable branch
[feedcatcher.git] / vendor / rails / actionpack / lib / action_controller / vendor / rack-1.0 / rack / handler / lsws.rb
1 require 'lsapi'
2 require 'rack/content_length'
3
4 module Rack
5 module Handler
6 class LSWS
7 def self.run(app, options=nil)
8 while LSAPI.accept != nil
9 serve app
10 end
11 end
12 def self.serve(app)
13 app = Rack::ContentLength.new(app)
14
15 env = ENV.to_hash
16 env.delete "HTTP_CONTENT_LENGTH"
17 env["SCRIPT_NAME"] = "" if env["SCRIPT_NAME"] == "/"
18 env.update({"rack.version" => [0,1],
19 "rack.input" => StringIO.new($stdin.read.to_s),
20 "rack.errors" => $stderr,
21 "rack.multithread" => false,
22 "rack.multiprocess" => true,
23 "rack.run_once" => false,
24 "rack.url_scheme" => ["yes", "on", "1"].include?(ENV["HTTPS"]) ? "https" : "http"
25 })
26 env["QUERY_STRING"] ||= ""
27 env["HTTP_VERSION"] ||= env["SERVER_PROTOCOL"]
28 env["REQUEST_PATH"] ||= "/"
29 status, headers, body = app.call(env)
30 begin
31 send_headers status, headers
32 send_body body
33 ensure
34 body.close if body.respond_to? :close
35 end
36 end
37 def self.send_headers(status, headers)
38 print "Status: #{status}\r\n"
39 headers.each { |k, vs|
40 vs.split("\n").each { |v|
41 print "#{k}: #{v}\r\n"
42 }
43 }
44 print "\r\n"
45 STDOUT.flush
46 end
47 def self.send_body(body)
48 body.each { |part|
49 print part
50 STDOUT.flush
51 }
52 end
53 end
54 end
55 end