diff --git a/Tesses.WebServer.NetStandard/Tesses.WebServer.NetStandard.csproj b/Tesses.WebServer.NetStandard/Tesses.WebServer.NetStandard.csproj index a77a8ac..7ce91b0 100644 --- a/Tesses.WebServer.NetStandard/Tesses.WebServer.NetStandard.csproj +++ b/Tesses.WebServer.NetStandard/Tesses.WebServer.NetStandard.csproj @@ -5,9 +5,9 @@ Tesses.WebServer Mike Nolan Tesses - 1.0.3.2 - 1.0.3.2 - 1.0.3.2 + 1.0.3.3 + 1.0.3.3 + 1.0.3.3 A TCP Listener HTTP(s) Server MIT HTTP, WebServer, Website diff --git a/Tesses.WebServer.NetStandard/TessesServer.cs b/Tesses.WebServer.NetStandard/TessesServer.cs index 8bb40fb..9888ce8 100644 --- a/Tesses.WebServer.NetStandard/TessesServer.cs +++ b/Tesses.WebServer.NetStandard/TessesServer.cs @@ -882,6 +882,81 @@ namespace Tesses.WebServer } } } + public async Task PushAsync(Stream strm,EndPoint local,EndPoint remote) + { + string request_line = ""; + string res=ReadHeaders(strm); + var headers=Headers(res,out request_line); + + // {Method} {Path} HTTP/1.1 + ServerContext ctx=null; + string[] request=request_line.Split(new char[] { ' ' }, 3); + string method = request[0]; + try + { + string path = request[1]; + string ver = request[2]; + ctx = new ServerContext(method, strm, path, headers); + ctx.Server =local as IPEndPoint; + ctx.Client = remote as IPEndPoint; + _server.AddCors(ctx); + if(PrintUrls) + { + Console.WriteLine(path); + } + if (!await _server.BeforeAsync(ctx)) + { + switch (method) + { + case "HEAD": + case "GET": + + await _server.GetAsync(ctx); + break; + case "POST": + await _server.PostAsync(ctx); + break; + case "OPTIONS": + await _server.OptionsAsync(ctx); + break; + default: + await _server.OtherAsync(ctx); + break; + } + } + }catch(Exception ex) + { + try + { + await ctx.SendExceptionAsync(ex); + }catch(Exception ex2) + { + _ = ex2; + } + } + + } + public async Task ListenAsync(CancellationToken token,Action endpoint) + { + _listener.Start(); + if(endpoint != null) + { + endpoint((IPEndPoint)_listener.LocalEndpoint); + } + using (var r = token.Register(() => _listener.Stop())) { + while (!token.IsCancellationRequested) + { + try{ + var socket=await _listener.AcceptTcpClientAsync(); + await CommunicateHostAsync(socket).ConfigureAwait(false); + }catch(Exception ex) + { + _=ex; + } + } + } + } + private string ReadHeaders(Stream strm) { @@ -956,57 +1031,7 @@ namespace Tesses.WebServer using (Stream strm = GetStream(clt)) { - string request_line = ""; - string res=ReadHeaders(strm); - var headers=Headers(res,out request_line); - - // {Method} {Path} HTTP/1.1 - ServerContext ctx=null; - string[] request=request_line.Split(new char[] { ' ' }, 3); - string method = request[0]; - try - { - string path = request[1]; - string ver = request[2]; - ctx = new ServerContext(method, strm, path, headers); - ctx.Server = clt.Client.LocalEndPoint as IPEndPoint; - ctx.Client = clt.Client.RemoteEndPoint as IPEndPoint; - _server.AddCors(ctx); - if(PrintUrls) - { - Console.WriteLine(path); - } - if (!await _server.BeforeAsync(ctx)) - { - switch (method) - { - case "HEAD": - case "GET": - - await _server.GetAsync(ctx); - break; - case "POST": - await _server.PostAsync(ctx); - break; - case "OPTIONS": - await _server.OptionsAsync(ctx); - break; - default: - await _server.OtherAsync(ctx); - break; - } - } - }catch(Exception ex) - { - try - { - await ctx.SendExceptionAsync(ex); - }catch(Exception ex2) - { - _ = ex2; - } - } - + await PushAsync(strm,clt.Client.LocalEndPoint,clt.Client.RemoteEndPoint); } }catch(Exception ex) {