Advertisement
parkdream1

ruby.rb

Mar 6th, 2012
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.79 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. # devilzc0de.org (c) 2012
  3. require 'socket'
  4. require 'uri'
  5.  
  6. port = 13123
  7.  
  8. t = Thread.new do
  9.     server = TCPServer.new Integer(port)
  10.     begin
  11.         while(client = server.accept)
  12.             headers = ["HTTP/1.1 200 OK",
  13.                      "Server: Ruby"]
  14.             req = URI.unescape(client.gets)
  15.             print req
  16.  
  17.             path = "/"
  18.             if req =~ /GET .* HTTP.*/
  19.                 path = req.gsub(/GET /, '').gsub(/ HTTP.*/, '')
  20.                 path = path.strip
  21.                 if File.directory?("#{path}")
  22.                     if not path.end_with?("/")
  23.                         path = path + "/"
  24.                     end
  25.                    
  26.                     resp = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">
  27.                            <html>
  28.                            <title>Directory listing for #{path}</title>
  29.                            <body>
  30.                            <h2>Directory listing for #{path}</h2>
  31.                            <hr><ul>"
  32.  
  33.                    
  34.                     Dir.foreach("#{path}").sort.each{ |x|
  35.                         if File.directory?("#{path}#{x}")
  36.                             if x != "." and x != ".."
  37.                                 resp += "<li><a href=\"#{path}#{x}/\">#{x}/</a></li>\r\n"
  38.                             end
  39.                         else
  40.                             resp += "<li><a href=\"#{path}#{x}\">#{x}</a></li>\r\n"
  41.                         end
  42.                     }
  43.                     resp += "</ul><hr>
  44.                            </body>
  45.                            </html>"
  46.                            
  47.                     conlen = resp.length
  48.                     contype = "text/html"
  49.                     print "Dir : #{path}\r\n"
  50.                 elsif File.file?("#{path}")
  51.                     conlen = File.size("#{path}")
  52.                     contype = "text/plain"
  53.                     print "File : #{path} (#{conlen})\r\n"
  54.                 end
  55.  
  56.                 print "contype : #{contype}\r\n"
  57.                 print "conlen : #{conlen}\r\n"
  58.                
  59.                 headers.push("Content-Type: #{contype}")
  60.                 headers.push("Content-Length: #{conlen}\r\n\r\n")
  61.                
  62.                 client.print headers.join("\r\n")
  63.                 if File.directory?("#{path}")
  64.                     client.print resp
  65.                 elsif File.file?("#{path}")
  66.                     src = File.open("#{path}", "rb")
  67.                     while not src.eof?
  68.                         buffer = src.read(256)
  69.                         client.write(buffer)
  70.                     end
  71.                     src.close
  72.                     src = nil
  73.                 end
  74.                 client.close
  75.             end
  76.         end
  77.     end
  78. end
  79. STDIN.getc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement