My goal is something akin to Chapter 12 of Land Of Lisp: using the usocket package in SBCL I want to write a local server that I can connect to with my browser. I started from a helpful example:
#!/usr/bin/sbcl --script
(load "~/quicklisp/setup.lisp")
(require :usocket)
(defparameter *sock* nil)
(defparameter *sock-listen* nil)
(defparameter *my-stream* nil)
(defun communi ()
;; bind socket
(setf *sock* (usocket:socket-listen "127.0.0.1" 4123))
;; listen to incoming connections
(setf *sock-listen* (usocket:socket-accept *sock* :element-type 'character))
; open stream for communication
(setf *my-stream* (usocket:socket-stream *sock-listen*))
;; print message from client
(format t "~a~%" (read *my-stream*))
(force-output)
;; send answer to client
(format *my-stream* "<html><body>Server will write something exciting here.</body></html>")
(force-output *my-stream*))
;; call communication and close socket, no matter what
(unwind-protect (communi)
(format t "Closing socket connection...~%")
(usocket:socket-close *sock-listen*)
(usocket:socket-close *sock*))
When I run this script from the command line (Ubuntu 22.04 LTS), I am able to connect to http://127.0.0.1:4123/
with Firefox. However, instead of rendering the HTML, Firefox only displays its source code:
<html><body>Server will write something exciting here.</body></html>
Question: How can I persuade Firefox to render the page instead of displaying the HTML source?
From the answers here and here I assume that I need to set text/html
in the HTTP headers. How can I do it in the usocket package?