Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Echo Server Bug Fixes and Presentation Cleanup #43

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 29 additions & 48 deletions rocs/cgi_bin/echoserver.rb
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,84 +1,65 @@
#!/usr/bin/ruby
# Design:
# (1) Accept STDIN as hash in JSon format
# (2) Convert the JSon format hash to a ruby hash
# (3) Create html using CGI to display the information in the hash
# (1) Accept STDIN as hash in JSon or XMLformat
# (2) Convert the JSon or XML format hash to a ruby hash
# (3) Create html to display the information in the hash
# (4) Return an html as string through STDOUT

require 'rubygems'
require 'json'
require 'xmlsimple'
# require 'cgi'

# cgi = CGI.new('html3')
#input = File.read('../test/tricky.xml') # For debugging
input = STDIN.gets

if input == nil || input == ""
return ""
STDOUT.puts("")
exit
end

puts input
exit

content_type = input.split[input.split.index("Content-Type:") + 1]

content_headers = input.split("\n\n")[0]

if content_type == "text/xml"
ruby_hash = XmlSimple.xml_in(input).sort
elsif content_type == "application/json"
begin
ruby_hash = JSON.parse(input.split("\n\n")[1]).sort
else raise "Input not in application/json or text/xml format."
rescue
begin
ruby_hash = XmlSimple.xml_in(input).sort
rescue
raise "Input not in application/json or text/xml format."
end
end

class Object
class String
def to_html
self.to_s.to_html
self
end
end

class Hash
class Object
def to_html
self.inject("") { |r,e|
r += "<h3>#{e[0]}:</h3>" + "<ul>" + e[1].to_html + "</ul>"
}
self.to_s
end
end

class Array
class Hash
def to_html
self.inject("") { |r,e|
r += "<li>" + e.to_html + "</li>"
self.sort.inject("") { |r,e|
r += "<h3>#{e[0]}:</h3>" + '<ul style="line-height: 0em">' + "&nbsp" + e[1].to_html + "</ul>"
}
end
end

class String
class Array
def to_html
"<li>" + self + "</li>"
if self[1].class == Array
Hash[self].to_html
else
self.inject("") { |r,e|
r += "<li>" + e.to_html + "</li>"
}
end
end
end


STDOUT.puts( content_headers.split("\n").inject(""){|r,e| r += "<p>#{e}</p>"} + ruby_hash.to_html )

# cgi.out {
# cgi.html {
# cgi.body {
# content_headers.split("\n").inject(""){|r,e| r += "<p>#{e}</p>"} + ruby_hash.to_html
# }
# }
# }

# 3 line echo server
#server = TCPServer.new('127.0.0.1', '8080')
#socket = server.accept

#while true
#Json hash looks like { "key" : "val" }
# json_hash = socket.readline
# ruby_hash = { json_hash.split("\"")[1] => json_hash.split("\"")[3] }

#Use CGI to create an HTML file and
#socket.puts html_file
#end
STDOUT.puts( content_headers.split("\n").inject(""){|r,e| r += "#{e}<br>"} + ruby_hash.to_html )