|
11 | 11 | (:documentation "Hunchentoot-specific SSE generator implementation.")) |
12 | 12 |
|
13 | 13 |
|
14 | | -(defmethod initialize-instance :after ((generator hunchentoot-sse-generator) &key request &allow-other-keys) |
15 | | - "Set up SSE headers and response stream for Hunchentoot." |
| 14 | +;; (defmethod initialize-instance :after ((generator hunchentoot-sse-generator) &key request &allow-other-keys) |
| 15 | +;; "Set up SSE headers and response stream for Hunchentoot." |
| 16 | +;; ;; Set headers using Hunchentoot API |
| 17 | +;; (setf (hunchentoot:content-type*) "text/event-stream; charset=utf-8" |
| 18 | +;; (hunchentoot:header-out "Cache-Control") "no-cache") |
| 19 | +;; ;; HTTP/1.1 specific headers |
| 20 | +;; (when (string= (hunchentoot:server-protocol request) "HTTP/1.1") |
| 21 | +;; (setf (hunchentoot:header-out "Connection") "keep-alive")) |
| 22 | +;; ;; Initialize response stream |
| 23 | +;; (let ((raw-stream (hunchentoot:send-headers))) |
| 24 | +;; (setf (response generator) |
| 25 | +;; (flex:make-flexi-stream raw-stream :external-format :utf-8)))) |
| 26 | + |
| 27 | + |
| 28 | +(defmethod initialize-instance :after ((generator hunchentoot-sse-generator) &key request &allow-other-keys) |
| 29 | + "Set up SSE headers and response stream for Hunchentoot. Use compression if available" |
16 | 30 | ;; Set headers using Hunchentoot API |
17 | | - (setf (hunchentoot:content-type*) "text/event-stream; charset=utf-8" |
18 | | - (hunchentoot:header-out "Cache-Control") "no-cache") |
19 | | - ;; HTTP/1.1 specific headers |
20 | | - (when (string= (hunchentoot:server-protocol request) "HTTP/1.1") |
21 | | - (setf (hunchentoot:header-out "Connection") "keep-alive")) |
22 | | - ;; Initialize response stream |
23 | | - (let ((raw-stream (hunchentoot:send-headers))) |
24 | | - (setf (response generator) |
25 | | - (flex:make-flexi-stream raw-stream :external-format :utf-8)))) |
| 31 | + (let ((accepts-zstd (search "zstd" (hunchentoot:header-in* :accept-encoding request) |
| 32 | + :test #'string-equal))) |
| 33 | + (setf (hunchentoot:content-type*) "text/event-stream; charset=utf-8" |
| 34 | + (hunchentoot:header-out "Cache-Control") "no-cache") |
| 35 | + ;; HTTP/1.1 specific headers |
| 36 | + |
| 37 | + (when (string= (hunchentoot:server-protocol request) "HTTP/1.1") |
| 38 | + (setf (hunchentoot:header-out "Connection") "keep-alive")) |
| 39 | + (when accepts-zstd |
| 40 | + (setf (hunchentoot:header-out "Content-Encoding") "zstd" |
| 41 | + (hunchentoot:header-out "Vary") "Accept-Encoding")) |
| 42 | + (let* ((raw-stream (hunchentoot:send-headers)) |
| 43 | + (compressed-stream (when accepts-zstd |
| 44 | + (zstd:make-compressing-stream raw-stream :level 3))) |
| 45 | + (utf8-stream (if compressed-stream |
| 46 | + (flex:make-flexi-stream compressed-stream :external-format :utf-8) |
| 47 | + (flex:make-flexi-stream raw-stream :external-format :utf-8) |
| 48 | + ))) |
| 49 | + |
| 50 | + (setf (response generator) utf8-stream) |
| 51 | + (setf (compressed-stream generator) compressed-stream)))) |
| 52 | + |
| 53 | + |
26 | 54 |
|
27 | 55 | (defmethod read-signals ((request hunchentoot:request) |
28 | 56 | &key (catch-errors *catch-errors-p*)) |
|
0 commit comments