-->

How to detect client-terminated connection using F

2019-09-04 13:45发布

问题:

I have a FCGI application written in C. We have cases where a client closes an active connection--i.e. leaves the page during page load--which we'd like to detect within the application, if possible. We have our web server (nginx) configured to close the FCGI connection when the client connection is unexpectedly closed, so my hope is that by looking at the FCGI stream state at the end of the transaction, we might be able to detect a client-initiated closed connection.

My question is: Is it possible to detect this using FCGX_GetError() on the output stream right before calling FCGX_Finish_r() ? If not, how might I trap such a condition?

I tried asking on the FastCGI mailing list, but it would seem that the list is down.

回答1:

If data has been transmitted over the socket and a error occurred, FCGX_GetError() will return the errno, you can also detect this using FCGX_PutStr() < 0. libfcgi internally buffers the output stream using a size of 8192 bytes, any remaining buffer is transmitted when FCGX_Finish_r() is called.



回答2:

You should receive SIGPIPE when the client closes the connection. Generally, it's safe to ignore this signal. But if you need to detect this, just install handler for SIGPIPE.