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

Inherits:
EventMachine::Connection
  • Object
show all
Includes:
EventMachine::HttpServer
Defined in:
lib/rflow/components/http/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#client_ipObject (readonly)

Returns the value of attribute client_ip.



56
57
58
# File 'lib/rflow/components/http/server.rb', line 56

def client_ip
  @client_ip
end

#client_portObject (readonly)

Returns the value of attribute client_port.



56
57
58
# File 'lib/rflow/components/http/server.rb', line 56

def client_port
  @client_port
end

#serverObject

Returns the value of attribute server.



55
56
57
# File 'lib/rflow/components/http/server.rb', line 55

def server
  @server
end

#server_ipObject (readonly)

Returns the value of attribute server_ip.



56
57
58
# File 'lib/rflow/components/http/server.rb', line 56

def server_ip
  @server_ip
end

#server_portObject (readonly)

Returns the value of attribute server_port.



56
57
58
# File 'lib/rflow/components/http/server.rb', line 56

def server_port
  @server_port
end

Instance Method Details

#post_initObject



58
59
60
61
62
63
64
# File 'lib/rflow/components/http/server.rb', line 58

def post_init
  @client_port, @client_ip = Socket.unpack_sockaddr_in(get_peername) rescue ["?", "?.?.?.?"]
  @server_port, @server_ip = Socket.unpack_sockaddr_in(get_sockname) rescue ["?", "?.?.?.?"]
  RFlow.logger.debug "Connection from #{@client_ip}:#{@client_port} to #{@server_ip}:#{@server_port}"
  super
  no_environment_strings
end

#process_http_requestObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rflow/components/http/server.rb', line 73

def process_http_request
  RFlow.logger.debug "Received a HTTP request from #{client_ip}:#{client_port} to #{@server_ip}:#{@server_port}"
  
  processing_event = RFlow::Message::ProcessingEvent.new(server.instance_uuid, Time.now.utc)

  request_message = RFlow::Message.new('RFlow::Message::Data::HTTP::Request')
  request_message.data.uri = @http_request_uri

  processing_event.context = signature
  processing_event.completed_at = Time.now.utc
  request_message.provenance << processing_event

  server.request_port.send_message request_message
end

#receive_data(data) ⇒ Object



67
68
69
70
# File 'lib/rflow/components/http/server.rb', line 67

def receive_data(data)
  RFlow.logger.debug "Received #{data.bytesize} bytes of data from #{client_ip}:#{client_port} to #{@server_ip}:#{@server_port}"
  super
end

#send_http_response(response_message = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rflow/components/http/server.rb', line 89

def send_http_response(response_message=nil)
  RFlow.logger.debug "Sending an HTTP response to #{client_ip}:#{client_port}"
  resp = EventMachine::DelegatedHttpResponse.new(self)

  # Default values
  resp.status                  = 200
  resp.content                 = ""
  resp.headers["Content-Type"] = "text/html"
  resp.headers["Server"]       = "Apache"
  
  if response_message
    resp.status  = response_message.data.status_code
    resp.content = response_message.data.content
    response_message.data.headers.each do |header, value|
      resp[headers] = value
    end
  end

  resp.send_response
  close_connection_after_writing
end

#unbind(reason = nil) ⇒ Object

Called when a connection is torn down for whatever reason. Remove this connection from the server’s list



114
115
116
117
# File 'lib/rflow/components/http/server.rb', line 114

def unbind(reason=nil)
  RFlow.logger.debug "Disconnected from HTTP client #{client_ip}:#{client_port} due to '#{reason}'"
  server.connections.delete(self.signature)
end