Changeset

3635:fd054689a64c

mod_http_stats_stream: Use existing header preparation This allows the CORS support in mod_http to work.
author Kim Alvefur <zash@zash.se>
date Wed, 31 Jul 2019 18:55:06 +0200
parents 3634:915e32d5a147
children 3636:afedc2430b0d
files mod_http_stats_stream/mod_http_stats_stream.lua
diffstat 1 files changed, 9 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/mod_http_stats_stream/mod_http_stats_stream.lua	Tue Jul 30 02:07:13 2019 +0200
+++ b/mod_http_stats_stream/mod_http_stats_stream.lua	Wed Jul 31 18:55:06 2019 +0200
@@ -1,4 +1,5 @@
 local statsman = require "core.statsmanager";
+local http = require "net.http.server";
 local json = require "util.json";
 
 local sessions = {};
@@ -13,16 +14,14 @@
 
 	response.on_destroy = updates_client_closed;
 
-	response.conn:write(table.concat({
-		"HTTP/1.1 200 OK";
-		"Content-Type: text/event-stream";
-		"X-Accel-Buffering: no"; -- For nginx maybe?
-		"";
-		"event: stats-full";
-		"data: "..json.encode(statsman.get_stats());
-		"";
-		"";
-	}, "\r\n"));
+	response.headers.content_type = "text/event-stream";
+	response.headers.x_accel_buffering = "no"; -- for nginx maybe?
+	local resp = http.prepare_header(response);
+	table.insert(resp, "event: stats-full\r\n");
+	table.insert(resp, "data: ");
+	table.insert(resp, json.encode(statsman.get_stats()));
+	table.insert(resp, "\r\n\r\n");
+	response.conn:write(table.concat(resp));
 
 	sessions[response] = request;
 	return true;