1 (in-package #:phos/gemini)
3 (defparameter *default-port* 1965
4 "The default port for gemini URL.")
6 (defparameter *code-to-keyword* '((10 :input)
10 (31 :permanent-redirect)
11 (40 :temporary-failure)
12 (41 :server-unavailable)
16 (50 :permanent-failure)
19 (53 :proxy-request-refused)
21 (60 :client-certificate-required)
22 (61 :certificate-not-authorised)
23 (62 :certificate-not-valid))
24 "Maps status code to keyword name.")
26 (define-condition malformed-response (error)
27 ((reason :initarg :reason :reader reason)))
29 (defun parse-status (s)
30 (let* ((n (parse-integer s))
31 (x (cadr (assoc n *code-to-keyword*))))
33 (return-from parse-status x))
34 (let* ((l (* (floor (/ n 10))
36 (x (cadr (assoc l *code-to-keyword*))))
39 (error 'malformed-response :reason (format nil "unknown response number ~a" s))))))
41 (defun parse-response (res)
42 (unless (uiop:string-suffix-p res (format nil "~c~c" #\return #\newline))
43 (error 'malformed-response :reason "response doesn't and with CRLF"))
44 (unless (< (length res) 1024)
45 (error 'malformed-response :reason "response is longer than 1024 bytes"))
46 (setf res (string-trim '(#\return #\newline) res))
47 (destructuring-bind (status &optional meta) (cl-ppcre:split "\\s+" res :limit 2)
48 (when (and (< (parse-integer status) 40) (not meta))
49 (error 'malformed-response :reason "missing meta"))
50 (values (parse-status status) meta)))
52 (defun read-all-string (in)
53 (with-output-to-string (out)
54 (loop with buffer = (make-array 1024 :element-type 'character)
55 for n-chars = (read-sequence buffer in)
57 do (write-sequence buffer out :start 0
60 (defun read-all-bytes (in)
61 (let ((data (make-array 0 :element-type '(unsigned-byte 8) :adjustable t :fill-pointer 0)))
62 (loop with buffer = (make-array 1024 :element-type '(unsigned-byte 8))
63 for n-bytes = (read-sequence buffer in)
64 for data-size = (array-dimension data 0)
66 do (adjust-array data (+ data-size n-bytes))
67 do (incf (fill-pointer data) n-bytes)
68 do (replace data buffer :start1 data-size :end2 n-bytes))
71 (defun read-until (in char)
72 (with-output-to-string (out)
73 (loop for ch = (read-char in)
76 do (write-char ch out))
77 (write-char char out)))
79 (defmacro with-gemini-request (((status meta stream) url) &body body)
80 "Expose a stream (STREAM) with Gemini response contents, available in BODY.
82 STATUS and META are bound to the status code (as keyword from
83 `*code-to-keyword*') and meta info (as optional/nullable string.)
85 URL should be a well-formed string/`quri:uri' URL."
86 (let* ((socket-var (gensym "SOCKET"))
87 (socket-stream-var (gensym "SOCKET-STREAM"))
88 (host-var (gensym "HOST"))
89 (port-var (gensym "PORT"))
90 (url-var (gensym "URL")))
91 `(let* ((,url-var (quri:uri ,url))
92 (,host-var (quri:uri-host ,url-var))
93 (,port-var (or (quri:uri-port ,url-var) phos/gemini:*default-port*)))
94 (usocket:with-client-socket (,socket-var ,socket-stream-var ,host-var ,port-var)
95 (let ((,stream (cl+ssl:make-ssl-client-stream
96 ,socket-stream-var :unwrap-stream-p t
97 :external-format '(:utf8 :eol-style :lf)
99 :hostname ,host-var)))
100 (format ,stream "~a~c~c" (quri:render-uri ,url-var) #\return #\newline)
101 (force-output ,stream)
102 (multiple-value-bind (,status ,meta)
103 (parse-response (read-until ,stream #\newline))
106 (defgeneric request (url)
108 (with-gemini-request ((status meta stream) url)
109 (values status meta (if (and (eq status :success)
110 meta (string= (subseq meta 0 5) "text/"))
111 (read-all-string stream)
112 (read-all-bytes stream)))))
113 (:documentation "Perform a request for the URL."))