Object
Represents HTTP message body.
Default value for chunk_size
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
Returns a message body itself.
# File lib/httpclient/http.rb, line 481 481: def content 482: @body 483: end
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
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
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
# 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
# File lib/httpclient/http.rb, line 519 519: def dump_chunk(str) 520: dump_chunk_size(str.size) + (str + CRLF) 521: end
# File lib/httpclient/http.rb, line 527 527: def dump_chunk_size(size) 528: sprintf("%x", size) + CRLF 529: end
# 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
# File lib/httpclient/http.rb, line 523 523: def dump_last_chunk 524: dump_chunk_size(0) 525: end
# 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
# 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
# File lib/httpclient/http.rb, line 508 508: def reset_pos(io) 509: io.pos = @positions[io] if @positions.key?(io) 510: end
# 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.
Generated with the Darkfish Rdoc Generator 1.1.6.