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

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

Overview

Methods to be mixed-in to Middleman::Application

Instance Method Summary (collapse)

Instance Method Details

- (Object) call(env)



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'middleman-core/lib/middleman-core/core_extensions/request.rb', line 214

def call(env)
  # Keep `__middleman__` messaging to this thread
  if env["PATH_INFO"] == "/__middleman__"
    if env["REQUEST_METHOD"] == "POST"
      req = ::Rack::Request.new(env)
      if req.params.has_key?("change")
        self.files.did_change(req.params["change"])
      elsif req.params.has_key?("delete")
        self.files.did_delete(req.params["delete"])
      end
    end
    
    res = ::Rack::Response.new
    res.status = 200
    return res.finish
  end
  
  dup.call!(env)
end

- (Object) call!(env)

Rack Interface

Parameters:

  • env

    Rack environment



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'middleman-core/lib/middleman-core/core_extensions/request.rb', line 237

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

  puts "== Request: #{env["PATH_INFO"]}" if logging?

  # Catch :halt exceptions and use that response if given
  catch(:halt) do
    process_request(env, req, res)

    res.status = 404
    res.finish
  end
end

- (void) content_type(res, type, params = {})

This method returns an undefined value.

Set the content type for the current request

Parameters:

  • type (String)

    Content type

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


357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'middleman-core/lib/middleman-core/core_extensions/request.rb', line 357

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

- (String) current_path

Accessor for current path

Returns:

  • (String)


169
170
171
# File 'middleman-core/lib/middleman-core/core_extensions/request.rb', line 169

def current_path
  Thread.current[:current_path]
end

- (void) current_path=(path)

This method returns an undefined value.

Set the current path

Parameters:

  • path (String)

    The new current path



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

def current_path=(path)
  Thread.current[:current_path] = path
  Thread.current[:request] = ::Thor::CoreExt::HashWithIndifferentAccess.new({ 
    :path   => path, 
    :params => req ? ::Thor::CoreExt::HashWithIndifferentAccess.new(req.params) : {} 
  })
end

- (Object) env

Rack env



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

def env
  Thread.current[:env]
end

- (Object) env=(value)



192
193
194
# File 'middleman-core/lib/middleman-core/core_extensions/request.rb', line 192

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

- (Object) halt(response)

Halt the current request and return a response

Parameters:

  • response (String)

    Response value



257
258
259
# File 'middleman-core/lib/middleman-core/core_extensions/request.rb', line 257

def halt(response)
  throw :halt, response
end

- (Object) map(*args, &block)



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

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

- (void) mime_type(type, value = nil)

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



322
323
324
325
326
327
# File 'middleman-core/lib/middleman-core/core_extensions/request.rb', line 322

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

- (Object) not_found(res)

Halt request and return 404



330
331
332
333
334
# File 'middleman-core/lib/middleman-core/core_extensions/request.rb', line 330

def not_found(res)
  res.status == 404
  res.write "<html><body><h1>File Not Found</h1><p>#{@request_path}</p></body>"
  res.finish
end

- (Object) process_request(env, req, res)

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)


268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'middleman-core/lib/middleman-core/core_extensions/request.rb', line 268

def process_request(env, req, res)
  start_time = Time.now

  # Normalize the path and add index if we're looking at a directory
  original_path = URI.decode(env["PATH_INFO"].dup)
  if original_path.respond_to? :force_encoding
    original_path.force_encoding('UTF-8')
  end
  request_path  = full_path(original_path)

  # Run before callbacks
  run_hook :before

  if original_path != request_path
    # Get the resource object for this path
    resource = sitemap.find_resource_by_destination_path(original_path)
  end

  # Get the resource object for this full path
  resource ||= sitemap.find_resource_by_destination_path(request_path)

  # Return 404 if not in sitemap
  return not_found(res) unless resource && !resource.ignored?

  # If this path is a static file, send it immediately
  return send_file(resource.source_file, env, res) unless resource.template?

  # Set the current path for use in helpers
  self.current_path = request_path.dup

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

  begin
    # Write out the contents of the page
    res.write resource.render

    # 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
  puts "== Finishing Request: #{self.current_path} (#{(Time.now - start_time).round(2)}s)" if logging?
  halt res.finish
end

- (Rack::Request) req

Rack request

Returns:

  • (Rack::Request)


198
199
200
# File 'middleman-core/lib/middleman-core/core_extensions/request.rb', line 198

def req
  Thread.current[:req]
end

- (Object) req=(value)



201
202
203
# File 'middleman-core/lib/middleman-core/core_extensions/request.rb', line 201

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

- (Object) request

Backwards-compatibility with old request.path signature



163
164
165
# File 'middleman-core/lib/middleman-core/core_extensions/request.rb', line 163

def request
  Thread.current[:request]
end

- (Rack::Response) res

Rack response

Returns:

  • (Rack::Response)


207
208
209
# File 'middleman-core/lib/middleman-core/core_extensions/request.rb', line 207

def res
  Thread.current[:res]
end

- (Object) res=(value)



210
211
212
# File 'middleman-core/lib/middleman-core/core_extensions/request.rb', line 210

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

- (Object) send_file(path, env, res)

Immediately send static file

Parameters:

  • path (String)

    File to send



339
340
341
342
343
344
345
346
347
348
349
350
# File 'middleman-core/lib/middleman-core/core_extensions/request.rb', line 339

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

- (Object) use(*args, &block)



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

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