From 0df59f4b0871beefe08d7d0366bdfd6ce2dcb5eb Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Sun, 24 Oct 2021 22:01:51 -0400 Subject: [PATCH] http chunked spec violations in event source server amusingly firefox never cared but chrome complains and meh i am legit in the wrong so here we go. --- cgi.d | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/cgi.d b/cgi.d index d38063e..9ac46b9 100644 --- a/cgi.d +++ b/cgi.d @@ -5031,6 +5031,7 @@ void sendAll(Socket s, const(void)[] data, string file = __FILE__, size_t line = throw new ConnectionException(s, lastSocketError, file, line); } assert(amount > 0); + data = data[amount .. $]; } while(data.length); } @@ -7403,9 +7404,9 @@ final class EventSourceServerImplementation : EventSourceServer, EventIoServer { foreach(url, connections; eventConnectionsByUrl) foreach(connection; connections) if(connection.needsChunking) - nonBlockingWrite(this, connection.fd, "2\r\n:\n"); + nonBlockingWrite(this, connection.fd, "2\r\n:\n\r\n"); else - nonBlockingWrite(this, connection.fd, ":\n"); + nonBlockingWrite(this, connection.fd, ":\n\r\n"); } void fileClosed(int fd) { @@ -7571,18 +7572,21 @@ final class EventSourceServerImplementation : EventSourceServer, EventIoServer { auto len = toHex(formattedMessage.length); buffer[4 .. 6] = "\r\n"[]; buffer[4 - len.length .. 4] = len[]; + buffer[6 + formattedMessage.length] = '\r'; + buffer[6 + formattedMessage.length + 1] = '\n'; - auto chunkedMessage = buffer[4 - len.length .. 6 + formattedMessage.length]; + auto chunkedMessage = buffer[4 - len.length .. 6 + formattedMessage.length +2]; // done // FIXME: send back requests when needed // FIXME: send a single ":\n" every 15 seconds to keep alive foreach(connection; connections) { - if(connection.needsChunking) + if(connection.needsChunking) { nonBlockingWrite(this, connection.fd, chunkedMessage); - else + } else { nonBlockingWrite(this, connection.fd, formattedMessage); + } } } }