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.



390
391
392
# File 'lib/httpclient/http.rb', line 390

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

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



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

def body
  @body
end

#headerObject

Returns the value of attribute header.



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

def header
  @header
end

#peer_certObject

Returns the value of attribute peer_cert.



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

def peer_cert
  @peer_cert
end

Class Method Details

.create_query_multipart_str(query, boundary) ⇒ Object



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

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



488
489
490
491
492
493
494
# File 'lib/httpclient/http.rb', line 488

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



543
544
545
546
547
# File 'lib/httpclient/http.rb', line 543

def escape(str)
  str.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
  	'%' + $1.unpack('H2' * $1.size).join('%').upcase
  }.tr(' ', '+')
end

.escape_query(query) ⇒ Object



536
537
538
539
540
# File 'lib/httpclient/http.rb', line 536

def escape_query(query)
  query.collect { |attr, value|
	escape(attr.to_s) << '=' << escape(value.to_s)
  }.join('&')
end

.get_mime_type_funcObject



484
485
486
# File 'lib/httpclient/http.rb', line 484

def get_mime_type_func
  @@mime_type_func
end

.internal_mime_type(path) ⇒ Object



562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/httpclient/http.rb', line 562

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



549
550
551
552
553
554
555
556
557
558
559
560
# File 'lib/httpclient/http.rb', line 549

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)


532
533
534
# File 'lib/httpclient/http.rb', line 532

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



399
400
401
402
403
404
405
406
# File 'lib/httpclient/http.rb', line 399

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



408
409
410
411
412
413
414
# File 'lib/httpclient/http.rb', line 408

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



480
481
482
# File 'lib/httpclient/http.rb', line 480

def set_mime_type_func(val)
  @@mime_type_func = val
end

Instance Method Details

#contentObject



436
437
438
# File 'lib/httpclient/http.rb', line 436

def content
  @body.content
end

#contenttypeObject



469
470
471
# File 'lib/httpclient/http.rb', line 469

def contenttype
  @header.contenttype
end

#contenttype=(contenttype) ⇒ Object



473
474
475
# File 'lib/httpclient/http.rb', line 473

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

#dump(dev = '') ⇒ Object



416
417
418
419
420
421
422
# File 'lib/httpclient/http.rb', line 416

def dump(dev = '')
  sync_header
  dev = header.dump(dev)
  dev << CRLF
  dev = body.dump(dev) if body
  dev
end

#load(str) ⇒ Object



424
425
426
427
428
429
# File 'lib/httpclient/http.rb', line 424

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

#reasonObject



461
462
463
# File 'lib/httpclient/http.rb', line 461

def reason
  @header.reason_phrase
end

#reason=(reason) ⇒ Object



465
466
467
# File 'lib/httpclient/http.rb', line 465

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

#statusObject



445
446
447
# File 'lib/httpclient/http.rb', line 445

def status
  @header.response_status_code
end

#status=(status) ⇒ Object



449
450
451
# File 'lib/httpclient/http.rb', line 449

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

#versionObject



453
454
455
# File 'lib/httpclient/http.rb', line 453

def version
  @header.http_version
end

#version=(version) ⇒ Object



457
458
459
# File 'lib/httpclient/http.rb', line 457

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