Module: Middleman::CoreExtensions::Request::InstanceMethods
- Defined in:
- lib/middleman-core/core_extensions/request.rb
Overview
Methods to be mixed-in to Middleman::Application
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#call!(env) ⇒ Object
Rack Interface.
-
#current_path ⇒ String
Accessor for current path.
-
#current_path=(path)
Set the current path.
-
#halt(response) ⇒ Object
Halt the current request and return a response.
-
#mime_type(type, value)
Add a new mime-type for a specific extension.
-
#not_found(res, path) ⇒ Object
Halt request and return 404.
-
#process_request(env, req, res) ⇒ Object
Core response method.
-
#req ⇒ Rack::Request
Rack request.
- #req=(value) ⇒ Object
-
#request ⇒ Object
Backwards-compatibility with old request.path signature.
-
#send_file(resource, env) ⇒ Object
Immediately send static file.
Instance Method Details
#call(env) ⇒ Object
190 191 192 |
# File 'lib/middleman-core/core_extensions/request.rb', line 190 def call(env) dup.call!(env) end |
#call!(env) ⇒ Object
Rack Interface
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/middleman-core/core_extensions/request.rb', line 197 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 |
#current_path ⇒ String
Accessor for current path
162 163 164 |
# File 'lib/middleman-core/core_extensions/request.rb', line 162 def current_path Thread.current[:current_path] end |
#current_path=(path)
This method returns an undefined value.
Set the current path
170 171 172 173 174 175 176 |
# File 'lib/middleman-core/core_extensions/request.rb', line 170 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(response) ⇒ Object
Halt the current request and return a response
217 218 219 |
# File 'lib/middleman-core/core_extensions/request.rb', line 217 def halt(response) throw :halt, response end |
#mime_type(type, value)
This method returns an undefined value.
Add a new mime-type for a specific extension
279 280 281 282 |
# File 'lib/middleman-core/core_extensions/request.rb', line 279 def mime_type(type, value) type = ".#{type}" unless type.to_s[0] == ?. ::Rack::Mime::MIME_TYPES[type] = value end |
#not_found(res, path) ⇒ Object
Halt request and return 404
285 286 287 288 289 |
# File 'lib/middleman-core/core_extensions/request.rb', line 285 def not_found(res, path) res.status = 404 res.write "<html><body><h1>File Not Found</h1><p>#{path}</p></body>" res.finish end |
#process_request(env, req, res) ⇒ Object
Core response method. We process the request, check with the sitemap, and return the correct file, response or status message.
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/middleman-core/core_extensions/request.rb', line 228 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.}" res.status = 500 end # End the request logger.debug "== Finishing Request: #{current_path} (#{(Time.now - start_time).round(2)}s)" halt res.finish end |
#req ⇒ Rack::Request
Rack request
183 184 185 |
# File 'lib/middleman-core/core_extensions/request.rb', line 183 def req Thread.current[:req] end |
#req=(value) ⇒ Object
186 187 188 |
# File 'lib/middleman-core/core_extensions/request.rb', line 186 def req=(value) Thread.current[:req] = value end |
#request ⇒ Object
Backwards-compatibility with old request.path signature
156 157 158 |
# File 'lib/middleman-core/core_extensions/request.rb', line 156 def request Thread.current[:legacy_request] end |
#send_file(resource, env) ⇒ Object
Immediately send static file
292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/middleman-core/core_extensions/request.rb', line 292 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 |