about summary refs log tree commit homepage
path: root/lib/yahns/stream_file.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/yahns/stream_file.rb')
-rw-r--r--lib/yahns/stream_file.rb34
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/yahns/stream_file.rb b/lib/yahns/stream_file.rb
new file mode 100644
index 0000000..eba9632
--- /dev/null
+++ b/lib/yahns/stream_file.rb
@@ -0,0 +1,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