Created
November 18, 2021 14:18
-
-
Save akostadinov/02361c02cca88ecfdde81ecfc914e89f to your computer and use it in GitHub Desktop.
Rack echo server
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" | |
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