server/lib/purr/server.rb
# Method required by the Rack API
#
# @see https://rack.github.io/
Method required by the Rack API
Method required by the Rack API
def call(env)
      upgrade = parse_headers(env)
      return not_found unless %i(websocket purr).include?(upgrade)
      host, port = @remote.call(env)
      if host.nil? || port.nil?
        logger(env, :error, "No matching endpoint found for request incoming from #{env['REMOTE_ADDR']}")
        return not_found
      end
      http = env['rack.hijack'].call
      http.write(http_response(upgrade))
      sock = TCPSocket.new(host, port)
      @proxy.push(http, sock)
      logger(env, :info, "Redirecting incoming request from #{env['REMOTE_ADDR']} to [#{host}]:#{port}")
      return [200, {}, []]
    rescue => ex
      logger(env, :error, "#{ex.class} happened for #{env['REMOTE_ADDR']} trying to access #{host}:#{port}")
      cleanup(http, sock)
      return not_found 
    end
def call ( env ) upgrade = parse_headers ( env ) return not_found unless %i( websocket purr ) . include? ( upgrade ) host , port = @remote . call ( env ) if host . nil? || port . nil? logger ( env , :error , "No matching endpoint found for request incoming from #{env['REMOTE_ADDR']}" ) return not_found end http = env [ 'rack.hijack' ] . call http . write ( http_response ( upgrade ) ) sock = TCPSocket . new ( host , port ) @proxy . push ( http , sock ) logger ( env , :info , "Redirecting incoming request from #{env['REMOTE_ADDR']} to [#{host}]:#{port}" ) return [ 200 , { } , [ ] ] rescue => ex logger ( env , :error , "#{ex.class} happened for #{env['REMOTE_ADDR']} trying to access #{host}:#{port}" ) cleanup ( http , sock ) return not_found end
Method required by the Rack API
Method required by the Rack API
# Return with a 404 error if the upgrade header is not present # Return with a 404 error if no host:port pair was determined # Hijack the HTTP socket from the Rack middleware # Write a proper HTTP response # Open the remote TCP socket # Start proxying # Rack requires this line below # Return with a 404 error
env
<not_specific>
see
None