Skip to content

Instantly share code, notes, and snippets.

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)
def pp_plain(hash) {|key,value| "#{key}: #{value}"}.sort.join("\n")
def pp_html(hash) {|key,value| CGI::escapeHTML("#{key}: #{value}")}.sort.join("<br/>")
def headers(hash)
headers = {}
headers['Content-Type'] = 'text/html' if hash["HTTP_ACCEPT"].include?("text/html")
end lambda {|env| [200,headers(env),[pp(env)]]} , :Port=>3000
# credits to:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment