lib/plezi/helpers/http_sender.rb in plezi-0.12.13 vs lib/plezi/helpers/http_sender.rb in plezi-0.12.14

- old
+ new

@@ -1,32 +1,35 @@ module Plezi module Base # Sends common basic HTTP responses. module HTTPSender - class CodeContext - attr_accessor :request - def initialize request - @request = request + class ErrorCtrl + include ::Plezi::Base::ControllerCore + include ::Plezi::ControllerMagic + + def index + render(response.status.to_s) || (params[:format] && (params[:format] != 'html'.freeze) && render(response.status.to_s, format: 'html'.freeze)) || ((response['content-type'.freeze] = 'text/plain'.freeze) && response.class::STATUS_CODES[response.status]) end + def requested_method + :index + end end + module_function ###### ## basic responses ## (error codes and static files) # sends a response for an error code, rendering the relevent file (if exists). def send_by_code request, response, code, headers = {} begin - base_code_path = request[:host_settings][:templates] || File.expand_path('.') - fn = File.join(base_code_path, "#{code}.html") - rendered = ::Plezi::Renderer.render fn, binding #CodeContext.new(request) - return send_raw_data request, response, rendered, 'text/html', code, headers if rendered - return send_file(request, response, fn, code, headers) if Plezi.file_exists?(fn) - return true if send_raw_data(request, response, response.class::STATUS_CODES[code], 'text/plain', code, headers) - rescue Exception => e + response.status = code + headers.each {|k, v| response[k] = v} + return ErrorCtrl.new(request, response).index + rescue => e Plezi.error e end false end @@ -34,12 +37,12 @@ # # returns true if data was sent. def send_static_file request, response root = request[:host_settings][:public] return false unless root - file_requested = request[:path].to_s.split('/') - unless file_requested.include? '..' + file_requested = request[:path].to_s.split('/'.freeze) + unless file_requested.include? '..'.freeze file_requested.shift file_requested = File.join(root, *file_requested) return true if send_file request, response, file_requested return send_file request, response, File.join(file_requested, request[:host_settings][:index_file]) end @@ -60,17 +63,15 @@ end # sends raw data through the connection. always returns true (data send). def send_raw_data request, response, data, mime, status_code = 200, headers = {} headers.each {|k, v| response[k] = v} response.status = status_code if response.status == 200 # avoid resetting a manually set status - response['content-type'] = mime - response['cache-control'] ||= 'public, max-age=86400' + response['content-type'.freeze] = mime + response['cache-control'.freeze] ||= 'public, max-age=86400'.freeze response.body = data # response['content-length'] = data.bytesize #this one is automated by the server and should be avoided to support Range requests. true - end########## - - + end end end end