Class: Baykit::BayServer::Util::Headers

Inherits:
Object
  • Object
show all
Includes:
Baykit::BayServer::Util
Defined in:
lib/baykit/bayserver/util/headers.rb

Constant Summary collapse

HEADER_SEPARATOR =

known header names

": "
CONTENT_TYPE =
"content-type"
CONTENT_LENGTH =
"content-length"
CONTENT_ENCODING =
"content-encoding"
HDR_TRANSFER_ENCODING =
"Transfer-Encoding"
CONNECTION =
"Connection"
AUTHORIZATION =
"Authorization"
WWW_AUTHENTICATE =
"WWW-Authenticate"
STATUS =
"Status"
LOCATION =
"Location"
HOST =
"Host"
"Cookie"
USER_AGENT =
"User-Agent"
ACCEPT =
"Accept"
ACCEPT_LANGUAGE =
"Accept-Language"
ACCEPT_ENCODING =
"Accept-Encoding"
UPGRADE_INSECURE_REQUESTS =
"Upgrade-Insecure-Requests"
SERVER =
"Server"
X_FORWARDED_HOST =
"X-Forwarded-Host"
X_FORWARDED_FOR =
"X-Forwarded-For"
X_FORWARDED_PROTO =
"X-Forwarded-Proto"
X_FORWARDED_PORT =
"X-Forwarded-Port"
CONNECTION_CLOSE =
1
CONNECTION_KEEP_ALIVE =
2
CONNECTION_UPGRADE =
3
CONNECTION_UNKOWN =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHeaders

Returns a new instance of Headers.



45
46
47
48
# File 'lib/baykit/bayserver/util/headers.rb', line 45

def initialize
  @headers = {}
  clear()
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



42
43
44
# File 'lib/baykit/bayserver/util/headers.rb', line 42

def headers
  @headers
end

#statusObject

Returns the value of attribute status.



41
42
43
# File 'lib/baykit/bayserver/util/headers.rb', line 41

def status
  @status
end

Instance Method Details

#add(name, value) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/baykit/bayserver/util/headers.rb', line 106

def add(name, value)
  name = name.downcase()
  values = @headers[name]
  if(values == nil)
    values = []
    @headers[name] = values
  end
  values.append(value)
end

#add_int(name, value) ⇒ Object



116
117
118
# File 'lib/baykit/bayserver/util/headers.rb', line 116

def add_int(name, value)
  add(name, value.to_s)
end

#clearObject



55
56
57
58
# File 'lib/baykit/bayserver/util/headers.rb', line 55

def clear()
  @headers.clear()
  @status = HttpStatus::OK
end

#contains(name) ⇒ Object



143
144
145
# File 'lib/baykit/bayserver/util/headers.rb', line 143

def contains(name)
  return @headers.keys.include?(name.downcase())
end

#content_lengthObject



162
163
164
165
166
167
168
169
# File 'lib/baykit/bayserver/util/headers.rb', line 162

def content_length()
  length = get(CONTENT_LENGTH)
  if(StringUtil.empty?(length))
    return -1
  else
    return Integer(length)
  end
end

#content_typeObject

Utility methods



154
155
156
# File 'lib/baykit/bayserver/util/headers.rb', line 154

def content_type()
  return get(CONTENT_TYPE)
end

#copy_to(dst) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/baykit/bayserver/util/headers.rb', line 60

def copy_to(dst)
  dst.status = @status
  @headers.keys.each do |name|
    values = @headers[name].dup
    dst.headers[name] = values
  end
end

#countObject



133
134
135
136
137
138
139
140
141
# File 'lib/baykit/bayserver/util/headers.rb', line 133

def count()
  c = 0
  @headers.keys.each do |name|
    @headers[name].each do |value|
      c += 1
    end
  end
  return c
end

#get(name) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/baykit/bayserver/util/headers.rb', line 72

def get(name)
  values = headers[name.downcase()]
  if(values == nil)
    return nil
  else
    return values[0]
  end
end

#get_connectionObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/baykit/bayserver/util/headers.rb', line 175

def get_connection
  con = get(CONNECTION)
  if(con != nil)
    con = con.downcase()
  end
  case con
  when "close" then
    return CONNECTION_CLOSE
  when "keep-alive" then
    return CONNECTION_KEEP_ALIVE
  when "upgrade" then
    return CONNECTION_UPGRADE
  else
    return CONNECTION_UNKOWN
  end
end

#get_int(name) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/baykit/bayserver/util/headers.rb', line 81

def get_int(name)
  val = get(name)
  if(val == nil)
    return -1
  else
    return val.to_i
  end
end

#namesObject



120
121
122
# File 'lib/baykit/bayserver/util/headers.rb', line 120

def names()
  return @headers.keys()
end

#remove(name) ⇒ Object



147
148
149
# File 'lib/baykit/bayserver/util/headers.rb', line 147

def remove(name)
  @headers.delete(name.downcase())
end

#set(name, value) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/baykit/bayserver/util/headers.rb', line 90

def set(name, value)
  name = name.downcase
  values = @headers[name]
  if(values == nil)
    values = []
    @headers[name] = values
  end
  values.clear()
  values.append(value)
end

#set_content_length(len) ⇒ Object



171
172
173
# File 'lib/baykit/bayserver/util/headers.rb', line 171

def set_content_length(len)
  set_int(CONTENT_LENGTH, len)
end

#set_content_type(type) ⇒ Object



158
159
160
# File 'lib/baykit/bayserver/util/headers.rb', line 158

def set_content_type(type)
  set(CONTENT_TYPE, type)
end

#set_int(name, value) ⇒ Object



102
103
104
# File 'lib/baykit/bayserver/util/headers.rb', line 102

def set_int(name, value)
  set(name, value.to_s)
end

#to_sObject



50
51
52
# File 'lib/baykit/bayserver/util/headers.rb', line 50

def to_s()
  return "Header(s=#{@status.to_s} h=#{@headers}"
end

#values(name) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/baykit/bayserver/util/headers.rb', line 124

def values(name)
  values = @headers[name.downcase()]
  if(values == nil)
    return []
  else
    return values
  end
end