Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect webpage after having sent some content

I am working with a web framework (uPortal) that is handling errors by just throwing an exception and then hanging. The framework works by rendering XML into HTML. When there is an exception, the browser recieves rendered content up to the XML template element that is failing, and then the browser just sits and waits for a timeout. Our team's theory is that the content is sent before the error occurs, which surprised me. Other frameworks I've worked with seem to finish rendering before sending content.

My question is, is there a way to redirect the browser after content has already been sent? In this case, we are in the middle of rendering the content of a <script> tag, but the error could occur potentially anywhere in the html.

My only current thought is to inject some javascript at the top of the page, and to try to change the framework's behavior to fail quickly and close the connection and add </body> and </html> tags when an error occurs. Then the above mentioned javascript would run on pageload and detect if the entire page's content was there and do a client-side redirect if not. Maybe it could look for a special hidden div at the bottom of the page.

Are there any examples of frameworks solving this problem differently or of people using similar framework working around this issue?

like image 447
xdhmoore Avatar asked Nov 09 '22 19:11

xdhmoore


1 Answers

You must either capture the error, or capture the output in a buffer. If you can handle the exception, you can probably print a simple script tag like

<script> window.location.href = 'some_new_url';</script>

If the browser understands the doctype to be something related to HTML, it will execute that tag.

If you can capture the output in a buffer, when you handle the error you can decide to send an HTTP redirect to the browser and destroy the output buffer up to that point.

As for other frameowrks, in PHP, you can simply enable output buffering with ob_start(), which won't start sending content until the request is fully completed.

like image 76
chugadie Avatar answered Nov 14 '22 21:11

chugadie