2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-07-23 12:51:36 -04:00
|
|
|
# erbhandler.rb -- ERBHandler Class
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-07-23 12:51:36 -04:00
|
|
|
# Author: IPR -- Internet Programming with Ruby -- writers
|
|
|
|
# Copyright (c) 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
|
|
|
|
# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
|
|
|
|
# reserved.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-07-23 12:51:36 -04:00
|
|
|
# $IPR: erbhandler.rb,v 1.25 2003/02/24 19:25:31 gotoyuzo Exp $
|
|
|
|
|
|
|
|
require 'webrick/httpservlet/abstract.rb'
|
|
|
|
|
|
|
|
require 'erb'
|
|
|
|
|
|
|
|
module WEBrick
|
|
|
|
module HTTPServlet
|
|
|
|
|
2011-05-12 18:37:01 -04:00
|
|
|
##
|
|
|
|
# ERBHandler evaluates an ERB file and returns the result. This handler
|
|
|
|
# is automatically used if there are .rhtml files in a directory served by
|
|
|
|
# the FileHandler.
|
|
|
|
#
|
|
|
|
# ERBHandler supports GET and POST methods.
|
|
|
|
#
|
|
|
|
# The ERB file is evaluated with the local variables +servlet_request+ and
|
|
|
|
# +servlet_response+ which are a WEBrick::HTTPRequest and
|
|
|
|
# WEBrick::HTTPResponse respectively.
|
|
|
|
#
|
|
|
|
# Example .rhtml file:
|
|
|
|
#
|
|
|
|
# Request to <%= servlet_request.request_uri %>
|
|
|
|
#
|
|
|
|
# Query params <%= servlet_request.query.inspect %>
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
class ERBHandler < AbstractServlet
|
2011-05-12 18:37:01 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new ERBHandler on +server+ that will evaluate and serve the
|
|
|
|
# ERB file +name+
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def initialize(server, name)
|
2005-08-07 16:16:15 -04:00
|
|
|
super(server, name)
|
2003-07-23 12:51:36 -04:00
|
|
|
@script_filename = name
|
|
|
|
end
|
|
|
|
|
2011-05-12 18:37:01 -04:00
|
|
|
##
|
|
|
|
# Handles GET requests
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def do_GET(req, res)
|
|
|
|
unless defined?(ERB)
|
|
|
|
@logger.warn "#{self.class}: ERB not defined."
|
|
|
|
raise HTTPStatus::Forbidden, "ERBHandler cannot work."
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
data = open(@script_filename){|io| io.read }
|
|
|
|
res.body = evaluate(ERB.new(data), req, res)
|
2011-06-16 01:37:31 -04:00
|
|
|
res['content-type'] ||=
|
2004-08-09 02:04:59 -04:00
|
|
|
HTTPUtils::mime_type(@script_filename, @config[:MimeTypes])
|
2014-06-26 05:05:58 -04:00
|
|
|
rescue StandardError
|
2003-07-23 12:51:36 -04:00
|
|
|
raise
|
|
|
|
rescue Exception => ex
|
|
|
|
@logger.error(ex)
|
|
|
|
raise HTTPStatus::InternalServerError, ex.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-12 18:37:01 -04:00
|
|
|
##
|
|
|
|
# Handles POST requests
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
alias do_POST do_GET
|
|
|
|
|
|
|
|
private
|
2011-05-12 18:37:01 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Evaluates +erb+ providing +servlet_request+ and +servlet_response+ as
|
|
|
|
# local variables.
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def evaluate(erb, servlet_request, servlet_response)
|
|
|
|
Module.new.module_eval{
|
2010-11-08 15:59:01 -05:00
|
|
|
servlet_request.meta_vars
|
|
|
|
servlet_request.query
|
2003-07-23 12:51:36 -04:00
|
|
|
erb.result(binding)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|