Module: Middleman::CoreExtensions::Request::InstanceMethods

Defined in:
lib/middleman-core/core_extensions/request.rb

Overview

Methods to be mixed-in to Middleman::Application

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_pathString

Accessor for current path

Returns:

  • (String)


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

def current_path
  @current_path
end

#envObject

Rack env



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

def env
  @env
end

#reqRack::Request

Rack request

Returns:

  • (Rack::Request)


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

def req
  @req
end

#requestObject (readonly)

Backwards-compatibility with old request.path signature



155
156
157
# File 'lib/middleman-core/core_extensions/request.rb', line 155

def request
  @request
end

#resRack::Response

Rack response

Returns:

  • (Rack::Response)


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

def res
  @res
end

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

Parameters:

  • env

    Rack environment



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

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

#content_type(res, type, params = {}) ⇒ void

This method returns an undefined value.

Set the content type for the current request

Parameters:

  • type (String)

    Content type

  • params (Hash) (defaults to: {})


314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/middleman-core/core_extensions/request.rb', line 314

def content_type(res, type, params={})
  return 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

#halt(response) ⇒ Object

Halt the current request and return a response

Parameters:

  • response (String)

    Response value



215
216
217
# File 'lib/middleman-core/core_extensions/request.rb', line 215

def halt(response)
  throw :halt, response
end

#map(*args, &block) ⇒ Object



174
# File 'lib/middleman-core/core_extensions/request.rb', line 174

def map(*args, &block); self.class.map(*args, &block); end

#mime_type(type, value = nil) ⇒ void

This method returns an undefined value.

Add a new mime-type for a specific extension

Parameters:

  • type (Symbol)

    File extension

  • value (String) (defaults to: nil)

    Mime type



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

def mime_type(type, value=nil)
  return type if type.nil? || type.to_s.include?('/')
  return ::Rack::Mime.mime_type('.txt') if type.empty?
  type = ".#{type}" unless type.to_s[0] == ?.
  return ::Rack::Mime.mime_type(type, nil) unless value
  ::Rack::Mime::MIME_TYPES[type] = value
end

#not_found(res, path) ⇒ Object

Halt request and return 404



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

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)


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

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.source_file, env, res) if resource.binary?

  current_path = resource.destination_path

  # Set a HTTP content type based on the request's extensions
  content_type(res, mime_type(resource.ext))

  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

#send_file(path, env, res) ⇒ Object

Immediately send static file

Parameters:

  • path (String)

    File to send



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

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

#use(*args, &block) ⇒ Object



173
# File 'lib/middleman-core/core_extensions/request.rb', line 173

def use(*args, &block); self.class.use(*args, &block); end