Skip to content

Commit

Permalink
send file via ChunkedFile when pipeline contains a ChunkedWriteHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
markdingram committed Oct 12, 2016
1 parent 3c5a85d commit d32ee4d
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions src/aleph/http/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
HttpResponse HttpResponseStatus
DefaultHttpContent
HttpVersion
LastHttpContent]
LastHttpContent HttpChunkedInput]
[io.netty.handler.stream
ChunkedFile ChunkedWriteHandler]
[java.io
File
RandomAccessFile
Expand Down Expand Up @@ -308,20 +310,37 @@

(netty/write-and-flush ch empty-last-content)))

(defn send-chunked-file [ch ^HttpMessage msg ^File file]
(let [raf (RandomAccessFile. file "r")
len (.length raf)
ci (HttpChunkedInput. (ChunkedFile. raf))]
(try-set-content-length! msg len)
(netty/write ch msg)
(netty/write-and-flush ch ci)))

(defn send-file-region [ch ^HttpMessage msg ^File file]
(let [raf (RandomAccessFile. file "r")
len (.length raf)
fc (.getChannel raf)
fr (DefaultFileRegion. fc 0 len)]
(try-set-content-length! msg len)
(netty/write ch msg)
(netty/write ch fr)
(netty/write-and-flush ch empty-last-content)))

(defn send-file-body [ch ssl? ^HttpMessage msg ^File file]
(if ssl?
(cond
ssl?
(send-streaming-body ch msg
(-> file
(bs/to-byte-buffers {:chunk-size 1e6})
s/->source))
(let [raf (RandomAccessFile. file "r")
len (.length raf)
fc (.getChannel raf)
fr (DefaultFileRegion. fc 0 len)]
(try-set-content-length! msg len)
(netty/write ch msg)
(netty/write ch fr)
(netty/write-and-flush ch empty-last-content))))

(-> ch (.pipeline) (.get ChunkedWriteHandler))
(send-chunked-file ch msg file)

:else
(send-file-region ch msg file)))

(defn send-contiguous-body [ch ^HttpMessage msg body]
(let [omitted? (identical? :aleph/omitted body)
Expand Down

0 comments on commit d32ee4d

Please sign in to comment.