Class: Baykit::BayServer::Docker::Http::H1::H1InboundHandler
- Inherits:
-
Object
- Object
- Baykit::BayServer::Docker::Http::H1::H1InboundHandler
- Includes:
- Agent, Common::InboundHandler, Command, H1Handler, Baykit::BayServer::Docker::Http::H2, Protocol, Tours, Util, OpenSSL
- Defined in:
- lib/baykit/bayserver/docker/http/h1/h1_inbound_handler.rb
Defined Under Namespace
Classes: InboundProtocolHandlerFactory
Constant Summary collapse
- STATE_READ_HEADER =
1- STATE_READ_CONTENT =
2- STATE_FINISHED =
3- FIXED_REQ_ID =
1
Instance Attribute Summary collapse
-
#cur_req_id ⇒ Object
readonly
Returns the value of attribute cur_req_id.
-
#cur_tour ⇒ Object
readonly
Returns the value of attribute cur_tour.
-
#cur_tour_id ⇒ Object
readonly
Returns the value of attribute cur_tour_id.
-
#header_read ⇒ Object
readonly
Returns the value of attribute header_read.
-
#http_protocol ⇒ Object
readonly
Returns the value of attribute http_protocol.
-
#protocol_handler ⇒ Object
readonly
Returns the value of attribute protocol_handler.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
- #handle_content(cmd) ⇒ Object
- #handle_end_content(cmd) ⇒ Object
-
#handle_header(cmd) ⇒ Object
implements H1CommandHandler.
- #init(proto_handler) ⇒ Object
-
#initialize ⇒ H1InboundHandler
constructor
A new instance of H1InboundHandler.
- #on_protocol_error(err) ⇒ Object
- #req_finished ⇒ Object
-
#reset ⇒ Object
implements Reusable.
- #send_end_tour(tur, keep_alive, &callback) ⇒ Object
- #send_res_content(tur, bytes, ofs, len, &callback) ⇒ Object
-
#send_res_headers(tur) ⇒ Object
implements InboundHandler.
Constructor Details
#initialize ⇒ H1InboundHandler
Returns a new instance of H1InboundHandler.
74 75 76 77 |
# File 'lib/baykit/bayserver/docker/http/h1/h1_inbound_handler.rb', line 74 def initialize super reset end |
Instance Attribute Details
#cur_req_id ⇒ Object (readonly)
Returns the value of attribute cur_req_id.
69 70 71 |
# File 'lib/baykit/bayserver/docker/http/h1/h1_inbound_handler.rb', line 69 def cur_req_id @cur_req_id end |
#cur_tour ⇒ Object (readonly)
Returns the value of attribute cur_tour.
70 71 72 |
# File 'lib/baykit/bayserver/docker/http/h1/h1_inbound_handler.rb', line 70 def cur_tour @cur_tour end |
#cur_tour_id ⇒ Object (readonly)
Returns the value of attribute cur_tour_id.
71 72 73 |
# File 'lib/baykit/bayserver/docker/http/h1/h1_inbound_handler.rb', line 71 def cur_tour_id @cur_tour_id end |
#header_read ⇒ Object (readonly)
Returns the value of attribute header_read.
67 68 69 |
# File 'lib/baykit/bayserver/docker/http/h1/h1_inbound_handler.rb', line 67 def header_read @header_read end |
#http_protocol ⇒ Object (readonly)
Returns the value of attribute http_protocol.
72 73 74 |
# File 'lib/baykit/bayserver/docker/http/h1/h1_inbound_handler.rb', line 72 def http_protocol @http_protocol end |
#protocol_handler ⇒ Object (readonly)
Returns the value of attribute protocol_handler.
66 67 68 |
# File 'lib/baykit/bayserver/docker/http/h1/h1_inbound_handler.rb', line 66 def protocol_handler @protocol_handler end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
68 69 70 |
# File 'lib/baykit/bayserver/docker/http/h1/h1_inbound_handler.rb', line 68 def state @state end |
Instance Method Details
#handle_content(cmd) ⇒ Object
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/baykit/bayserver/docker/http/h1/h1_inbound_handler.rb', line 289 def handle_content(cmd) BayLog.debug("%s handleContent: len=%s", ship, cmd.len) if @state != STATE_READ_CONTENT s = @state reset_state() raise ProtocolException.new("Content command not expected: state=#{s}") end tur = @cur_tour tur_id = @cur_tour_id begin sid = ship.ship_id success = tur.req.post_req_content(tur_id, cmd.buf, cmd.start, cmd.len) do |len, resume| if resume tur.ship.resume_read(sid) end end if tur.req.bytes_posted == tur.req.bytes_limit if tur.error != nil # Error has occurred on header completed #tur.res.send_http_exception(tur_id, tur.error) #reset_state() #return NextSocketAction::WRITE BayLog.debug("%s Delay report error", tur) raise tur.error else end_req_content(tur_id, tur) return NextSocketAction::CONTINUE end end if !success return NextSocketAction::SUSPEND # end reading else return NextSocketAction::CONTINUE end rescue HttpException => e tur.res.send_http_exception(tur_id, e) reset_state() return NextSocketAction::WRITE end end |
#handle_end_content(cmd) ⇒ Object
335 336 337 |
# File 'lib/baykit/bayserver/docker/http/h1/h1_inbound_handler.rb', line 335 def handle_end_content(cmd) raise Sink.new() end |
#handle_header(cmd) ⇒ Object
implements H1CommandHandler
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/baykit/bayserver/docker/http/h1/h1_inbound_handler.rb', line 184 def handle_header(cmd) BayLog.debug("%s handleHeader: method=%s uri=%s proto=", ship, cmd.method, cmd.uri, cmd.version); sip = ship if @state == STATE_FINISHED change_state(STATE_READ_HEADER) end if @state != STATE_READ_HEADER || @cur_tour != nil msg = "Header command not expected: state=#{@state} curTur=#{@cur_tour}" BayLog.error(msg) self.reset_state(); raise ProtocolException.new(msg) end # Check HTTP2 protocol = cmd.version.upcase if protocol == "HTTP/2.0" if ship.port_docker.support_h2 ship.port_docker.return_protocol_handler(ship.agent_id, @protocol_handler) new_hnd = ProtocolHandlerStore.get_store(HtpDocker::H2_PROTO_NAME, true, sip.agent_id).rent() sip.set_protocol_handler(new_hnd) raise UpgradeException.new() else raise ProtocolException.new( BayMessage.get(:HTP_UNSUPPORTED_PROTOCOL, protocol)) end end tur = sip.get_tour(@cur_req_id) if tur == nil BayLog.error(BayMessage.get(:INT_NO_MORE_TOURS)) tur = sip.get_tour(self.cur_req_id, true) tur.res.send_error(Tour::TOUR_ID_NOCHECK, HttpStatus::SERVICE_UNAVAILABLE, "No available tours") return NextSocketAction::CONTINUE end @cur_tour = tur @cur_tour_id = tur.id() @cur_req_id += 1 ship.keeping = false @http_protocol = protocol tur.req.uri = URLEncoder.encode_tilde(cmd.uri) tur.req.method = cmd.method.upcase tur.req.protocol = protocol if !(tur.req.protocol == "HTTP/1.1" || tur.req.protocol == "HTTP/1.0" || tur.req.protocol == "HTTP/0.9") raise ProtocolException.new(BayMessage.get(:HTP_UNSUPPORTED_PROTOCOL, tur.req.protocol)) end cmd.headers.each do |nv| tur.req.headers.add(nv[0], nv[1]) end req_cont_len = tur.req.headers.content_length BayLog.debug("%s read header method=%s protocol=%s uri=%s contlen=%d", ship, tur.req.method, tur.req.protocol, tur.req.uri, tur.req.headers.content_length) if BayServer.harbor.trace_header cmd.headers.each do |item| BayLog.info("%s h1: reqHeader: %s=%s", tur, item[0], item[1]) end end if req_cont_len > 0 tur.req.set_limit(req_cont_len) end begin start_tour(tur) if req_cont_len <= 0 end_req_content(@cur_tour_id, tur) return NextSocketAction::SUSPEND # end reading else change_state(STATE_READ_CONTENT) return NextSocketAction::CONTINUE end rescue HttpException => e BayLog.debug_e(e, "%s Http error occurred: %s", self, e) if req_cont_len <= 0 # no post data tur.req.abort tur.res.send_http_exception(Tour::TOUR_ID_NOCHECK, e) reset_state() # next: read empty stdin command return NextSocketAction::CONTINUE else # Delay send BayLog.trace("%s error sending is delayed", self) change_state(STATE_READ_CONTENT) tur.error = e tur.req.set_content_handler(ReqContentHandler::DEV_NULL) return NextSocketAction::CONTINUE end end end |
#init(proto_handler) ⇒ Object
79 80 81 |
# File 'lib/baykit/bayserver/docker/http/h1/h1_inbound_handler.rb', line 79 def init(proto_handler) @protocol_handler = proto_handler end |
#on_protocol_error(err) ⇒ Object
170 171 172 173 174 175 176 177 178 179 |
# File 'lib/baykit/bayserver/docker/http/h1/h1_inbound_handler.rb', line 170 def on_protocol_error(err) if @cur_tour == nil tur = ship.get_error_tour() else tur = @cur_tour end tur.res.send_error(Tour::TOUR_ID_NOCHECK, HttpStatus::BAD_REQUEST, err., err) true end |
#req_finished ⇒ Object
339 340 341 |
# File 'lib/baykit/bayserver/docker/http/h1/h1_inbound_handler.rb', line 339 def req_finished() return @state == STATE_FINISHED end |
#reset ⇒ Object
implements Reusable
86 87 88 89 90 91 92 93 94 |
# File 'lib/baykit/bayserver/docker/http/h1/h1_inbound_handler.rb', line 86 def reset() reset_state() @header_read = false @http_protocol = nil @cur_req_id = 1 @cur_tour = nil @cur_req_id = 0 end |
#send_end_tour(tur, keep_alive, &callback) ⇒ 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 |
# File 'lib/baykit/bayserver/docker/http/h1/h1_inbound_handler.rb', line 142 def send_end_tour(tur, keep_alive, &callback) BayLog.trace("%s sendEndTour: tur=%s keep=%s", ship, tur, keep_alive) # Send dummy end request command cmd = CmdEndContent.new() sid = ship.ship_id ensure_func = lambda do if keep_alive ship.keeping = true ship.resume_read(sid) else ship.post_close end end begin @protocol_handler.post(cmd) do BayLog.debug("%s call back of end content command: tur=%s", ship, tur) ensure_func.call() callback.call() end rescue IOError => e ensure_func.call() raise e end end |
#send_res_content(tur, bytes, ofs, len, &callback) ⇒ Object
136 137 138 139 140 |
# File 'lib/baykit/bayserver/docker/http/h1/h1_inbound_handler.rb', line 136 def send_res_content(tur, bytes, ofs, len, &callback) BayLog.debug("%s H1 send_res_content len=%d", self, len) cmd = CmdContent.new(bytes, ofs, len) @protocol_handler.post(cmd, &callback) end |
#send_res_headers(tur) ⇒ Object
implements InboundHandler
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 |
# File 'lib/baykit/bayserver/docker/http/h1/h1_inbound_handler.rb', line 99 def send_res_headers(tur) # determine Connection header value if tur.req.headers.get_connection() != Headers::CONNECTION_KEEP_ALIVE # If client doesn't support "Keep-Alive", set "Close" res_con = "Close" else res_con = "Keep-Alive" # Client supports "Keep-Alive" if tur.res.headers.get_connection() != Headers::CONNECTION_KEEP_ALIVE # If tours doesn't need "Keep-Alive" if tur.res.headers.content_length() == -1 # If content-length not specified if tur.res.headers.content_type() != nil && tur.res.headers.content_type().start_with?("text/") # If content is text, connection must be closed res_con = "Close" end end end end tur.res.headers.set(Headers::CONNECTION, res_con) if BayServer.harbor.trace_header BayLog.info("%s resStatus:%d", tur, tur.res.headers.status) tur.res.headers.names.each do |name| tur.res.headers.values(name).each do |value| BayLog.info("%s resHeader:%s=%s", tur, name, value) end end end cmd = CmdHeader.new_res_header(tur.res.headers, tur.req.protocol) @protocol_handler.post(cmd) end |