ASP.NET custom HTTPModule - how to tell when writing last block of HTML in Write method?
I have written an Http Module that hooks onto the Response.Filter property of the current request and does various replacements within the HTML before it is sent to the client.
All the work is done in the Write method which is overriding Write in the base class Stream.
The Write method is called multiple times for a single response - the HTML seems to be written to the output stream in chunks. My problem is that I don't have an efficient & reliable way of telling if the current chunk is the last chunk (for why I want to know this see below). The only way I have come up with is to check if the chunk contains a closing html tag - but this is not very efficient or reliable.
The reason this is needed is that the module must add the "Refresh" HTTP header to the response, but only if the HTML fulfills certain conditions (and there are certain conditions that mean the header must not be added). So, only when the last chunk has been seen does the code know if the header can be added or not. So, I either need a test for the last chunk, or on each call to Write I add the header if the current block of HTML passes the test (if it has not already been added) or remove the header if the current block of HTML fails the test (if it has already been added).
So, is there a better way to test for the last chuck OR is there a way to test for a particular header being in the response and delete it (there doesn't seem to be a way to do this - only to append headers)?
If you can figure out the total number of bytes to be written from your html source, then just count the bytes as you call Write() to figure out when you are writing the last chunk.
Also, you can't add headers after sending content to the browser. I think that if you avoid calling Flush() to actually send your output, you will be ok to add a header after writing your content.