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



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

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

#call!(env) ⇒ Object

Rack Interface

Parameters:

  • env

    Rack environment



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/middleman-core/core_extensions/request.rb', line 193

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:



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

def current_path
  Thread.current[:current_path]
end

#current_path=(path) ⇒ void

This method returns an undefined value.

Set the current path

Parameters:

  • path (String)

    The new 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

Parameters:

  • response (String)

    Response value



213
214
215
# File 'lib/middleman-core/core_extensions/request.rb', line 213

def halt(response)
  throw :halt, response
end

#mime_type(type, value) ⇒ void

This method returns an undefined value.

Add a new mime-type for a specific extension

Parameters:

  • type (Symbol)

    File extension

  • value (String)

    Mime type



275
276
277
278
# File 'lib/middleman-core/core_extensions/request.rb', line 275

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



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

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.

Parameters:

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


224
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
# File 'lib/middleman-core/core_extensions/request.rb', line 224

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 = full_path(request_path)

  # 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)


179
180
181
# File 'lib/middleman-core/core_extensions/request.rb', line 179

def req
  Thread.current[:req]
end

#req=(value) ⇒ Object



182
183
184
# File 'lib/middleman-core/core_extensions/request.rb', line 182

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

#requestObject

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

Parameters:

  • path (String)

    File to send



290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/middleman-core/core_extensions/request.rb', line 290

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