Class: Baykit::BayServer::Util::HttpUtil
- Inherits:
-
Object
- Object
- Baykit::BayServer::Util::HttpUtil
- Includes:
- Protocol, Baykit::BayServer::Util
- Defined in:
- lib/baykit/bayserver/util/http_util.rb
Constant Summary collapse
- MAX_LINE_LEN =
5000
Class Method Summary collapse
- .check_uri(uri) ⇒ Object
- .parse_authorization(tur) ⇒ Object
- .parse_host_port(tur, default_port) ⇒ Object
-
.parse_message_headers(file, header) ⇒ Object
Parse message headers message-header = field-name ":" [field-value].
- .read_line(file) ⇒ Object
- .resolve_remote_host(adr) ⇒ Object
-
.send_mime_headers(headers, buf) ⇒ Object
Send MIME headers This method is called from send_headers.
- .send_new_line(buf) ⇒ Object
Class Method Details
.check_uri(uri) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/baykit/bayserver/util/http_util.rb', line 157 def HttpUtil.check_uri(uri) if uri == nil raise ProtocolException, "path is null" end if uri.include?("\x00") raise ProtocolException, "path contains null byte" end if uri.each_char.any? { |ch| (ch.ord < 0x20) || (ch.ord == 0x7f) } raise ProtocolException, "path contains control character" end end |
.parse_authorization(tur) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/baykit/bayserver/util/http_util.rb', line 96 def HttpUtil.(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
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/baykit/bayserver/util/http_util.rb', line 118 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
-header = field-name ":" [field-value]
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/baykit/bayserver/util/http_util.rb', line 58 def HttpUtil.(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
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 51 52 |
# File 'lib/baykit/bayserver/util/http_util.rb', line 19 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
145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/baykit/bayserver/util/http_util.rb', line 145 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
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/baykit/bayserver/util/http_util.rb', line 80 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
92 93 94 |
# File 'lib/baykit/bayserver/util/http_util.rb', line 92 def HttpUtil.send_new_line(buf) buf.put(CharUtil::CRLF) end |