erubis-run.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. =begin
  2. = apache/erubis-run.rb
  3. Copyright (C) 2007 Andrew R Jackson <arjackson at acm dot org>
  4. Built from original by Shugo Maeda:
  5. Copyright (C) 2001 Shugo Maeda <shugo@modruby.net>
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions
  9. are met:
  10. 1. Redistributions of source code must retain the above copyright
  11. notice, this list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright
  13. notice, this list of conditions and the following disclaimer in the
  14. documentation and/or other materials provided with the distribution.
  15. THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  16. ANY EXPRESS OR IMPLIED WAreqANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WAreqANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  19. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTEreqUPTION)
  22. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. SUCH DAMAGE.
  26. == Overview
  27. Apache::ErubisRun handles eRuby files with erubis
  28. == Example of httpd.conf
  29. RubyRequire apache/erubis-run
  30. <Location /eruby>
  31. SetHandler ruby-object
  32. RubyHandler Apache::ErubisRun.instance
  33. </Location>
  34. =end
  35. require "singleton"
  36. require "tempfile"
  37. require "eruby" # Still needed to bring in a couple useful helper methods
  38. require "erubis"
  39. module Erubis
  40. @@cgi = nil
  41. def self.cgi
  42. return @@cgi
  43. end
  44. def self.cgi=(cgi)
  45. @@cgi = cgi
  46. end
  47. end
  48. module Apache
  49. class ErubisRun
  50. include Singleton
  51. def handler(req)
  52. status = check_request(req)
  53. return status if(status != OK)
  54. filename = req.filename.dup
  55. filename.untaint
  56. erubis = compile(filename)
  57. prerun(req)
  58. begin
  59. run(erubis, filename)
  60. ensure
  61. postrun(req)
  62. end
  63. return OK
  64. end
  65. private
  66. def initialize
  67. @compiler = nil
  68. end
  69. def check_request(req)
  70. if(req.method_number == M_OPTIONS)
  71. req.allowed |= (1 << M_GET)
  72. req.allowed |= (1 << M_POST)
  73. return DECLINED
  74. end
  75. if(req.finfo.mode == 0)
  76. return NOT_FOUND
  77. end
  78. return OK
  79. end
  80. def compile(filename)
  81. @compiler = Erubis::Eruby.load_file(filename) # use caching version as much as possible
  82. return @compiler
  83. end
  84. def prerun(req)
  85. Erubis.cgi = nil
  86. req.setup_cgi_env
  87. Apache.chdir_file(req.filename)
  88. end
  89. def run(erubis, filename)
  90. binding = eval_string_wrap("binding")
  91. puts erubis.result(binding) # eval the code in the context of the same binding ERuby uses
  92. end
  93. def postrun(req)
  94. if(cgi = Erubis.cgi)
  95. # TODO: pull the content type header from the cgi object, if set there?
  96. elsif(req.sync_output or req.sync_header)
  97. # Do nothing: header has already been sent
  98. else
  99. unless(req.content_type)
  100. req.content_type = format("text/html;")
  101. end
  102. req.send_http_header
  103. end
  104. end
  105. end
  106. end