Binary-safe send-file.

This commit is contained in:
Alex Shinn 2014-04-06 22:17:17 +09:00
parent 46a8b11645
commit 7685d1f097

View file

@ -175,17 +175,18 @@
(out (if (pair? o) (car o) (current-output-port))) (out (if (pair? o) (car o) (current-output-port)))
(fd (if (port? in) (port-fileno in) in)) (fd (if (port? in) (port-fileno in) in))
(sock (if (port? out) (port-fileno out) out))) (sock (if (port? out) (port-fileno out) out)))
(define (copy-bytes)
(let ((b (read-u8 in)))
(cond ((not (eof-object? b))
(write-u8 b out)
(copy-bytes)))))
(if (and fd sock (is-a-socket? sock)) (if (and fd sock (is-a-socket? sock))
(let lp ((start 0)) (let lp ((start 0))
(let ((res (%send-file fd sock start))) (let ((res (%send-file fd sock start)))
(cond (cond
((not res) (lp start)) ((not res) (copy-bytes))
((not (zero? res)) (lp (+ start res)))))) ((not (zero? res)) (lp (+ start res))))))
(let lp () (copy-bytes))))
(let ((str (read-string 8192 in)))
(cond ((not (eof-object? str))
(display str out)
(lp))))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; higher order port operations ;; higher order port operations