Created
December 5, 2013 23:07
-
-
Save jszmajda/7815695 to your computer and use it in GitHub Desktop.
Simple rack server without http
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rack' | |
require 'rack/content_length' | |
require 'rack/rewindable_input' | |
raw = File.read('simple.ru') | |
app = eval("Rack::Builder.new {( #{raw} )}.to_app") | |
a = Rack::Builder.new do | |
use Rack::ContentLength | |
use Rack::Chunked | |
use Rack::CommonLogger, $stderr | |
use Rack::Lint | |
run app | |
end.to_app | |
while true do | |
puts "Your request?" | |
uri = gets.strip | |
env = ENV.to_hash | |
env.update({ | |
"rack.version" => Rack::VERSION, | |
"rack.input" => Rack::RewindableInput.new($stdin), | |
"rack.errors" => $stderr, | |
"rack.multithread" => false, | |
"rack.multiprocess" => true, | |
"rack.run_once" => true, | |
"rack.url_scheme" => ["yes", "on", "1"].include?(ENV["HTTPS"]) ? "https" : "http", | |
'REQUEST_METHOD' => 'GET', | |
'SERVER_NAME' => 'howdy', | |
'SERVER_PORT' => '8080', | |
'PATH_INFO' => uri, | |
'QUERY_STRING' => '', | |
'HTTP_VERSION' => '1.1', | |
'REQUEST_PATH' => uri, | |
}) | |
response_code, headers, body = a.call(env) | |
puts "Response code: #{response_code}" | |
puts "Headers: #{headers.inspect}" | |
puts "Body Follows:" | |
body.each{|chunk| puts chunk } | |
body.close | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment