Class: CapProxy::Client
- Inherits:
-
EM::Connection
- Object
- EM::Connection
- CapProxy::Client
- Defined in:
- lib/cap_proxy/client.rb
Instance Attribute Summary collapse
-
#head ⇒ Object
readonly
Returns the value of attribute head.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Instance Method Summary collapse
- #chunks_finish ⇒ Object
- #chunks_send(data) ⇒ Object
- #chunks_start(status, headers = {}) ⇒ Object
-
#initialize(server) ⇒ Client
constructor
A new instance of Client.
- #receive_data(data) ⇒ Object
- #respond(status, headers, body = nil) ⇒ Object
- #unbind ⇒ Object
- #verify_headers! ⇒ Object
- #write_head(status, headers) ⇒ Object
Constructor Details
#initialize(server) ⇒ Client
Returns a new instance of Client.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/cap_proxy/client.rb', line 10 def initialize(server) @server = server @remote = nil @data = nil @http_parser = HTTP::Parser.new @http_parser.on_headers_complete = proc do verify_headers! end end |
Instance Attribute Details
#head ⇒ Object (readonly)
Returns the value of attribute head.
8 9 10 |
# File 'lib/cap_proxy/client.rb', line 8 def head @head end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
8 9 10 |
# File 'lib/cap_proxy/client.rb', line 8 def server @server end |
Instance Method Details
#chunks_finish ⇒ Object
84 85 86 87 |
# File 'lib/cap_proxy/client.rb', line 84 def chunks_finish send_data "0\r\n\r\n" close_connection_after_writing end |
#chunks_send(data) ⇒ Object
78 79 80 81 82 |
# File 'lib/cap_proxy/client.rb', line 78 def chunks_send(data) send_data "#{data.bytesize.to_s(16)}\r\n" send_data data send_data "\r\n" end |
#chunks_start(status, headers = {}) ⇒ Object
74 75 76 |
# File 'lib/cap_proxy/client.rb', line 74 def chunks_start(status, headers = {}) write_head(status, headers.merge("Transfer-Encoding" => "chunked")) end |
#receive_data(data) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/cap_proxy/client.rb', line 61 def receive_data(data) if @remote @remote.send_data data else if @data @data << data else @data = data end @http_parser << data end end |
#respond(status, headers, body = nil) ⇒ Object
40 41 42 43 44 |
# File 'lib/cap_proxy/client.rb', line 40 def respond(status, headers, body = nil) write_head(status, headers) send_data(body) if body close_connection_after_writing end |
#unbind ⇒ Object
21 22 23 24 25 |
# File 'lib/cap_proxy/client.rb', line 21 def unbind if @remote @remote.close_connection_after_writing end end |
#verify_headers! ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/cap_proxy/client.rb', line 46 def verify_headers! parser = @http_parser filter = server.filters.find {|f| f[:filter].apply?(parser) } server.log.info "#{parser.http_method} #{parser.request_url} - #{filter ? "filtered" : "raw"}" if server.log if filter filter[:handler].call self, parser else @remote = EM.connect(server.target_host, server.target_port, RemoteConnection, self) @remote.send_data @data @data = nil @http_parser = nil end end |
#write_head(status, headers) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/cap_proxy/client.rb', line 27 def write_head(status, headers) head = [ "HTTP/1.1 #{status} #{HTTPCodes[status]}\r\n" ] if headers headers.each_pair do |key, value| head << "#{key}: #{value}\r\n" end end head << "\r\n" send_data head.join end |