Sha256: 5a1bb83aa6664859a7bae283fd3944040c4a5849eb3b87eac32d9f0cbef97d74

Contents?: true

Size: 1.55 KB

Versions: 7

Compression:

Stored size: 1.55 KB

Contents

module Awestruct
  module Rack

    class App
      def initialize(doc_root)
        @doc_root = doc_root
      end

      def call(env)
        path = env['REQUEST_PATH']
        fs_path = File.join( @doc_root, path )
        
        if ( File.directory?( fs_path ) )
          if ( ! ( path =~ %r(/$) ) )
            return [ 301,
                     { :location=>path + '/' },
                     "Redirecting to: #{path}" ]
          elsif ( File.exist?( File.join( fs_path, 'index.html' ) ) )
            fs_path = File.join( fs_path, 'index.html' )
          end
        end

        if ( File.exist?( fs_path ) )
          [ 200,
            {},
            read_content( fs_path ) ]
        else
          [ 404,
            {},
            read_error_document(path) ]
        end
      end

      def read_error_document( path )
        doc_path = nil
        htaccess = File.join( @doc_root, '.htaccess' ) 
        if ( File.exist?( htaccess ) )
          File.open( htaccess ).each_line do |line|
            if ( line =~ %r(^.*ErrorDocument[ \t]+404[ \t]+(.+)$) )
              doc_path = $1
            end
          end
        end
        if ( doc_path )
          fs_doc_path = File.join( @doc_root, doc_path )
          return read_content( fs_doc_path ) if File.exist?( fs_doc_path ) 
        end
        "404: Not Found: #{path}"
      end


      def read_content( path )
        input_stream = IO.open(IO.sysopen(path, "rb"), "rb" )
        result = input_stream.read
        return result
      ensure
        input_stream.close
      end
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
awestruct-0.4.6 lib/awestruct/rack/app.rb
awestruct-0.4.5 lib/awestruct/rack/app.rb
awestruct-0.4.4 lib/awestruct/rack/app.rb
awestruct-0.4.3 lib/awestruct/rack/app.rb
awestruct-0.4.2 lib/awestruct/rack/app.rb
awestruct-0.4.2.x10 lib/awestruct/rack/app.rb
awestruct-0.4.2.x9 lib/awestruct/rack/app.rb