Sha256: 370a6544998654a1df6963995db4ea85e1be486e0c89b5f054cd45144e7afc05

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

module JsTestCore
  module Resources
    class File < ThinRest::Resource
      MIME_TYPES = {
        '.js' => 'text/javascript',
        '.css' => 'text/css',
        '.png' => 'image/png',
        '.jpg' => 'image/jpeg',
        '.jpeg' => 'image/jpeg',
        '.gif' => 'image/gif',
        }

      property :absolute_path, :relative_path

      def get
        extension = ::File.extname(absolute_path)
        content_type = MIME_TYPES[extension] || 'text/html'

        connection.terminate_after_sending do
          if !rack_request.env["HTTP_IF_MODIFIED_SINCE"].to_s.empty? && Time.parse(rack_request.env["HTTP_IF_MODIFIED_SINCE"]) >= ::File.mtime(absolute_path)
            connection.send_head(
              304,
                'Content-Type' => content_type,
                'Last-Modified' => ::File.mtime(absolute_path).rfc822,
                'Content-Length' => 0
            )
          else
            connection.send_head(
              200,
                'Content-Type' => content_type,
                'Last-Modified' => ::File.mtime(absolute_path).rfc822,
                'Content-Length' => ::File.size(absolute_path)
            )
            ::File.open(absolute_path) do |file|
              while !file.eof?
                connection.send_data(file.read(1024))
              end
            end
          end
        end

        def ==(other)
          return false unless other.class == self.class
          absolute_path == other.absolute_path && relative_path == other.relative_path
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
screw-unit-0.3.1 vendor/js-test-core/lib/js_test_core/resources/file.rb