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

Instance Method Details

#call(env) ⇒ Object



194
195
196
# File 'lib/middleman-core/core_extensions/request.rb', line 194

def call(env)
  dup.call!(env)
end

#call!(env) ⇒ Object

Rack Interface

Parameters:

  • env

    Rack environment



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/middleman-core/core_extensions/request.rb', line 201

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_pathString

Accessor for current path

Returns:

  • (String)


165
166
167
# File 'lib/middleman-core/core_extensions/request.rb', line 165

def current_path
  Thread.current[:current_path]
end

#current_path=(path)

This method returns an undefined value.

Set the current path

Parameters:

  • path (String)

    The new current path



173
174
175
176
177
178
179
# File 'lib/middleman-core/core_extensions/request.rb', line 173

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

Parameters:

  • response (String)

    Response value



221
222
223
# File 'lib/middleman-core/core_extensions/request.rb', line 221

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

Parameters:

  • type (Symbol)

    File extension

  • value (String)

    Mime type



283
284
285
286
# File 'lib/middleman-core/core_extensions/request.rb', line 283

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



289
290
291
292
293
# File 'lib/middleman-core/core_extensions/request.rb', line 289

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.

Parameters:

  • env
  • req (Rack::Request)
  • res (Rack::Response)


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
273
274
275
276
# File 'lib/middleman-core/core_extensions/request.rb', line 232

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

#reqRack::Request

Rack request

Returns:

  • (Rack::Request)


186
187
188
# File 'lib/middleman-core/core_extensions/request.rb', line 186

def req
  Thread.current[:req]
end

#req=(value) ⇒ Object



190
191
192
# File 'lib/middleman-core/core_extensions/request.rb', line 190

def req=(value)
  Thread.current[:req] = value
end

#requestObject

Backwards-compatibility with old request.path signature



159
160
161
# File 'lib/middleman-core/core_extensions/request.rb', line 159

def request
  Thread.current[:legacy_request]
end

#send_file(resource, env) ⇒ Object

Immediately send static file



296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/middleman-core/core_extensions/request.rb', line 296

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