Class: Baykit::BayServer::Util::HttpUtil

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

Constant Summary collapse

MAX_LINE_LEN =
5000

Class Method Summary collapse

Class Method Details

.parse_authorization(tur) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/baykit/bayserver/util/http_util.rb', line 94

def HttpUtil.parse_authorization(tur)
  auth = tur.req.headers.get(Headers::AUTHORIZATION)
  if StringUtil.set?(auth)
    ptn = /Basic (.*)/
    mch = auth.match(ptn)
    if !mch
      BayLog.warn("Not matched with basic authentication format")
    else
      auth = mch[1]

      auth = Base64.decode64(auth)
      ptn = /(.*):(.*)/
      mch = auth.match(ptn)
      if mch
        tur.req.remote_user = mch[1]
        tur.req.remote_pass = mch[2]
      end
    end
  end
end

.parse_host_port(tur, default_port) ⇒ Object



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
141
# File 'lib/baykit/bayserver/util/http_util.rb', line 116

def HttpUtil.parse_host_port(tur, default_port)
  tur.req.req_host = ""

  host_port = tur.req.headers.get(Headers::X_FORWARDED_HOST)
  if StringUtil.set?(host_port)
    tur.req.headers.remove(Headers::X_FORWARDED_HOST)
    tur.req.headers.set(Headers::HOST, host_port)
  end

  host_port = tur.req.headers.get(Headers::HOST)

  if StringUtil.set?(host_port)
    pos = host_port.rindex(':')
    if pos == nil
      tur.req.req_host = host_port
      tur.req.req_port = default_port
    else
      tur.req.req_host = host_port[0, pos]
      begin
        tur.req.req_port = host_port[pos + 1 .. -1].to_i
      rescue => e
        BayLog.error(e)
      end
    end
  end
end

.parse_message_headers(file, header) ⇒ Object

Parse message headers

message-header = field-name ":" [field-value]


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/baykit/bayserver/util/http_util.rb', line 56

def HttpUtil.parse_message_headers(file, header)
  while true
    line = read_line(file)

    #  if line is empty ("\r\n")
    #  finish reading.
    if StringUtil.empty?(line)
      break
    end

    pos = line.index ":"
    if pos != nil
      key = line[0 .. pos - 1].strip
      val = line[pos + 1 .. -1].strip
      header.add(key, val)
    end
  end
end

.read_line(file) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/baykit/bayserver/util/http_util.rb', line 17

def HttpUtil.read_line(file)
  # Current reading line
  buf = StringUtil.alloc(MAX_LINE_LEN)

  n = 0
  eof = false
  while true
    begin
      c = file.readchar
    rescue EOFError => e
      eof = true
      break
    end

    # If line is too long, return error
    if n >= MAX_LINE_LEN
      raise RuntimeError.new("Request line too long")
    end
    # If character is newline, end to read line
    if c == CharUtil::LF
      break
    end

    # Put the character to buffer
    buf.concat(c)
    n += 1
  end

  if n == 0 && eof
    return nil
  else
    return buf.chomp
  end
end

.resolve_remote_host(adr) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/baykit/bayserver/util/http_util.rb', line 143

def HttpUtil.resolve_remote_host(adr)
  if adr == nil
    return nil
  end
  begin
    return Resolv.getname(adr)
  rescue => e
    BayLog.warn_e(e, "Cannot get remote host name: %s", e)
    return nil
  end
end

.send_mime_headers(headers, buf) ⇒ Object

Send MIME headers This method is called from send_headers



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

def HttpUtil.send_mime_headers(headers, buf)

  headers.names.each do |name|
    headers.values(name).each do |value|
      buf.put(name)
      buf.put(Headers::HEADER_SEPARATOR)
      buf.put(value)
      send_new_line(buf)
    end
  end
end

.send_new_line(buf) ⇒ Object



90
91
92
# File 'lib/baykit/bayserver/util/http_util.rb', line 90

def HttpUtil.send_new_line(buf)
  buf.put(CharUtil::CRLF)
end