Top Level Namespace

Constant Summary collapse

WEB_ROOT =
'./public'
CONTENT_TYPE_MAPPING =
{
  'html' => 'text/html',
  'txt' => 'text/plain',
  'png' => 'image/png',
  'jpg' => 'image/jpg'
}
DEFAULT_CONTENT_TYPE =
'application/octet-stream'

Instance Method Summary collapse

Instance Method Details

#content_type(path) ⇒ Object



15
16
17
18
# File 'lib/rubyserver.rb', line 15

def content_type(path)
  ext = File.extname(path).split(".").last
  CONTENT_TYPE_MAPPING.fetch(ext, DEFAULT_CONTENT_TYPE)
end

#requested_file(request_line) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rubyserver.rb', line 20

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