Compare commits
12 Commits
759c66889c
...
master
Author | SHA1 | Date | |
---|---|---|---|
d788f6376e | |||
7114336863 | |||
6529f4118c | |||
0dc93d369e | |||
4323f759fa | |||
3240b30b6f | |||
bc5ddeb84c | |||
b0af59a316 | |||
4ea51c0ed5 | |||
e6d28d6798 | |||
91c04ac3c6 | |||
b05dc90e70 |
335
client.rkt
335
client.rkt
@@ -3,7 +3,8 @@
|
|||||||
(provide client%)
|
(provide client%)
|
||||||
|
|
||||||
(require (prefix-in net: "net.rkt")
|
(require (prefix-in net: "net.rkt")
|
||||||
(prefix-in gmi: "gmi.rkt"))
|
(prefix-in gmi: "gmi.rkt")
|
||||||
|
(prefix-in txt: "txt.rkt"))
|
||||||
|
|
||||||
(require net/url-string)
|
(require net/url-string)
|
||||||
|
|
||||||
@@ -21,44 +22,42 @@
|
|||||||
(53 . "proxy request refused")
|
(53 . "proxy request refused")
|
||||||
(59 . "bad request")))
|
(59 . "bad request")))
|
||||||
|
|
||||||
|
;;; CLIENT
|
||||||
|
|
||||||
(define client%
|
(define client%
|
||||||
(class object%
|
(class object%
|
||||||
(define document-object '())
|
(define document-object '())
|
||||||
(define document-buffer '())
|
(define document-buffer '())
|
||||||
(define current-url '())
|
(define history '())
|
||||||
|
|
||||||
(super-new)
|
(super-new)
|
||||||
|
|
||||||
(define/public (up-cmd)
|
;;;
|
||||||
(if (null? current-url)
|
;;; client commands
|
||||||
|
;;;
|
||||||
|
|
||||||
(displayln "you need to 'go' somewhere first!")
|
;; 'go <url>' to go to a specific page
|
||||||
|
(define/public (go-cmd destination-string)
|
||||||
|
;; the 'go' command is the only command with any sort of URL
|
||||||
|
;; processing that isn't strictly RFC3986. Specifically, the
|
||||||
|
;; user MAY omit the scheme. Rackets 'url' module will interpret
|
||||||
|
;; the domain as part of the path in lieu of a scheme, and
|
||||||
|
;; performing this operation on an existing url struct is
|
||||||
|
;; non-trivial.
|
||||||
|
(when (not (string-contains? destination-string "://"))
|
||||||
|
(set! destination-string
|
||||||
|
(string-append "gemini://" destination-string)))
|
||||||
|
|
||||||
(if (string=? (path->string
|
(get-document (string->url destination-string)))
|
||||||
(simplify-path
|
|
||||||
(url->path current-url)))
|
|
||||||
"/")
|
|
||||||
|
|
||||||
(displayln "already at root!")
|
|
||||||
|
|
||||||
(let ([parent
|
|
||||||
(simplify-path
|
|
||||||
(build-path (url->path current-url) 'up)
|
|
||||||
#f)])
|
|
||||||
(get (struct-copy url current-url
|
|
||||||
[path (url-path (path->url parent))]))))))
|
|
||||||
|
|
||||||
(define/public (go-cmd url-string)
|
|
||||||
(if (non-empty-string? url-string)
|
|
||||||
(let ()
|
|
||||||
(when (not (string-contains? url-string "://"))
|
|
||||||
(set! url-string (string-append "gemini://" url-string)))
|
|
||||||
|
|
||||||
(get (string->url url-string)))
|
|
||||||
|
|
||||||
(displayln "go where?")))
|
|
||||||
|
|
||||||
|
;; 'next' to display the next page
|
||||||
(define/public (next-cmd)
|
(define/public (next-cmd)
|
||||||
|
(when (null? document-buffer)
|
||||||
|
(raise-user-error
|
||||||
|
'next-cmd
|
||||||
|
"there is currently no visited document"))
|
||||||
|
|
||||||
|
;; displays remaining buffer content, maximum of 10 lines.
|
||||||
(define (iter depth)
|
(define (iter depth)
|
||||||
(when (and (> depth 0)
|
(when (and (> depth 0)
|
||||||
(> (pipe-content-length document-buffer) 0))
|
(> (pipe-content-length document-buffer) 0))
|
||||||
@@ -66,107 +65,215 @@
|
|||||||
(displayln (read-line document-buffer))
|
(displayln (read-line document-buffer))
|
||||||
(iter (sub1 depth)))))
|
(iter (sub1 depth)))))
|
||||||
|
|
||||||
(if (null? document-buffer)
|
(let ()
|
||||||
(displayln "you need to 'go' somewhere first!")
|
(iter 10)
|
||||||
(let ()
|
(newline)
|
||||||
(iter 10)
|
(let ([remaining (pipe-content-length document-buffer)])
|
||||||
(newline)
|
(printf "~a bytes remaining\n" remaining))))
|
||||||
(let ([remaining (pipe-content-length document-buffer)])
|
|
||||||
(printf "~a bytes remaining\n" remaining)))))
|
|
||||||
|
|
||||||
(define/public (visit-cmd line)
|
;; 'visit {n}' to visit a link in the page
|
||||||
(if (null? document-object)
|
(define/public (visit-cmd id)
|
||||||
(displayln "you need to 'go' somewhere first!")
|
(when (not (gmi:document? document-object))
|
||||||
(let ([url (gmi:match-link document-object line)])
|
(raise-user-error
|
||||||
(get (combine-url/relative current-url (url->string url))))))
|
'visit-cmd
|
||||||
|
"currently visited document is not a gemini document"))
|
||||||
|
|
||||||
(define/public (url-cmd (link-id #f))
|
(get-document
|
||||||
(if (null? document-object)
|
(gmi:match-link document-object id)))
|
||||||
(displayln "you need to 'go' somewhere first!")
|
|
||||||
(if link-id
|
|
||||||
(let ([link (gmi:match-link document-object link-id)])
|
|
||||||
(if link
|
|
||||||
(displayln (url->string link))
|
|
||||||
(displayln "no link with that id")))
|
|
||||||
(displayln (url->string current-url)))))
|
|
||||||
|
|
||||||
(define/private (handle-gmi url c-in)
|
;; 'url [n]' to get current url or links url
|
||||||
(let-values ([(doc) (gmi:parse (port->lines c-in))]
|
(define/public (url-cmd (id #f))
|
||||||
[(db-in db-out) (make-pipe #f)])
|
(if id
|
||||||
|
(if (gmi:document? document-object)
|
||||||
|
(displayln (url->string
|
||||||
|
(absolutise-url
|
||||||
|
(gmi:match-link document-object id))))
|
||||||
|
(raise-user-error
|
||||||
|
'url-cmd
|
||||||
|
"currently visited document is not a gemini document"))
|
||||||
|
(displayln (url->string (car history)))))
|
||||||
|
|
||||||
(set! document-object doc)
|
;; 'back' to go back one in history
|
||||||
|
(define/public (back-cmd)
|
||||||
|
(when (< (length history) 2)
|
||||||
|
(raise-user-error
|
||||||
|
'back-cmd
|
||||||
|
"cannot go any further back in history!"))
|
||||||
|
|
||||||
(set! document-buffer db-in)
|
;; drop current page
|
||||||
(parameterize ([current-output-port db-out])
|
(set! history (cdr history))
|
||||||
(gmi:render doc))
|
(let ([visiting (car history)])
|
||||||
|
;; drop the page before that, too, since get-document will add
|
||||||
|
;; it
|
||||||
|
(set! history (cdr history))
|
||||||
|
|
||||||
(set! current-url url)
|
(get-document visiting)))
|
||||||
|
|
||||||
(let ([remaining (pipe-content-length db-in)])
|
;; 'up' to go up one directory
|
||||||
(printf "document retrieved. ~a bytes\n" remaining))
|
(define/public (up-cmd)
|
||||||
|
(when (null? history)
|
||||||
|
(error 'no-where))
|
||||||
|
|
||||||
(next-cmd)))
|
(when (string=? (path->string
|
||||||
|
(simplify-path
|
||||||
|
(url->path (car history))))
|
||||||
|
"/")
|
||||||
|
|
||||||
(define/private (get url)
|
(error 'at-root))
|
||||||
(define (iter url depth)
|
|
||||||
(with-handlers
|
|
||||||
([exn:fail:network?
|
|
||||||
(λ (exn)
|
|
||||||
(displayln (exn-message exn)))]
|
|
||||||
[net:exn:fail:response?
|
|
||||||
(λ (exn)
|
|
||||||
(displayln (exn-message exn)))])
|
|
||||||
|
|
||||||
(let-values ([(status meta c-in) (net:request url)])
|
(let ([parent
|
||||||
;; TODO there are bunch of other status codes to deal with for
|
(simplify-path
|
||||||
;; compliance
|
(build-path (url->path (car history)) 'up)
|
||||||
(cond
|
#f)])
|
||||||
;; clients MUST reject status codes outside of the 10-69 range
|
(get-document
|
||||||
[(or (< status 10)
|
(struct-copy url (car history)
|
||||||
(> status 69))
|
[path (url-path (path->url parent))]))))
|
||||||
(displayln "WARNING: server returned invalid status code")]
|
|
||||||
|
|
||||||
;; 10-19 INPUT REQUIRED
|
;;;
|
||||||
[(and (>= status 10)
|
;;; MIME handling
|
||||||
(<= status 19))
|
;;;
|
||||||
(display "input requested > ")
|
|
||||||
(get (string-append url "?" (read-line)))]
|
|
||||||
|
|
||||||
;; 20-29 SUCCESS
|
(define/private (mime/handle parse render body-port)
|
||||||
[(and (>= status 20)
|
(set! document-object (parse body-port))
|
||||||
(<= status 29))
|
|
||||||
|
|
||||||
(let ([mime (car (string-split meta ";"))])
|
(let-values ([(input output) (make-pipe #f)])
|
||||||
(cond
|
(parameterize ([current-output-port output])
|
||||||
[(string=? mime "text/gemini")
|
(gmi:render document-object))
|
||||||
(handle-gmi url c-in)]
|
|
||||||
[else
|
|
||||||
(printf "unsupported mime type: ~a~n" mime)]))]
|
|
||||||
|
|
||||||
;; 30-39 REDIRECT
|
(set! document-buffer input)))
|
||||||
[(and (>= status 30)
|
|
||||||
(<= status 39))
|
|
||||||
(if (> depth 5)
|
|
||||||
(displayln "WARNING: maximum redirection depth exceeded")
|
|
||||||
(iter (string->url meta) (sub1 depth)))]
|
|
||||||
|
|
||||||
;; 40-49 TEMPORARY FAILURE
|
(define/private (mime/handle-gmi body-port)
|
||||||
[(and (>= status 40)
|
(mime/handle gmi:parse gmi:render body-port))
|
||||||
(<= status 49))
|
|
||||||
(printf "status ~a: ~a\n" status
|
|
||||||
(dict-ref temporary-failures status "temporary failure"))]
|
|
||||||
|
|
||||||
;; 50-59 PERMANENT FAILURE
|
(define/private (mime/handle-txt body-port)
|
||||||
[(and (>= status 50)
|
(mime/handle txt:parse txt:render body-port))
|
||||||
(<= status 59))
|
|
||||||
(printf "status ~a: ~a\n" status
|
|
||||||
(dict-ref permanent-failures status "permanent failure"))]
|
|
||||||
|
|
||||||
;; 60-69 CERTIFICATE REQUIRED
|
;;;
|
||||||
[(and (>= status 60)
|
;;; document fetching
|
||||||
(<= status 69))
|
;;;
|
||||||
(displayln "certificate handling not yet implemented")]))))
|
|
||||||
|
|
||||||
(if (not (string=? (url-scheme url) "gemini"))
|
(define/private (absolutise-url partial)
|
||||||
(printf "unsupported url: ~a~n" (url->string url))
|
(combine-url/relative (car history)
|
||||||
(iter url 5)))))
|
(url->string partial)))
|
||||||
|
|
||||||
|
(define/public (get-document destination (redirect-depth 0))
|
||||||
|
;; handle urls which are relative to currently visited document
|
||||||
|
(when (not (null? history))
|
||||||
|
(set! destination (absolutise-url destination)))
|
||||||
|
|
||||||
|
(when (not (string=? (url-scheme destination) "gemini"))
|
||||||
|
(raise-user-error
|
||||||
|
'get-document
|
||||||
|
(string-append "unsupported scheme: ~a~n"
|
||||||
|
" url: ~a~n")
|
||||||
|
(url-scheme destination)
|
||||||
|
(url->string destination)))
|
||||||
|
|
||||||
|
(define-values (status meta body-port)
|
||||||
|
(net:request destination))
|
||||||
|
|
||||||
|
;; if net:request does not raise an exception, then status
|
||||||
|
;; meta and body-port are necessarily valid
|
||||||
|
(cond
|
||||||
|
;; clients MUST reject status codes outside of the 10-69
|
||||||
|
;; range
|
||||||
|
[(or (< status 10) (> status 69))
|
||||||
|
(raise-user-error
|
||||||
|
'get-document
|
||||||
|
(string-append "server returned out of range status: ~a~n"
|
||||||
|
" valid range: [10, 69]~a~n"
|
||||||
|
" url: ~a~n")
|
||||||
|
status (url->string destination))]
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; 10-19 INPUT REQUIRED
|
||||||
|
;;
|
||||||
|
[(and (>= status 10) (<= status 19))
|
||||||
|
(displayln "input requested")
|
||||||
|
(display "? ")
|
||||||
|
(flush-output)
|
||||||
|
|
||||||
|
(get-document
|
||||||
|
(struct-copy
|
||||||
|
url
|
||||||
|
destination
|
||||||
|
[query `((,(string->symbol
|
||||||
|
(read-line (current-input-port)
|
||||||
|
'any)) . #f))]))]
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; 20-29 SUCCESS
|
||||||
|
;;
|
||||||
|
[(and (>= status 20) (<= status 29))
|
||||||
|
;; SUCCESS only has a single defined status in this
|
||||||
|
;; group. All actions are necessarily default
|
||||||
|
|
||||||
|
;; This client does not use the lang MIME parameter for
|
||||||
|
;; anything.
|
||||||
|
(match (car (string-split meta ";"))
|
||||||
|
["text/gemini" (mime/handle-gmi body-port)]
|
||||||
|
["text/plaintext" (mime/handle-txt body-port)]
|
||||||
|
[else
|
||||||
|
(raise-user-error
|
||||||
|
'get-document
|
||||||
|
(string-append "unsupported mime ~a~n"
|
||||||
|
" url: ~a~n")
|
||||||
|
meta (url->string destination))])
|
||||||
|
|
||||||
|
;; If the document failed to be fetched (parser error,
|
||||||
|
;; unsupported mime, so on and so forth) this would not
|
||||||
|
;; ever be reached.
|
||||||
|
(set! history (cons destination history))
|
||||||
|
(next-cmd)]
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; 30-39 REDIRECT
|
||||||
|
;;
|
||||||
|
[(and (>= status 30) (<= status 39))
|
||||||
|
(if (> redirect-depth 5)
|
||||||
|
(raise-user-error
|
||||||
|
'get-document
|
||||||
|
(string-append "maximum redirect depth exceeded. "
|
||||||
|
"bailing"))
|
||||||
|
(get-document (string->url meta)
|
||||||
|
(add1 redirect-depth)))]
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; 40-49 TEMPORARY FAILURE
|
||||||
|
;;
|
||||||
|
[(and (>= status 40) (<= status 49))
|
||||||
|
(raise-user-error
|
||||||
|
'get-document
|
||||||
|
(string-append "server reports temporary failure: ~a~n"
|
||||||
|
" url: ~a~n"
|
||||||
|
" explanation: ~a~n")
|
||||||
|
status
|
||||||
|
(url->string destination)
|
||||||
|
(dict-ref temporary-failures
|
||||||
|
status
|
||||||
|
"temporary failure"))]
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; 50-59 PERMANENT FAILURE
|
||||||
|
;;
|
||||||
|
[(and (>= status 50) (<= status 59))
|
||||||
|
(raise-user-error
|
||||||
|
'get-document
|
||||||
|
(string-append "server reports permanent failure: ~a~n"
|
||||||
|
" url: ~a~n"
|
||||||
|
" explanation: ~a~n")
|
||||||
|
status
|
||||||
|
(url->string destination)
|
||||||
|
(dict-ref permanent-failures
|
||||||
|
status
|
||||||
|
"temporary failure"))]
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; 60-69 CERTIFICATE REQUIRED
|
||||||
|
;;
|
||||||
|
[(and (>= status 60) (<= status 69))
|
||||||
|
(raise-user-error
|
||||||
|
'get-document
|
||||||
|
(string-append "resource requires a client certificate, "
|
||||||
|
"which this client does not yet support~a~n"
|
||||||
|
" url: ~a~n"))]))))
|
||||||
|
69
gem300.rkt
Normal file → Executable file
69
gem300.rkt
Normal file → Executable file
@@ -1,27 +1,68 @@
|
|||||||
|
#! /usr/bin/env /usr/local/bin/racket
|
||||||
#lang racket
|
#lang racket
|
||||||
|
|
||||||
(require (prefix-in client: "client.rkt"))
|
(require (prefix-in net: "net.rkt")
|
||||||
|
(prefix-in client: "client.rkt"))
|
||||||
|
|
||||||
(define client (new client:client%))
|
(define client (new client:client%))
|
||||||
|
|
||||||
(define (repl)
|
(define (repl)
|
||||||
(display "G-300 > ")
|
(display "G-300 > ")
|
||||||
|
|
||||||
(let ([matches (regexp-match #px"(\\w+)\\s*(.*)" (read-line))])
|
(flush-output)
|
||||||
(cond
|
|
||||||
;; next command. also default
|
(with-handlers
|
||||||
[(or (not matches)
|
([(or/c exn:fail:user?
|
||||||
(string=? (cadr matches) "next")
|
;; todo: catch these errors separately and reformat them
|
||||||
(string=? (cadr matches) "n"))
|
;; in a user-y way
|
||||||
|
exn:fail:network?
|
||||||
|
net:exn:fail:response?)
|
||||||
|
(λ (exn)
|
||||||
|
(displayln (exn-message exn)))])
|
||||||
|
|
||||||
|
(match (regexp-match #px"(\\w+)\\s*(.*)"
|
||||||
|
(read-line (current-input-port) 'any))
|
||||||
|
[(or (list _ "go" url)
|
||||||
|
(list _ "g" url))
|
||||||
|
(send client go-cmd url)]
|
||||||
|
|
||||||
|
[(or (list _ "visit" id)
|
||||||
|
(list _ "v" id))
|
||||||
|
(send client visit-cmd (string->number id))]
|
||||||
|
|
||||||
|
[(or (list _ "next" _)
|
||||||
|
(list _ "n" _))
|
||||||
(send client next-cmd)]
|
(send client next-cmd)]
|
||||||
|
|
||||||
;; go command
|
[(or (list _ "url" id)
|
||||||
[(or (string=? (cadr matches) "go")
|
(list _ "u" id))
|
||||||
(string=? (cadr matches) "g"))
|
(send client url-cmd (string->number id))]
|
||||||
(send client go-cmd (caddr matches))]
|
|
||||||
|
|
||||||
;; visit link command
|
[(or (list _ "url")
|
||||||
[(andmap char-numeric? (string->list (cadr matches)))
|
(list _ "u"))
|
||||||
(send client visit-cmd (cadr matches))]))
|
(send client url-cmd)]
|
||||||
|
|
||||||
|
[(list _ "up" _)
|
||||||
|
(send client up-cmd)]
|
||||||
|
|
||||||
|
[(or (list _ "back" _)
|
||||||
|
(list _ "b" _))
|
||||||
|
(send client back-cmd)]
|
||||||
|
|
||||||
|
[(or (list _ "quit" _)
|
||||||
|
(list _ "q" _ ))
|
||||||
|
(exit)]
|
||||||
|
|
||||||
|
[else
|
||||||
|
(displayln "no such command")]))
|
||||||
|
|
||||||
(repl))
|
(repl))
|
||||||
|
|
||||||
|
(displayln
|
||||||
|
(string-append "welcome to gem300, a gemini client.\n"
|
||||||
|
"to learn more, type:\n"
|
||||||
|
"'go w6vvn.flounder.online/gem300/tutorial.gmi'"))
|
||||||
|
|
||||||
|
(flush-output)
|
||||||
|
|
||||||
|
(repl)
|
||||||
|
24
gmi.rkt
24
gmi.rkt
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
(provide (contract-out
|
(provide (contract-out
|
||||||
[render (-> document? void?)]
|
[render (-> document? void?)]
|
||||||
[parse (-> (listof string?) document?)]
|
[parse (-> input-port? document?)]
|
||||||
[match-link (-> document? integer? (or/c url? #f))]
|
[match-link (-> document? integer? (or/c url? #f))]
|
||||||
[struct document ((items (listof (or/c text? link? pre?))))]))
|
[struct document ((items (listof (or/c text? link? pre?))))]))
|
||||||
|
|
||||||
@@ -30,7 +30,7 @@
|
|||||||
(car split))
|
(car split))
|
||||||
link-#))))
|
link-#))))
|
||||||
|
|
||||||
(define (parse lines)
|
(define (parse body-port)
|
||||||
(define (iter document lines state link-id)
|
(define (iter document lines state link-id)
|
||||||
;; when there are no more lines, we have finished parsing.
|
;; when there are no more lines, we have finished parsing.
|
||||||
(if (empty? lines)
|
(if (empty? lines)
|
||||||
@@ -95,7 +95,7 @@
|
|||||||
'normal
|
'normal
|
||||||
link-id)])))
|
link-id)])))
|
||||||
|
|
||||||
(document (iter (list) lines 'normal 1)))
|
(document (iter (list) (port->lines body-port) 'normal 1)))
|
||||||
|
|
||||||
;;;
|
;;;
|
||||||
;;; RENDERING
|
;;; RENDERING
|
||||||
@@ -110,7 +110,7 @@
|
|||||||
(define (inner-iter acc rst)
|
(define (inner-iter acc rst)
|
||||||
(let ([line (string-join acc)])
|
(let ([line (string-join acc)])
|
||||||
(if (or (empty? rst)
|
(if (or (empty? rst)
|
||||||
(> (string-length line) 80))
|
(> (string-length line) 40))
|
||||||
(values acc rst)
|
(values acc rst)
|
||||||
(inner-iter (append acc (list (car rst)))
|
(inner-iter (append acc (list (car rst)))
|
||||||
(cdr rst)))))
|
(cdr rst)))))
|
||||||
@@ -156,16 +156,6 @@
|
|||||||
(document-items document)))
|
(document-items document)))
|
||||||
|
|
||||||
(define (match-link document id)
|
(define (match-link document id)
|
||||||
(define (iter next-structure)
|
(let ([link (findf (λ (link) (= (link-id link) id))
|
||||||
(cond
|
(filter link? (document-items document)))])
|
||||||
[(and (link? (car next-structure))
|
(if link (link-url link) #f)))
|
||||||
(= (link-id (car next-structure)) id))
|
|
||||||
(link-url (car next-structure))]
|
|
||||||
|
|
||||||
[(empty? (cdr next-structure))
|
|
||||||
#f]
|
|
||||||
|
|
||||||
[else
|
|
||||||
(iter (cdr next-structure))]))
|
|
||||||
|
|
||||||
(iter (document-items document)))
|
|
||||||
|
Reference in New Issue
Block a user