Class: RFlow::Components::HTTP::Server

Inherits:
RFlow::Component
  • Object
show all
Defined in:
lib/rflow/components/http/server.rb

Defined Under Namespace

Classes: ClosedConnection, Connection

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#closed_connectionsObject

Returns the value of attribute closed_connections.



12
13
14
# File 'lib/rflow/components/http/server.rb', line 12

def closed_connections
  @closed_connections
end

#connectionsObject

Returns the value of attribute connections.



12
13
14
# File 'lib/rflow/components/http/server.rb', line 12

def connections
  @connections
end

#listenObject

Returns the value of attribute listen.



12
13
14
# File 'lib/rflow/components/http/server.rb', line 12

def listen
  @listen
end

#portObject

Returns the value of attribute port.



12
13
14
# File 'lib/rflow/components/http/server.rb', line 12

def port
  @port
end

#proxy_real_client_ip_headerObject

Returns the value of attribute proxy_real_client_ip_header.



12
13
14
# File 'lib/rflow/components/http/server.rb', line 12

def proxy_real_client_ip_header
  @proxy_real_client_ip_header
end

#proxy_real_client_port_headerObject

Returns the value of attribute proxy_real_client_port_header.



12
13
14
# File 'lib/rflow/components/http/server.rb', line 12

def proxy_real_client_port_header
  @proxy_real_client_port_header
end

#proxy_real_server_ip_headerObject

Returns the value of attribute proxy_real_server_ip_header.



12
13
14
# File 'lib/rflow/components/http/server.rb', line 12

def proxy_real_server_ip_header
  @proxy_real_server_ip_header
end

#proxy_real_server_port_headerObject

Returns the value of attribute proxy_real_server_port_header.



12
13
14
# File 'lib/rflow/components/http/server.rb', line 12

def proxy_real_server_port_header
  @proxy_real_server_port_header
end

#server_signatureObject

Returns the value of attribute server_signature.



12
13
14
# File 'lib/rflow/components/http/server.rb', line 12

def server_signature
  @server_signature
end

Instance Method Details

#configure!(config) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/rflow/components/http/server.rb', line 16

def configure!(config)
  @listen = config['listen'] ? config['listen'] : '127.0.0.1'
  @port = config['port'] ? config['port'].to_i : 8000
  @proxy_real_client_ip_header = config.has_key?('proxy-real-client-ip-header') ? config['proxy-real-client-ip-header'] : 'X-Real-IP'
  @proxy_real_client_port_header = config.has_key?('proxy-real-client-port-header') ? config['proxy-real-client-port-header'] : 'X-Real-Port'
  @proxy_real_server_ip_header = config.has_key?('proxy-real-server-ip-header') ? config['proxy-real-server-ip-header'] : 'X-Server-IP'
  @proxy_real_server_port_header = config.has_key?('proxy-real-server-port-header') ? config['proxy-real-server-port-header'] : 'X-Server-Port'
  @connections = {}
  @closed_connections = ActiveSupport::Cache::MemoryStore.new(expires_in: 5.minutes)
end

#process_message(input_port, input_port_key, connection, message) ⇒ Object

Getting all messages to response_port, which we need to filter for those that pertain to this component and have active connections. This is done by inspecting the provenance, specifically the context attribute that we stored originally



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rflow/components/http/server.rb', line 39

def process_message(input_port, input_port_key, connection, message)
  return unless message.data_type_name == 'RFlow::Message::Data::HTTP::Response'
  my_events = message.provenance.find_all {|processing_event| processing_event.component_instance_uuid == uuid}

  my_events.each do |processing_event|
    connection_signature_string = processing_event.context.to_s
    if connections[connection_signature_string]
      connections[connection_signature_string].send_http_response message
    else
      conn = closed_connections.read(connection_signature_string)
      if conn
        RFlow.logger.info "#{name}: Could not send HTTP response to #{conn.client_details}: connection is already closed"
      else
        RFlow.logger.info "#{name}: Could not send HTTP response to <client details expired>: connection is already closed"
      end
    end
  end
end

#run!Object



27
28
29
30
31
32
33
# File 'lib/rflow/components/http/server.rb', line 27

def run!
  @server_signature = EM.start_server(@listen, @port, Connection) do |conn|
    conn.server = self
    self.connections[conn.signature.to_s] = conn
    RFlow.logger.debug { "#{name}: Connection from #{conn.client_details} to #{conn.server_details}" }
  end
end