Parent

Namespace

Files

HTTP::Message::Body

Represents HTTP message body.

Constants

DEFAULT_CHUNK_SIZE

Default value for chunk_size

Attributes

size[R]

Size of body. nil when size is unknown (e.g. chunked response).

chunk_size[RW]

maxbytes of IO#read for streaming request. See DEFAULT_CHUNK_SIZE.

Public Class Methods

new() click to toggle source

Creates a Message::Body. Use init_request or init_response for acutual initialize.

     # File lib/httpclient/http.rb, line 399
399:       def initialize
400:         @body = nil
401:         @size = nil
402:         @positions = nil
403:         @chunk_size = nil
404:       end

Public Instance Methods

content() click to toggle source

Returns a message body itself.

     # File lib/httpclient/http.rb, line 481
481:       def content
482:         @body
483:       end
dump(header = '', dev = '') click to toggle source

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.

     # File lib/httpclient/http.rb, line 431
431:       def dump(header = '', dev = '')
432:         if @body.is_a?(Parts)
433:           dev << header
434:           buf = ''
435:           @body.parts.each do |part|
436:             if Message.file?(part)
437:               reset_pos(part)
438:               while !part.read(@chunk_size, buf).nil?
439:                 dev << buf
440:               end
441:             else
442:               dev << part
443:             end
444:           end
445:         elsif @body
446:           dev << header + @body
447:         else
448:           dev << header
449:         end
450:         dev
451:       end
dump_chunked(header = '', dev = '') click to toggle source

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.

     # File lib/httpclient/http.rb, line 460
460:       def dump_chunked(header = '', dev = '')
461:         dev << header
462:         if @body.is_a?(Parts)
463:           @body.parts.each do |part|
464:             if Message.file?(part)
465:               reset_pos(part)
466:               dump_chunks(part, dev)
467:             else
468:               dev << dump_chunk(part)
469:             end
470:           end
471:           dev << (dump_last_chunk + CRLF)
472:         elsif @body
473:           reset_pos(@body)
474:           dump_chunks(@body, dev)
475:           dev << (dump_last_chunk + CRLF)
476:         end
477:         dev
478:       end
init_request(body = nil, boundary = nil) click to toggle source

Initialize this instance as a request.

     # File lib/httpclient/http.rb, line 407
407:       def init_request(body = nil, boundary = nil)
408:         @boundary = boundary
409:         @positions = {}
410:         set_content(body, boundary)
411:         @chunk_size = DEFAULT_CHUNK_SIZE
412:       end
init_response(body = nil) click to toggle source

Initialize this instance as a response.

     # File lib/httpclient/http.rb, line 415
415:       def init_response(body = nil)
416:         @body = body
417:         if @body.respond_to?(:size)
418:           @size = @body.size
419:         else
420:           @size = nil
421:         end
422:       end

Private Instance Methods

build_query_multipart_str(query, boundary) click to toggle source
     # File lib/httpclient/http.rb, line 574
574:       def build_query_multipart_str(query, boundary)
575:         parts = Parts.new
576:         query.each do |attr, value|
577:           value ||= ''
578:           headers = ["--#{boundary}"]
579:           if Message.file?(value)
580:             remember_pos(value)
581:             param_str = params_from_file(value).collect { |k, v|
582:               "#{k}=\"#{v}\""
583:             }.join("; ")
584:             if value.respond_to?(:mime_type)
585:               content_type = value.mime_type
586:             else
587:               content_type = Message.mime_type(value.path)
588:             end
589:             headers << %{Content-Disposition: form-data; name="#{attr}"; #{param_str}}
590:             headers << %{Content-Type: #{content_type}}
591:           else
592:             headers << %{Content-Disposition: form-data; name="#{attr}"}
593:           end
594:           parts.add(headers.join(CRLF) + CRLF + CRLF)
595:           parts.add(value)
596:           parts.add(CRLF)
597:         end
598:         parts.add("--#{boundary}--" + CRLF + CRLF) # empty epilogue
599:         parts
600:       end
dump_chunk(str) click to toggle source
     # File lib/httpclient/http.rb, line 519
519:       def dump_chunk(str)
520:         dump_chunk_size(str.size) + (str + CRLF)
521:       end
dump_chunk_size(size) click to toggle source
     # File lib/httpclient/http.rb, line 527
527:       def dump_chunk_size(size)
528:         sprintf("%x", size) + CRLF
529:       end
dump_chunks(io, dev) click to toggle source
     # File lib/httpclient/http.rb, line 512
512:       def dump_chunks(io, dev)
513:         buf = ''
514:         while !io.read(@chunk_size, buf).nil?
515:           dev << dump_chunk(buf)
516:         end
517:       end
dump_last_chunk() click to toggle source
     # File lib/httpclient/http.rb, line 523
523:       def dump_last_chunk
524:         dump_chunk_size(0)
525:       end
params_from_file(value) click to toggle source
     # File lib/httpclient/http.rb, line 602
602:       def params_from_file(value)
603:         params = {}
604:         params['filename'] = File.basename(value.path || '')
605:         # Creation time is not available from File::Stat
606:         if value.respond_to?(:mtime)
607:           params['modification-date'] = value.mtime.rfc822
608:         end
609:         if value.respond_to?(:atime)
610:           params['read-date'] = value.atime.rfc822
611:         end
612:         params
613:       end
remember_pos(io) click to toggle source
     # File lib/httpclient/http.rb, line 503
503:       def remember_pos(io)
504:         # IO may not support it (ex. IO.pipe)
505:         @positions[io] = io.pos rescue nil
506:       end
reset_pos(io) click to toggle source
     # File lib/httpclient/http.rb, line 508
508:       def reset_pos(io)
509:         io.pos = @positions[io] if @positions.key?(io)
510:       end
set_content(body, boundary = nil) click to toggle source
     # File lib/httpclient/http.rb, line 487
487:       def set_content(body, boundary = nil)
488:         if body.respond_to?(:read)
489:           # uses Transfer-Encoding: chunked.  bear in mind that server may not
490:           # support it.  at least ruby's CGI doesn't.
491:           @body = body
492:           remember_pos(@body)
493:           @size = nil
494:         elsif boundary and Message.multiparam_query?(body)
495:           @body = build_query_multipart_str(body, boundary)
496:           @size = @body.size
497:         else
498:           @body = Message.create_query_part_str(body)
499:           @size = @body.size
500:         end
501:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.