def call(env)
      # Avoid modifying the session state, don't set cookies, etc
      env["rack.session.options"] ||= {}
      env["rack.session.options"].merge! :defer => true, :skip => true

      root = env["SCRIPT_NAME"]
      path = Rack::Utils.unescape(env["PATH_INFO"].to_s).sub(%r[^/], "")

      if path.include? ".."
        return [403, { "Content-Type" => "text/plain", "Content-Length" => "9" }, ["Forbidden"]]
      end

      # Pull out the md5sum if it's part of the given path
      # e.g. foo/bar-a95c53a7a0f5f492a74499e70578d150.js -> a95c53a7a0f5f492a74499e70578d150
      checksum_tag = bundle_name = nil
      matches = path.match /([^\/]+)-([\da-f]{32})\..+$/
      if matches && matches.size == 3
        bundle_name = matches[1]
        checksum_tag = matches[2]
        path.sub!("-#{checksum_tag}", "")
      end

      # First see if there is a bundle with this name
      asset = Bundle[bundle_name]
      if asset
        # If the checksum doesn't match the bundle, we want to return a 404.
        asset = nil unless asset.checksum == checksum_tag
      else
        # If there is no bundle with this name, find another type of Asset with the name instead.
        asset = Asset[path]
      end

      if asset
        # If the ETag matches, give a 304
        return [304, {}, []] if env["HTTP_IF_NONE_MATCH"] == %Q["#{asset.checksum}"]

        if Pinion.environment == "production"
          # In production, if there is a checksum, cache for a year (because if the asset changes with a
          # redeploy, the checksum will change). If there is no checksum, cache for 10 minutes (this way at
          # worst we only serve 10 minute stale assets, and caches in front of Pinion will be able to help
          # out).
          cache_policy = checksum_tag ? "max-age=31536000" : "max-age=600"
        else
          # Don't cache in dev.
          cache_policy = "must-revalidate"
        end
        headers = {
          "Content-Type" => asset.content_type,
          "Content-Length" => asset.length.to_s,
          "ETag" => %Q["#{asset.checksum}"],
          "Cache-Control" => "public, #{cache_policy}",
          "Last-Modified" => asset.mtime.httpdate,
        }
        body = env["REQUEST_METHOD"] == "HEAD" ? [] : asset
        [200, headers, body]
      else
        [404, { "Content-Type" => "text/plain", "Content-Length" => "9" }, ["Not found"]]
      end
    rescue Exception => e
      # TODO: logging
      STDERR.puts "Error compiling #{path}:"
      STDERR.puts "#{e.class.name}: #{e.message}"
      # TODO: render nice custom errors in the browser
      raise
    end
def call(env)
      # Avoid modifying the session state, don't set cookies, etc
      env["rack.session.options"] ||= {}
      env["rack.session.options"].merge! :defer => true, :skip => true

      root = env["SCRIPT_NAME"]
      path = Rack::Utils.unescape(env["PATH_INFO"].to_s).sub(%r[^/], "")

      if path.include? ".."
        return [403, { "Content-Type" => "text/plain", "Content-Length" => "9" }, ["Forbidden"]]
      end

      # Pull out the md5sum if it's part of the given path
      # e.g. foo/bar-a95c53a7a0f5f492a74499e70578d150.js -> a95c53a7a0f5f492a74499e70578d150
      checksum_tag = bundle_name = nil
      matches = path.match /([^\/]+)-([\da-f]{32})\..+$/
      if matches && matches.size == 3
        bundle_name = matches[1]
        checksum_tag = matches[2]
        path.sub!("-#{checksum_tag}", "")
      end

      # First see if there is a bundle with this name
      asset = Bundle[bundle_name]
      if asset
        # If the checksum doesn't match the bundle, we want to return a 404.
        asset = nil unless asset.checksum == checksum_tag
      else
        # If there is no bundle with this name, find another type of Asset with the name instead.
        asset = Asset[path]
      end

      if asset
        # If the ETag matches, give a 304
        return [304, {}, []] if env["HTTP_IF_NONE_MATCH"] == %Q["#{asset.checksum}"]

        if Pinion.environment == "production"
          # In production, if there is a checksum, cache for a year (because if the asset changes with a
          # redeploy, the checksum will change). If there is no checksum, cache for 10 minutes (this way at
          # worst we only serve 10 minute stale assets, and caches in front of Pinion will be able to help
          # out).
          cache_policy = checksum_tag ? "max-age=31536000" : "max-age=600"
        else
          # Don't cache in dev.
          cache_policy = "must-revalidate"
        end
        headers = {
          "Content-Type" => asset.content_type,
          "Content-Length" => asset.length.to_s,
          "ETag" => %Q["#{asset.checksum}"],
          "Cache-Control" => "public, #{cache_policy}",
          "Last-Modified" => asset.mtime.httpdate,
        }
        body = env["REQUEST_METHOD"] == "HEAD" ? [] : asset
        [200, headers, body]
      else
        [404, { "Content-Type" => "text/plain", "Content-Length" => "9" }, ["Not found"]]
      end
    rescue Exception => e
      # TODO: logging
      STDERR.puts "Error compiling #{path}:"
      STDERR.puts "#{e.class.name}: #{e.message}"
      # TODO: render nice custom errors in the browser
      raise
    end
Boilerplate mostly stolen from sprockets
 https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/server.rb