Class: Baykit::BayServer::Docker::Fcgi::FcgInboundHandler

Inherits:
Object
  • Object
show all
Includes:
Agent, Common::InboundHandler, Command, FcgHandler, Protocol, Tours, Util
Defined in:
lib/baykit/bayserver/docker/fcgi/fcg_inbound_handler.rb

Defined Under Namespace

Classes: InboundProtocolHandlerFactory

Constant Summary collapse

STATE_BEGIN_REQUEST =
1
STATE_READ_PARAMS =
2
STATE_READ_STDIN =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFcgInboundHandler

Returns a new instance of FcgInboundHandler.



54
55
56
57
# File 'lib/baykit/bayserver/docker/fcgi/fcg_inbound_handler.rb', line 54

def initialize
  @env = {}
  reset()
end

Instance Attribute Details

#cont_lenObject (readonly)

Returns the value of attribute cont_len.



48
49
50
# File 'lib/baykit/bayserver/docker/fcgi/fcg_inbound_handler.rb', line 48

def cont_len
  @cont_len
end

#envObject (readonly)

Returns the value of attribute env.



50
51
52
# File 'lib/baykit/bayserver/docker/fcgi/fcg_inbound_handler.rb', line 50

def env
  @env
end

#protocol_handlerObject (readonly)

Returns the value of attribute protocol_handler.



47
48
49
# File 'lib/baykit/bayserver/docker/fcgi/fcg_inbound_handler.rb', line 47

def protocol_handler
  @protocol_handler
end

#req_idObject (readonly)

Returns the value of attribute req_id.



51
52
53
# File 'lib/baykit/bayserver/docker/fcgi/fcg_inbound_handler.rb', line 51

def req_id
  @req_id
end

#req_keep_aliveObject (readonly)

Returns the value of attribute req_keep_alive.



52
53
54
# File 'lib/baykit/bayserver/docker/fcgi/fcg_inbound_handler.rb', line 52

def req_keep_alive
  @req_keep_alive
end

#stateObject (readonly)

Returns the value of attribute state.



46
47
48
# File 'lib/baykit/bayserver/docker/fcgi/fcg_inbound_handler.rb', line 46

def state
  @state
end

Instance Method Details

#handle_begin_request(cmd) ⇒ Object

implements FcgCommandHandler



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/baykit/bayserver/docker/fcgi/fcg_inbound_handler.rb', line 146

def handle_begin_request(cmd)
  sip = ship()
  BayLog.debug("%s handle_begin_request req_id=%d} keep=%s", sip, cmd.req_id, cmd.keep_conn)

  if state != STATE_BEGIN_REQUEST
    raise ProtocolException.new("Invalid FCGI command: %d state=%d", cmd.type, @state)
  end

  check_req_id(cmd.req_id)

  @req_id = cmd.req_id
  BayLog.debug("%s begin_req get_tour req_id=%d", sip, cmd.req_id)
  tur = sip.get_tour(cmd.req_id)
  if tur == nil
    BayLog.error(BayMessage.get(:INT_NO_MORE_TOURS))
    tur = sip.ship.get_tour(cmd.req_id, true)
    tur.res.send_error(Tour::TOUR_ID_NOCHECK, HttpStatus.SERVICE_UNAVAILABLE, "No available tours")
    return NextSocketAction::CONTINUE
  end

  @req_keep_alive = cmd.keep_conn
  change_state(STATE_READ_PARAMS)
  NextSocketAction::CONTINUE
end

#handle_end_request(cmd) ⇒ Object



171
172
173
# File 'lib/baykit/bayserver/docker/fcgi/fcg_inbound_handler.rb', line 171

def handle_end_request(cmd)
  raise ProtocolException("Invalid FCGI command: %d", cmd.type)
end

#handle_params(cmd) ⇒ Object



175
176
177
178
179
180
181
182
183
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
# File 'lib/baykit/bayserver/docker/fcgi/fcg_inbound_handler.rb', line 175

def handle_params(cmd)
  BayLog.debug("%s handle_params req_id=%d", ship, cmd.req_id)

  if state != STATE_READ_PARAMS
    raise ProtocolException.new("Invalid FCGI command: %d state=%d", cmd.type, @state)
  end

  check_req_id(cmd.req_id)

  BayLog.debug("%s handle_param get_tour req_id=%d", ship, cmd.req_id)
  tur = ship.get_tour(cmd.req_id)

  if cmd.params.empty?
    # Header completed

    # check keep-alive
    #  keep-alive flag of BeginRequest has high priority
    if @req_keep_alive
      if !tur.req.headers.contains(Headers::CONNECTION)
        tur.req.headers.set(Headers::CONNECTION, "Keep-Alive")
      else
        tur.req.headers.set(Headers::CONNECTION, "Close")
      end
    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, @cont_len)

    if BayServer.harbor.trace_header
      cmd.params.each do |nv|
        BayLog.info("%s  reqHeader: %s=%s", tur, nv[0], nv[1])
      end
    end

    if req_cont_len > 0
      tur.req.set_limit(req_cont_len)
    end

    change_state(STATE_READ_STDIN)
    begin
      start_tour(tur)
    rescue HttpException => e
      BayLog.debug("%s Http error occurred: %s", ship, e)

      if req_cont_len <= 0
        # no post data
        tur.res.send_http_exception(Tour::TOUR_ID_NOCHECK, e)
        change_state(STATE_READ_STDIN)
        return NextSocketAction::CONTINUE
      else
        # Delay send
        change_state(STATE_READ_STDIN)
        tur.error = e
        tur.req.set_content_handler(ReqContentHandler::DEV_NULL)
        return NextSocketAction::CONTINUE
      end
    end

  else
    if BayServer.harbor.trace_header
      BayLog.info("%s Read FcgiParam", tur)
    end

    cmd.params.each do |nv|
      name = nv[0]
      value = nv[1]
      if BayServer.harbor.trace_header
        BayLog.info("%s  param: %s=%s", tur, name, value);
      end
      @env[name] = value

      if name.start_with?("HTTP_")
        hname = name[5 .. -1]
        tur.req.headers.add(hname, value)
      elsif name == "CONTENT_TYPE"
        tur.req.headers.add(Headers::CONTENT_TYPE, value)
      elsif name == "CONTENT_LENGTH"
        tur.req.headers.add(Headers::CONTENT_LENGTH, value)
      elsif name == "HTTPS"
        tur.is_secure = value.downcase.casecmp? "on"
      end
    end

    tur.req.uri = @env["REQUEST_URI"]
    tur.req.protocol  = @env["SERVER_PROTOCOL"]
    tur.req.method = @env["REQUEST_METHOD"]

    return NextSocketAction::CONTINUE
  end
end

#handle_stderr(cmd) ⇒ Object

Raises:

  • (ProtocolException)


268
269
270
# File 'lib/baykit/bayserver/docker/fcgi/fcg_inbound_handler.rb', line 268

def handle_stderr(cmd)
  raise ProtocolException.new("Invalid FCGI command: %d", cmd.type)
end

#handle_stdin(cmd) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
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
# File 'lib/baykit/bayserver/docker/fcgi/fcg_inbound_handler.rb', line 272

def handle_stdin(cmd)
  BayLog.debug("%s handle_stdin req_id=%d len=%d", ship, cmd.req_id, cmd.length)

  if @state != STATE_READ_STDIN
    raise ProtocolException.new("Invalid FCGI command: %d state=%d", cmd.type, @state)
  end

  begin
    check_req_id(cmd.req_id)

    tur = ship.get_tour(cmd.req_id)
    if cmd.length == 0
      #  request content completed
      if tur.error != nil
        # Error has occurred on header completed
        BayLog.debug("%s Delay send error", tur)
        raise tur.error
      else
        begin
          end_req_content(Tour::TOUR_ID_NOCHECK, tur)
          return NextSocketAction::CONTINUE
        end
      end
    else
      sid = ship.ship_id
      success = tur.req.post_req_content(Tour::TOUR_ID_NOCHECK, cmd.data, cmd.start, cmd.length) do |len, resume|
        if resume
          ship.resume_read(sid)
        end
      end

      if !success
        return NextSocketAction::SUSPEND
      else
        return NextSocketAction::CONTINUE
      end
    end

  rescue HttpException => e
    tur.req.abort
    tur.res.send_http_exception(Tour::TOUR_ID_NOCHECK, e)
    reset_state()
    return NextSocketAction::WRITE
  end
end

#handle_stdout(cmd) ⇒ Object

Raises:

  • (ProtocolException)


318
319
320
# File 'lib/baykit/bayserver/docker/fcgi/fcg_inbound_handler.rb', line 318

def handle_stdout(cmd)
  raise ProtocolException.new("Invalid FCGI command: %d", cmd.type)
end

#init(proto_handler) ⇒ Object



59
60
61
# File 'lib/baykit/bayserver/docker/fcgi/fcg_inbound_handler.rb', line 59

def init(proto_handler)
  @protocol_handler = proto_handler
end

#on_protocol_error(err) ⇒ Object



137
138
139
140
141
# File 'lib/baykit/bayserver/docker/fcgi/fcg_inbound_handler.rb', line 137

def on_protocol_error(err)
  tur = ship.get_error_tour()
  tur.res.send_error(Tour::TOUR_ID_NOCHECK, HttpStatus::BAD_REQUEST, err.message, err)
  true
end

#resetObject

implements Reusable



70
71
72
73
# File 'lib/baykit/bayserver/docker/fcgi/fcg_inbound_handler.rb', line 70

def reset
  @env.clear()
  reset_state()
end

#send_end_tour(tur, keep_alive, &callback) ⇒ Object



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
135
# File 'lib/baykit/bayserver/docker/fcgi/fcg_inbound_handler.rb', line 107

def send_end_tour(tur, keep_alive, &callback)
  BayLog.debug("%s PH:endTour: tur=%s keep=%s", ship, tur, keep_alive)

  # Send empty stdout command
  cmd = CmdStdOut.new(tur.req.key)
  @protocol_handler.post(cmd)

  # Send end request command
  cmd = CmdEndRequest.new(tur.req.key)

  ensure_func = lambda do
    # DO NOT close socket by FCGI server
    #if !keep_alive
    #  ship.post_close
    #end
  end

  begin
    @protocol_handler.post(cmd) do
      BayLog.debug("%s call back in sendEndTour: tur=%s keep=%s", self, tur, keep_alive)
      ensure_func.call()
      callback.call()
    end
  rescue IOError => e
    BayLog.debug("%s post failed in sendEndTour: tur=%s keep=%s", self, tur, keep_alive)
    ensure_func.call()
    raise e
  end
end

#send_res_content(tur, bytes, ofs, len, &callback) ⇒ Object



102
103
104
105
# File 'lib/baykit/bayserver/docker/fcgi/fcg_inbound_handler.rb', line 102

def send_res_content(tur, bytes, ofs, len, &callback)
  cmd = CmdStdOut.new(tur.req.key, bytes, ofs, len);
  @protocol_handler.post(cmd, &callback)
end

#send_res_headers(tur) ⇒ Object

implements TourHandler



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/baykit/bayserver/docker/fcgi/fcg_inbound_handler.rb', line 79

def send_res_headers(tur)
  BayLog.debug("%s PH:sendHeaders: tur=%s", ship, tur)

  scode = tur.res.headers.status
  status = "#{scode} #{HttpStatus.description(scode)}"
  tur.res.headers.set(Headers::STATUS, status)

  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) do |value|
        BayLog.info("%s resHeader:%s=%s", tur, name, value)
      end
    end
  end

  buf = SimpleBuffer.new
  HttpUtil.send_mime_headers(tur.res.headers, buf)
  HttpUtil.send_new_line(buf)
  cmd = CmdStdOut.new(tur.req.key, buf.buf, 0, buf.length)
  @protocol_handler.post(cmd)
end

#to_sObject



63
64
65
# File 'lib/baykit/bayserver/docker/fcgi/fcg_inbound_handler.rb', line 63

def to_s()
  return ClassUtil.get_local_name(self.class)
end