Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
RogerHardiman opened this issue Sep 24, 2024 · 1 comment

Comments

@RogerHardiman
Copy link

RogerHardiman commented Sep 24, 2024

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

@PJB3005
Copy link
Member

PJB3005 commented Sep 24, 2024

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.

@RogerHardiman RogerHardiman changed the title request.RemoteEndPoint goes null when reqsponse.outputStream is closed - differs from Windows native HttpListener request.RemoteEndPoint goes null when response.outputStream is closed - differs from Windows native HttpListener Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants