Making http ports binary.

This commit is contained in:
Alex Shinn 2014-03-21 16:04:21 +09:00
parent d7a8ff0a76
commit 1e06cd215a
3 changed files with 41 additions and 6 deletions

View file

@ -344,7 +344,7 @@
(set! offset (+ offset n)) (set! offset (+ offset n))
n) n)
(else (else
(string-copy! str start buf offset len) (string-copy! str start buf offset (+ offset len))
(let lp ((i (+ start (- len offset)))) (let lp ((i (+ start (- len offset))))
(set! buf (generator)) (set! buf (generator))
(cond (cond

View file

@ -51,6 +51,40 @@
n n
(if (>= j len) "" (substring line (+ j 1) len)))))) (if (>= j len) "" (substring line (+ j 1) len))))))
(define (make-generated-binary-input-port generator)
(let ((buf #u8())
(len 0)
(offset 0))
(make-custom-binary-input-port
(lambda (bv start end)
(let ((n (- end start)))
(cond
((>= (- len offset) n)
(bytevector-copy! bv start buf offset (+ offset n))
(set! offset (+ offset n))
n)
(else
(bytevector-copy! bv start buf offset (+ offset len))
(let lp ((i (+ start (- len offset))))
(set! buf (generator))
(cond
((not (bytevector? buf))
(set! buf #u8())
(set! len 0)
(set! offset 0)
(- i start))
(else
(set! len (bytevector-length buf))
(set! offset 0)
(cond
((>= (- len offset) (- n i))
(bytevector-copy! bv i buf offset (+ offset (- n i)))
(set! offset (+ offset (- n i)))
n)
(else
(bytevector-copy! bv i buf offset len)
(lp (+ i (- len offset)))))))))))))))
(define (http-wrap-chunked-input-port in) (define (http-wrap-chunked-input-port in)
(define (read-chunk in) (define (read-chunk in)
(let* ((line (read-line in)) (let* ((line (read-line in))
@ -59,8 +93,8 @@
((not (and (integer? n) (<= 0 n http-chunked-size-limit))) ((not (and (integer? n) (<= 0 n http-chunked-size-limit)))
(error "invalid chunked size line" line)) (error "invalid chunked size line" line))
((zero? n) "") ((zero? n) "")
(else (read-string n in))))) (else (read-bytevector n in)))))
(make-generated-input-port (make-generated-binary-input-port
(lambda () (read-chunk in)))) (lambda () (read-chunk in))))
(define (http-get/raw url in-headers limit) (define (http-get/raw url in-headers limit)
@ -91,7 +125,7 @@
(display (cdr x) out) (display "\r\n" out)) (display (cdr x) out) (display "\r\n" out))
in-headers) in-headers)
(display "Connection: close\r\n\r\n" out) (display "Connection: close\r\n\r\n" out)
(flush-output out) (flush-output-port out)
(let* ((resp (http-parse-response (read-line in))) (let* ((resp (http-parse-response (read-line in)))
(headers (mime-headers->list in)) (headers (mime-headers->list in))
(status (quotient (cadr resp) 100))) (status (quotient (cadr resp) 100)))

View file

@ -4,6 +4,7 @@
call-with-input-url call-with-input-url/headers call-with-input-url call-with-input-url/headers
with-input-from-url with-input-from-url
http-parse-request http-parse-form) http-parse-request http-parse-form)
(import (chibi) (srfi 39) (chibi net) (chibi io) (import (scheme base) (scheme write) (scheme char)
(chibi uri) (chibi mime)) (srfi 39)
(chibi net) (chibi io) (chibi uri) (chibi mime))
(include "http.scm")) (include "http.scm"))