Class: HTTP::Message::Body
- Inherits:
-
Object
- Object
- HTTP::Message::Body
- Defined in:
- lib/httpclient/http.rb
Overview
Represents HTTP message body.
Defined Under Namespace
Classes: Parts
Constant Summary collapse
- DEFAULT_CHUNK_SIZE =
Default value for chunk_size
1024 * 16
Instance Attribute Summary collapse
-
#chunk_size ⇒ Object
maxbytes of IO#read for streaming request.
-
#size ⇒ Object
readonly
Size of body.
Instance Method Summary collapse
-
#content ⇒ Object
Returns a message body itself.
-
#dump(header = '', dev = '') ⇒ Object
Dumps message body to given dev.
-
#dump_chunked(header = '', dev = '') ⇒ Object
Dumps message body with chunked encoding to given dev.
-
#init_request(body = nil, boundary = nil) ⇒ Object
Initialize this instance as a request.
-
#init_response(body = nil) ⇒ Object
Initialize this instance as a response.
-
#initialize ⇒ Body
constructor
Creates a Message::Body.
Constructor Details
#initialize ⇒ Body
Creates a Message::Body. Use init_request or init_response for acutual initialize.
408 409 410 411 412 413 |
# File 'lib/httpclient/http.rb', line 408 def initialize @body = nil @size = nil @positions = nil @chunk_size = nil end |
Instance Attribute Details
#chunk_size ⇒ Object
maxbytes of IO#read for streaming request. See DEFAULT_CHUNK_SIZE.
401 402 403 |
# File 'lib/httpclient/http.rb', line 401 def chunk_size @chunk_size end |
#size ⇒ Object (readonly)
Size of body. nil when size is unknown (e.g. chunked response).
399 400 401 |
# File 'lib/httpclient/http.rb', line 399 def size @size end |
Instance Method Details
#content ⇒ Object
Returns a message body itself.
493 494 495 |
# File 'lib/httpclient/http.rb', line 493 def content @body end |
#dump(header = '', dev = '') ⇒ Object
Dumps message body to given dev. dev needs to respond to <<.
Message header must be given as the first argument for performance reason. (header is dumped to dev, too) If no dev (the second argument) given, this method returns a dumped String.
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
# File 'lib/httpclient/http.rb', line 442 def dump(header = '', dev = '') if @body.is_a?(Parts) dev << header buf = '' @body.parts.each do |part| if Message.file?(part) reset_pos(part) while !part.read(@chunk_size, buf).nil? dev << buf end part.rewind else dev << part end end elsif @body dev << header + @body else dev << header end dev end |
#dump_chunked(header = '', dev = '') ⇒ Object
Dumps message body with chunked encoding to given dev. dev needs to respond to <<.
Message header must be given as the first argument for performance reason. (header is dumped to dev, too) If no dev (the second argument) given, this method returns a dumped String.
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 |
# File 'lib/httpclient/http.rb', line 472 def dump_chunked(header = '', dev = '') dev << header if @body.is_a?(Parts) @body.parts.each do |part| if Message.file?(part) reset_pos(part) dump_chunks(part, dev) else dev << dump_chunk(part) end end dev << (dump_last_chunk + CRLF) elsif @body reset_pos(@body) dump_chunks(@body, dev) dev << (dump_last_chunk + CRLF) end dev end |
#init_request(body = nil, boundary = nil) ⇒ Object
Initialize this instance as a request.
416 417 418 419 420 421 |
# File 'lib/httpclient/http.rb', line 416 def init_request(body = nil, boundary = nil) @boundary = boundary @positions = {} set_content(body, boundary) @chunk_size = DEFAULT_CHUNK_SIZE end |
#init_response(body = nil) ⇒ Object
Initialize this instance as a response.
424 425 426 427 428 429 430 431 432 433 |
# File 'lib/httpclient/http.rb', line 424 def init_response(body = nil) @body = body if @body.respond_to?(:bytesize) @size = @body.bytesize elsif @body.respond_to?(:size) @size = @body.size else @size = nil end end |