Class: Knjappserver::Httpsession::Http_response

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#cgroupObject

Returns the value of attribute cgroup.



5
6
7
# File 'lib/include/class_httpsession_http_response.rb', line 5

def cgroup
  @cgroup
end

#chunkedObject

Returns the value of attribute chunked.



5
6
7
# File 'lib/include/class_httpsession_http_response.rb', line 5

def chunked
  @chunked
end

#headersObject

Returns the value of attribute headers.



5
6
7
# File 'lib/include/class_httpsession_http_response.rb', line 5

def headers
  @headers
end

#headers_sentObject

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_trailingObject

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_versionObject

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

#nlObject

Returns the value of attribute nl.



5
6
7
# File 'lib/include/class_httpsession_http_response.rb', line 5

def nl
  @nl
end

#socketObject

Returns the value of attribute socket.



5
6
7
# File 'lib/include/class_httpsession_http_response.rb', line 5

def socket
  @socket
end

#statusObject

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



73
74
75
# File 'lib/include/class_httpsession_http_response.rb', line 73

def cookie(cookie)
  @cookies << cookie
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_strObject



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 |cookie|
    res << "Set-Cookie: #{Knj::Web.cookie_str(cookie)}#{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

#writeObject



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