Class: Hayabusa::Http_session::Response
- Inherits:
-
Object
- Object
- Hayabusa::Http_session::Response
- Defined in:
- lib/hayabusa_http_session_response.rb
Overview
This object writes headers, trailing headers, status headers and more for HTTP-sessions.
Constant Summary collapse
- STATUS_CODES =
{ 100 => "Continue", 200 => "OK", 201 => "Created", 202 => "Accepted", 204 => "No Content", 205 => "Reset Content", 206 => "Partial Content", 301 => "Moved Permanently", 302 => "Found", 303 => "See Other", 304 => "Not Modified", 307 => "Temporary Redirect", 400 => "Bad Request", 401 => "Unauthorized", 403 => "Forbidden", 404 => "Not Found", 500 => "Internal Server Error" }
- NL =
"\r\n"
Instance Attribute Summary collapse
-
#cgroup ⇒ Object
Returns the value of attribute cgroup.
-
#chunked ⇒ Object
Returns the value of attribute chunked.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#headers_sent ⇒ Object
Returns the value of attribute headers_sent.
-
#headers_trailing ⇒ Object
Returns the value of attribute headers_trailing.
-
#http_version ⇒ Object
Returns the value of attribute http_version.
-
#nl ⇒ Object
Returns the value of attribute nl.
-
#socket ⇒ Object
Returns the value of attribute socket.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
- #cookie(cookie) ⇒ Object
- #header(key, val) ⇒ Object
- #header_str ⇒ Object
-
#initialize(args) ⇒ Response
constructor
A new instance of Response.
- #reset(args) ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize(args) ⇒ Response
Returns a new instance of Response.
28 29 30 31 32 |
# File 'lib/hayabusa_http_session_response.rb', line 28 def initialize(args) @chunked = false @socket = args[:socket] @hb = args[:hb] end |
Instance Attribute Details
#cgroup ⇒ Object
Returns the value of attribute cgroup.
5 6 7 |
# File 'lib/hayabusa_http_session_response.rb', line 5 def cgroup @cgroup end |
#chunked ⇒ Object
Returns the value of attribute chunked.
5 6 7 |
# File 'lib/hayabusa_http_session_response.rb', line 5 def chunked @chunked end |
#headers ⇒ Object
Returns the value of attribute headers.
5 6 7 |
# File 'lib/hayabusa_http_session_response.rb', line 5 def headers @headers end |
#headers_sent ⇒ Object
Returns the value of attribute headers_sent.
5 6 7 |
# File 'lib/hayabusa_http_session_response.rb', line 5 def headers_sent @headers_sent end |
#headers_trailing ⇒ Object
Returns the value of attribute headers_trailing.
5 6 7 |
# File 'lib/hayabusa_http_session_response.rb', line 5 def headers_trailing @headers_trailing end |
#http_version ⇒ Object
Returns the value of attribute http_version.
5 6 7 |
# File 'lib/hayabusa_http_session_response.rb', line 5 def http_version @http_version end |
#nl ⇒ Object
Returns the value of attribute nl.
5 6 7 |
# File 'lib/hayabusa_http_session_response.rb', line 5 def nl @nl end |
#socket ⇒ Object
Returns the value of attribute socket.
5 6 7 |
# File 'lib/hayabusa_http_session_response.rb', line 5 def socket @socket end |
#status ⇒ Object
Returns the value of attribute status.
5 6 7 |
# File 'lib/hayabusa_http_session_response.rb', line 5 def status @status end |
Instance Method Details
#cookie(cookie) ⇒ Object
80 81 82 83 |
# File 'lib/hayabusa_http_session_response.rb', line 80 def () @cookies << @session_cookie[["name"]] = ["value"] end |
#header(key, val) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/hayabusa_http_session_response.rb', line 68 def header(key, val) lines = val.to_s.count("\n") + 1 raise "Value contains more lines than 1 (#{lines})." if lines > 1 if !@headers_sent @headers[key.to_s.downcase] = [key, val] else raise "Headers already sent and given header was not in trailing headers: '#{key}'." if @trailers.index(key) == nil @headers_trailing[key.to_s.downcase] = [key, val] end end |
#header_str ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/hayabusa_http_session_response.rb', line 85 def header_str if @skip_statuscode res = "" else if @http_version == "1.0" res = "HTTP/1.0 #{@status}" else res = "HTTP/1.1 #{@status}" end code = STATUS_CODES[@status] res << " #{code}" if code res << NL end @headers.each do |key, val| res << "#{val[0]}: #{val[1]}#{NL}" end if @http_version == "1.1" @headers_11.each do |key, val| res << "#{key}: #{val}#{NL}" end @trailers.each do |trailer| res << "Trailer: #{trailer}#{NL}" end end @cookies.each do || res << "Set-Cookie: #{Knj::Web.()}#{NL}" end res << NL return res end |
#reset(args) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/hayabusa_http_session_response.rb', line 34 def reset(args) @status = 200 @http_version = args[:http_version] @close = args[:close] @fileobj = nil @close = true if @http_version == "1.0" @trailers = [] @skip_statuscode = true if args[:mode] == :cgi @session_cookie = args[:cookie] @headers_sent = false @headers_trailing = {} @mode = args[:mode] @headers = { "date" => ["Date", Time.now.httpdate] } @headers_11 = { "Connection" => "Keep-Alive" } if args[:mode] != :cgi and (!args.key?(:chunked) or args[:chunked]) @headers_11["Transfer-Encoding"] = "chunked" end #Socket-timeout is currently broken in JRuby. if RUBY_ENGINE != "jruby" @headers_11["Keep-Alive"] = "timeout=15, max=30" end @cookies = [] end |
#write ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/hayabusa_http_session_response.rb', line 123 def write @headers_sent = true @socket.write(self.header_str) if @status == 304 #do nothing. else if @chunked @cgroup.write_to_socket @socket.write("0#{NL}") @headers_trailing.each do |header_id_str, header| @socket.write("#{header[0]}: #{header[1]}#{NL}") end @socket.write(NL) else @cgroup.write_to_socket @socket.write("#{NL}#{NL}") if @mode != :cgi end end if @close and @mode != :cgi @hb.log_puts("Closing socket.") @socket.close end end |