- A device file whose document is not UTF-8 is an error: "the document in it is not UTF-8". - A page title that does not decode in its charset is read as Windows-1252, by matching the result rather than catching. - Hex digits, charset names and the tests' response heads are read with bytes->string/lossy. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
272 lines
12 KiB
Text
272 lines
12 KiB
Text
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
;; bjomarks/tests/title.bjo — (bjomarks title): the title in the bytes of a
|
|
;; page, and the title fetched from a server on the loopback.
|
|
;;
|
|
;; bjo run tests/title.bjo
|
|
;;
|
|
;; The server is written here, on (std net), so that a test can make it
|
|
;; slow, make it stop after the title, or send bytes of another charset.
|
|
|
|
(import (std prelude))
|
|
(import (std test))
|
|
(import (std datetime))
|
|
(import (std net))
|
|
(import (std ports))
|
|
(import (only (text encodings) iso-8859-1))
|
|
(import (bjomarks title))
|
|
|
|
(: (-> string (Array byte)))
|
|
(defun (bytes-of text) (string->bytes text))
|
|
|
|
(: (-> (Array byte) (Option string) (Option string)))
|
|
(defun (title-in bytes content-type) (page-title bytes (array-length bytes) content-type))
|
|
|
|
;; ---- The title in the bytes ----------------------------------------------------------
|
|
|
|
(deftest "the title of a page"
|
|
#:cases ((html wanted)
|
|
("<html><head><title>Example Domain</title></head></html>" (Some "Example Domain"))
|
|
("<HTML><HEAD><TITLE>Upper Case</TITLE></HEAD>" (Some "Upper Case"))
|
|
("<title lang=\"en\">With attributes</title>" (Some "With attributes"))
|
|
("<head><titlebar>no</titlebar><title>After a longer tag</title>" (Some "After a longer tag"))
|
|
("<title>\n Rust\n docs\n </title>" (Some "Rust docs"))
|
|
("<title>Tom & Jerry — "Home" 's x</title>"
|
|
(Some "Tom & Jerry — \"Home\" 's x"))
|
|
("<title>First</title><title>Second</title>" (Some "First"))
|
|
("<html><head></head><body>none</body>" None)
|
|
("<title></title>" None)
|
|
("<title> </title>" None)
|
|
("<title>Cut off" None)
|
|
("<title" None))
|
|
(check (= (title-in (bytes-of html) None) wanted)))
|
|
|
|
(deftest "only the first n bytes are read"
|
|
;; 22 bytes; the end tag is whole at 21 without its `>`.
|
|
(def bytes (bytes-of "<title>Example</title>"))
|
|
(check (= (page-title bytes 21 None) (Some "Example")))
|
|
(check (= (page-title bytes 20 None) None))
|
|
(check (= (page-title bytes 0 None) None)))
|
|
|
|
(deftest "the charset is the header's, else the page's, else UTF-8"
|
|
(def latin (string->bytes "<title>Café crème</title>" #:encoding iso-8859-1))
|
|
(check (= (title-in latin (Some "text/html; charset=ISO-8859-1")) (Some "Café crème")))
|
|
(check (= (title-in latin (Some "text/html; charset=\"windows-1252\"")) (Some "Café crème")))
|
|
(def meta (string->bytes "<meta charset=\"iso-8859-1\"><title>Café</title>" #:encoding iso-8859-1))
|
|
(check (= (title-in meta (Some "text/html")) (Some "Café")))
|
|
(def equiv (string->bytes (str "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1252\">"
|
|
"<title>Café</title>")
|
|
#:encoding iso-8859-1))
|
|
(check (= (title-in equiv None) (Some "Café")))
|
|
;; The header wins over the page.
|
|
(check (= (title-in (bytes-of "<meta charset=\"iso-8859-1\"><title>Café</title>") (Some "text/html; charset=utf-8"))
|
|
(Some "Café")))
|
|
;; No charset: UTF-8.
|
|
(check (= (title-in (bytes-of "<title>Café</title>") None) (Some "Café")))
|
|
;; A charset that nothing knows: UTF-8.
|
|
(check (= (title-in (bytes-of "<title>Café</title>") (Some "text/html; charset=x-nothing")) (Some "Café"))))
|
|
|
|
(deftest "bytes that are not of the charset are read as Windows-1252"
|
|
(def latin (string->bytes "<title>Café crème</title>" #:encoding iso-8859-1))
|
|
(check (= (title-in latin None) (Some "Café crème")))
|
|
(check (= (title-in latin (Some "text/html; charset=utf-8")) (Some "Café crème"))))
|
|
|
|
;; ---- A server on the loopback ----------------------------------------------------------
|
|
|
|
;; What the server answers to a path.
|
|
;; - `status` is as the status line gives it: "200 OK".
|
|
;; - `headers` are whole lines, without the line break.
|
|
;; - `pieces` are written one at a time, with `pause` ms between them.
|
|
;; - `wait` ms pass before the answer starts.
|
|
;; - With `hold`, the body has no length and the connection stays open,
|
|
;; after the pieces, until the server stops.
|
|
(type (: Reply (Record (: status string)
|
|
(: headers (Vec string))
|
|
(: pieces (Vec (Array byte)))
|
|
(: pause long)
|
|
(: wait long)
|
|
(: hold bool))))
|
|
|
|
(: (-> string (Vec string) (Vec (Array byte)) (#:pause long) (#:wait long) (#:hold bool) Reply))
|
|
(defun (reply status headers pieces #:pause 0L #:wait 0L #:hold #f)
|
|
(Reply (status status) (headers headers) (pieces pieces) (pause pause) (wait wait) (hold hold)))
|
|
|
|
(: (-> (Vec (Array byte)) Reply))
|
|
(defun (html pieces) (reply "200 OK" ["Content-Type: text/html; charset=utf-8"] pieces))
|
|
|
|
(: (-> string Reply))
|
|
(defun (reply-for path)
|
|
(match path
|
|
("/plain"
|
|
(html [(bytes-of (str "<!doctype html><html><head><meta charset=\"utf-8\">\n"
|
|
"<title>Tom & Jerry — Home</title></head><body>hi</body></html>"))]))
|
|
("/moved" (reply "301 Moved Permanently" ["Location: /plain"] []))
|
|
("/latin1"
|
|
(reply "200 OK" ["Content-Type: text/html; charset=ISO-8859-1"]
|
|
[(string->bytes "<html><head><title>Café crème</title></head></html>" #:encoding iso-8859-1)]))
|
|
("/split"
|
|
(reply "200 OK" ["Content-Type: text/html"]
|
|
[(bytes-of "<html><head><ti") (bytes-of "tle>Split ac") (bytes-of "ross</ti") (bytes-of "tle></head>")]
|
|
#:pause 30L))
|
|
("/no-type" (reply "200 OK" [] [(bytes-of "<title>No type</title>")]))
|
|
("/stalls" (reply "200 OK" ["Content-Type: text/html"] [(bytes-of "<html><head><title>Early</title>")] #:hold #t))
|
|
("/missing" (reply "404 Not Found" ["Content-Type: text/html"] [(bytes-of "<title>Not Found</title>")]))
|
|
("/image" (reply "200 OK" ["Content-Type: image/png"] [#[137 80 78 71]]))
|
|
("/untitled" (html [(bytes-of "<html><head></head><body>no title</body></html>")]))
|
|
;; The title starts after 600 KiB: after the most that is read.
|
|
("/huge" (html [(bytes-of "<html><head>") (: (make-array 614400) (Array byte)) (bytes-of "<title>Too late</title>")]))
|
|
("/slow" (reply "200 OK" ["Content-Type: text/html"] [(bytes-of "<title>Slow</title>")] #:wait 5000L))
|
|
(_ (reply "404 Not Found" [] []))))
|
|
|
|
;; The path of the request line: "GET /plain HTTP/1.1" gives "/plain".
|
|
(: (-> string string))
|
|
(defun (path-of head)
|
|
(def line (match (string-split-once head "\r\n") ((Some (Tuple l _)) l) (None head)))
|
|
(def parts (string-split line " "))
|
|
(if (>= (vec-length parts) 2) (vec-ref parts 1) "/"))
|
|
|
|
;; Reads the request's head, to its empty line, and returns its path.
|
|
(: (-bjo-> InputPort string))
|
|
(defbjo (request-path in)
|
|
(let go ((seen ""))
|
|
(if (string-contains? seen "\r\n\r\n")
|
|
(path-of seen)
|
|
(match (read-some in)
|
|
((Some bs) (go (str seen (bytes->string/lossy bs))))
|
|
(None (path-of seen))))))
|
|
|
|
;; Waits `ms`, or less when the server stops.
|
|
(: (-bjo-> Listener long void))
|
|
(defbjo (pause-for l ms)
|
|
(ignore (sync (choose (wrap (timeout (milliseconds ms)) (fun (_) 0))
|
|
(wrap (listener-closed l) (fun (_) 0))))))
|
|
|
|
;; Writes the reply. Returns the length of its body.
|
|
(: (-bjo-> Listener OutputPort Reply int))
|
|
(defbjo (send l out r)
|
|
(def length (vec-fold (fun (p n) (+ n (array-length p))) 0 (.pieces r)))
|
|
(def head (str "HTTP/1.1 " (.status r) "\r\n"
|
|
(string-join (vec-map #(str & "\r\n") (.headers r)) "")
|
|
(if (.hold r) "" (str "Content-Length: " (int->string length) "\r\n"))
|
|
"Connection: close\r\n\r\n"))
|
|
(write-bytes! out (bytes-of head))
|
|
(flush-port out)
|
|
(let go ((i 0))
|
|
(when (< i (vec-length (.pieces r)))
|
|
(when (and (> i 0) (> (.pause r) 0L)) (pause-for l (.pause r)))
|
|
(write-bytes! out (vec-ref (.pieces r) i))
|
|
(flush-port out)
|
|
(go (+ i 1))))
|
|
(when (.hold r) (ignore (sync (listener-closed l))))
|
|
length)
|
|
|
|
(: (-bjo-> InputPort OutputPort void))
|
|
(defbjo (closing in out)
|
|
(close-input-port in)
|
|
(close-output-port out))
|
|
|
|
;; One connection: one request, one reply. A client that has gone, as the
|
|
;; client of /huge has, makes a write raise; that is not a failure here.
|
|
(: (-bjo-> Listener InputPort OutputPort int))
|
|
(defbjo (answer l in out)
|
|
(def r (reply-for (request-path in)))
|
|
(when (> (.wait r) 0L) (pause-for l (.wait r)))
|
|
(def sent (match (try (send l out r) #:catch (System.Exception))
|
|
((Ok n) n)
|
|
((Err _) -1)))
|
|
(ignore (try (closing in out) #:catch (System.Exception)))
|
|
sent)
|
|
|
|
;; Accepts until the listener closes. Each connection has a fiber of its
|
|
;; own. Returns how many there were.
|
|
(: (-bjo-> Listener int))
|
|
(defbjo (serve l)
|
|
(let go ((n 0))
|
|
(match (sync (choose (wrap (accept-evt l) Some)
|
|
(wrap (listener-closed l) (fun (_) None))))
|
|
((Some (Ok (Tuple in out)))
|
|
(ignore (bjo (answer l in out)))
|
|
(go (+ n 1)))
|
|
((Some (Err _)) n)
|
|
(None n))))
|
|
|
|
;; A port that nothing listens on: one the system gave and took back.
|
|
(: (-bjo-> int))
|
|
(defbjo (free-port)
|
|
(match (tcp-listen "127.0.0.1" 0)
|
|
((Err e) (raise e))
|
|
((Ok l)
|
|
(def port (listener-port l))
|
|
(close-listener! l)
|
|
(close-owned-or-dispose l)
|
|
port)))
|
|
|
|
;; Starts a server, fetches the title of `path` from it with a deadline of
|
|
;; `within` seconds, and stops the server.
|
|
(: (-bjo-> string int (Result string string)))
|
|
(defbjo (fetch-served path within)
|
|
(match (tcp-listen "127.0.0.1" 0)
|
|
((Err e) (Err (str "listen failed: " (.-Message e))))
|
|
((Ok l)
|
|
(with-open ((listening l))
|
|
(def server (bjo (serve l)))
|
|
(def got (fetch-title (str "http://127.0.0.1:" (int->string (listener-port l)) path) #:within within))
|
|
(close-listener! l)
|
|
(ignore (sync (promise-join server)))
|
|
got))))
|
|
|
|
(: (-> (Result Exception (Result string string)) (Result string string)))
|
|
(defun (joined r)
|
|
(match r
|
|
((Ok got) got)
|
|
((Err e) (Err (str "raised: " (.-Message e))))))
|
|
|
|
;; `fetch-title` of `path` on a new server, with 3 seconds.
|
|
(: (-> string (Result string string)))
|
|
(defun (fetched path) (joined (sync/blocking (promise-join (bjo (fetch-served path 3))))))
|
|
|
|
;; `fetch-title` of `url`, with no server.
|
|
(: (-> string (Result string string)))
|
|
(defun (fetched-from url) (joined (sync/blocking (promise-join (bjo (fetch-title url #:within 3))))))
|
|
|
|
;; ---- Fetching -----------------------------------------------------------------------
|
|
|
|
(deftest "a page's title, fetched"
|
|
(check (= (fetched "/plain") (Ok "Tom & Jerry — Home")))
|
|
(check (= (fetched "/latin1") (Ok "Café crème")))
|
|
(check (= (fetched "/split") (Ok "Split across")))
|
|
(check (= (fetched "/no-type") (Ok "No type"))))
|
|
|
|
(deftest "a redirect is followed"
|
|
(check (= (fetched "/moved") (Ok "Tom & Jerry — Home"))))
|
|
|
|
(deftest "reading stops at the end of the title"
|
|
;; The server keeps the connection open after the title, past the deadline.
|
|
(check (= (fetched "/stalls") (Ok "Early"))))
|
|
|
|
(deftest "why a page gives no title"
|
|
(check (= (fetched "/missing") (Err "the server answered 404")))
|
|
(check (= (fetched "/image") (Err "it is not a web page, but image/png")))
|
|
(check (= (fetched "/untitled") (Err "the page has no title")))
|
|
(check (= (fetched "/huge") (Err "the page has no title"))))
|
|
|
|
(deftest "a slow server meets the deadline"
|
|
(def started (now))
|
|
(check (= (joined (sync/blocking (promise-join (bjo (fetch-served "/slow" 1)))))
|
|
(Err "no answer in 1 second")))
|
|
;; The server waits 5 seconds before it answers.
|
|
(check (< (instant->unix-milliseconds (now)) (+ (instant->unix-milliseconds started) 4000L))))
|
|
|
|
(deftest "a server that is not there is an Err"
|
|
(def port (match (sync/blocking (promise-join (bjo (free-port)))) ((Ok p) p) ((Err e) (raise e))))
|
|
(check-match (fetched-from (str "http://127.0.0.1:" (int->string port) "/")) (Err _)))
|
|
|
|
(deftest "only http and https are fetched"
|
|
(check (= (fetched-from "ftp://example.org/") (Err "only http and https addresses are fetched: ftp://example.org/")))
|
|
(check (= (fetched-from "mailto:a@example.org")
|
|
(Err "only http and https addresses are fetched: mailto:a@example.org")))
|
|
(check (= (fetched-from "example.org") (Err "only http and https addresses are fetched: example.org"))))
|
|
|
|
(run-tests)
|