class Object

Constants

CONTENT_TYPE_MAPPING

Map extensions to their content type

DEFAULT_CONTENT_TYPE

Treat as binary data if content type cannot be found

SERVER_PORT
WEB_ROOT

Files will be served from this directory

Public Instance Methods

content_type(path) click to toggle source

This helper function parses the extension of the requested file and then looks up its content type.

# File lib/serveall.rb, line 34
def content_type(path)
  ext = File.extname(path).split(".").last
  CONTENT_TYPE_MAPPING.fetch(ext, DEFAULT_CONTENT_TYPE)
end
requested_file(request_line) click to toggle source

This helper function parses the Request-Line and generates a path to a file on the server.

# File lib/serveall.rb, line 41
def requested_file(request_line)
  request_uri = request_line.split(" ")[1]
  path = URI.unescape(URI(request_uri).path)

  clean = []
  parts = path.split("/")

  parts.each do |part|
    next if part.empty? || part == "."
    part == '..' ? clean.pop : clean << part
  end
  File.join(WEB_ROOT, *clean)
end