Re: A simple mod_ruby file handler example

Clayton Cottingham <[email protected]> Thu, 28 Apr 2005 15:06:49 -0700
Newsgroups gmane.comp.apache.mod-ruby
Organization WinterMarket NetWorks
Message-ID <[email protected]>
thats similar to what i have succeeded with
{ive done mod_perl before so i managed to piece things together}

included into httpd.conf 

RubyRequire newhandler
<Location /clay>
SetHandler ruby-object
RubyHandler Clay::Handle.instance
</Location>

my ruby file newhandler.rb:

#!/usr/bin/ruby
module Clay
  class Handle < Apache::RubyRun
    def handler( r )
      begin
        r.content_type = 'text/html'
        r.send_http_header
        r.print( "hiya<br> Time is #{Time.now}" )
        return Apache::OK
      end
    end
  end  
end 



if i use your i get

/usr/lib/ruby/1.8/singleton.rb:84: warning: already initialized constant
FirstInstanceCall
[Thu Apr 28 14:46:50 2005] [error] mod_ruby: failed to require
apache/file-handler
[Thu Apr 28 14:46:50 2005] [error] mod_ruby: error in ruby
[Thu Apr 28 14:46:50 2005] [error]
mod_ruby: /usr/lib/ruby/1.8/singleton.rb:167:in `undef_method':
undefined method `extend_object' for `Singleton' (NameError)


unless i remove 

RubyRequire apache/ruby-run

in the conf file


mine works without removing this line 


Can someone shed some light on which way is best?

BTW I based mine of of the Arrow code base
Arrow::Dispatch to be precise











On Thu, 2005-04-28 at 12:23 +0900, Shugo Maeda wrote:
> Hi,
> 
> Clayton Cottingham wrote:
> > New to ruby/mod_ruby
> > 
> > Trying to figure out how to make a simple mod_ruby filehandle
> 
> Is this what you want?
> 
> =begin
> 
> = apache/file-handler.rb
> 
> Copyright (C) 2005  Shugo Maeda <[email protected]>
> All rights reserved.
> 
> Redistribution and use in source and binary forms, with or without
> modification, are permitted provided that the following conditions
> are met:
> 1. Redistributions of source code must retain the above copyright
>    notice, this list of conditions and the following disclaimer.
> 2. Redistributions in binary form must reproduce the above copyright
>    notice, this list of conditions and the following disclaimer in the
>    documentation and/or other materials provided with the distribution.
> 
> THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
> ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
> IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
> ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
> FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
> DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
> OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
> HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
> LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
> OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
> SUCH DAMAGE.
> 
> == Overview
> 
> Apache::FileHandler handles static files.
> 
> == Example of httpd.conf
> 
>   RubyRequire apache/file-handler
>   <Location /files>
>   SetHandler ruby-object
>   RubyHandler Apache::FileHandler.instance
>   </Location>
> 
> =end
> 
> require "singleton"
> 
> module Apache
>   class FileHandler
>     include Singleton
> 
>     def handler(r)
>       begin
>         r.content_type = "text/plain"
>         filename = r.filename.dup.untaint
>         r.print(File.read(filename))
>         return OK
>       rescue Errno::ENOENT
>         return NOT_FOUND
>       rescue Errno::EPERM, Errno::EISDIR
>         return FORBIDDEN
>       end
>     end
>   end
> end
>