Sha256: c21a6d2fdb25742d4d76c9dc99ab6eff3f1f616756bcda59afb71308f8ad8c04

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

# encoding: utf-8

module Hatetepe
  class Server::KeepAlive
    CONNECTION = 'Connection'.freeze
    CLOSE = 'close'.freeze
    KEEP_ALIVE = 'keep-alive'.freeze

    def setup(_, server, _)
      @server = server
      @requests, @previous = {}, nil
    end

    def serve(request, served)
      @requests[request] = [served, @previous]
      @previous = request
    end

    def respond(request, response)
      synchronize_responses(request)

      if close_connection?(request)
        response.headers[CONNECTION] = CLOSE
        # XXX: possible race condition with other locations waiting for this
        response.closed { @server.close }
      else
        response.headers[CONNECTION] = KEEP_ALIVE
      end
    end

    def finish(request)
      @requests.delete(request)
    end

    private

    def synchronize_responses(request)
      _, previous = @requests[request]
      served, _   = @requests[previous]

      served.sync.closed! if served
    end

    def connection_header(request)
      response = @requests[request][0].value
      request.headers[CONNECTION] || response.headers[CONNECTION]
    end

    def close_connection?(request)
      header = connection_header(request).to_s.downcase
      header == CLOSE || http10_close?(request.http_version, header)
    end

    def http10_close?(version, header)
      version < 1.1 && header != KEEP_ALIVE
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hatetepe-0.6.0.pre.2 lib/hatetepe/server/keep_alive.rb