SpaceWizards.HttpListener icon indicating copy to clipboard operation
SpaceWizards.HttpListener copied to clipboard

request.RemoteEndPoint goes null when response.outputStream is closed - differs from Windows native HttpListener

Open RogerHardiman opened this issue 1 year ago • 1 comments
trafficstars

Hi Found a difference between this project and the Windows native HttpListener It seems that request.RemoteEndPoint goes invalid after respose.outputStream.Close() or after response.Close() On Windows with the native implementation you can still use RemoteEndPoint after the response close, eg for a Logging Message of "Response Sent back to " + request.RemoteEndPoint.ToString()

Here is a mini example.

        private void processHTTPRequest(HttpListenerContext context)
        {

            HttpListenerRequest request = context.Request;
            HttpListenerResponse response = context.Response;

            Console.WriteLine(request.RemoteEndPoint);
            byte[] output_bytes = {1, 2, 3};

            Stream outputStream = response.OutputStream;
            outputStream.Write(output_bytes, 0, output_bytes.Length);
            outputStream.Close();

            Console.WriteLine(request.RemoteEndPoint);   // <--	'request.RemoteEndPoint' threw an exception of type 'System.NullReferenceException'	System.Net.IPEndPoint {System.NullReferenceException}
        }

It's not the end of the world. I can cache the value. If you think it is an easy fix, I can fork, test and do a PR.

Thanks, Roger

RogerHardiman avatar Sep 24 '24 09:09 RogerHardiman

I think it's because closing the output returns the TCP connection to the pool. Probably just caching the properties on the type is the best solution and not too hard.

PJB3005 avatar Sep 24 '24 10:09 PJB3005