Changeset 793
- Timestamp:
- 10/30/07 14:22:26 (1 year ago)
- Files:
-
- trunk/lib/merb/mongrel_handler.rb (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/lib/merb/mongrel_handler.rb
r745 r793 2 2 NO_CLOSE_STATUS_FORMAT = "HTTP/1.1 %d %s\r\n".freeze 3 3 def send_status_no_connection_close(content_length=@body.length) 4 if not@status_sent4 unless @status_sent 5 5 @header['Content-Length'] = content_length unless @status == 304 6 6 write(NO_CLOSE_STATUS_FORMAT % [@status, Mongrel::HTTP_STATUS_CODES[@status]]) … … 11 11 12 12 class MerbHandler < Mongrel::HttpHandler 13 @@file_only_methods = ["GET","HEAD"]14 @@path_prefix = nil13 @@file_only_methods = ["GET","HEAD"] 14 @@path_prefix = nil 15 15 @@path_prefix_original = nil 16 16 … … 20 20 def path_prefix=(prefix) 21 21 @@path_prefix_original = prefix 22 @@path_prefix = (prefix.is_a?(String) ? Regexp.new("^" + prefix.escape_regexp): prefix)22 @@path_prefix = (prefix.is_a?(String) ? /^#{prefix.escape_regexp}/ : prefix) 23 23 end 24 24 end … … 40 40 # serve the request. 41 41 # 4. After the controller has done its thing, we check for the X-SENDFILE 42 # header. if you set this header to the path toa file in your controller42 # header. if you set this header to the path of a file in your controller 43 43 # then mongrel will serve the file directly and your controller can go on 44 44 # processing other requests. 45 def process(request, response) 46 47 start = Time.now 45 def process(request, response) 46 start = Time.now 48 47 benchmarks = {} 49 48 50 if response.socket.closed? 51 return 52 end 49 return if response.socket.closed? 53 50 54 MERB_LOGGER.info("\nRequest: REQUEST_URI: #{request.params[Mongrel::Const::REQUEST_URI]} (#{Time.now.strftime("%Y-%m-%d %H:%M:%S")})") 51 MERB_LOGGER.info("\nRequest: REQUEST_URI: #{ 52 request.params[Mongrel::Const::REQUEST_URI]} (#{Time.now.strftime("%Y-%m-%d %H:%M:%S")})") 55 53 56 54 # Truncate the request URI if there's a path prefix so that an app can be … … 75 73 get_or_head = @@file_only_methods.include? request.params[Mongrel::Const::REQUEST_METHOD] 76 74 77 if get_or_head and@files.can_serve(path_info)75 if get_or_head && @files.can_serve(path_info) 78 76 # File exists as-is so serve it up 79 77 MERB_LOGGER.info("Serving static file: #{path_info}") 80 78 @files.process(request,response) 81 elsif get_or_head and@files.can_serve(page_cached)79 elsif get_or_head && @files.can_serve(page_cached) 82 80 # Possible cached page, serve it up 83 81 MERB_LOGGER.info("Serving static file: #{page_cached}") … … 86 84 else 87 85 # Let Merb:Dispatcher find the route and call the filter chain and action 88 controller, action = Merb::Dispatcher.handle(request, response)86 controller, action = Merb::Dispatcher.handle(request, response) 89 87 benchmarks.merge!(controller._benchmarks) 90 88 benchmarks[:controller] = controller.class.to_s 91 benchmarks[:action] = action89 benchmarks[:action] = action 92 90 93 91 MERB_LOGGER.info("Routing to controller: #{controller.class} action: #{action}\nRoute Recognition & Parsing HTTP Input took: #{benchmarks[:setup_time]} seconds") 94 92 95 93 sendfile, clength = nil 96 response.status = controller.status94 response.status = controller.status 97 95 # Check for the X-SENDFILE header from your Merb::Controller and serve 98 96 # the file directly instead of buffering. … … 111 109 if sendfile 112 110 benchmarks[:sendfile_time] = Time.now - start 113 MERB_LOGGER.info("X-SENDFILE: #{sendfile}\nComplete Request took: #{benchmarks[:sendfile_time]} seconds") 114 file_status = File.stat(sendfile) 111 MERB_LOGGER.info("X-SENDFILE: #{sendfile}\nComplete Request took: #{ 112 benchmarks[:sendfile_time]} seconds") 113 file_status = File.stat(sendfile) 115 114 response.status = 200 116 115 # Set the last modified times as well and etag for all files … … 156 155 ensure 157 156 MERB_LOGGER.flush 158 end 159 157 end 160 158 end
