event_route[xhttp:request] {
	set_reply_close();
	set_reply_no_connect();
	
#!ifdef WITH_XMLRPC
	if ($hu =~ "^/RPC") {
		route(XMLRPC);
		exit;
	}
#!endif

	if ($Rp != MY_WS_PORT
#!ifdef WITH_TLS
	    && $Rp != MY_WSS_PORT
#!endif
	) {
		xlog("L_WARN", "HTTP request received on $Rp\n");
		xhttp_reply("403", "Forbidden - HTTP request received on $Rp", "", "");
		exit;
	}

	if ($hdr(Upgrade)=~"websocket"
			&&  in_list("Upgrade", $hdr(Connection), ",")
			&& $rm=~"GET") {

		# Validate Host - make sure the client is using the correct
		# alias for WebSockets
		if ($hdr(Host) == $null || !is_myself("sip:" + $hdr(Host))) {
			xlog("L_WARN", "Bad host $hdr(Host)\n");
			xhttp_reply("403", "Forbidden - invalid host", "", "");
			exit;
		}

#!ifdef WEBSOCKET_WEBSERVER
		# Validate Origin - make sure the client is from the authorised website
		if ($hdr(Origin) != "http://"+WEBSOCKET_WEBSERVER
#!ifdef WITH_TLS
		  && $hdr(Origin) != "https://"+WEBSOCKET_WEBSERVER
#!endif
		) {
			xlog("L_WARN", "Unauthorised client $hdr(Origin)\n");
			xhttp_reply("403", "Forbidden - invalid website", "", "");
			exit;
		}
#!endif

		# ws_handle_handshake() exits (no further configuration file
		# processing of the request) when complete.
		if (ws_handle_handshake()) {
			# Optional... cache some information about the
			# successful connection
			exit;
		}
	}

	# xhttp_reply("200", "OK", "text/html", "<html><body>Wrong URL $hu</body></html>");
	xhttp_reply("404", "Not Found", "", "");
}

event_route[websocket:closed] {
	xlog("L_DBG", "WebSocket connection from $si:$sp has closed\n");
}
