Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add chunked encoding

  • Loading branch information...
commit 95079e940a69ea397e6dc0a4bb8e6eb96587a4d6 1 parent af5b1b7
@bickfordb authored
View
35 src/peloton/httpd.clj
@@ -497,3 +497,38 @@
(react)))
threads (doseq [i (range num-threads)] (doto (Thread. f) (.start)))]
(doseq [^Thread t threads] (.join t))))
+
+(defn write-file-channel-chunk!
+ [^FileChannel channel
+ offset
+ len]
+
+(defn write-file-channel!
+ [^FileChannel channel
+ offset
+ len]
+ (loop [offset offset
+ len len]
+ (let [amt (safe -1 (.transferTo offset len (.socketChannel *conn*)))]
+ (cond
+ (< amt -1) (close-with-error! "failed to write!")
+ (= amt 0) (on-socket-write-ready write-file-channel! offset len)
+ :else (recur (+ offset amt) (- len amt))))))
+
+(defn create-file-handler
+ [^String dir & opts]
+ (let [opts0 (apply hash-map opts)]
+ (fn [^String path]
+ (let [f (safe nil (java.io.RandomAccessFile. (File. (java.io.File. dir) path) "r"))
+ ch (when (not-nil? f) (safe nil (.getChannel f)))
+ offset 0
+ content-type "application/octet-stream"
+ sz (if (not-nil? f) (.size ch) 0)]
+ (if (nil? ch)
+ (on-404)
+ (let []
+ (add-response-header! "Content-Type" content-type)
+ (add-response-header! "Content-Length" sz)
+ (send-headers!)
+ (write-file-channel! ch sz)))))))
+
View
1  src/peloton/main.clj
@@ -33,5 +33,6 @@
:listen-backlog 10
:ports (filter #(not (nil? %)) (map safe-int ports))}
[:GET #"^/$" index-handler]
+ [:GET #"^/etc/(.+)$" (create-file-handler "/etc/")]
[:GET #"^/chunked$" chunked-handler]))
View
2  src/peloton/util.clj
@@ -14,6 +14,8 @@
~@body
(catch Exception e# ~default)))
+(defn not-nil? [x] (not (nil? x)))
+
(defn safe-int
[^String s]
(try
Please sign in to comment.
Something went wrong with that request. Please try again.