diff options
| author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2026-08-31 22:27:42 +0200 |
|---|---|---|
| committer | Thijs Schreijer <thijs@thijsschreijer.nl> | 2026-09-01 07:59:54 +0200 |
| commit | cd6b41a13af403d32e08ff416e60982b5cb718dc (patch) | |
| tree | 3865d91753fd636a866d4711f50ab27613e58634 /test/httpfixture.lua | |
| parent | c5f169ec303a70707bc48fbece9b52cc90a48de0 (diff) | |
| download | luasocket-cd6b41a13af403d32e08ff416e60982b5cb718dc.tar.gz luasocket-cd6b41a13af403d32e08ff416e60982b5cb718dc.tar.bz2 luasocket-cd6b41a13af403d32e08ff416e60982b5cb718dc.zip | |
Add SSE sink (socket.sse module)feat/sse
Implements SSE by using the replacement body sink. Replaces
the sink with an SSE parser if the response is a text stream.
Example application included
Diffstat (limited to 'test/httpfixture.lua')
| -rw-r--r-- | test/httpfixture.lua | 47 |
1 files changed, 35 insertions, 12 deletions
diff --git a/test/httpfixture.lua b/test/httpfixture.lua index 3ea0737..14c7548 100644 --- a/test/httpfixture.lua +++ b/test/httpfixture.lua | |||
| @@ -49,18 +49,12 @@ local function quote(s) | |||
| 49 | return "\"" .. s .. "\"" | 49 | return "\"" .. s .. "\"" |
| 50 | end | 50 | end |
| 51 | 51 | ||
| 52 | -- Scripts the server to accept one connection (into the shared `data` | 52 | -- Appends the script parts for one accept-send-close cycle (see |
| 53 | -- global, mirroring test/testclnt.lua's reconnect()), write a sequence of | 53 | -- accept_and_send_sequence) onto `parts`. |
| 54 | -- raw byte chunks to it, then close it. `chunks` is an array of either | 54 | local function append_connection(parts, chunks) |
| 55 | -- plain strings, or {body, delay = seconds} tables -- the delay (via | 55 | parts[#parts + 1] = "if data then data:close() data = nil end" |
| 56 | -- socket.sleep) is applied before sending that chunk, so callers can prove | 56 | parts[#parts + 1] = "data = server:accept()" |
| 57 | -- incremental/partial delivery instead of one atomic send. | 57 | parts[#parts + 1] = "data:setoption(\"tcp-nodelay\", true)" |
| 58 | function M.accept_and_send(remote, chunks) | ||
| 59 | local parts = { | ||
| 60 | "if data then data:close() data = nil end", | ||
| 61 | "data = server:accept()", | ||
| 62 | "data:setoption(\"tcp-nodelay\", true)", | ||
| 63 | } | ||
| 64 | for _, chunk in ipairs(chunks) do | 58 | for _, chunk in ipairs(chunks) do |
| 65 | local body, delay | 59 | local body, delay |
| 66 | if type(chunk) == "table" then | 60 | if type(chunk) == "table" then |
| @@ -74,6 +68,35 @@ function M.accept_and_send(remote, chunks) | |||
| 74 | parts[#parts + 1] = "data:send(" .. quote(body) .. ")" | 68 | parts[#parts + 1] = "data:send(" .. quote(body) .. ")" |
| 75 | end | 69 | end |
| 76 | parts[#parts + 1] = "data:close() data = nil" | 70 | parts[#parts + 1] = "data:close() data = nil" |
| 71 | end | ||
| 72 | |||
| 73 | -- Scripts the server to accept one connection (into the shared `data` | ||
| 74 | -- global, mirroring test/testclnt.lua's reconnect()), write a sequence of | ||
| 75 | -- raw byte chunks to it, then close it. `chunks` is an array of either | ||
| 76 | -- plain strings, or {body, delay = seconds} tables -- the delay (via | ||
| 77 | -- socket.sleep) is applied before sending that chunk, so callers can prove | ||
| 78 | -- incremental/partial delivery instead of one atomic send. | ||
| 79 | function M.accept_and_send(remote, chunks) | ||
| 80 | local parts = {} | ||
| 81 | append_connection(parts, chunks) | ||
| 82 | remote(table.concat(parts, "\n")) | ||
| 83 | end | ||
| 84 | |||
| 85 | -- Like accept_and_send, but scripts several accept-send-close cycles as one | ||
| 86 | -- server-side script sent over a single remote() round trip. Needed for a | ||
| 87 | -- client call that opens more than one connection in sequence within a | ||
| 88 | -- single call of its own (e.g. socket.http.request following a redirect): | ||
| 89 | -- queuing a second accept_and_send for that connection ahead of time would | ||
| 90 | -- deadlock, since its remote() call can't get an ack until the server | ||
| 91 | -- finishes the first accept_and_send's blocking accept() -- which itself | ||
| 92 | -- can't complete until the client makes the very call that's stuck waiting | ||
| 93 | -- on that ack. `connections` is an array of `chunks` arrays, one per | ||
| 94 | -- connection, handled in order. | ||
| 95 | function M.accept_and_send_sequence(remote, connections) | ||
| 96 | local parts = {} | ||
| 97 | for _, chunks in ipairs(connections) do | ||
| 98 | append_connection(parts, chunks) | ||
| 99 | end | ||
| 77 | remote(table.concat(parts, "\n")) | 100 | remote(table.concat(parts, "\n")) |
| 78 | end | 101 | end |
| 79 | 102 | ||
