Class: ThreadedProxy::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/threaded_proxy/client.rb

Constant Summary collapse

DISALLOWED_RESPONSE_HEADERS =
%w[keep-alive].freeze
HTTP_METHODS =
{
  'get' => Net::HTTP::Get,
  'post' => Net::HTTP::Post,
  'put' => Net::HTTP::Put,
  'delete' => Net::HTTP::Delete,
  'head' => Net::HTTP::Head,
  'options' => Net::HTTP::Options,
  'trace' => Net::HTTP::Trace
}.freeze
CALLBACK_METHODS =
%i[
  on_response
  on_headers
  on_body
  on_complete
  on_error
].freeze
DEFAULT_OPTIONS =
{
  headers: {},
  debug: false,
  method: :get
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(origin_url, options = {}) {|_self| ... } ⇒ Client

Returns a new instance of Client.

Yields:

  • (_self)

Yield Parameters:



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/threaded_proxy/client.rb', line 42

def initialize(origin_url, options = {})
  @origin_url = Addressable::URI.parse(origin_url)
  @options = DEFAULT_OPTIONS.merge(options)

  @callbacks = {}
  CALLBACK_METHODS.each do |method_name|
    @callbacks[method_name] = proc {}
  end

  yield(self) if block_given?
end

Instance Method Details

#default_port(uri) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/threaded_proxy/client.rb', line 123

def default_port(uri)
  case uri.scheme
  when 'http'
    80
  when 'https'
    443
  end
end

#log(message) ⇒ Object



54
55
56
# File 'lib/threaded_proxy/client.rb', line 54

def log(message)
  warn message if @options[:debug]
end

#start(socket) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/threaded_proxy/client.rb', line 58

def start(socket)
  request_method = @options[:method].to_s.downcase
  request_headers = @options[:headers].merge('Connection' => 'close')

  request_class = HTTP_METHODS[request_method]
  http_request = request_class.new(@origin_url, request_headers)
  if @options[:body].respond_to?(:read)
    http_request.body_stream = @options[:body]
  elsif @options[:body].is_a?(String)
    http_request.body = @options[:body]
  end

  ActiveSupport::Notifications.instrument('threaded_proxy.fetch', method: request_method, url: @origin_url.to_s,
                                                                  headers: request_headers) do
    http = HTTP.new(@origin_url.host, @origin_url.port || default_port(@origin_url))
    http.use_ssl = (@origin_url.scheme == 'https')
    http.set_debug_output($stderr) if @options[:debug]
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @options[:ignore_ssl_errors]

    http.start do
      http.request(http_request) do |client_response|
        @callbacks[:on_response].call(client_response, socket)
        break if socket.closed?

        log('Writing response status and headers')
        write_headers(client_response, socket)
        break if socket.closed?

        @callbacks[:on_body].call(client_response, socket)
        break if socket.closed?

        # There may have been some existing data in client_response's read buffer, flush it out
        # before we manually connect the raw sockets
        log('Flushing existing response buffer to client')
        http.flush_existing_buffer_to(socket)

        # Copy the rest of the client response to the socket
        log('Copying response body to client')
        http.copy_to(socket)

        @callbacks[:on_complete].call(client_response)
      end
    rescue StandardError => e
      @callbacks[:on_error].call(e) or raise
    end
  end
end

#write_headers(client_response, socket) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/threaded_proxy/client.rb', line 106

def write_headers(client_response, socket)
  socket.write "HTTP/1.1 #{client_response.code} #{client_response.message}\r\n"

  # We don't support reusing connections once we have disconnected them from rack
  client_response['connection'] = 'close'

  @callbacks[:on_headers].call(client_response, socket)
  return if socket.closed?

  client_response.each_header do |key, value|
    socket.write "#{key}: #{value}\r\n" unless DISALLOWED_RESPONSE_HEADERS.include?(key.downcase)
  end

  # Done with headers
  socket.write "\r\n"
end