diff --git a/gem300.rkt b/gem300.rkt index 99fd096..ca9be90 100644 --- a/gem300.rkt +++ b/gem300.rkt @@ -15,13 +15,36 @@ ;; global state for the url of the currently visited document (define current-url null) +(define (get url-str) + (define (iter url-str depth) + (let-values ([(status header c-in) (net:request url-str)]) + ;; TODO there are bunch of other status codes to deal with for + ;; compliance + (cond + ;; clients MUST reject status codes outside of the 10-69 range + [(or (< status 10) + (> status 69)) + (error "server returned invalid status code")] + + ;; 30-39 redirection + [(and (>= status 30) + (<= status 39)) + (if (> depth 5) + (error "maximum redirection depth exceeded") + (iter header (sub1 depth)))] + + [else + (values status header c-in)]))) + + (iter url-str 5)) + (define (go-cmd url) (if (non-empty-string? url) (let () (when (not (string-contains? url "://")) (set! url (string-append "gemini://" url))) - (let-values ([(status meta c-in) (net:get url)]) + (let-values ([(status meta c-in) (get url)]) (let-values ([(doc) (gmi:parse (port->lines c-in))] [(db-in db-out) (make-pipe #f)]) diff --git a/net.rkt b/net.rkt index 52d1abd..c075859 100644 --- a/net.rkt +++ b/net.rkt @@ -1,6 +1,6 @@ #lang racket -(provide get) +(provide request) (require openssl) (require net/url-string) @@ -9,7 +9,7 @@ ;; and the input port for the rest of the body. ;; this procedure will fail if the response is malformed, however, it ;; is not up to it to validate the contents of the response. -(define (send-request url-str) +(define (request url-str) (define url (string->url url-str)) (define-values (c-in c-out) (ssl-connect (url-host url) @@ -46,26 +46,3 @@ [else (values (string->number status) meta)]))))) - -(define (get url-str) - (define (iter url-str depth) - (let-values ([(status header c-in) (send-request url-str)]) - ;; TODO there are bunch of other status codes to deal with for - ;; compliance - (cond - ;; clients MUST reject status codes outside of the 10-69 range - [(or (< status 10) - (> status 69)) - (error "server returned invalid status code")] - - ;; 30-39 redirection - [(and (>= status 30) - (<= status 39)) - (if (> depth 5) - (error "maximum redirection depth exceeded") - (iter header (sub1 depth)))] - - [else - (values status header c-in)]))) - - (iter url-str 5))