Class: HTTP::Message
- Inherits:
-
Object
- Object
- HTTP::Message
- Defined in:
- lib/rss-client/http-access2/http.rb
Overview
HTTP::Message – HTTP message.
DESCRIPTION
A class that describes 1 HTTP request / response message.
Defined Under Namespace
Constant Summary collapse
- CRLF =
"\r\n"
- @@mime_type_func =
nil
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#header ⇒ Object
Returns the value of attribute header.
-
#peer_cert ⇒ Object
Returns the value of attribute peer_cert.
Class Method Summary collapse
- .create_query_multipart_str(query, boundary) ⇒ Object
- .create_query_part_str(query) ⇒ Object
-
.escape(str) ⇒ Object
from CGI.escape.
- .escape_query(query) ⇒ Object
- .get_mime_type_func ⇒ Object
- .internal_mime_type(path) ⇒ Object
- .mime_type(path) ⇒ Object
- .multiparam_query?(query) ⇒ Boolean
- .new_request(method, uri, query = nil, body = nil, proxy = nil, boundary = nil) ⇒ Object
- .new_response(body = '') ⇒ Object
- .set_mime_type_func(val) ⇒ Object
Instance Method Summary collapse
- #content ⇒ Object
- #contenttype ⇒ Object
- #contenttype=(contenttype) ⇒ Object
- #dump(dev = '') ⇒ Object
-
#initialize ⇒ Message
constructor
A new instance of Message.
- #load(str) ⇒ Object
- #reason ⇒ Object
- #reason=(reason) ⇒ Object
- #status ⇒ Object
- #status=(status) ⇒ Object
- #version ⇒ Object
- #version=(version) ⇒ Object
Constructor Details
#initialize ⇒ Message
Returns a new instance of Message.
388 389 390 |
# File 'lib/rss-client/http-access2/http.rb', line 388 def initialize @body = @header = @peer_cert = nil end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
385 386 387 |
# File 'lib/rss-client/http-access2/http.rb', line 385 def body @body end |
#header ⇒ Object
Returns the value of attribute header.
384 385 386 |
# File 'lib/rss-client/http-access2/http.rb', line 384 def header @header end |
#peer_cert ⇒ Object
Returns the value of attribute peer_cert.
386 387 388 |
# File 'lib/rss-client/http-access2/http.rb', line 386 def peer_cert @peer_cert end |
Class Method Details
.create_query_multipart_str(query, boundary) ⇒ Object
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
# File 'lib/rss-client/http-access2/http.rb', line 494 def create_query_multipart_str(query, boundary) if multiparam_query?(query) query.collect { |attr, value| value ||= '' extra_content_disposition = content_type = content = nil if value.is_a? File params = { 'filename' => File.basename(value.path), # Creation time is not available from File::Stat # 'creation-date' => value.ctime.rfc822, 'modification-date' => value.mtime.rfc822, 'read-date' => value.atime.rfc822, } param_str = params.to_a.collect { |k, v| "#{k}=\"#{v}\"" }.join("; ") extra_content_disposition = " #{param_str}" content_type = mime_type(value.path) content = value.read else extra_content_disposition = '' content_type = mime_type(nil) content = value.to_s end "--#{boundary}" + CRLF + %{Content-Disposition: form-data; name="#{attr.to_s}";} + extra_content_disposition + CRLF + "Content-Type: " + content_type + CRLF + CRLF + content + CRLF }.join('') + "--#{boundary}--" + CRLF + CRLF # empty epilogue else query.to_s end end |
.create_query_part_str(query) ⇒ Object
486 487 488 489 490 491 492 |
# File 'lib/rss-client/http-access2/http.rb', line 486 def create_query_part_str(query) if multiparam_query?(query) escape_query(query) else query.to_s end end |
.escape(str) ⇒ Object
from CGI.escape
541 542 543 544 545 |
# File 'lib/rss-client/http-access2/http.rb', line 541 def escape(str) str.gsub(/([^ a-zA-Z0-9_.-]+)/n) { '%' + $1.unpack('H2' * $1.size).join('%').upcase }.tr(' ', '+') end |
.escape_query(query) ⇒ Object
534 535 536 537 538 |
# File 'lib/rss-client/http-access2/http.rb', line 534 def escape_query(query) query.collect { |attr, value| escape(attr.to_s) << '=' << escape(value.to_s) }.join('&') end |
.get_mime_type_func ⇒ Object
482 483 484 |
# File 'lib/rss-client/http-access2/http.rb', line 482 def get_mime_type_func @@mime_type_func end |
.internal_mime_type(path) ⇒ Object
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 |
# File 'lib/rss-client/http-access2/http.rb', line 560 def internal_mime_type(path) case path when /\.txt$/i 'text/plain' when /\.(htm|html)$/i 'text/html' when /\.doc$/i 'application/msword' when /\.png$/i 'image/png' when /\.gif$/i 'image/gif' when /\.(jpg|jpeg)$/i 'image/jpeg' else 'application/octet-stream' end end |
.mime_type(path) ⇒ Object
547 548 549 550 551 552 553 554 555 556 557 558 |
# File 'lib/rss-client/http-access2/http.rb', line 547 def mime_type(path) if @@mime_type_func res = @@mime_type_func.call(path) if !res || res.to_s == '' return 'application/octet-stream' else return res end else internal_mime_type(path) end end |
.multiparam_query?(query) ⇒ Boolean
530 531 532 |
# File 'lib/rss-client/http-access2/http.rb', line 530 def multiparam_query?(query) query.is_a?(Array) or query.is_a?(Hash) end |
.new_request(method, uri, query = nil, body = nil, proxy = nil, boundary = nil) ⇒ Object
397 398 399 400 401 402 403 404 |
# File 'lib/rss-client/http-access2/http.rb', line 397 def self.new_request(method, uri, query = nil, body = nil, proxy = nil, boundary = nil) m = self.__new m.header = Headers.new m.header.init_request(method, uri, query, proxy) m.body = Body.new(body, nil, nil, nil, boundary) m end |
.new_response(body = '') ⇒ Object
406 407 408 409 410 411 412 |
# File 'lib/rss-client/http-access2/http.rb', line 406 def self.new_response(body = '') m = self.__new m.header = Headers.new m.header.init_response(Status::OK) m.body = Body.new(body) m end |
.set_mime_type_func(val) ⇒ Object
478 479 480 |
# File 'lib/rss-client/http-access2/http.rb', line 478 def set_mime_type_func(val) @@mime_type_func = val end |
Instance Method Details
#content ⇒ Object
434 435 436 |
# File 'lib/rss-client/http-access2/http.rb', line 434 def content @body.content end |
#contenttype ⇒ Object
467 468 469 |
# File 'lib/rss-client/http-access2/http.rb', line 467 def contenttype @header.contenttype end |
#contenttype=(contenttype) ⇒ Object
471 472 473 |
# File 'lib/rss-client/http-access2/http.rb', line 471 def contenttype=(contenttype) @header.contenttype = contenttype end |
#dump(dev = '') ⇒ Object
414 415 416 417 418 419 420 |
# File 'lib/rss-client/http-access2/http.rb', line 414 def dump(dev = '') sync_header dev = header.dump(dev) dev << CRLF dev = body.dump(dev) if body dev end |
#load(str) ⇒ Object
422 423 424 425 426 427 |
# File 'lib/rss-client/http-access2/http.rb', line 422 def load(str) buf = str.dup unless self.header.load(buf) self.body.load(buf) end end |
#reason ⇒ Object
459 460 461 |
# File 'lib/rss-client/http-access2/http.rb', line 459 def reason @header.reason_phrase end |
#reason=(reason) ⇒ Object
463 464 465 |
# File 'lib/rss-client/http-access2/http.rb', line 463 def reason=(reason) @header.reason_phrase = reason end |
#status ⇒ Object
443 444 445 |
# File 'lib/rss-client/http-access2/http.rb', line 443 def status @header.response_status_code end |
#status=(status) ⇒ Object
447 448 449 |
# File 'lib/rss-client/http-access2/http.rb', line 447 def status=(status) @header.response_status_code = status end |
#version ⇒ Object
451 452 453 |
# File 'lib/rss-client/http-access2/http.rb', line 451 def version @header.http_version end |
#version=(version) ⇒ Object
455 456 457 |
# File 'lib/rss-client/http-access2/http.rb', line 455 def version=(version) @header.http_version = version end |