public
Created

  • Download Gist
chunking-port.scm
Scheme
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
;; rough conception - chunks output only
(use-modules (ice-9 q))
(define (q-for-each f q)
(while (not (q-empty? q))
(f (deq! q))))
 
(define f! #f)
 
(define (port->chunking-port port)
(define queue (make-q))
(define (put-char c)
(enq! queue c))
(define (put-string s)
(string-for-each (lambda (c) (enq! queue c))
s))
(define (flush)
(let ((len (q-length queue)))
(display (number->string len 16) port)
(display "\r\n" port)
(q-for-each (lambda (elem) (write-char elem port))
queue)
(display "\r\n" port)))
(define (get-char)
(read-char port))
(define (close)
(flush)
(flush) ;; this way a zero chunk gets written
(close-port port))
(set! f! flush)
(make-soft-port
(vector
put-char
put-string
flush
get-char
close)
"rw"))
 
(call-with-output-string
(lambda (out-raw)
(let ((out-chunked (port->chunking-port out-raw)))
(display "First chunk" out-chunked)
(flush-all-ports)
(f!)
;(force-output out-chunked)
(display "Second chunk" out-chunked)
(f!)
;(force-output out-chunked)
(display "Third chunk" out-chunked)
(f!)
(f!)
;(close-port out-chunked)
)))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.