Class: Net::AJP13::Server::BodyInput
- Inherits:
-
Object
- Object
- Net::AJP13::Server::BodyInput
- Includes:
- Enumerable, Constants
- Defined in:
- lib/net/ajp13/server.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
-
#length ⇒ Object
(also: #size)
readonly
Content-Length.
Instance Method Summary collapse
-
#binmode ⇒ Object
Does nothing.
-
#clone ⇒ Object
(also: #dup)
Raises TypeError.
-
#close ⇒ Object
(also: #close_read)
Closes the BodyInput.
-
#closed? ⇒ Boolean
Returns true if the BodyInput is closed.
-
#each_byte ⇒ Object
Same as IO#each_byte.
- #each_line(rs = $/) ⇒ Object (also: #each)
- #eof? ⇒ Boolean (also: #eof)
-
#fcntl(*args) ⇒ Object
Raises NotImplementedError.
-
#fileno ⇒ Object
(also: #to_i)
Always returns nil.
- #getc ⇒ Object
- #gets(rs = $/) ⇒ Object
-
#initialize(sock, length) ⇒ BodyInput
constructor
sock
- socket connection to the web server
length
-
Content-Length.
- socket connection to the web server
-
#iocntl(*args) ⇒ Object
Raises NotImplementedError.
-
#isatty ⇒ Object
(also: #tty?)
Returns false.
- #lineno ⇒ Object
-
#pid ⇒ Object
Returns nil.
-
#pos ⇒ Object
(also: #tell)
Returns current read position.
-
#read(length = nil, buf = '') ⇒ Object
(also: #sysread)
:args: [length[, buf]].
- #readchar ⇒ Object
- #readline(rs = $/) ⇒ Object
-
#reopen(*args) ⇒ Object
Raises NotImplementedError.
- #sync ⇒ Object
- #sync=(val) ⇒ Object
-
#to_io ⇒ Object
Returns self.
- #ungetc(char) ⇒ Object
Constructor Details
#initialize(sock, length) ⇒ BodyInput
sock
-
socket connection to the web server
length
-
Content-Length
256 257 258 259 260 261 262 |
# File 'lib/net/ajp13/server.rb', line 256 def initialize(sock, length) @sock = sock @packet = Net::AJP13::Packet.from_io(sock) @length = length packet_content_length = @packet.read_integer @read_length = 0 end |
Instance Attribute Details
#length ⇒ Object (readonly) Also known as: size
Content-Length
265 266 267 |
# File 'lib/net/ajp13/server.rb', line 265 def length @length end |
Instance Method Details
#binmode ⇒ Object
Does nothing
269 |
# File 'lib/net/ajp13/server.rb', line 269 def binmode; self end |
#clone ⇒ Object Also known as: dup
Raises TypeError. You can’t clone BodyInput.
272 273 274 |
# File 'lib/net/ajp13/server.rb', line 272 def clone raise TypeError, "can't clone #{self.class}" end |
#close ⇒ Object Also known as: close_read
Closes the BodyInput. Note that this method does not close the internal socket connection.
279 280 281 |
# File 'lib/net/ajp13/server.rb', line 279 def close @packet = @sock = nil end |
#closed? ⇒ Boolean
Returns true if the BodyInput is closed.
284 285 286 |
# File 'lib/net/ajp13/server.rb', line 284 def closed? @sock.nil? end |
#each_byte ⇒ Object
Same as IO#each_byte
290 291 292 293 294 |
# File 'lib/net/ajp13/server.rb', line 290 def each_byte while ch = getc yield ch end end |
#each_line(rs = $/) ⇒ Object Also known as: each
419 420 421 422 423 |
# File 'lib/net/ajp13/server.rb', line 419 def each_line(rs = $/) while line = gets(rs) yield line end end |
#eof? ⇒ Boolean Also known as: eof
296 297 298 |
# File 'lib/net/ajp13/server.rb', line 296 def eof? @read_length >= @length end |
#fcntl(*args) ⇒ Object
Raises NotImplementedError
302 303 304 |
# File 'lib/net/ajp13/server.rb', line 302 def fcntl(*args) raise NotImplementedError, "#{self} does not support fcntl" end |
#fileno ⇒ Object Also known as: to_i
Always returns nil
311 |
# File 'lib/net/ajp13/server.rb', line 311 def fileno; nil end |
#getc ⇒ Object
314 315 316 317 |
# File 'lib/net/ajp13/server.rb', line 314 def getc str = read(1) str and str[0] end |
#gets(rs = $/) ⇒ Object
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/net/ajp13/server.rb', line 391 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
306 307 308 |
# File 'lib/net/ajp13/server.rb', line 306 def iocntl(*args) raise NotImplementedError, "#{self} does not support iocntl" end |
#isatty ⇒ Object Also known as: tty?
Returns false
320 |
# File 'lib/net/ajp13/server.rb', line 320 def isatty; false end |
#lineno ⇒ Object
323 |
# File 'lib/net/ajp13/server.rb', line 323 def lineno; @lineno end |
#pid ⇒ Object
Returns nil
326 |
# File 'lib/net/ajp13/server.rb', line 326 def pid; nil end |
#pos ⇒ Object Also known as: tell
Returns current read position.
329 |
# File 'lib/net/ajp13/server.rb', line 329 def pos; @read_length end |
#read(length = nil, buf = '') ⇒ Object Also known as: sysread
:args: [length[, buf]]
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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/net/ajp13/server.rb', line 332 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 packet_content_length = @packet.read_integer chunk = @packet.read_bytes([length - written_length, packet_content_length].min) 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 |
#readchar ⇒ Object
381 382 383 384 385 386 387 388 |
# File 'lib/net/ajp13/server.rb', line 381 def readchar str = read(1) if str.nil? raise EOFError else str[0] end end |
#readline(rs = $/) ⇒ Object
411 412 413 414 415 416 417 418 |
# File 'lib/net/ajp13/server.rb', line 411 def readline(rs = $/) line = gets(rs) if line.nil? raise EOFError else line end end |
#reopen(*args) ⇒ Object
Raises NotImplementedError
427 428 429 |
# File 'lib/net/ajp13/server.rb', line 427 def reopen(*args) raise NotImplementedError, "#{self.class} does not support reopen" end |
#sync ⇒ Object
431 |
# File 'lib/net/ajp13/server.rb', line 431 def sync; @sock.sync end |
#sync=(val) ⇒ Object
432 |
# File 'lib/net/ajp13/server.rb', line 432 def sync=(val); @sock.sync = val end |
#to_io ⇒ Object
Returns self
437 |
# File 'lib/net/ajp13/server.rb', line 437 def to_io; self end |
#ungetc(char) ⇒ Object
439 440 441 442 443 444 445 446 |
# File 'lib/net/ajp13/server.rb', line 439 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 |