Class: HTTP::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/http-access2/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"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMessage

Returns a new instance of Message.



358
359
360
# File 'lib/http-access2/http.rb', line 358

def initialize
  @body = @header = nil
end

Class Method Details

.create_query_multipart_str(query, boundary) ⇒ Object



462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/http-access2/http.rb', line 462

def create_query_multipart_str(query, boundary)
  if multiparam_query?(query)
    query.collect { |attr, value|
      value ||= ''
      if value.is_a? File
        params = {
          'filename' => 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("; ")
        "--#{boundary}\n" +
          %{Content-Disposition: form-data; name="#{attr.to_s}"; #{param_str}\n} +
          "Content-Type: #{mime_type(value.path)}\n\n#{value.read}\n"
      else
        "--#{boundary}\n" +
          %{Content-Disposition: form-data; name="#{attr.to_s}"\n} +
          "\n#{value.to_s}\n"
      end
    }.join('') + "--#{boundary}--\n"
  else
    query.to_s
  end
end

.create_query_part_str(query) ⇒ Object



454
455
456
457
458
459
460
# File 'lib/http-access2/http.rb', line 454

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



502
503
504
505
506
# File 'lib/http-access2/http.rb', line 502

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

.escape_query(query) ⇒ Object



495
496
497
498
499
# File 'lib/http-access2/http.rb', line 495

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

.mime_type(path) ⇒ Object



508
509
510
511
512
513
514
515
516
517
# File 'lib/http-access2/http.rb', line 508

def mime_type(path)
  case path
  when /.(htm|html)$/
    'text/html'
  when /.doc$/
    'application/msword'
  else
    'text/plain'
  end
end

.multiparam_query?(query) ⇒ Boolean

Returns:

  • (Boolean)


491
492
493
# File 'lib/http-access2/http.rb', line 491

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



367
368
369
370
371
372
373
374
# File 'lib/http-access2/http.rb', line 367

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



376
377
378
379
380
381
382
# File 'lib/http-access2/http.rb', line 376

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

Instance Method Details

#bodyObject



412
413
414
# File 'lib/http-access2/http.rb', line 412

def body
  @body
end

#body=(body) ⇒ Object



416
417
418
419
# File 'lib/http-access2/http.rb', line 416

def body=(body)
  @body = body
  sync_header
end

#contentObject



408
409
410
# File 'lib/http-access2/http.rb', line 408

def content
  @body.content
end

#contenttypeObject



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

def contenttype
  @header.contenttype
end

#contenttype=(contenttype) ⇒ Object



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

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

#dump(dev = '') ⇒ Object



384
385
386
387
388
389
390
# File 'lib/http-access2/http.rb', line 384

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

#headerObject



399
400
401
# File 'lib/http-access2/http.rb', line 399

def header
  @header
end

#header=(header) ⇒ Object



403
404
405
406
# File 'lib/http-access2/http.rb', line 403

def header=(header)
  @header = header
  sync_body
end

#load(str) ⇒ Object



392
393
394
395
396
397
# File 'lib/http-access2/http.rb', line 392

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

#reasonObject



437
438
439
# File 'lib/http-access2/http.rb', line 437

def reason
  @header.reason_phrase
end

#reason=(reason) ⇒ Object



441
442
443
# File 'lib/http-access2/http.rb', line 441

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

#statusObject



421
422
423
# File 'lib/http-access2/http.rb', line 421

def status
  @header.response_status_code
end

#status=(status) ⇒ Object



425
426
427
# File 'lib/http-access2/http.rb', line 425

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

#versionObject



429
430
431
# File 'lib/http-access2/http.rb', line 429

def version
  @header.http_version
end

#version=(version) ⇒ Object



433
434
435
# File 'lib/http-access2/http.rb', line 433

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