Class: Zebra::ProxyWorkerReceiveMessageHandler
- Inherits:
-
Object
- Object
- Zebra::ProxyWorkerReceiveMessageHandler
- Defined in:
- lib/zebra/proxy_worker.rb
Instance Attribute Summary collapse
-
#received ⇒ Object
readonly
Returns the value of attribute received.
Instance Method Summary collapse
- #fetch(method, uri, request_headers = {}) ⇒ Object
- #get_conn(uri) ⇒ Object
- #handle_message(m) ⇒ Object
-
#initialize(config) ⇒ ProxyWorkerReceiveMessageHandler
constructor
A new instance of ProxyWorkerReceiveMessageHandler.
- #on_readable(socket, messages) ⇒ Object
- #on_writeable(socket) ⇒ Object
- #to_headers(response_headers) ⇒ Object
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
#received ⇒ Object (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 (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, ) @logger.debug "on_readable #{.inspect}" fiber = Fiber.new do m = .first response = (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 |