Class: SimpleServer

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/simple-server.rb

Constant Summary collapse

VERSION =
'0.3'

Instance Method Summary collapse

Instance Method Details

#parse_request(request) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/simple-server.rb', line 27

def parse_request(request)
  begin
    uri = Addressable::URI.parse(request.url)
  rescue Addressable::URI::InvalidURIError => e
    logger.info "#{uri}: invalid URI (#{e.message}): denying"
    halt 400, 'invalid URI'
  end
  uri.host = request.host
  uri
end

#path_for_uri(uri) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/simple-server.rb', line 19

def path_for_uri(uri)
  path = Path.new(settings.root)
  path /= (uri.normalized_host ||= settings.default_host).sub(/^(www|web).*?\./, '') if settings.multihosting
  path /= Path.new(Addressable::URI.unencode_component(uri.normalized_path)).relative_to('/')
  path /= 'index.html' if uri.normalized_path[-1] == '/'
  path
end