/
echo.lisp
27 lines (23 loc) · 992 Bytes
/
echo.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
(cl:defpackage #:conserv.examples.http.echo
(:use #:cl)
(:import-from #:conserv
#:with-event-loop)
(:import-from #:conserv.http
#:*request*
#:http-listen
#:set-headers
#:on-http-request
#:on-request-data))
(cl:in-package #:conserv.examples.http.echo)
(defclass echo () ())
;; Users have control over when the request is completed, and are able to keep a request connection
;; alive and stream data in and out of it. In this case, we set up an echo server over HTTP, with
;; the incoming request body being echoed back in the response.
(defmethod on-http-request ((driver echo))
(set-headers *request* :content-type "text/plain")
(format *request* "Welcome to the HTTP echo server."))
(defmethod on-request-data ((driver echo) data)
(write-sequence data *request*))
(defun start ()
(with-event-loop ()
(http-listen (make-instance 'echo) :port 8888 :external-format-in nil)))