Class: HttpResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/action/module/http_pack_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status = 200) ⇒ HttpResponse

Returns a new instance of HttpResponse.



35
36
37
38
39
# File 'lib/action/module/http_pack_helper.rb', line 35

def initialize(status=200)
  @status = status
  @status_message = nil
  @header = Table.new
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



33
34
35
# File 'lib/action/module/http_pack_helper.rb', line 33

def body
  @body
end

#headerObject (readonly)

Returns the value of attribute header.



32
33
34
# File 'lib/action/module/http_pack_helper.rb', line 32

def header
  @header
end

#statusObject

Returns the value of attribute status.



33
34
35
# File 'lib/action/module/http_pack_helper.rb', line 33

def status
  @status
end

#status_messageObject

Returns the value of attribute status_message.



33
34
35
# File 'lib/action/module/http_pack_helper.rb', line 33

def status_message
  @status_message
end

Instance Method Details

#charsetObject



41
42
43
44
45
46
# File 'lib/action/module/http_pack_helper.rb', line 41

def charset
	return nil if @header["Content-Type"]==nil
	charset_array=@header["Content-Type"].scan(/charset=([^; ]*)/)[0]
	return nil if charset_array==nil
	return charset_array[0]
end

#to_http_pack(status_code = nil, status_message = nil, header = nil, body = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/action/module/http_pack_helper.rb', line 48

def to_http_pack(status_code=nil, status_message=nil, header=nil, body=nil)
	_status_code_=(status_code==nil) ? @status : status_code
	_status_message_=(status_message==nil) ? StatusCodeMapping[_status_code_] : status_message
	_header_=(header==nil) ? @header : header
	_body_=(body==nil) ? @body : body
	
#    _status_message_ ||= StatusCodeMapping[_status_code_]
	
	#encoding body.
	unless _body_.nil?
if charset==nil
	_body_=_body_.force_encoding("gbk")
else
	encod=charset.downcase
	if encod=="gbk" or encod=="gb2312"
		_body_=_body_.force_encoding("gbk")
	else
		_body_=_body_.force_encoding("gbk").encode(encod)
	end
end
	end

	#to pack.
  http_pack = ""
  http_pack << "#{HTTP_PROTO} #{_status_code_} #{_status_message_}" << CRLF
  http_header(_header_, _body_).writeTo(http_pack)
  http_pack << CRLF

  http_pack << _body_ unless _body_.nil?
  http_pack
end