mirror of
				https://github.com/RandomityGuy/MBHaxe.git
				synced 2025-10-30 08:11:25 +00:00 
			
		
		
		
	fix signalling server CORS
This commit is contained in:
		
							parent
							
								
									78c3ed1d67
								
							
						
					
					
						commit
						ae75f966c6
					
				
					 1 changed files with 55 additions and 0 deletions
				
			
		| 
						 | 
				
			
			@ -2,12 +2,67 @@ import haxe.Json;
 | 
			
		|||
import hx.ws.SocketImpl;
 | 
			
		||||
import hx.ws.WebSocketHandler;
 | 
			
		||||
import hx.ws.WebSocketServer;
 | 
			
		||||
import hx.ws.State;
 | 
			
		||||
import hx.ws.Log;
 | 
			
		||||
import hx.ws.HttpHeader;
 | 
			
		||||
import hx.ws.HttpResponse;
 | 
			
		||||
import hx.ws.HttpRequest;
 | 
			
		||||
 | 
			
		||||
using Lambda;
 | 
			
		||||
 | 
			
		||||
class SignallingHandler extends WebSocketHandler {
 | 
			
		||||
	static var clients:Array<SignallingHandler> = [];
 | 
			
		||||
 | 
			
		||||
	public override function handshake(httpRequest:HttpRequest) {
 | 
			
		||||
		var httpResponse = new HttpResponse();
 | 
			
		||||
 | 
			
		||||
		httpResponse.headers.set(HttpHeader.SEC_WEBSOSCKET_VERSION, "13");
 | 
			
		||||
		httpResponse.headers.set("Access-Control-Allow-Origin", "*"); // Enable CORS pls, why do i have to override this entire function for a single line
 | 
			
		||||
		if (httpRequest.method != "GET" || httpRequest.httpVersion != "HTTP/1.1") {
 | 
			
		||||
			httpResponse.code = 400;
 | 
			
		||||
			httpResponse.text = "Bad";
 | 
			
		||||
			httpResponse.headers.set(HttpHeader.CONNECTION, "close");
 | 
			
		||||
			httpResponse.headers.set(HttpHeader.X_WEBSOCKET_REJECT_REASON, 'Bad request');
 | 
			
		||||
		} else if (httpRequest.headers.get(HttpHeader.SEC_WEBSOSCKET_VERSION) != "13") {
 | 
			
		||||
			httpResponse.code = 426;
 | 
			
		||||
			httpResponse.text = "Upgrade";
 | 
			
		||||
			httpResponse.headers.set(HttpHeader.CONNECTION, "close");
 | 
			
		||||
			httpResponse.headers.set(HttpHeader.X_WEBSOCKET_REJECT_REASON,
 | 
			
		||||
				'Unsupported websocket client version: ${httpRequest.headers.get(HttpHeader.SEC_WEBSOSCKET_VERSION)}, Only version 13 is supported.');
 | 
			
		||||
		} else if (httpRequest.headers.get(HttpHeader.UPGRADE) != "websocket") {
 | 
			
		||||
			httpResponse.code = 426;
 | 
			
		||||
			httpResponse.text = "Upgrade";
 | 
			
		||||
			httpResponse.headers.set(HttpHeader.CONNECTION, "close");
 | 
			
		||||
			httpResponse.headers.set(HttpHeader.X_WEBSOCKET_REJECT_REASON, 'Unsupported upgrade header: ${httpRequest.headers.get(HttpHeader.UPGRADE)}.');
 | 
			
		||||
		} else if (httpRequest.headers.get(HttpHeader.CONNECTION).indexOf("Upgrade") == -1) {
 | 
			
		||||
			httpResponse.code = 426;
 | 
			
		||||
			httpResponse.text = "Upgrade";
 | 
			
		||||
			httpResponse.headers.set(HttpHeader.CONNECTION, "close");
 | 
			
		||||
			httpResponse.headers.set(HttpHeader.X_WEBSOCKET_REJECT_REASON, 'Unsupported connection header: ${httpRequest.headers.get(HttpHeader.CONNECTION)}.');
 | 
			
		||||
		} else {
 | 
			
		||||
			Log.debug('Handshaking', id);
 | 
			
		||||
			var key = httpRequest.headers.get(HttpHeader.SEC_WEBSOCKET_KEY);
 | 
			
		||||
			var result = makeWSKeyResponse(key);
 | 
			
		||||
			Log.debug('Handshaking key - ${result}', id);
 | 
			
		||||
 | 
			
		||||
			httpResponse.code = 101;
 | 
			
		||||
			httpResponse.text = "Switching Protocols";
 | 
			
		||||
			httpResponse.headers.set(HttpHeader.UPGRADE, "websocket");
 | 
			
		||||
			httpResponse.headers.set(HttpHeader.CONNECTION, "Upgrade");
 | 
			
		||||
			httpResponse.headers.set(HttpHeader.SEC_WEBSOSCKET_ACCEPT, result);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		sendHttpResponse(httpResponse);
 | 
			
		||||
 | 
			
		||||
		if (httpResponse.code == 101) {
 | 
			
		||||
			_onopenCalled = false;
 | 
			
		||||
			state = State.Head;
 | 
			
		||||
			Log.debug('Connected', id);
 | 
			
		||||
		} else {
 | 
			
		||||
			close();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function new(s:SocketImpl) {
 | 
			
		||||
		super(s);
 | 
			
		||||
		onopen = () -> {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue