Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- # devilzc0de.org (c) 2012
- require 'socket'
- require 'uri'
- port = 13123
- t = Thread.new do
- server = TCPServer.new Integer(port)
- begin
- while(client = server.accept)
- headers = ["HTTP/1.1 200 OK",
- "Server: Ruby"]
- req = URI.unescape(client.gets)
- print req
- path = "/"
- if req =~ /GET .* HTTP.*/
- path = req.gsub(/GET /, '').gsub(/ HTTP.*/, '')
- path = path.strip
- if File.directory?("#{path}")
- if not path.end_with?("/")
- path = path + "/"
- end
- resp = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">
- <html>
- <title>Directory listing for #{path}</title>
- <body>
- <h2>Directory listing for #{path}</h2>
- <hr><ul>"
- Dir.foreach("#{path}").sort.each{ |x|
- if File.directory?("#{path}#{x}")
- if x != "." and x != ".."
- resp += "<li><a href=\"#{path}#{x}/\">#{x}/</a></li>\r\n"
- end
- else
- resp += "<li><a href=\"#{path}#{x}\">#{x}</a></li>\r\n"
- end
- }
- resp += "</ul><hr>
- </body>
- </html>"
- conlen = resp.length
- contype = "text/html"
- print "Dir : #{path}\r\n"
- elsif File.file?("#{path}")
- conlen = File.size("#{path}")
- contype = "text/plain"
- print "File : #{path} (#{conlen})\r\n"
- end
- print "contype : #{contype}\r\n"
- print "conlen : #{conlen}\r\n"
- headers.push("Content-Type: #{contype}")
- headers.push("Content-Length: #{conlen}\r\n\r\n")
- client.print headers.join("\r\n")
- if File.directory?("#{path}")
- client.print resp
- elsif File.file?("#{path}")
- src = File.open("#{path}", "rb")
- while not src.eof?
- buffer = src.read(256)
- client.write(buffer)
- end
- src.close
- src = nil
- end
- client.close
- end
- end
- end
- end
- STDIN.getc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement