Class: Baykit::BayServer::Docker::SendFile::FileContentHandler

Inherits:
Object
  • Object
show all
Includes:
Agent, Agent::Multiplexer, Rudders, Tours, Tours::ReqContentHandler, Util
Defined in:
lib/baykit/bayserver/docker/send_file/file_content_handler.rb

Constant Summary

Constants included from Tours::ReqContentHandler

Tours::ReqContentHandler::DEV_NULL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FileContentHandler

Returns a new instance of FileContentHandler.



23
24
25
26
# File 'lib/baykit/bayserver/docker/send_file/file_content_handler.rb', line 23

def initialize(path)
  @path = path
  @abortable = true
end

Instance Attribute Details

#abortableObject (readonly)

Returns the value of attribute abortable.



21
22
23
# File 'lib/baykit/bayserver/docker/send_file/file_content_handler.rb', line 21

def abortable
  @abortable
end

#pathObject (readonly)

Returns the value of attribute path.



20
21
22
# File 'lib/baykit/bayserver/docker/send_file/file_content_handler.rb', line 20

def path
  @path
end

Instance Method Details

#on_abort_req(tur) ⇒ Object



43
44
45
46
# File 'lib/baykit/bayserver/docker/send_file/file_content_handler.rb', line 43

def on_abort_req(tur)
  BayLog.debug("%s onAbortReq aborted=%s", tur, abortable)
  return abortable
end

#on_end_req_content(tur) ⇒ Object



37
38
39
40
41
# File 'lib/baykit/bayserver/docker/send_file/file_content_handler.rb', line 37

def on_end_req_content(tur)
  BayLog.debug("%s endReqContent", tur)
  send_file_async(tur, path, tur.res.charset)
  @abortable = false
end

#on_read_req_content(tur, buf, start, len, &lis) ⇒ Object

Implements ReqContentHandler



32
33
34
35
# File 'lib/baykit/bayserver/docker/send_file/file_content_handler.rb', line 32

def on_read_req_content(tur, buf, start, len, &lis)
  BayLog.debug("%s onReadReqContent(Ignore) len=%d", tur, len)
  tur.req.consumed(tur.tour_id, len, &lis)
end

#send_file_async(tur, file, charset) ⇒ Object

Sending file methods



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/baykit/bayserver/docker/send_file/file_content_handler.rb', line 52

def send_file_async(tur, file, charset)

  if File.directory?(file)
    raise HttpException.new HttpStatus::FORBIDDEN, file
  elsif !File.exist?(file)
    raise HttpException.new HttpStatus::NOT_FOUND, file
  end

  mime_type = nil

  rname = File.basename(file)
  pos = rname.rindex('.')
  if pos
    ext = rname[pos + 1 .. -1].downcase
    mime_type = Mimes.type(ext)
  end

  if !mime_type
    mime_type = "application/octet-stream"
  end

  if mime_type.start_with?("text/") && charset != nil
    mime_type = mime_type + "; charset=" + charset
  end

  file_len = ::File.size(file)

  tur.res.headers.set_content_type(mime_type)
  tur.res.headers.set_content_length(file_len)

  begin
    tur.res.send_headers(Tour::TOUR_ID_NOCHECK)

    bufsize = tur.ship.protocol_handler.max_res_packet_data_size
    agt = GrandAgent.get(tur.ship.agent_id)

    f = File.open(file, "rb")
    rd = IORudder.new(f)

    case(BayServer.harbor.file_multiplexer)

    when Harbor::MULTIPLEXER_TYPE_SPIDER
      mpx = agt.spider_multiplexer

    when Harbor::MULTIPLEXER_TYPE_SPIN
      mpx = agt.spin_multiplexer

    when Harbor::MULTIPLEXER_TYPE_TAXI
      mpx = agt.taxi_multiplexer

    else
      raise Sink.new
    end

    send_file_ship = SendFileShip.new
    tp = PlainTransporter.new(
      mpx,
      send_file_ship,
      true,
      8195,
      false)

    send_file_ship.init(rd, tp, tur)
    sid = send_file_ship.ship_id

    tur.res.set_consume_listener do |len, resume|
      if resume
        send_file_ship.resume_read(sid)
      end
    end

    mpx.add_rudder_state(rd, RudderState.new(rd, tp))
    mpx.req_read(rd)

  rescue IOError => e
    BayLog.error_e(e)
    raise HttpException.new(HttpStatus::INTERNAL_SERVER_ERROR, file)
  end

end