Added the ability to get ip/port info for :0

This commit is contained in:
Mike Nolan 2022-05-16 15:57:46 -05:00
parent 9218a90d49
commit 68bf4deb21
2 changed files with 79 additions and 54 deletions

View File

@ -5,9 +5,9 @@
<PackageId>Tesses.WebServer</PackageId> <PackageId>Tesses.WebServer</PackageId>
<Author>Mike Nolan</Author> <Author>Mike Nolan</Author>
<Company>Tesses</Company> <Company>Tesses</Company>
<Version>1.0.3.2</Version> <Version>1.0.3.3</Version>
<AssemblyVersion>1.0.3.2</AssemblyVersion> <AssemblyVersion>1.0.3.3</AssemblyVersion>
<FileVersion>1.0.3.2</FileVersion> <FileVersion>1.0.3.3</FileVersion>
<Description>A TCP Listener HTTP(s) Server</Description> <Description>A TCP Listener HTTP(s) Server</Description>
<PackageLicenseExpression>MIT</PackageLicenseExpression> <PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>HTTP, WebServer, Website</PackageTags> <PackageTags>HTTP, WebServer, Website</PackageTags>

View File

@ -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<IPEndPoint> 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) private string ReadHeaders(Stream strm)
{ {
@ -956,57 +1031,7 @@ namespace Tesses.WebServer
using (Stream strm = GetStream(clt)) using (Stream strm = GetStream(clt))
{ {
string request_line = ""; await PushAsync(strm,clt.Client.LocalEndPoint,clt.Client.RemoteEndPoint);
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;
}
}
} }
}catch(Exception ex) }catch(Exception ex)
{ {