relocate "get" logic. see message
this stumped me for quite a minute. this procedure sometimes needs to halt execution to get input, like a server requesting input, or the client asking for permission to follow cross site redirects. the problem is if the get procedure is thought of as being part of the net interface, and compartmentalized from the program loop, actually doing that would require heavy use of continuations to go back and forth across the boundary i -do- think that the way i ultimately want to go in the end is using continuations to halt execution, catch it in the user interface to get input, and then continue. however, its a lot simpler and more immediate to change where i'm drawing the line in the separation of concerns. the continuations-based approach is enough of a diversion that i haven't managed to get anything done for the last couple of hours.
This commit is contained in:
25
gem300.rkt
25
gem300.rkt
@@ -15,13 +15,36 @@
|
|||||||
;; global state for the url of the currently visited document
|
;; global state for the url of the currently visited document
|
||||||
(define current-url null)
|
(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)
|
(define (go-cmd url)
|
||||||
(if (non-empty-string? url)
|
(if (non-empty-string? url)
|
||||||
(let ()
|
(let ()
|
||||||
(when (not (string-contains? url "://"))
|
(when (not (string-contains? url "://"))
|
||||||
(set! url (string-append "gemini://" 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))]
|
(let-values ([(doc) (gmi:parse (port->lines c-in))]
|
||||||
[(db-in db-out) (make-pipe #f)])
|
[(db-in db-out) (make-pipe #f)])
|
||||||
|
|
||||||
|
27
net.rkt
27
net.rkt
@@ -1,6 +1,6 @@
|
|||||||
#lang racket
|
#lang racket
|
||||||
|
|
||||||
(provide get)
|
(provide request)
|
||||||
|
|
||||||
(require openssl)
|
(require openssl)
|
||||||
(require net/url-string)
|
(require net/url-string)
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
;; and the input port for the rest of the body.
|
;; and the input port for the rest of the body.
|
||||||
;; this procedure will fail if the response is malformed, however, it
|
;; this procedure will fail if the response is malformed, however, it
|
||||||
;; is not up to it to validate the contents of the response.
|
;; 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 url (string->url url-str))
|
||||||
(define-values (c-in c-out)
|
(define-values (c-in c-out)
|
||||||
(ssl-connect (url-host url)
|
(ssl-connect (url-host url)
|
||||||
@@ -46,26 +46,3 @@
|
|||||||
|
|
||||||
[else
|
[else
|
||||||
(values (string->number status) meta)])))))
|
(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))
|
|
||||||
|
Reference in New Issue
Block a user