module Middleman::CoreExtensions::Request::InstanceMethods
Methods to be mixed-in to Middleman::Application
Public Instance Methods
# File lib/middleman-core/core_extensions/request.rb, line 187 def call(env) dup.call!(env) end
Rack Interface
@param env Rack environment
# File lib/middleman-core/core_extensions/request.rb, line 194 def call!(env) # Store environment, request and response for later self.req = req = ::Rack::Request.new(env) res = ::Rack::Response.new logger.debug "== Request: #{env['PATH_INFO']}" # Catch :halt exceptions and use that response if given catch(:halt) do process_request(env, req, res) res.status = 404 res.finish end end
Accessor for current path @return [String]
# File lib/middleman-core/core_extensions/request.rb, line 158 def current_path Thread.current[:current_path] end
Set the current path
@param [String] path The new current path @return [void]
# File lib/middleman-core/core_extensions/request.rb, line 166 def current_path=(path) Thread.current[:current_path] = path Thread.current[:legacy_request] = ::Thor::CoreExt::HashWithIndifferentAccess.new( path: path, params: req ? ::Thor::CoreExt::HashWithIndifferentAccess.new(req.params) : {} ) end
Halt the current request and return a response
@param [String] response Response value
# File lib/middleman-core/core_extensions/request.rb, line 214 def halt(response) throw :halt, response end
Add a new mime-type for a specific extension
@param [Symbol] type File extension @param [String] value Mime type @return [void]
# File lib/middleman-core/core_extensions/request.rb, line 276 def mime_type(type, value) type = ".#{type}" unless type.to_s[0] == '.' ::Rack::Mime::MIME_TYPES[type] = value end
Halt request and return 404
# File lib/middleman-core/core_extensions/request.rb, line 282 def not_found(res, path) res.status = 404 res.write "<html><head></head><body><h1>File Not Found</h1><p>#{path}</p></body></html>" res.finish end
Core response method. We process the request, check with the sitemap, and return the correct file, response or status message.
@param env @param [Rack::Request] req @param [Rack::Response] res
# File lib/middleman-core/core_extensions/request.rb, line 225 def process_request(env, req, res) start_time = Time.now current_path = nil request_path = URI.decode(env['PATH_INFO'].dup) if request_path.respond_to? :force_encoding request_path.force_encoding('UTF-8') end request_path = ::Middleman::Util.full_path(request_path, self) # Run before callbacks run_hook :before # Get the resource object for this path resource = sitemap.find_resource_by_destination_path(request_path) # Return 404 if not in sitemap return not_found(res, request_path) unless resource && !resource.ignored? # If this path is a binary file, send it immediately return send_file(resource, env) if resource.binary? current_path = resource.destination_path res['Content-Type'] = resource.content_type || 'text/plain' begin # Write out the contents of the page output = resource.render do self.req = req self.current_path = current_path end res.write output # Valid content is a 200 status res.status = 200 rescue Middleman::CoreExtensions::Rendering::TemplateNotFound => e res.write "Error: #{e.message}" res.status = 500 end # End the request logger.debug "== Finishing Request: #{current_path} (#{(Time.now - start_time).round(2)}s)" halt res.finish end
Rack request @return [Rack::Request]
# File lib/middleman-core/core_extensions/request.rb, line 179 def req Thread.current[:req] end
# File lib/middleman-core/core_extensions/request.rb, line 183 def req=(value) Thread.current[:req] = value end
Backwards-compatibility with old request.path signature
# File lib/middleman-core/core_extensions/request.rb, line 152 def request Thread.current[:legacy_request] end
Immediately send static file
# File lib/middleman-core/core_extensions/request.rb, line 289 def send_file(resource, env) file = ::Rack::File.new nil file.path = resource.source_file response = file.serving(env) status = response[0] response[1]['Content-Encoding'] = 'gzip' if %w(.svgz .gz).include?(resource.ext) # Do not set Content-Type if status is 1xx, 204, 205 or 304, otherwise # Rack will throw an error (500) if !(100..199).include?(status) && ![204, 205, 304].include?(status) response[1]['Content-Type'] = resource.content_type || 'application/octet-stream' end halt response end