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
187 188 189 |
# File 'lib/middleman-core/core_extensions/request.rb', line 187 def call(env) dup.call!(env) end |
#call!(env) ⇒ Object
Rack Interface
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# 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 |
#current_path ⇒ String
Accessor for current path
158 159 160 |
# File 'lib/middleman-core/core_extensions/request.rb', line 158 def current_path Thread.current[:current_path] end |
#current_path=(path)
This method returns an undefined value.
Set the current path
166 167 168 169 170 171 172 |
# 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(response) ⇒ Object
Halt the current request and return a response
214 215 216 |
# File 'lib/middleman-core/core_extensions/request.rb', line 214 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
276 277 278 279 |
# 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 |
#not_found(res, path) ⇒ Object
Halt request and return 404
282 283 284 285 286 |
# 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 |
#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.
225 226 227 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 |
# 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.}" 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
179 180 181 |
# File 'lib/middleman-core/core_extensions/request.rb', line 179 def req Thread.current[:req] end |
#req=(value) ⇒ Object
183 184 185 |
# File 'lib/middleman-core/core_extensions/request.rb', line 183 def req=(value) Thread.current[:req] = value end |
#request ⇒ Object
Backwards-compatibility with old request.path signature
152 153 154 |
# File 'lib/middleman-core/core_extensions/request.rb', line 152 def request Thread.current[:legacy_request] end |
#send_file(resource, env) ⇒ Object
Immediately send static file
289 290 291 292 293 294 295 296 297 298 299 300 301 |
# 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 |