From b36da69c25e6cca3883908f3e82df92116cb36bd Mon Sep 17 00:00:00 2001 From: tong Date: Fri, 30 Jun 2023 14:22:36 +0200 Subject: [PATCH] Cleanup --- src/wtri/Request.hx | 19 ++++++++++++++----- src/wtri/Response.hx | 9 ++++----- src/wtri/handler/WebSocketHandler.hx | 26 ++++++++++++++++---------- src/wtri/http/Error.hx | 2 +- src/wtri/net/Socket.hx | 2 +- 5 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/wtri/Request.hx b/src/wtri/Request.hx index 9b7897a..58cabfe 100644 --- a/src/wtri/Request.hx +++ b/src/wtri/Request.hx @@ -8,23 +8,24 @@ class Request { static var EXPR_HTTP_HEADER = ~/^([a-zA-Z-]+) *: *(.+)$/; public final socket : Socket; + public final input : haxe.io.Input; public final method : Method; public final path : String; public final protocol : String; - public final params : Map; + public final params = new Map(); public final headers = new wtri.http.Headers(); - public final data : Data; - + //public final data : Data; + public function new( socket : Socket, input : haxe.io.Input ) { this.socket = socket; + this.input = input; var line = input.readLine(); if( !EXPR_HTTP.match( line ) ) throw new Error( BAD_REQUEST ); method = EXPR_HTTP.matched(1); path = EXPR_HTTP.matched(2); protocol = EXPR_HTTP.matched(3); - params = new Map(); var pos = path.indexOf( '?' ); if( pos != -1 ) { var s = path.substr( pos+1 ); @@ -44,6 +45,8 @@ class Request { val = EXPR_HTTP_HEADER.matched(2); headers.set( key, val ); } + /* + /TODO: switch method { case POST, PUT: final _len = headers.get( Content_Length ); @@ -51,19 +54,25 @@ class Request { throw new Error( BAD_REQUEST ); final len = Std.parseInt( headers.get( Content_Length ) ); input.readBytes( data = Bytes.alloc( len ), 0, len ); + trace(data); case _: } + */ } public function getEncoding( header : HeaderName = Accept_Encoding ) : Array { return headers.exists( header ) ? ~/ ?, ?/g.split( headers.get( header ) ) : []; } - +/* public function createResponse() : Response { final res = new Response( this ); if( headers.get( Connection ) == 'keep-alive' ) { res.headers.set( Connection, 'close' ); } return res; + }*/ + + public function toString() { + return '$method $path $headers'; } } diff --git a/src/wtri/Response.hx b/src/wtri/Response.hx index bfb563b..f239856 100644 --- a/src/wtri/Response.hx +++ b/src/wtri/Response.hx @@ -24,7 +24,7 @@ class Response { this.protocol = protocol; this.headers = (headers != null) ? headers : []; } - + public function writeHead( ?code : StatusCode, ?extraHeaders :Headers ) { if( code != null ) this.code = code; writeLine( '${protocol} ${this.code} '+StatusMessage.fromStatusCode( this.code ) ); @@ -65,8 +65,7 @@ class Response { finished = true; } - inline function writeLine( str : String ) { - socket.write( '$str\r\n' ); + inline function writeLine(line: Data) { + socket.write('$line\r\n'); } - -} \ No newline at end of file +} diff --git a/src/wtri/handler/WebSocketHandler.hx b/src/wtri/handler/WebSocketHandler.hx index 664f45b..9605680 100644 --- a/src/wtri/handler/WebSocketHandler.hx +++ b/src/wtri/handler/WebSocketHandler.hx @@ -33,8 +33,14 @@ class WebSocketHandler implements wtri.Handler { res.headers.set( Upgrade, 'websocket' ); res.headers.set( Sec_WebSocket_Accept, key ); res.end(); + switch Type.typeof(res.socket) { + case TClass(c): + if(Type.getClassName(c) != 'wtri.net.TCPSocket') + return false; + case _: + } final client = createClient(cast(res.socket, TCPSocket).socket); - //if(client == null) return false; + if(client == null) return false; clients.push( client ); client.read(); onconnect( client ); @@ -67,18 +73,17 @@ class Client { public function read() { thread = Thread.create( () -> { - //var main : Thread = Thread.readMessage( true ); var sock : sys.net.Socket = Thread.readMessage( true ); var _onmessage : Bytes->Void = Thread.readMessage( true ); var _ondisconnect : Void->Void = Thread.readMessage( true ); var frame : Bytes = null; while( true ) { try { - frame = WebSocket.readFrame( sock.input ); - } catch(e:haxe.io.Eof) { - trace(e); - _ondisconnect(); - break; + frame = WebSocket.readFrame(sock.input); + // } catch(e:haxe.io.Eof) { + // trace(e); + // _ondisconnect(); + // break; } catch(e) { trace(e); _ondisconnect(); @@ -87,7 +92,6 @@ class Client { if( frame != null ) _onmessage( frame ); } }); - //thread.sendMessage( Thread.current() ); thread.sendMessage( socket ); thread.sendMessage( s -> { onmessage(s); @@ -103,8 +107,10 @@ class Client { public function close() { handler.clients.remove( this ); - try socket.close() catch(e) { - trace(e); + if(socket != null) { + try socket.close() catch(e) { + trace(e); + } } ondisconnect(); } diff --git a/src/wtri/http/Error.hx b/src/wtri/http/Error.hx index 641023d..b20ff91 100644 --- a/src/wtri/http/Error.hx +++ b/src/wtri/http/Error.hx @@ -11,4 +11,4 @@ class Error { this.code = code; this.message = (message == null) ? StatusMessage.fromStatusCode( code ) : message; } -} \ No newline at end of file +} diff --git a/src/wtri/net/Socket.hx b/src/wtri/net/Socket.hx index 954fe51..4f93c11 100644 --- a/src/wtri/net/Socket.hx +++ b/src/wtri/net/Socket.hx @@ -37,7 +37,7 @@ class UVSocket implements Socket { public inline function writeInput( input : haxe.io.Input, len : Int ) socket.write( input.readAll() ); - + public inline function close() socket.close(); }