yahns.git  about / heads / tags
sleepy, multi-threaded, non-blocking application server for Ruby
blob eba9632f87efa9893e86c0deb8d17a3590b2635d 893 bytes (raw)
$ git show v0.0.1:lib/yahns/stream_file.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
# -*- encoding: binary -*-
# Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> et. al.
# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
require_relative 'wbuf_common'

class Yahns::StreamFile # :nodoc:
  include Yahns::WbufCommon

  def initialize(body, persist, offset, count)
    if body.respond_to?(:to_io)
      @tmpio = body.to_io
    else
      path = body.to_path
      if path =~ %r{\A/dev/fd/(\d+)\z}
        @tmpio = IO.for_fd($1.to_i)
        @tmpio.autoclose = false
      else
        @tmpio = File.open(path)
      end
    end
    @sf_offset = offset
    @sf_count = count || @tmpio.stat.size
    @wbuf_persist = persist # whether or not we keep the connection alive
    @body = body
  end

  # called by last wbuf_flush
  def wbuf_close(client)
    if File === @tmpio && @tmpio != @body
      @tmpio.close
    end
    wbuf_close_common(client)
  end
end

git clone git://yhbt.net/yahns.git
git clone https://yhbt.net/yahns.git