Class: Baykit::BayServer::Docker::Terminal::FullyHijackersShip

Inherits:
HijackersShip
  • Object
show all
Includes:
Http::H1, Http::H1::H1CommandHandler, Protocol, Util
Defined in:
lib/baykit/bayserver/docker/terminal/fully_hijackers_ship.rb

Constant Summary collapse

STATE_READ_HEADER =
1
STATE_READ_CONTENT =
2
STATE_FINISHED =
3

Instance Attribute Summary collapse

Attributes inherited from HijackersShip

#file_wrote_len, #tour, #tour_id

Instance Method Summary collapse

Methods inherited from HijackersShip

#check_timeout, #notify_close, #notify_eof, #notify_error, #to_s

Constructor Details

#initializeFullyHijackersShip

Returns a new instance of FullyHijackersShip.



30
31
32
# File 'lib/baykit/bayserver/docker/terminal/fully_hijackers_ship.rb', line 30

def initialize
  super
end

Instance Attribute Details

#command_unpackerObject (readonly)

Returns the value of attribute command_unpacker.



28
29
30
# File 'lib/baykit/bayserver/docker/terminal/fully_hijackers_ship.rb', line 28

def command_unpacker
  @command_unpacker
end

#packet_storeObject (readonly)

Returns the value of attribute packet_store.



26
27
28
# File 'lib/baykit/bayserver/docker/terminal/fully_hijackers_ship.rb', line 26

def packet_store
  @packet_store
end

#packet_unpackerObject (readonly)

Returns the value of attribute packet_unpacker.



27
28
29
# File 'lib/baykit/bayserver/docker/terminal/fully_hijackers_ship.rb', line 27

def packet_unpacker
  @packet_unpacker
end

#stateObject (readonly)

Returns the value of attribute state.



25
26
27
# File 'lib/baykit/bayserver/docker/terminal/fully_hijackers_ship.rb', line 25

def state
  @state
end

Instance Method Details

#handle_content(cmd) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/baykit/bayserver/docker/terminal/fully_hijackers_ship.rb', line 112

def handle_content(cmd)

  if @state != STATE_READ_CONTENT
    raise ProtocolException.new("Content command not expected")
  end

  available = @tour.res.send_res_content(@tour_id, cmd.buf, cmd.start, cmd.len)
  if @tour.res.bytes_posted == @tour.res.bytes_limit
    end_res_content(@tour)
    return NextSocketAction::CONTINUE
  elsif !available
    return NextSocketAction::SUSPEND
  else
    NextSocketAction::CONTINUE
  end
end

#handle_end_content(cmd) ⇒ Object

Raises:

  • (Sink)


129
130
131
132
# File 'lib/baykit/bayserver/docker/terminal/fully_hijackers_ship.rb', line 129

def handle_end_content(cmd)
  # never called
  raise Sink.new()
end

#handle_header(cmd) ⇒ Object

Implements H1CommandHandler



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/baykit/bayserver/docker/terminal/fully_hijackers_ship.rb', line 72

def handle_header(cmd)
  if @state == STATE_FINISHED
    change_state(STATE_READ_HEADER)
  end

  if @state != STATE_READ_HEADER
    raise ProtocolException.new("Header command not expected: state=%d", @state)
  end

  if BayServer.harbor.trace_header
    BayLog.info("%s hijack: resStatus: %d", self, cmd.status)
  end

  cmd.headers.each do |nv|
    @tour.res.headers.add(nv[0], nv[1])
    if BayServer.harbor.trace_header
      BayLog.info("%s hijack: resHeader: %s=%s", self, nv[0], nv[1]);
    end
  end

  @tour.res.headers.status = cmd.status != nil ? cmd.status : HttpStatus::OK
  @tour.res.send_headers(@tour_id)

  res_cont_len = @tour.res.headers.content_length
  BayLog.debug("%s contLen in header: %d", self, res_cont_len)

  if res_cont_len == 0 || cmd.status == HttpStatus::NOT_MODIFIED
    end_res_content(@tour)
  else
    change_state(STATE_READ_CONTENT)
    sid = @tour.ship.id()
    @tour.res.set_consume_listener do |len, resume|
      if resume
        @tour.ship.resume_read(sid)
      end
    end
  end
  return NextSocketAction::CONTINUE
end

#init(tur, rd, tp) ⇒ Object

Init method



38
39
40
41
42
43
# File 'lib/baykit/bayserver/docker/terminal/fully_hijackers_ship.rb', line 38

def init(tur, rd, tp)
  super
  @packet_store = PacketStore.new(tur.ship, H1PacketFactory.new)
  @command_unpacker = H1CommandUnPacker.new(self, false)
  @packet_unpacker = H1PacketUnPacker.new(@command_unpacker, @packet_store)
end

#notify_read(buf) ⇒ Object

Override



59
60
61
62
63
64
65
66
# File 'lib/baykit/bayserver/docker/terminal/fully_hijackers_ship.rb', line 59

def notify_read(buf)
  @file_wrote_len += buf.length

  BayLog.debug "#{self} read hijack #{buf.length} bytes: total=#{@file_wrote_len}"

  return @packet_unpacker.bytes_received(buf)

end

#req_finishedObject



134
135
136
# File 'lib/baykit/bayserver/docker/terminal/fully_hijackers_ship.rb', line 134

def req_finished()
  return @state == STATE_FINISHED
end

#resetObject

implements Reusable



49
50
51
52
# File 'lib/baykit/bayserver/docker/terminal/fully_hijackers_ship.rb', line 49

def reset()
  super
  @state = STATE_FINISHED
end