Class: HTTP::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/httpclient/http.rb

Overview

HTTP::Message – HTTP message.

DESCRIPTION

A class that describes 1 HTTP request / response message.

Defined Under Namespace

Classes: Body, Headers

Constant Summary collapse

CRLF =
"\r\n"
@@mime_type_func =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMessage

Returns a new instance of Message.



388
389
390
# File 'lib/httpclient/http.rb', line 388

def initialize
  @body = @header = @peer_cert = nil
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



385
386
387
# File 'lib/httpclient/http.rb', line 385

def body
  @body
end

#headerObject

Returns the value of attribute header.



384
385
386
# File 'lib/httpclient/http.rb', line 384

def header
  @header
end

#peer_certObject

Returns the value of attribute peer_cert.



386
387
388
# File 'lib/httpclient/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/httpclient/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/httpclient/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/httpclient/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/httpclient/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_funcObject



482
483
484
# File 'lib/httpclient/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/httpclient/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/httpclient/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

Returns:

  • (Boolean)


530
531
532
# File 'lib/httpclient/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/httpclient/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/httpclient/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/httpclient/http.rb', line 478

def set_mime_type_func(val)
  @@mime_type_func = val
end

Instance Method Details

#contentObject



434
435
436
# File 'lib/httpclient/http.rb', line 434

def content
  @body.content
end

#contenttypeObject



467
468
469
# File 'lib/httpclient/http.rb', line 467

def contenttype
  @header.contenttype
end

#contenttype=(contenttype) ⇒ Object



471
472
473
# File 'lib/httpclient/http.rb', line 471

def contenttype=(contenttype)
  @header.contenttype = contenttype
end

#dump(dev = '') ⇒ Object



414
415
416
417
418
419
420
# File 'lib/httpclient/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/httpclient/http.rb', line 422

def load(str)
  buf = str.dup
  unless self.header.load(buf)
    self.body.load(buf)
  end
end

#reasonObject



459
460
461
# File 'lib/httpclient/http.rb', line 459

def reason
  @header.reason_phrase
end

#reason=(reason) ⇒ Object



463
464
465
# File 'lib/httpclient/http.rb', line 463

def reason=(reason)
  @header.reason_phrase = reason
end

#statusObject



443
444
445
# File 'lib/httpclient/http.rb', line 443

def status
  @header.response_status_code
end

#status=(status) ⇒ Object



447
448
449
# File 'lib/httpclient/http.rb', line 447

def status=(status)
  @header.response_status_code = status
end

#versionObject



451
452
453
# File 'lib/httpclient/http.rb', line 451

def version
  @header.http_version
end

#version=(version) ⇒ Object



455
456
457
# File 'lib/httpclient/http.rb', line 455

def version=(version)
  @header.http_version = version
end