Module: Wildcloud::Router::Client

Defined in:
lib/wildcloud/router/client.rb

Constant Summary collapse

CODE =

Borrowed from em-http-request

{
  100  => 'Continue',
  101  => 'Switching Protocols',
  102  => 'Processing',
  200  => 'OK',
  201  => 'Created',
  202  => 'Accepted',
  203  => 'Non-Authoritative Information',
  204  => 'No Content',
  205  => 'Reset Content',
  206  => 'Partial Content',
  207  => 'Multi-Status',
  226  => 'IM Used',
  300  => 'Multiple Choices',
  301  => 'Moved Permanently',
  302  => 'Found',
  303  => 'See Other',
  304  => 'Not Modified',
  305  => 'Use Proxy',
  306  => 'Reserved',
  307  => 'Temporary Redirect',
  400  => 'Bad Request',
  401  => 'Unauthorized',
  402  => 'Payment Required',
  403  => 'Forbidden',
  404  => 'Not Found',
  405  => 'Method Not Allowed',
  406  => 'Not Acceptable',
  407  => 'Proxy Authentication Required',
  408  => 'Request Timeout',
  409  => 'Conflict',
  410  => 'Gone',
  411  => 'Length Required',
  412  => 'Precondition Failed',
  413  => 'Request Entity Too Large',
  414  => 'Request-URI Too Long',
  415  => 'Unsupported Media Type',
  416  => 'Requested Range Not Satisfiable',
  417  => 'Expectation Failed',
  422  => 'Unprocessable Entity',
  423  => 'Locked',
  424  => 'Failed Dependency',
  426  => 'Upgrade Required',
  500  => 'Internal Server Error',
  501  => 'Not Implemented',
  502  => 'Bad Gateway',
  503  => 'Service Unavailable',
  504  => 'Gateway Timeout',
  505  => 'HTTP Version Not Supported',
  506  => 'Variant Also Negotiates',
  507  => 'Insufficient Storage',
  510  => 'Not Extended'
}

Instance Method Summary collapse

Instance Method Details

#closed?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/wildcloud/router/client.rb', line 83

def closed?
  @closed
end

#initialize(target) ⇒ Object



21
22
23
24
# File 'lib/wildcloud/router/client.rb', line 21

def initialize(target)
  @target = target
  @closed = false
end

#make_request(proxy, version, method, url, headers) ⇒ Object

Tools



67
68
69
70
71
72
73
74
75
76
# File 'lib/wildcloud/router/client.rb', line 67

def make_request(proxy, version, method, url, headers)
  @proxy = proxy
  @inbound = 0
  @outbound = 0
  send_line("#{method} #{url} HTTP/#{version}")
  headers.each do |name, value|
    send_line("#{name}: #{value}")
  end
  send_line('')
end

#on_body(chunk) ⇒ Object



57
58
59
# File 'lib/wildcloud/router/client.rb', line 57

def on_body(chunk)
  @proxy.send_data(chunk)
end

#on_headers_complete(headers) ⇒ Object

HTTP parser



49
50
51
52
53
54
55
# File 'lib/wildcloud/router/client.rb', line 49

def on_headers_complete(headers)
  @proxy.send_line("HTTP/#{@parser.http_version.join('.')} #{@parser.status_code} #{CODE[@parser.status_code]}")
  headers.each do |name, value|
    @proxy.send_line("#{name}: #{value}")
  end
  @proxy.send_line('')
end

#on_message_completeObject



61
62
63
# File 'lib/wildcloud/router/client.rb', line 61

def on_message_complete
  close_connection
end

#post_initObject

EventMachine



28
29
30
# File 'lib/wildcloud/router/client.rb', line 28

def post_init
  @parser = Http::ResponseParser.new(self)
end

#receive_data(data) ⇒ Object



32
33
34
35
36
37
# File 'lib/wildcloud/router/client.rb', line 32

def receive_data(data)
  @outbound += data.size
  @parser << data
rescue Http::Parser::Error => error
  Router.logger.debug('Proxy client') { "Error #{error}" }
end

#send_data(data) ⇒ Object



78
79
80
81
# File 'lib/wildcloud/router/client.rb', line 78

def send_data(data)
  @inbound += data.size
  super(data)
end

#send_line(data) ⇒ Object



87
88
89
# File 'lib/wildcloud/router/client.rb', line 87

def send_line(data)
  send_data("#{data}\r\n")
end

#unbindObject



39
40
41
42
43
44
45
# File 'lib/wildcloud/router/client.rb', line 39

def unbind
  @parser = nil
  @stop_time = Time.now
  Router.monitor.request(@target, @stop_time - @proxy.start_time, @inbound, @outbound)
  @closed = true
  @proxy.close_connection(true) if @proxy and !@proxy.closed?
end