Class: Knjappserver::Httpsession::Http_response
- Inherits:
-
Object
- Object
- Knjappserver::Httpsession::Http_response
- Defined in:
- lib/include/class_httpsession_http_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) ⇒ Http_response
constructor
A new instance of Http_response.
- #reset(args) ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize(args) ⇒ Http_response
Returns a new instance of Http_response.
28 29 30 31 |
# File 'lib/include/class_httpsession_http_response.rb', line 28 def initialize(args) @chunked = false @socket = args[:socket] end |
Instance Attribute Details
#cgroup ⇒ Object
Returns the value of attribute cgroup.
5 6 7 |
# File 'lib/include/class_httpsession_http_response.rb', line 5 def cgroup @cgroup end |
#chunked ⇒ Object
Returns the value of attribute chunked.
5 6 7 |
# File 'lib/include/class_httpsession_http_response.rb', line 5 def chunked @chunked end |
#headers ⇒ Object
Returns the value of attribute headers.
5 6 7 |
# File 'lib/include/class_httpsession_http_response.rb', line 5 def headers @headers end |
#headers_sent ⇒ Object
Returns the value of attribute headers_sent.
5 6 7 |
# File 'lib/include/class_httpsession_http_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/include/class_httpsession_http_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/include/class_httpsession_http_response.rb', line 5 def http_version @http_version end |
#nl ⇒ Object
Returns the value of attribute nl.
5 6 7 |
# File 'lib/include/class_httpsession_http_response.rb', line 5 def nl @nl end |
#socket ⇒ Object
Returns the value of attribute socket.
5 6 7 |
# File 'lib/include/class_httpsession_http_response.rb', line 5 def socket @socket end |
#status ⇒ Object
Returns the value of attribute status.
5 6 7 |
# File 'lib/include/class_httpsession_http_response.rb', line 5 def status @status end |
Instance Method Details
#cookie(cookie) ⇒ Object
73 74 75 |
# File 'lib/include/class_httpsession_http_response.rb', line 73 def () @cookies << end |
#header(key, val) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/include/class_httpsession_http_response.rb', line 61 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
77 78 79 80 81 82 83 84 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 |
# File 'lib/include/class_httpsession_http_response.rb', line 77 def header_str 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 @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
33 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 |
# File 'lib/include/class_httpsession_http_response.rb', line 33 def reset(args) @status = 200 @http_version = args[:http_version] @close = args[:close] @fileobj = nil @close = true if @http_version == "1.0" @trailers = [] @headers_sent = false @headers_trailing = {} @headers = { "date" => ["Date", Time.now.httpdate] } @headers_11 = { "Connection" => "Keep-Alive", "Transfer-Encoding" => "chunked" } #Socket-timeout is currently broken in JRuby. if RUBY_ENGINE != "jruby" @headers_11["Keep-Alive"] = "timeout=15, max=30" end @cookies = [] end |
#write ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/include/class_httpsession_http_response.rb', line 111 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}") end end @socket.close if @close end |