Class: Puma::Client

Inherits:
Object
  • Object
show all
Includes:
ClientEnv
Defined in:
lib/puma/client.rb

Overview

An instance of this class wraps a connection/socket. For example, this could be an http request from a browser or from CURL.

An instance of Puma::Client can be used as if it were an IO object by the reactor. The reactor is expected to call #to_io on any non-IO objects it polls. For example, nio4r internally calls IO::try_convert (which may call #to_io) when a new socket is registered.

Instances of this class are responsible for knowing if the request line, headers and body are fully buffered and verified via the try_to_finish method. All verification of each request is done in the Client object. They can be used to "time out" a response via the timeout_at reader.

Most of the code for env processing and verification is contained in Puma::ClientEnv, which is included.

Constant Summary collapse

ALLOWED_TRANSFER_ENCODING =

this tests all values but the last, which must be chunked

%w[compress deflate gzip].freeze
CHUNK_SIZE_VALID =

chunked body validation

/\A\h+\z/.freeze
CHUNK_VALID_ENDING =
Const::LINE_END
CHUNK_VALID_ENDING_SIZE =
CHUNK_VALID_ENDING.bytesize
MAX_CHUNK_HEADER_SIZE =

The maximum number of bytes we'll buffer looking for a valid chunk header, and the maximum size of a trailer section.

4096
SHUTDOWN_READ_GRACE =
1
MAX_CHUNK_EXCESS =

The maximum amount of excess data the client sends using chunk size extensions before we abort the connection.

16 * 1024
CONTENT_LENGTH_VALUE_INVALID =

Content-Length header value validation

/[^\d]/.freeze
TE_ERR_MSG =
'Invalid Transfer-Encoding'
STRIP_OWS =
/\A[ \t]+|[ \t]+\z/
EmptyBody =

The object used for a request with no body. All requests with no body share this one object since it has no state.

NullIO.new

Constants included from Const

Puma::Const::ACCEPT_ERROR_DELAY, Puma::Const::BANNED_HEADER_KEY, Puma::Const::CGI_VER, Puma::Const::CHUNKED, Puma::Const::CHUNK_SIZE, Puma::Const::CLOSE, Puma::Const::CLOSE_CHUNKED, Puma::Const::CODE_NAME, Puma::Const::COLON, Puma::Const::CONNECTION_CLOSE, Puma::Const::CONNECTION_KEEP_ALIVE, Puma::Const::CONTENT_LENGTH, Puma::Const::CONTENT_LENGTH2, Puma::Const::CONTENT_LENGTH_S, Puma::Const::CONTINUE, Puma::Const::DQUOTE, Puma::Const::EARLY_HINTS, Puma::Const::ERROR_RESPONSE, Puma::Const::GATEWAY_INTERFACE, Puma::Const::HALT_COMMAND, Puma::Const::HEAD, Puma::Const::HIJACK, Puma::Const::HIJACK_IO, Puma::Const::HIJACK_P, Puma::Const::HTTP, Puma::Const::HTTPS, Puma::Const::HTTPS_KEY, Puma::Const::HTTP_10_200, Puma::Const::HTTP_11, Puma::Const::HTTP_11_100, Puma::Const::HTTP_11_200, Puma::Const::HTTP_CONNECTION, Puma::Const::HTTP_EXPECT, Puma::Const::HTTP_HEADER_DELIMITER, Puma::Const::HTTP_HOST, Puma::Const::HTTP_VERSION, Puma::Const::HTTP_X_FORWARDED_FOR, Puma::Const::HTTP_X_FORWARDED_PROTO, Puma::Const::HTTP_X_FORWARDED_SCHEME, Puma::Const::HTTP_X_FORWARDED_SSL, Puma::Const::IANA_HTTP_METHODS, Puma::Const::ILLEGAL_HEADER_KEY_REGEX, Puma::Const::ILLEGAL_HEADER_VALUE_REGEX, Puma::Const::KEEP_ALIVE, Puma::Const::LINE_END, Puma::Const::LOCALHOST, Puma::Const::LOCALHOST_IPV4, Puma::Const::LOCALHOST_IPV6, Puma::Const::MAX_BODY, Puma::Const::MAX_HEADER, Puma::Const::NEWLINE, Puma::Const::PATH_INFO, Puma::Const::PORT_443, Puma::Const::PORT_80, Puma::Const::PROXY_PROTOCOL_V1_MAX_LENGTH, Puma::Const::PROXY_PROTOCOL_V1_REGEX, Puma::Const::PUMA_CONFIG, Puma::Const::PUMA_PEERCERT, Puma::Const::PUMA_SERVER_STRING, Puma::Const::PUMA_SOCKET, Puma::Const::PUMA_TMP_BASE, Puma::Const::PUMA_UNDERSCORE_HEADERS, Puma::Const::PUMA_VERSION, Puma::Const::QUERY_STRING, Puma::Const::RACK_AFTER_REPLY, Puma::Const::RACK_INPUT, Puma::Const::RACK_RESPONSE_FINISHED, Puma::Const::RACK_URL_SCHEME, Puma::Const::REMOTE_ADDR, Puma::Const::REQUEST_METHOD, Puma::Const::REQUEST_PATH, Puma::Const::REQUEST_URI, Puma::Const::RESTART_COMMAND, Puma::Const::SERVER_NAME, Puma::Const::SERVER_PORT, Puma::Const::SERVER_PROTOCOL, Puma::Const::SERVER_SOFTWARE, Puma::Const::STOP_COMMAND, Puma::Const::SUPPORTED_HTTP_METHODS, Puma::Const::TRANSFER_ENCODING, Puma::Const::TRANSFER_ENCODING2, Puma::Const::TRANSFER_ENCODING_CHUNKED, Puma::Const::UNMASKABLE_HEADERS, Puma::Const::UNSPECIFIED_IPV4, Puma::Const::UNSPECIFIED_IPV6, Puma::Const::WRITE_TIMEOUT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClientEnv

#default_server_port, #normalize_env, #req_env_post_parse, #validate_env

Constructor Details

#initialize(io, env = nil) ⇒ Client

Returns a new instance of Client.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/puma/client.rb', line 99

def initialize(io, env=nil)
  @io = io
  @to_io = io.to_io
  @io_buffer = IOBuffer.new
  @proto_env = env
  @env = env&.dup

  @parser = HttpParser.new
  @parsed_bytes = 0
  @read_header = true
  @read_proxy = false
  @ready = false

  @body = nil
  @body_read_start = nil
  @buffer = nil
  @tempfile = nil

  @timeout_at = nil

  @requests_served = 0
  @hijacked = false

  @http_content_length_limit = nil
  @http_content_length_limit_exceeded = nil
  @allow_underscore_headers = true
  @continue_callback = nil
  @error_status_code = nil
  @reactor_error = nil

  @peerip = nil
  @peer_family = nil
  @listener = nil
  @remote_addr_header = nil
  @expect_proxy_proto = false

  @body_remain = 0
  @bytes_read = 0
  @pending_write = nil

  @in_last_chunk = false

  # need unfrozen ASCII-8BIT, +'' is UTF-8
  @read_buffer = String.new # rubocop: disable Performance/UnfreezeString
end

Instance Attribute Details

#allow_underscore_headers=(value) ⇒ Object (writeonly)

Sets the attribute allow_underscore_headers

Parameters:

  • value —

    the value to set the attribute allow_underscore_headers to.



93
94
95
# File 'lib/puma/client.rb', line 93

def allow_underscore_headers=(value)
  @allow_underscore_headers = value
end

#body ⇒ Object (readonly)

Returns the value of attribute body.



89
90
91
# File 'lib/puma/client.rb', line 89

def body
  @body
end

#bytes_read ⇒ Object (readonly)

Returns the value of attribute bytes_read.



89
90
91
# File 'lib/puma/client.rb', line 89

def bytes_read
  @bytes_read
end

#continue_callback=(value) ⇒ Object (writeonly)

Sets the attribute continue_callback

Parameters:

  • value —

    the value to set the attribute continue_callback to.



93
94
95
# File 'lib/puma/client.rb', line 93

def continue_callback=(value)
  @continue_callback = value
end

#env ⇒ Object (readonly)

Returns the value of attribute env.



89
90
91
# File 'lib/puma/client.rb', line 89

def env
  @env
end

#env_set_http_version ⇒ Object

Returns the value of attribute env_set_http_version.



96
97
98
# File 'lib/puma/client.rb', line 96

def env_set_http_version
  @env_set_http_version
end

#error_status_code ⇒ Object (readonly)

Returns the value of attribute error_status_code.



89
90
91
# File 'lib/puma/client.rb', line 89

def error_status_code
  @error_status_code
end

#hijacked ⇒ Object (readonly)

Returns the value of attribute hijacked.



89
90
91
# File 'lib/puma/client.rb', line 89

def hijacked
  @hijacked
end

#http_content_length_limit=(value) ⇒ Object (writeonly)

Sets the attribute http_content_length_limit

Parameters:

  • value —

    the value to set the attribute http_content_length_limit to.



93
94
95
# File 'lib/puma/client.rb', line 93

def http_content_length_limit=(value)
  @http_content_length_limit = value
end

#http_content_length_limit_exceeded ⇒ Object (readonly)

Returns the value of attribute http_content_length_limit_exceeded.



89
90
91
# File 'lib/puma/client.rb', line 89

def http_content_length_limit_exceeded
  @http_content_length_limit_exceeded
end

#in_data_phase ⇒ Object (readonly)



169
170
171
# File 'lib/puma/client.rb', line 169

def in_data_phase
  !(@read_header || @read_proxy)
end

#inspect ⇒ Object (readonly)



157
158
159
# File 'lib/puma/client.rb', line 157

def inspect
  "#<Puma::Client:0x#{object_id.to_s(16)} @ready=#{@ready.inspect}>"
end

#io ⇒ Object (readonly)

Returns the value of attribute io.



89
90
91
# File 'lib/puma/client.rb', line 89

def io
  @io
end

#io_buffer ⇒ Object (readonly)

Returns the value of attribute io_buffer.



89
90
91
# File 'lib/puma/client.rb', line 89

def io_buffer
  @io_buffer
end

#listener ⇒ Object

Returns the value of attribute listener.



96
97
98
# File 'lib/puma/client.rb', line 96

def listener
  @listener
end

#peerip ⇒ Object



454
455
456
457
458
459
460
461
462
463
464
# File 'lib/puma/client.rb', line 454

def peerip
  return @peerip if @peerip

  if @remote_addr_header
    hdr = (@env[@remote_addr_header] || socket_peerip).split(/[\s,]/).first
    @peerip = hdr
    return hdr
  end

  @peerip ||= socket_peerip
end

#reactor_error ⇒ Object

Returns the value of attribute reactor_error.



96
97
98
# File 'lib/puma/client.rb', line 96

def reactor_error
  @reactor_error
end

#ready ⇒ Object (readonly)

Returns the value of attribute ready.



89
90
91
# File 'lib/puma/client.rb', line 89

def ready
  @ready
end

#remote_addr_header ⇒ Object

Returns the value of attribute remote_addr_header.



96
97
98
# File 'lib/puma/client.rb', line 96

def remote_addr_header
  @remote_addr_header
end

#requests_served ⇒ Object (readonly)

Returns the value of attribute requests_served.



89
90
91
# File 'lib/puma/client.rb', line 89

def requests_served
  @requests_served
end

#supported_http_methods=(value) ⇒ Object (writeonly)

Sets the attribute supported_http_methods

Parameters:

  • value —

    the value to set the attribute supported_http_methods to.



93
94
95
# File 'lib/puma/client.rb', line 93

def supported_http_methods=(value)
  @supported_http_methods = value
end

#tempfile ⇒ Object (readonly)

Returns the value of attribute tempfile.



89
90
91
# File 'lib/puma/client.rb', line 89

def tempfile
  @tempfile
end

#timeout ⇒ Object (readonly)

Number of seconds until the timeout elapses.



179
180
181
# File 'lib/puma/client.rb', line 179

def timeout
  [@timeout_at - Process.clock_gettime(Process::CLOCK_MONOTONIC), 0].max
end

#timeout_at ⇒ Object (readonly)

Returns the value of attribute timeout_at.



89
90
91
# File 'lib/puma/client.rb', line 89

def timeout_at
  @timeout_at
end

#to_io ⇒ Object (readonly)

Returns the value of attribute to_io.



89
90
91
# File 'lib/puma/client.rb', line 89

def to_io
  @to_io
end

Instance Method Details

#can_close? ⇒ Boolean

Returns true if the persistent connection can be closed immediately without waiting for the configured idle/shutdown timeout.

Returns:

  • (Boolean)

Version:

  • 5.0.0



480
481
482
483
# File 'lib/puma/client.rb', line 480

def can_close?
  # Allow connection to close if we're not in the middle of parsing a request.
  @parsed_bytes == 0
end

#close ⇒ Object



215
216
217
218
219
220
221
# File 'lib/puma/client.rb', line 215

def close
  tempfile_close
  begin
    @io.close
  rescue IOError, Errno::EBADF
  end
end

#closed? ⇒ Boolean

Remove in Puma 7?

Returns:

  • (Boolean)


146
147
148
# File 'lib/puma/client.rb', line 146

def closed?
  @to_io.closed?
end

#eagerly_finish(&deadline) ⇒ Object



308
309
310
311
312
313
314
315
316
317
# File 'lib/puma/client.rb', line 308

def eagerly_finish(&deadline)
  return true if @ready
  while !write_pending? && @to_io.wait_readable(0)
    read = @bytes_read
    return true if try_to_finish(&deadline)
    check_deadline(&deadline) if deadline
    return false if @bytes_read == read
  end
  false
end

#expect_proxy_proto=(val) ⇒ Object



485
486
487
488
489
490
491
492
493
494
# File 'lib/puma/client.rb', line 485

def expect_proxy_proto=(val)
  if val
    if @read_header
      @read_proxy = true
    end
  else
    @read_proxy = false
  end
  @expect_proxy_proto = val
end

#finish(timeout, &deadline) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/puma/client.rb', line 319

def finish(timeout, &deadline)
  return if @ready
  set_timeout timeout
  read = @bytes_read
  until try_to_finish(&deadline)
    if @bytes_read > read
      set_timeout timeout
      read = @bytes_read
    elsif self.timeout == 0
      timeout!
    end
    if deadline
      check_deadline(&deadline)
      limit = yield
      wait = [self.timeout, limit ? limit - Process.clock_gettime(Process::CLOCK_MONOTONIC) : 0.1].min
      wait_io(wait) if wait > 0
    else
      wait_io(self.timeout) || timeout!
    end
  end
end

#full_hijack ⇒ Object

For the full hijack protocol, env['rack.hijack'] is set to client.method :full_hijack



163
164
165
166
# File 'lib/puma/client.rb', line 163

def full_hijack
  @hijacked = true
  env[HIJACK_IO] ||= @io
end

#has_back_to_back_requests? ⇒ Boolean

if a client sends back-to-back requests, the buffer may contain one or more of them.

Returns:

  • (Boolean)


211
212
213
# File 'lib/puma/client.rb', line 211

def has_back_to_back_requests?
  !(@buffer.nil? || @buffer.empty?)
end

#io_ok? ⇒ Boolean

Test to see if io meets a bare minimum of functioning, @to_io needs to be used for MiniSSL::Socket

Returns:

  • (Boolean)


152
153
154
# File 'lib/puma/client.rb', line 152

def io_ok?
  @to_io.is_a?(::BasicSocket) && !closed?
end

#parser_execute ⇒ Integer

Wraps @parser.execute and adds meaningful error messages

Returns:

  • (Integer) —

    bytes of buffer read by parser



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/puma/client.rb', line 360

def parser_execute
  ret = @parser.execute(@env, @buffer, @parsed_bytes)

  if @env[REQUEST_METHOD] && @supported_http_methods != :any && !@supported_http_methods.key?(@env[REQUEST_METHOD])
    raise HttpParserError501, "#{@env[REQUEST_METHOD]} method is not supported"
  end
  ret
rescue => e
  @env[HTTP_CONNECTION] = 'close'
  raise e unless HttpParserError === e && e.message.include?('non-SSL')

  req, _ = @buffer.split "\r\n\r\n"
  raise e if req.nil? || req.empty?

  request_line, headers = req.split "\r\n", 2

  # below checks for request issues and changes error message accordingly
  if !@env.key? REQUEST_METHOD
    if request_line.count(' ') != 2
       # maybe this is an SSL connection ?
      raise e
    else
      method = request_line[/\A[^ ]+/]
      raise e, "Invalid HTTP format, parsing fails. Bad method #{method}"
    end
  elsif !@env.key? REQUEST_PATH
    path = request_line[/\A[^ ]+ +([^ ?\r\n]+)/, 1]
    raise e, "Invalid HTTP format, parsing fails. Bad path #{path}"
  elsif request_line.match?(/\A[^ ]+ +[^ ?\r\n]+\?/) && !@env.key?(QUERY_STRING)
    query = request_line[/\A[^ ]+ +[^? ]+\?([^ ]+)/, 1]
    raise e, "Invalid HTTP format, parsing fails. Bad query #{query}"
  elsif !@env.key? SERVER_PROTOCOL
    # protocol is bad
    text = request_line[/[^ ]*\z/]
    raise HttpParserError, "Invalid HTTP format, parsing fails. Bad protocol #{text}"
  elsif !headers.empty?
    # headers are bad
    hdrs = headers.split("\r\n").map { |h| h.gsub "\n", '\n'}.join "\n"
    raise HttpParserError, "Invalid HTTP format, parsing fails. Bad headers\n#{hdrs}"
  end
end

#peer_family ⇒ Object



466
467
468
469
470
471
472
473
474
# File 'lib/puma/client.rb', line 466

def peer_family
  return @peer_family if @peer_family

  @peer_family ||= begin
                     @io.local_address.afamily
                   rescue
                     Socket::AF_INET
                   end
end

#process_back_to_back_requests ⇒ Object

only used with back-to-back requests contained in the buffer



199
200
201
202
203
204
205
206
207
# File 'lib/puma/client.rb', line 199

def process_back_to_back_requests
  if @buffer
    return false unless try_to_parse_proxy_protocol

    @parsed_bytes = parser_execute

    @parser.finished? ? process_env_body : false
  end
end

#process_env_body ⇒ Object

processes the env and the request body

Raises:



403
404
405
406
407
408
409
410
# File 'lib/puma/client.rb', line 403

def process_env_body
  validate_env
  temp = setup_body
  normalize_env
  req_env_post_parse
  raise HttpParserError if @error_status_code
  temp
end

#reset ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/puma/client.rb', line 183

def reset
  @parser.reset
  @io_buffer.reset
  @read_header = true
  @read_proxy = !!@expect_proxy_proto && @requests_served.zero?
  @env = @proto_env.dup
  @parsed_bytes = 0
  @ready = false
  @body_remain = 0
  @peerip = nil if @remote_addr_header
  @in_last_chunk = false
  @http_content_length_limit_exceeded = nil
  @error_status_code = nil
end

#set_timeout(val) ⇒ Object



173
174
175
# File 'lib/puma/client.rb', line 173

def set_timeout(val)
  @timeout_at = Process.clock_gettime(Process::CLOCK_MONOTONIC) + val
end

#take_pending_write ⇒ Object



345
346
347
348
349
# File 'lib/puma/client.rb', line 345

def take_pending_write
  str = @pending_write
  @pending_write = nil
  str
end

#tempfile_close ⇒ Object



223
224
225
226
227
228
229
230
# File 'lib/puma/client.rb', line 223

def tempfile_close
  tf_path = @tempfile&.path
  @tempfile&.close
  File.unlink(tf_path) if tf_path
  @tempfile = nil
  @body = nil
rescue Errno::ENOENT, IOError
end

#timeout! ⇒ Object

Raises:



412
413
414
415
# File 'lib/puma/client.rb', line 412

def timeout!
  write_error(408) if in_data_phase
  raise ConnectionError
end

#try_to_finish(&deadline) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/puma/client.rb', line 272

def try_to_finish(&deadline)
  return false unless flush_pending_write
  return read_body(&deadline) if in_data_phase

  data = nil
  begin
    data = @io.read_nonblock(CHUNK_SIZE)
  rescue IO::WaitReadable
    return false
  rescue EOFError
    # Swallow error, don't log
  rescue SystemCallError, IOError
    raise ConnectionError, "Connection error detected during read"
  end

  # No data means a closed socket
  unless data
    @buffer = nil
    set_ready
    raise EOFError
  end

  @bytes_read += data.bytesize
  if @buffer
    @buffer << data
  else
    @buffer = data
  end

  return false unless try_to_parse_proxy_protocol

  @parsed_bytes = parser_execute

  @parser.finished? ? process_env_body : false
end

#try_to_parse_proxy_protocol ⇒ Object

If necessary, read the PROXY protocol from the buffer. Returns false if more data is needed.



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/puma/client.rb', line 234

def try_to_parse_proxy_protocol
  if @read_proxy
    if @expect_proxy_proto == :v1
      crlf_index = @buffer.index "\r\n"

      unless crlf_index
        if "PROXY ".start_with? @buffer
          return false
        elsif @buffer.start_with? "PROXY "
          if @buffer.bytesize >= PROXY_PROTOCOL_V1_MAX_LENGTH
            raise ConnectionError, "PROXY protocol v1 line is too long"
          end
          return false
        end

        @read_proxy = false
        return true
      end

      if @buffer.start_with?("PROXY ") && crlf_index + 2 > PROXY_PROTOCOL_V1_MAX_LENGTH
        raise ConnectionError, "PROXY protocol v1 line is too long"
      end

      if md = PROXY_PROTOCOL_V1_REGEX.match(@buffer)
        if md[1]
          @peerip = md[1].split(" ")[0]
        end
        @buffer = md.post_match
      end
      # if the buffer has a \r\n but doesn't have a PROXY protocol
      # request, this is just HTTP from a non-PROXY client; move on
      @read_proxy = false
      return @buffer.size > 0
    end
  end
  true
end

#write_error(status_code) ⇒ Object



417
418
419
420
421
422
# File 'lib/puma/client.rb', line 417

def write_error(status_code)
  begin
    write_interim ERROR_RESPONSE[status_code]
  rescue StandardError
  end
end

#write_pending? ⇒ Boolean

Returns:

  • (Boolean)


341
342
343
# File 'lib/puma/client.rb', line 341

def write_pending?
  !@pending_write.nil? || (@io.respond_to?(:write_pending?) && @io.write_pending?)
end