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", 408 => "Request Timeout", 415 => "Unsupported media type", 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
-
#get_header_value(header) ⇒ Object
Returns the value of a header.
-
#has_header?(header) ⇒ Boolean
Returns true if the given header-name is sat.
- #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.
30 31 32 33 34 |
# File 'lib/hayabusa_http_session_response.rb', line 30 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
91 92 93 94 |
# File 'lib/hayabusa_http_session_response.rb', line 91 def () @cookies << @session_cookie[["name"]] = ["value"] end |
#get_header_value(header) ⇒ Object
Returns the value of a header.
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/hayabusa_http_session_response.rb', line 72 def get_header_value(header) header_p = header.to_s.downcase.strip gothrough = [@headers, @headers_trailing] gothrough.each do |headers| headers.each do |key, val| return val[1] if header_p == key end end return nil end |
#has_header?(header) ⇒ Boolean
Returns true if the given header-name is sat.
86 87 88 89 |
# File 'lib/hayabusa_http_session_response.rb', line 86 def has_header?(header) header_p = header.to_s.downcase.strip return @headers.key?(header_p) || @headers_trailing.key?(header_p) end |
#header(key, val) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/hayabusa_http_session_response.rb', line 59 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.strip] = [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.strip] = [key, val] end end |
#header_str ⇒ Object
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/hayabusa_http_session_response.rb', line 96 def header_str if @http_version == "1.1" && @chunked self.header("Connection", "Keep-Alive") self.header("Transfer-Encoding", "chunked") elsif @http_version == "1.1" && get_header_value("content-length").to_i > 0 self.header("Connection", "Keep-Alive") end self.header("Keep-Alive", "timeout=15, max=30") if self.get_header_value("connection") == "Keep-Alive" 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 # The status header is used to make CGI or FCGI use the correct status-code. self.header("Status", "#{@status} #{STATUS_CODES[@status]}") @headers.each do |key, val| res << "#{val[0]}: #{val[1]}#{NL}" end if @http_version == "1.1" @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
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/hayabusa_http_session_response.rb', line 36 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] @cookies = [] @headers = { "date" => ["Date", Time.now.httpdate] } if args[:mode] != :cgi and (!args.key?(:chunked) or args[:chunked]) @chunked = true end end |
#write ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/hayabusa_http_session_response.rb', line 142 def write # Write headers to socket. @socket.write(self.header_str) @headers_sent = true @cgroup.chunked = @chunked # Set the content-length on the content-group to enable write-lenght-validation. if self.has_header?("content-length") @cgroup.content_length = self.get_header_value("content-length").to_i else @cgroup.content_length = nil end 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 end # Validate that no more has been written than given in content-length, since that will corrupt the client. if self.has_header?("content-length") length = cgroup.length_written content_length = self.get_header_value("content-length").to_i raise "More written than given in content-length: #{length}, #{content_length}" if length != content_length end # Close socket if that should be done. if @close and @mode != :cgi @hb.log_puts("Hauabusa: Closing socket.") @socket.close end end |