Skip to content

Instantly share code, notes, and snippets.

@akostadinov
Created November 18, 2021 14:18
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save akostadinov/02361c02cca88ecfdde81ecfc914e89f to your computer and use it in GitHub Desktop.
Rack echo server
require "rack"
def pp(hash)
hash["HTTP_ACCEPT"].include?("text/html") ? pp_html(hash) : pp_plain(hash)
end
def pp_plain(hash)
hash.map {|key,value| "#{key}: #{value}"}.sort.join("\n")
end
def pp_html(hash)
hash.map {|key,value| CGI::escapeHTML("#{key}: #{value}")}.sort.join("<br/>")
end
def headers(hash)
headers = {}
headers['Content-Type'] = 'text/html' if hash["HTTP_ACCEPT"].include?("text/html")
headers
end
Rack::Handler::WEBrick.run lambda {|env| [200,headers(env),[pp(env)]]} , :Port=>3000
# credits to: https://stackoverflow.com/a/17396721/520567
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment