MeddlerScript for testing ServerSentEvents. See https://textslashplain.com/2019/12/04/the-pitfalls-of-eventsource-over-http-1-1/ for discussion.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Meddler; | |
import System; | |
import System.Net.Sockets; | |
import System.Windows.Forms; | |
class Handlers | |
{ | |
static function OnConnection(oSession: Session) | |
{ | |
try { | |
if (oSession.ReadRequest()){ | |
var oHeaders: ResponseHeaders = new ResponseHeaders(); | |
oHeaders.Status = "200 OK"; | |
if (oSession.urlContains("eventsource.cgi?")) { | |
oHeaders["Connection"] = "keep-alive"; | |
oHeaders["Content-Type"] = "text/event-stream"; | |
oSession.WriteString(oHeaders); | |
var myId = oSession.url.Substring(oSession.url.IndexOf('eventsource.cgi')); | |
oSession.WriteString("data: This is the first event from " + myId + "\n\n"); | |
// Ten minutes | |
for(var i:int = 0; i < 600; i++){ | |
oSession.WriteString("data: from " + myId + ": The server time is: " + DateTime.Now.ToString("h:mm:ss") + "\n\n"); | |
System.Threading.Thread.Sleep(1000); | |
} | |
oSession.WriteString("data: This is the last event from " + myId + "\n\n"); | |
} | |
else if (oSession.urlContains(".jpg")) | |
{ | |
oHeaders["Connection"] = "close"; | |
oHeaders["Content-Type"] = "image/jpg"; | |
oHeaders["Cache-Control"] = "no-store"; | |
oSession.WriteString(oHeaders); | |
oSession.WriteBytes(Fuzz.NewJPG("TestJPG...", Fuzz.NewInteger(40,100), 20)); | |
} | |
else if (oSession.urlContains('frame')) { | |
oHeaders["Connection"] = "close"; | |
oHeaders["Content-Type"] = "text/html"; | |
oHeaders["Cache-Control"] = "no-store"; | |
oSession.WriteString(oHeaders); | |
oSession.WriteString("<!doctype html><html><body>"); | |
for (var x:int = 0; x < 15; x++) { | |
oSession.WriteString("<iframe src='/?" + Fuzz.NewInteger(100, 1000) + "' width=600 height=400></iframe>"); | |
} | |
oSession.WriteString("</body></html>"); | |
} | |
else { | |
oHeaders["Connection"] = "close"; | |
oHeaders["Content-Type"] = "text/html"; | |
oHeaders["Cache-Control"] = "no-store"; | |
oSession.WriteString(oHeaders); | |
oSession.WriteString("<!doctype html><html><body style='background-color: #FFA4A4'><img id='imgTop' src='" + Fuzz.NewInteger(0, 999999) + ".jpg' />"); | |
oSession.WriteString("<h1>EventSource Connection Limit Test</h1>This page uses an EventSource for serversentevents. Use it to test connection limits by opening multiple tabs to this same page."); | |
oSession.WriteString("<hr /><a href='/?"+Fuzz.NewInteger(0,10000) +"' target='_blank'>Open another tab</a><hr />"); | |
oSession.WriteString("<h2>Latest Message</h2><div id='divLatest'></div>"); | |
oSession.WriteString("<script>function addImg() { let a=document.createElement('img'); a.src='dyn'+Math.random(99999)+'.jpg'; document.body.appendChild(a);} setInterval(addImg, 400); var evtSource = new EventSource('eventsource.cgi?" + Fuzz.NewInteger(0,999999) + "');var latestEvent = document.getElementById('divLatest');evtSource.onmessage = function(e) {" + | |
"let bFirst= e.data.includes('first'); if (bFirst) document.body.style='background-color: #D9EAC8'; latestEvent.style = 'font-weight: 500; font-size: 24px; '+ (bFirst ? 'color:red' : 'color:green'); latestEvent.textContent = e.data;}</script>"); | |
oSession.WriteString("</body></html>"); | |
} | |
} | |
oSession.CloseSocket(); | |
} | |
catch (e) { | |
MeddlerObject.Log.LogString(e.message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment