Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tong committed Jun 30, 2023
1 parent 486a8c9 commit b36da69
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 22 deletions.
19 changes: 14 additions & 5 deletions src/wtri/Request.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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<String,String>;
public final params = new Map<String,String>();
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<String,String>();
var pos = path.indexOf( '?' );
if( pos != -1 ) {
var s = path.substr( pos+1 );
Expand All @@ -44,26 +45,34 @@ class Request {
val = EXPR_HTTP_HEADER.matched(2);
headers.set( key, val );
}
/*
/TODO:
switch method {
case POST, PUT:
final _len = headers.get( Content_Length );
if( _len == null )
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<String> {
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';
}
}
9 changes: 4 additions & 5 deletions src/wtri/Response.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
Expand Down Expand Up @@ -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');
}

}
}
26 changes: 16 additions & 10 deletions src/wtri/handler/WebSocketHandler.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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();
Expand All @@ -87,7 +92,6 @@ class Client {
if( frame != null ) _onmessage( frame );
}
});
//thread.sendMessage( Thread.current() );
thread.sendMessage( socket );
thread.sendMessage( s -> {
onmessage(s);
Expand All @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/wtri/http/Error.hx
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ class Error {
this.code = code;
this.message = (message == null) ? StatusMessage.fromStatusCode( code ) : message;
}
}
}
2 changes: 1 addition & 1 deletion src/wtri/net/Socket.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit b36da69

Please sign in to comment.