Module: Tonysserver
- Defined in:
- lib/tonysserver.rb,
lib/tonysserver/version.rb
Constant Summary collapse
- WEB_ROOT =
Files will be served from this directory
'./public'- CONTENT_TYPE_MAPPING =
Map extensions to their content type
{ 'html' => 'text/html', 'txt' => 'text/plain', 'png' => 'image/png', 'jpg' => 'image/jpeg' }
- DEFAULT_CONTENT_TYPE =
Treat as binary data if content type cannot be found
'application/octet-stream'- VERSION =
"0.0.3"
Instance Method Summary collapse
-
#content_type(path) ⇒ Object
This helper function parses the extension of the requested file and then looks up its content type.
-
#requested_file(request_line) ⇒ Object
This helper function parses the Request-Line and generates a path to a file on the server.
Instance Method Details
#content_type(path) ⇒ Object
This helper function parses the extension of the requested file and then looks up its content type.
24 25 26 27 |
# File 'lib/tonysserver.rb', line 24 def content_type(path) ext = File.extname(path).split(".").last CONTENT_TYPE_MAPPING.fetch(ext, DEFAULT_CONTENT_TYPE) end |
#requested_file(request_line) ⇒ Object
This helper function parses the Request-Line and generates a path to a file on the server.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/tonysserver.rb', line 32 def requested_file(request_line) request_uri = request_line.split(" ")[1] path = URI.unescape(URI(request_uri).path) clean = [] # Split the path into components parts = path.split("/") parts.each do |part| # skip any empty or current directory (".") path components next if part.empty? || part == '.' # If the path component goes up one directory level (".."), # remove the last clean component. # Otherwise, add the component to the Array of clean components part == '..' ? clean.pop : clean << part end # return the web root joined to the clean path File.join(WEB_ROOT, *clean) end |