Class: Httpserver::WebServer

Inherits:
Object
  • Object
show all
Includes:
Mime, Response
Defined in:
lib/httpserver/webserver.rb

Instance Method Summary collapse

Methods included from Response

#response

Methods included from Mime

#get_content_type

Constructor Details

#initializeWebServer

Returns a new instance of WebServer.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/httpserver/webserver.rb', line 7

def initialize
  port = ARGV.first.to_i
  port = 8080 if port == 0
  webserver = TCPServer.new('0.0.0.0', port)
  puts "Listening on http://0.0.0.0:#{port}"
  puts "Ctrl + c will break"
  base_dir = Dir.new(".")

  while (session = webserver.accept)
    request = session.gets.gsub(/GET\ \//, '').gsub(/\ HTTP.*/, '').chomp
    resource = request
    resource = "." if resource == ""
    
    # Is the user trying to go to a file that doesn't exist?
    if !File.exists?(resource)
      session.print response(code:404, content:"404 - Resource cannot be found.")
      session.close
      next
    end

    # if the resource is a directory, lets print out the contents
    if File.directory?(resource)
      session.print response(code:200)
      base_dir = Dir.new("./#{request}")

      base_dir.entries.each do |file|
        dir_sign = ""
        base_path = resource + "/"
        base_path = "" if resource == ""
        resource_path = base_path + file
        dir_sign = "/" if File.directory?(resource_path)

        # If this entry is the ".." then build the breadcrumb directory
        if file == ".."
          breadcrumb = "/" + base_path.split('/')[0..-2].join('/')
          session.print("<a href='#{breadcrumb}'>#{file}</a><br />")
        else
          # Otherwise, link to the file
          session.print("<a href='/#{resource_path}'>#{file}#{dir_sign}</a><br />")
        end
      end
    else
      # We are looking at a piece of content here!
      session.print response(:code => 200, :mime => get_content_type(resource))
      File.open(resource, "rb") do |file|
        while (!file.eof?) do
          buffer = file.read(256)
          session.write(buffer)
        end
      end
    end
    session.close
  end
end