lib/slinky/server.rb in slinky-0.6.1 vs lib/slinky/server.rb in slinky-0.7.0

- old
+ new

@@ -6,11 +6,11 @@ def self.dir= _dir; @dir = _dir; end # Gets the root directory from which files should be served def self.dir; @dir || "."; end def self.config= _config; @config = _config; end - def self.config; @config || {}; end + def self.config; @config || ConfigReader.empty; end def self.manifest= _manifest; @manifest = _manifest; end def self.manifest; @manifest; end # Splits a uri into its components, returning only the path sans @@ -18,21 +18,51 @@ def self.path_for_uri uri _, _, _, _, _, path, _, _ = URI.split uri path[1..-1] #get rid of the leading / end + # Method called for every HTTP request made + def process_http_request + resp = EventMachine::DelegatedHttpResponse.new(self) + + begin + path = Server.path_for_uri(@http_request_uri) + rescue + resp.status = 500 + resp.content = "Invalid request" + return + end + + Server.process_path(resp, path).send_response + end + + def self.process_path resp, path, pushstate = false + file = manifest.find_by_path(path).first + if file.is_a? ManifestDir + file = manifest.find_by_path(path+"/index.html").first + end + + resp.content_type MIME::Types.type_for(path).first + + if file + handle_file(resp, file) + elsif !pushstate && p = config.pushstate_for_path("/" + path) + path = p[0] == "/" ? p[1..-1] : p + self.process_path(resp, path, true) + else + not_found resp + end + resp + end + # Takes a manifest file and produces a response for it def self.handle_file resp, mf - if mf - if path = mf.process - serve_file resp, path.to_s - else - resp.status = 500 - resp.content = "Error compiling #{mf.source}\n" - end + if path = mf.process + serve_file resp, path.to_s else - not_found resp + resp.status = 500 + resp.content = "Error compiling #{mf.source}\n" end resp end # Serves a file from the file system @@ -57,29 +87,8 @@ # Returns the proper response for files that do not exist def self.not_found resp resp.status = 404 resp.content = "File not found\n" - end - - # Method called for every HTTP request made - def process_http_request - manifest = Server.manifest - resp = EventMachine::DelegatedHttpResponse.new(self) - - begin - path = Server.path_for_uri(@http_request_uri) - rescue - resp.status = 500 - resp.content = "Invalid request" - return - end - - file = manifest.find_by_path(path).first - if file.is_a? ManifestDir - file = manifest.find_by_path(path+"/index.html").first - end - resp.content_type MIME::Types.type_for(path).first - Server.handle_file(resp, file).send_response end end end