set the scene for mime handling. however, some changes to how internal state is handled will need to be made before adding plaintext rendering.
This commit is contained in:
42
client.rkt
42
client.rkt
@@ -80,6 +80,23 @@
|
|||||||
(let ([url (gmi:match-link document-object line)])
|
(let ([url (gmi:match-link document-object line)])
|
||||||
(get (combine-url/relative current-url (url->string url))))))
|
(get (combine-url/relative current-url (url->string url))))))
|
||||||
|
|
||||||
|
(define/private (handle-gmi c-in)
|
||||||
|
(let-values ([(doc) (gmi:parse (port->lines c-in))]
|
||||||
|
[(db-in db-out) (make-pipe #f)])
|
||||||
|
|
||||||
|
(set! document-object doc)
|
||||||
|
|
||||||
|
(set! document-buffer db-in)
|
||||||
|
(parameterize ([current-output-port db-out])
|
||||||
|
(gmi:render doc))
|
||||||
|
|
||||||
|
(set! current-url url)
|
||||||
|
|
||||||
|
(let ([remaining (pipe-content-length db-in)])
|
||||||
|
(printf "document retrieved. ~a bytes\n" remaining))
|
||||||
|
|
||||||
|
(next-cmd)))
|
||||||
|
|
||||||
(define/private (get url)
|
(define/private (get url)
|
||||||
(define (iter url depth)
|
(define (iter url depth)
|
||||||
(with-handlers
|
(with-handlers
|
||||||
@@ -90,7 +107,7 @@
|
|||||||
(λ (exn)
|
(λ (exn)
|
||||||
(displayln (exn-message exn)))])
|
(displayln (exn-message exn)))])
|
||||||
|
|
||||||
(let-values ([(status header c-in) (net:request url)])
|
(let-values ([(status meta c-in) (net:request url)])
|
||||||
;; TODO there are bunch of other status codes to deal with for
|
;; TODO there are bunch of other status codes to deal with for
|
||||||
;; compliance
|
;; compliance
|
||||||
(cond
|
(cond
|
||||||
@@ -109,28 +126,19 @@
|
|||||||
[(and (>= status 20)
|
[(and (>= status 20)
|
||||||
(<= status 29))
|
(<= status 29))
|
||||||
|
|
||||||
(let-values ([(doc) (gmi:parse (port->lines c-in))]
|
(let ([mime (car (string-split meta ";"))])
|
||||||
[(db-in db-out) (make-pipe #f)])
|
(cond
|
||||||
|
[(string=? mime "text/gemini")
|
||||||
(set! document-object doc)
|
(handle-gmi c-in)]
|
||||||
|
[else
|
||||||
(set! document-buffer db-in)
|
(printf "unsupported mime type: ~a~n" mime)]))]
|
||||||
(parameterize ([current-output-port db-out])
|
|
||||||
(gmi:render doc))
|
|
||||||
|
|
||||||
(set! current-url url)
|
|
||||||
|
|
||||||
(let ([remaining (pipe-content-length db-in)])
|
|
||||||
(printf "document retrieved. ~a bytes\n" remaining))
|
|
||||||
|
|
||||||
(next-cmd))]
|
|
||||||
|
|
||||||
;; 30-39 REDIRECT
|
;; 30-39 REDIRECT
|
||||||
[(and (>= status 30)
|
[(and (>= status 30)
|
||||||
(<= status 39))
|
(<= status 39))
|
||||||
(if (> depth 5)
|
(if (> depth 5)
|
||||||
(displayln "WARNING: maximum redirection depth exceeded")
|
(displayln "WARNING: maximum redirection depth exceeded")
|
||||||
(iter (string->url header) (sub1 depth)))]
|
(iter (string->url meta) (sub1 depth)))]
|
||||||
|
|
||||||
;; 40-49 TEMPORARY FAILURE
|
;; 40-49 TEMPORARY FAILURE
|
||||||
[(and (>= status 40)
|
[(and (>= status 40)
|
||||||
|
Reference in New Issue
Block a user