Class: Puma::Client

Inherits:
Object
  • Object
show all
Extended by:
Delegation
Includes:
Const
Defined in:
lib/puma/client.rb

Overview

An instance of this class represents a unique request from a client. For example a web request from a browser or from CURL. This

An instance of ‘Puma::Client` can be used as if it were an IO object for example it is passed into `IO.select` inside of the `Puma::Reactor`. This is accomplished by the `to_io` method which gets called on any non-IO objects being used with the IO api such as `IO.select.

Instances of this class are responsible for knowing if the header and body are fully buffered via the ‘try_to_finish` method. They can be used to “time out” a response via the `timeout_at` reader.

Constant Summary collapse

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::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::EARLY_HINTS, Puma::Const::ERROR_400_RESPONSE, Puma::Const::ERROR_404_RESPONSE, Puma::Const::ERROR_408_RESPONSE, Puma::Const::ERROR_500_RESPONSE, Puma::Const::ERROR_503_RESPONSE, Puma::Const::FAST_TRACK_KA_TIMEOUT, Puma::Const::FIRST_DATA_TIMEOUT, 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_HOST, Puma::Const::HTTP_INJECTION_REGEX, Puma::Const::HTTP_VERSION, Puma::Const::HTTP_X_FORWARDED_FOR, Puma::Const::KEEP_ALIVE, Puma::Const::LINE_END, Puma::Const::LOCALHOST, Puma::Const::LOCALHOST_ADDR, Puma::Const::LOCALHOST_IP, Puma::Const::MAX_BODY, Puma::Const::MAX_FAST_INLINE, Puma::Const::MAX_HEADER, Puma::Const::NEWLINE, Puma::Const::PATH_INFO, Puma::Const::PERSISTENT_TIMEOUT, Puma::Const::PORT_443, Puma::Const::PORT_80, 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_VERSION, Puma::Const::QUERY_STRING, Puma::Const::RACK_AFTER_REPLY, Puma::Const::RACK_INPUT, 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::TRANSFER_ENCODING, Puma::Const::TRANSFER_ENCODING2, Puma::Const::TRANSFER_ENCODING_CHUNKED, Puma::Const::WRITE_TIMEOUT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Delegation

forward

Constructor Details

#initialize(io, env = nil) ⇒ Client

Returns a new instance of Client.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/puma/client.rb', line 41

def initialize(io, env=nil)
  @io = io
  @to_io = io.to_io
  @proto_env = env
  if !env
    @env = nil
  else
    @env = env.dup
  end

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

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

  @timeout_at = nil

  @requests_served = 0
  @hijacked = false

  @peerip = nil
  @remote_addr_header = nil
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



69
70
71
# File 'lib/puma/client.rb', line 69

def body
  @body
end

#envObject (readonly)

Returns the value of attribute env.



69
70
71
# File 'lib/puma/client.rb', line 69

def env
  @env
end

#hijackedObject (readonly)

Returns the value of attribute hijacked.



69
70
71
# File 'lib/puma/client.rb', line 69

def hijacked
  @hijacked
end

#ioObject (readonly)

Returns the value of attribute io.



69
70
71
# File 'lib/puma/client.rb', line 69

def io
  @io
end

#peeripObject



468
469
470
471
472
473
474
475
476
477
478
# File 'lib/puma/client.rb', line 468

def peerip
  return @peerip if @peerip

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

  @peerip ||= @io.peeraddr.last
end

#readyObject (readonly)

Returns the value of attribute ready.



69
70
71
# File 'lib/puma/client.rb', line 69

def ready
  @ready
end

#remote_addr_headerObject

Returns the value of attribute remote_addr_header.



74
75
76
# File 'lib/puma/client.rb', line 74

def remote_addr_header
  @remote_addr_header
end

#tempfileObject (readonly)

Returns the value of attribute tempfile.



69
70
71
# File 'lib/puma/client.rb', line 69

def tempfile
  @tempfile
end

#timeout_atObject (readonly)

Returns the value of attribute timeout_at.



69
70
71
# File 'lib/puma/client.rb', line 69

def timeout_at
  @timeout_at
end

#to_ioObject (readonly)

Returns the value of attribute to_io.



69
70
71
# File 'lib/puma/client.rb', line 69

def to_io
  @to_io
end

Instance Method Details

#callObject

For the hijack protocol (allows us to just put the Client object into the env)



84
85
86
87
# File 'lib/puma/client.rb', line 84

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

#closeObject



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

def close
  begin
    @io.close
  rescue IOError
    Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
  end
end

#decode_chunk(chunk) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/puma/client.rb', line 147

def decode_chunk(chunk)
  if @partial_part_left > 0
    if @partial_part_left <= chunk.size
      @body << chunk[0..(@partial_part_left-3)] # skip the \r\n
      chunk = chunk[@partial_part_left..-1]
    else
      @body << chunk
      @partial_part_left -= chunk.size
      return false
    end
  end

  if @prev_chunk.empty?
    io = StringIO.new(chunk)
  else
    io = StringIO.new(@prev_chunk+chunk)
    @prev_chunk = ""
  end

  while !io.eof?
    line = io.gets
    if line.end_with?("\r\n")
      len = line.strip.to_i(16)
      if len == 0
        @body.rewind
        rest = io.read
        rest = rest[2..-1] if rest.start_with?("\r\n")
        @buffer = rest.empty? ? nil : rest
        @requests_served += 1
        @ready = true
        return true
      end

      len += 2

      part = io.read(len)

      unless part
        @partial_part_left = len
        next
      end

      got = part.size

      case
      when got == len
        @body << part[0..-3] # to skip the ending \r\n
      when got <= len - 2
        @body << part
        @partial_part_left = len - part.size
      when got == len - 1 # edge where we get just \r but not \n
        @body << part[0..-2]
        @partial_part_left = len - part.size
      end
    else
      @prev_chunk = line
      return false
    end
  end

  return false
end

#eagerly_finishObject



372
373
374
375
376
377
378
379
380
381
# File 'lib/puma/client.rb', line 372

def eagerly_finish
  return true if @ready

  if @io.kind_of? OpenSSL::SSL::SSLSocket
    return true if jruby_start_try_to_finish
  end

  return false unless IO.select([@to_io], nil, nil, 0)
  try_to_finish
end

#finishObject

IS_JRUBY



392
393
394
395
396
397
398
# File 'lib/puma/client.rb', line 392

def finish
  return true if @ready
  until try_to_finish
    IO.select([@to_io], nil, nil)
  end
  true
end

#in_data_phaseObject



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

def in_data_phase
  !@read_header
end

#inspectObject



78
79
80
# File 'lib/puma/client.rb', line 78

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

#jruby_start_try_to_finishObject



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/puma/client.rb', line 336

def jruby_start_try_to_finish
  return read_body unless @read_header

  begin
    data = @io.sysread_nonblock(CHUNK_SIZE)
  rescue OpenSSL::SSL::SSLError => e
    return false if e.kind_of? IO::WaitReadable
    raise e
  end

  # No data means a closed socket
  unless data
    @buffer = nil
    @requests_served += 1
    @ready = true
    raise EOFError
  end

  if @buffer
    @buffer << data
  else
    @buffer = data
  end

  @parsed_bytes = @parser.execute(@env, @buffer, @parsed_bytes)

  if @parser.finished?
    return setup_body
  elsif @parsed_bytes >= MAX_HEADER
    raise HttpParserError,
      "HEADER is longer than allowed, aborting client early."
  end

  false
end

#read_bodyObject



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/puma/client.rb', line 400

def read_body
  if @chunked_body
    return read_chunked_body
  end

  # Read an odd sized chunk so we can read even sized ones
  # after this
  remain = @body_remain

  if remain > CHUNK_SIZE
    want = CHUNK_SIZE
  else
    want = remain
  end

  begin
    chunk = @io.read_nonblock(want)
  rescue Errno::EAGAIN
    return false
  rescue SystemCallError, IOError
    raise ConnectionError, "Connection error detected during read"
  end

  # No chunk means a closed socket
  unless chunk
    @body.close
    @buffer = nil
    @requests_served += 1
    @ready = true
    raise EOFError
  end

  remain -= @body.write(chunk)

  if remain <= 0
    @body.rewind
    @buffer = nil
    @requests_served += 1
    @ready = true
    return true
  end

  @body_remain = remain

  false
end

#read_chunked_bodyObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/puma/client.rb', line 210

def read_chunked_body
  while true
    begin
      chunk = @io.read_nonblock(4096)
    rescue Errno::EAGAIN
      return false
    rescue SystemCallError, IOError
      raise ConnectionError, "Connection error detected during read"
    end

    # No chunk means a closed socket
    unless chunk
      @body.close
      @buffer = nil
      @requests_served += 1
      @ready = true
      raise EOFError
    end

    return true if decode_chunk(chunk)
  end
end

#reset(fast_check = true) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/puma/client.rb', line 97

def reset(fast_check=true)
  @parser.reset
  @read_header = true
  @env = @proto_env.dup
  @body = nil
  @tempfile = nil
  @parsed_bytes = 0
  @ready = false

  if @buffer
    @parsed_bytes = @parser.execute(@env, @buffer, @parsed_bytes)

    if @parser.finished?
      return setup_body
    elsif @parsed_bytes >= MAX_HEADER
      raise HttpParserError,
        "HEADER is longer than allowed, aborting client early."
    end

    return false
  elsif fast_check &&
        IO.select([@to_io], nil, nil, FAST_TRACK_KA_TIMEOUT)
    return try_to_finish
  end
end

#set_timeout(val) ⇒ Object



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

def set_timeout(val)
  @timeout_at = Time.now + val
end

#setup_bodyObject



233
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
271
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
# File 'lib/puma/client.rb', line 233

def setup_body
  if @env[HTTP_EXPECT] == CONTINUE
    # TODO allow a hook here to check the headers before
    # going forward
    @io << HTTP_11_100
    @io.flush
  end

  @read_header = false

  body = @parser.body

  te = @env[TRANSFER_ENCODING2]

  if te
    if te.include?(",")
      te.split(",").each do |part|
        if CHUNKED.casecmp(part.strip) == 0
          return setup_chunked_body(body)
        end
      end
    elsif CHUNKED.casecmp(te) == 0
      return setup_chunked_body(body)
    end
  end

  @chunked_body = false

  cl = @env[CONTENT_LENGTH]

  unless cl
    @buffer = body.empty? ? nil : body
    @body = EmptyBody
    @requests_served += 1
    @ready = true
    return true
  end

  remain = cl.to_i - body.bytesize

  if remain <= 0
    @body = StringIO.new(body)
    @buffer = nil
    @requests_served += 1
    @ready = true
    return true
  end

  if remain > MAX_BODY
    @body = Tempfile.new(Const::PUMA_TMP_BASE)
    @body.binmode
    @tempfile = @body
  else
    # The body[0,0] trick is to get an empty string in the same
    # encoding as body.
    @body = StringIO.new body[0,0]
  end

  @body.write body

  @body_remain = remain

  return false
end

#setup_chunked_body(body) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/puma/client.rb', line 135

def setup_chunked_body(body)
  @chunked_body = true
  @partial_part_left = 0
  @prev_chunk = ""

  @body = Tempfile.new(Const::PUMA_TMP_BASE)
  @body.binmode
  @tempfile = @body

  return decode_chunk(body)
end

#try_to_finishObject



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/puma/client.rb', line 298

def try_to_finish
  return read_body unless @read_header

  begin
    data = @io.read_nonblock(CHUNK_SIZE)
  rescue Errno::EAGAIN
    return false
  rescue SystemCallError, IOError
    raise ConnectionError, "Connection error detected during read"
  end

  # No data means a closed socket
  unless data
    @buffer = nil
    @requests_served += 1
    @ready = true
    raise EOFError
  end

  if @buffer
    @buffer << data
  else
    @buffer = data
  end

  @parsed_bytes = @parser.execute(@env, @buffer, @parsed_bytes)

  if @parser.finished?
    return setup_body
  elsif @parsed_bytes >= MAX_HEADER
    raise HttpParserError,
      "HEADER is longer than allowed, aborting client early."
  end

  false
end

#write_400Object



447
448
449
450
451
452
# File 'lib/puma/client.rb', line 447

def write_400
  begin
    @io << ERROR_400_RESPONSE
  rescue StandardError
  end
end

#write_408Object



454
455
456
457
458
459
# File 'lib/puma/client.rb', line 454

def write_408
  begin
    @io << ERROR_408_RESPONSE
  rescue StandardError
  end
end

#write_500Object



461
462
463
464
465
466
# File 'lib/puma/client.rb', line 461

def write_500
  begin
    @io << ERROR_500_RESPONSE
  rescue StandardError
  end
end