Class: Zebra::ProxyWorkerReceiveMessageHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/zebra/proxy_worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ProxyWorkerReceiveMessageHandler

Returns a new instance of ProxyWorkerReceiveMessageHandler.



18
19
20
21
22
# File 'lib/zebra/proxy_worker.rb', line 18

def initialize(config)
  @config = config
  @logger = config[:logger] || Logger.new(STDERR)
  @conns = {}
end

Instance Attribute Details

#receivedObject (readonly)

Returns the value of attribute received.



16
17
18
# File 'lib/zebra/proxy_worker.rb', line 16

def received
  @received
end

Instance Method Details

#fetch(method, uri, request_headers = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/zebra/proxy_worker.rb', line 40

def fetch(method, uri, request_headers = {})
  @response = nil
  @logger.debug "Proxying #{method} #{uri} #{request_headers.inspect}"
  t_start = Time.now
  conn = get_conn(uri)
  request_headers['Host'] = uri.host
  http = conn.send(method, path: uri.path, query: uri.query, head: request_headers, :keepalive => true)
  @logger.debug "Request finished"
  response_headers = to_headers(http.response_header)
  #ap response_headers
  response_headers['X-Proxied-By'] = 'Zebra'
  response_headers.delete('Connection')
  response_headers.delete('Content-Length')
  response_headers.delete('Transfer-Encoding')
  t_end = Time.now
  elapsed = t_end.to_f - t_start.to_f
  @logger.info "#{elapsed} elapsed"
  @logger.info "Received #{http.response_header.status} from server, #{http.response.length} bytes"
  [http.response_header.status, response_headers, Base64.encode64(http.response)]
end

#get_conn(uri) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/zebra/proxy_worker.rb', line 30

def get_conn(uri)
  conn_key = uri.scheme + '://' + uri.host
  return EM::HttpRequest.new(conn_key, :connect_timeout => 1, :inactivity_timeout => 1)
  if @conns.has_key?(conn_key)
    return @conns[conn_key]
  else
    return @conns[conn_key] = EM::HttpRequest.new(conn_key, :connect_timeout => 1, :inactivity_timeout => 1)
  end
end

#handle_message(m) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/zebra/proxy_worker.rb', line 65

def handle_message(m)
  #ap m.copy_out_string
  env = JSON.parse(m.copy_out_string)
  uri = URI.parse(env['REQUEST_URI'])
  uri.host = env['HTTP_HOST'] if uri.host.nil? || uri.host.empty?
  uri.path = '/' if uri.path.nil? || uri.path.empty?
  puts env.inspect
  uri.scheme = env['HTTP_X_FORWARDED_PROTO'].downcase if env.has_key?('HTTP_X_FORWARDED_PROTO')
  uri.scheme = 'http' if uri.scheme.nil? || uri.scheme.empty?
  method = env['REQUEST_METHOD'].downcase.to_sym
  puts "uri: #{uri.to_s}"
  fetch(method, uri)
end

#on_readable(socket, messages) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/zebra/proxy_worker.rb', line 79

def on_readable(socket, messages)
  @logger.debug "on_readable #{messages.inspect}"
  fiber = Fiber.new do
    m = messages.first
    response = handle_message(m).to_json
    socket.send_msg response
  end
  fiber.resume
  @logger.debug "Finished on_readable"
end

#on_writeable(socket) ⇒ Object



61
62
63
# File 'lib/zebra/proxy_worker.rb', line 61

def on_writeable(socket)
  @logger.debug("Writable")
end

#to_headers(response_headers) ⇒ Object



24
25
26
27
28
# File 'lib/zebra/proxy_worker.rb', line 24

def to_headers(response_headers)
  raw_headers = {}
  response_headers.select { |k,v| k =~ /^[A-Z0-9_]+$/ }.each_pair { |k,v| raw_headers[ k.downcase.split('_').collect { |e| e.capitalize }.join('-') ] = v}
  raw_headers
end