module Middleman::CoreExtensions::Request::InstanceMethods
def call(env)
def call(env) dup.call!(env) end
def call!(env)
-
env
() -- Rack environment
def call!(env) self.env = env # Store environment, request and response for later self.req = req = ::Rack::Request.new(env) self.res = 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
def content_type(res, type, params={})
-
(void)
-
Parameters:
-
params
(Hash
) -- -
type
(String
) -- Content type
def content_type(res, type, params={}) return res['Content-Type'] unless type default = params.delete :default mime_type = mime_type(type) || default throw "Unknown media type: %p" % type if mime_type.nil? mime_type = mime_type.dup unless params.include? :charset params[:charset] = params.delete('charset') || "utf-8" end params.delete :charset if mime_type.include? 'charset' unless params.empty? mime_type << (mime_type.include?(';') ? ', ' : ';') mime_type << params.map { |kv| kv.join('=') }.join(', ') end res['Content-Type'] = mime_type end
def current_path=(path)
-
(void)
-
Parameters:
-
path
(String
) -- The new current path
def current_path=(path) @current_path = path @request = ::Thor::CoreExt::HashWithIndifferentAccess.new({ :path => path, :params => req ? ::Thor::CoreExt::HashWithIndifferentAccess.new(req.params) : {} }) end
def halt(response)
-
response
(String
) -- Response value
def halt(response) throw :halt, response end
def map(*args, &block); self.class.map(*args, &block); end
def map(*args, &block); self.class.map(*args, &block); end
def mime_type(type, value=nil)
-
(void)
-
Parameters:
-
value
(String
) -- Mime type -
type
(Symbol
) -- File extension
def mime_type(type, value=nil) return type if type.nil? || type.to_s.include?('/') type = ".#{type}" unless type.to_s[0] == ?. return ::Rack::Mime.mime_type(type, nil) unless value ::Rack::Mime::MIME_TYPES[type] = value end
def not_found(res)
def not_found(res) res.status == 404 res.write "<html><body><h1>File Not Found</h1><p>#{@request_path}</p></body>" res.finish end
def process_request(env, req, res)
-
res
(Rack::Response
) -- -
req
(Rack::Request
) -- -
env
() --
def process_request(env, req, res) start_time = Time.now @current_path = nil # Normalize the path and add index if we're looking at a directory original_path = URI.decode(env["PATH_INFO"].dup) if original_path.respond_to? :force_encoding original_path.force_encoding('UTF-8') end request_path = full_path(original_path) # Run before callbacks run_hook :before if original_path != request_path # Get the resource object for this path resource = sitemap.find_resource_by_destination_path(original_path) end # Get the resource object for this full path resource ||= sitemap.find_resource_by_destination_path(request_path) # Return 404 if not in sitemap return not_found(res) unless resource && !resource.ignored? # If this path is a static file, send it immediately return send_file(resource.source_file, env, res) unless resource.template? current_path = request_path.dup # Set a HTTP content type based on the request's extensions content_type(res, resource.mime_type) 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: #{request_path} (#{(Time.now - start_time).round(2)}s)" halt res.finish end
def send_file(path, env, res)
-
path
(String
) -- File to send
def send_file(path, env, res) extension = File.extname(path) matched_mime = mime_type(extension) matched_mime = "application/octet-stream" if matched_mime.nil? content_type res, matched_mime file = ::Rack::File.new nil file.path = path response = file.serving(env) response[1]['Content-Encoding'] = 'gzip' if %w(.svgz).include?(extension) halt response end
def use(*args, &block); self.class.use(*args, &block); end
def use(*args, &block); self.class.use(*args, &block); end