module Merb module ControllerMixin # redirect to another url It can be like /foo/bar # for redirecting within your same app. Or it can # be a fully qualified url to another site. def redirect(url) MERB_LOGGER.info("Redirecting to: #{url}") @status = 302 headers.merge!({'Location'=> url}) return '' end # pass in a path to a file and this will set the # right headers and let mongrel do its thang and # serve the static file directly. def send_file(file, opts={}) opts.update(Merb::Const::DEFAULT_SEND_FILE_OPTIONS.merge(opts)) [:type, :disposition].each do |arg| raise ArgumentError, ":#{arg} option required" if opts[arg].nil? end disposition = opts[:disposition].dup || 'attachment' disposition << %(; filename="#{opts[:filename] ? opts[:filename] : File.basename(file)}") headers.update( 'Content-Type' => opts[:type].strip, # fixes a problem with extra '\r' with some browsers 'Content-Disposition' => disposition, 'Content-Transfer-Encoding' => 'binary', 'X-SENDFILE' => file ) return end # This uses nginx X-Accel-Redirect header to send # a file directly from nginx. See the nginx wiki: # http://wiki.codemongers.com/NginxXSendfile def nginx_send_file(file) headers['X-Accel-Redirect'] = file return end # parses a query string or the payload of a POST # request into the params hash. So for example: # /foo?bar=nik&post[title]=heya&post[body]=whatever # parses into: # {:bar => 'nik', :post => {:title => 'heya', :body => 'whatever}} def query_parse(qs, d = '&;') m = proc {|_,o,n|o.u(n,&m)rescue([*o]<"']/) { |s| Merb::Const::ESCAPE_TABLE[s] } end alias h escape_xml alias html_escape escape_xml # does url escaping def escape(s) Mongrel::HttpRequest.escape(s) end # does url unescaping def unescape(s) Mongrel::HttpRequest.unescape(s) end end end