Class: Net::AJP13::Server::BodyInput

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Constants
Defined in:
lib/net/ajp13server.rb

Overview

Input stream that corresponds the request body from the web server. BodyInput object acts as an IO except writing methods.

Constant Summary collapse

GETS_BLOCK_SIZE =

:nodoc:

256

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sock, length) ⇒ BodyInput

sock

socket connection to the web server

length

Content-Length



240
241
242
243
244
245
# File 'lib/net/ajp13server.rb', line 240

def initialize(sock, length)
  @sock = sock
  @packet = Net::AJP13::Packet.from_io(sock)
  @length = length
  @read_length = 0
end

Instance Attribute Details

#lengthObject (readonly) Also known as: size

Content-Length



248
249
250
# File 'lib/net/ajp13server.rb', line 248

def length
  @length
end

Instance Method Details

#binmodeObject

Does nothing



252
# File 'lib/net/ajp13server.rb', line 252

def binmode; self end

#cloneObject Also known as: dup

Raises TypeError. You can’t clone BodyInput.

Raises:

  • (TypeError)


255
256
257
# File 'lib/net/ajp13server.rb', line 255

def clone
  raise TypeError, "can't clone #{self.class}"
end

#closeObject Also known as: close_read

Closes the BodyInput. Note that this method does not close the internal socket connection.



262
263
264
# File 'lib/net/ajp13server.rb', line 262

def close
  @packet = @sock = nil
end

#closed?Boolean

Returns true if the BodyInput is closed.

Returns:

  • (Boolean)


267
268
269
# File 'lib/net/ajp13server.rb', line 267

def closed?
  @sock.nil?
end

#each_byteObject

Same as IO#each_byte



273
274
275
276
277
# File 'lib/net/ajp13server.rb', line 273

def each_byte
  while ch = getc
    yield ch
  end
end

#each_line(rs = $/) ⇒ Object Also known as: each



401
402
403
404
405
# File 'lib/net/ajp13server.rb', line 401

def each_line(rs = $/)
  while line = gets(rs)
    yield line
  end
end

#eof?Boolean Also known as: eof

Returns:

  • (Boolean)


279
280
281
# File 'lib/net/ajp13server.rb', line 279

def eof?
  @read_length >= @length 
end

#fcntl(*args) ⇒ Object

Raises NotImplementedError

Raises:

  • (NotImplementedError)


285
286
287
# File 'lib/net/ajp13server.rb', line 285

def fcntl(*args)
  raise NotImplementedError, "#{self} does not support fcntl"
end

#filenoObject Also known as: to_i

Always returns nil



294
# File 'lib/net/ajp13server.rb', line 294

def fileno; nil end

#getcObject



297
298
299
300
# File 'lib/net/ajp13server.rb', line 297

def getc
  str = read(1)
  str and str[0]
end

#gets(rs = $/) ⇒ Object



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/net/ajp13server.rb', line 373

def gets(rs = $/)
  return read if rs.nil?
  @lineno ||= 0
  @line ||= ''
  pattern = /\A.+?#{rs=='' ? "\\r?\\n\\r?\\n" : Regexp.escape(rs)}/
  until md = pattern.match(@line)
    block = read(GETS_BLOCK_SIZE)
	if block.nil?
	  line = @line
	  @line = nil
	  @lineno += 1
	  return line == '' ? nil : line
	else
      @line << block
	end
  end
  @line = md.post_match
  @lineno += 1
  return md.to_s
end

#iocntl(*args) ⇒ Object

Raises NotImplementedError

Raises:

  • (NotImplementedError)


289
290
291
# File 'lib/net/ajp13server.rb', line 289

def iocntl(*args)
  raise NotImplementedError, "#{self} does not support iocntl"
end

#isattyObject Also known as: tty?

Returns false



303
# File 'lib/net/ajp13server.rb', line 303

def isatty; false end

#linenoObject



306
# File 'lib/net/ajp13server.rb', line 306

def lineno; @lineno end

#pidObject

Returns nil



309
# File 'lib/net/ajp13server.rb', line 309

def pid; nil end

#posObject Also known as: tell

Returns current read position.



312
# File 'lib/net/ajp13server.rb', line 312

def pos; @read_length end

#read(length = nil, buf = '') ⇒ Object Also known as: sysread

:args: [length[, buf]]

Raises:

  • (TypeError)


315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
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
# File 'lib/net/ajp13server.rb', line 315

def read(length = nil, buf = '') #:args: [length[, buf]]
  raise TypeError, "can't modify frozen stream" if frozen?
  raise IOError, 'closed stream' if closed?
  if length.nil?
    return '' if eof?
	length = [0, @length - @read_length].max
  else
    raise ArgumentError, "negative length #{length} given" if length < 0
    if eof?
      buf[0..-1] = ''
	  return nil
	end
  end

  if @packet.eof?
    written_length = 0
  else
    chunk = @packet.read_bytes(length)
    written_length = chunk.length
	@read_length += written_length
    buf[0, written_length] = chunk
  end
  while written_length < length and !eof?
    packet = Net::AJP13::Packet.new
	packet.direction = :from_app
	packet.append_byte GET_BODY_CHUNK
	packet.append_integer [@length - @read_length, MAX_BODY_CHUNK_SIZE].min
	packet.send_to @sock

    @packet = Net::AJP13::Packet.from_io(@sock)
	if @packet.length == 0
	  # this means eof
	  break
	else
	  chunk = @packet.read_bytes(length - written_length)
	  buf[written_length, chunk.length] = chunk
	  written_length += chunk.length
	  @read_length += chunk.length
	end
  end
  if written_length < buf.length
    buf[written_length..-1] = ''
  end
  
  return buf
end

#readcharObject



363
364
365
366
367
368
369
370
# File 'lib/net/ajp13server.rb', line 363

def readchar
  str = read(1)
  if str.nil?
    raise EOFError
  else
    str[0]
  end
end

#readline(rs = $/) ⇒ Object



393
394
395
396
397
398
399
400
# File 'lib/net/ajp13server.rb', line 393

def readline(rs = $/)
  line = gets(rs)
  if line.nil? 
    raise EOFError
  else
    line
  end
end

#reopen(*args) ⇒ Object

Raises NotImplementedError

Raises:

  • (NotImplementedError)


409
410
411
# File 'lib/net/ajp13server.rb', line 409

def reopen(*args)
  raise NotImplementedError, "#{self.class} does not support reopen"
end

#syncObject



413
# File 'lib/net/ajp13server.rb', line 413

def sync; @sock.sync end

#sync=(val) ⇒ Object



414
# File 'lib/net/ajp13server.rb', line 414

def sync=(val); @sock.sync = val end

#to_ioObject

Returns self



419
# File 'lib/net/ajp13server.rb', line 419

def to_io; self end

#ungetc(char) ⇒ Object

Raises:

  • (TypeError)


421
422
423
424
425
426
427
428
# File 'lib/net/ajp13server.rb', line 421

def ungetc(char)
  raise TypeError, "#{char} is not a Fixnum" unless char.is_a? Fixnum
  raise ArgumentError, "#{char} must be a byte, but negative" if char < 0
  raise ArgumentError, "#{char} is too large to treat as a byte" if char > 0xFF
  @packet.unread_byte(char)
  @read_length -= 1
  nil
end