diff --git a/rocs/cgi_bin/echoserver.rb b/rocs/cgi_bin/echoserver.rb old mode 100755 new mode 100644 index ba54c1b..0662f75 --- a/rocs/cgi_bin/echoserver.rb +++ b/rocs/cgi_bin/echoserver.rb @@ -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 += "

#{e[0]}:

" + "" - } + self.to_s end end -class Array +class Hash def to_html - self.inject("") { |r,e| - r += "
  • " + e.to_html + "
  • " + self.sort.inject("") { |r,e| + r += "

    #{e[0]}:

    " + '" } end end -class String +class Array def to_html - "
  • " + self + "
  • " + if self[1].class == Array + Hash[self].to_html + else + self.inject("") { |r,e| + r += "
  • " + e.to_html + "
  • " + } + end end end -STDOUT.puts( content_headers.split("\n").inject(""){|r,e| r += "

    #{e}

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

    #{e}

    "} + 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}
    "} + ruby_hash.to_html )