5

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?

xdhmoore
  • 8,935
  • 11
  • 47
  • 90

5 Answers5

1

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.

chugadie
  • 2,786
  • 1
  • 24
  • 33
0

I don't know that framework, but

In http, every response has a response-code associated with it. Since the page is already half-way transferred / rendered that status code (usually "200") was sent (and received) already.

There's no way for the browser to accept another response code (like "301" for redirect) for the same response! Also the server is not able to send another response code, because the original response code was already commited and sent to the client.

Your description of the error and knowledge of the http-protocol implies that there is probably some implementation error in the framework / server components used, OR it was done deliberatly, risking the situation that you are in now...

0

to redirect a page , you need to set redirect information in header. but you can write header once you start writing content ( may be header is already received by client by the time you compete writing whole document )

But, you can do it in different way as below

1.let document loading complete and record if you need to redirect the page while rendering
2. add a unique  request-id identifier for each page load
3. invoke ajax call with request-id ( may be rest call)  to server asking if page needs to be redirected.
4. if page needs to be redirected , do so, via javascript in browser at client end. 
0

A HTTP response consists of headers and an optional response content. Once you have started to write the response to the socket connection you can't revert it. In your example: If you run into an error in the middle of content generation you can't add a redirect header - the header section has already be written.

The statement above is not entirely true: in HTTP chunked transfer encoding the response is sent in separate chunks. The last chunk can have an optional trailer containing entity-header fields and theoretically a redirect header. But if you can use these mechanism is a different question. For instance a servlet container may use chunked transfer encoding but does not give you an API to set the trailer.

But writing must not start immediately: For instance HttpServletResponse maintains a buffer for the response content. If you set headers and start writing the content only the buffer is filled and you still can reset the response and start all over. But once the buffer overflows the response is written to the connection and the HttpServletResponse is now committed.

Such a mechanism gives you way to deal with errors during content generation which happen when the response is not yet committed: Just reset the response and send an error message instead. You could examine your framework if it supports such an mechanism. But obviously this is not a solution for larger responses.

A second way to avoid errors during content generation is simply to make sure that they can't happen. First gather all your data needed for the response (e.g. making unsafe database calls), then in a second step generate the response - the second now step should not fail (except if you have bugs in your code).

You already mentioned a third way to handle an error, by having the client sanitize the response and take some action it errors are detected (e.g. by including a script in the generated HTML response).

wero
  • 32,544
  • 3
  • 59
  • 84
  • How do other frameworks solve this problem? Like Spring, for example? Doesn't spring wait until views are rendered before writing to the response? Or do all frameworks have this issue? – xdhmoore Mar 22 '16 at 15:18
  • @xdhmoore in the end Spring is also based on servlet technology, and as far as I know following the second strategy mentioned in my answer. This https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc may be of interest, but it is mainly about how to dispatch to a meaningful error page and not how to avoid or recover from an exception. – wero Mar 22 '16 at 16:03
  • Does spring allow you to catch exceptions from the view/jsp layer? Where I am confused is that in this framework we are seeing the page freeze at the exception after loading all the previous content. I had thought other frameworks like spring rendered everything before starting to return content (so that they could return an error page in case of an exception), but this framework seems to begin returning content before it is done rendering, which seems to me to make your option 3 above the only choice... – xdhmoore Mar 23 '16 at 15:59
  • This uPortal code doesn't use JSP, but this question about JSP autoflushing and buffering seems related: https://stackoverflow.com/questions/10868281/optimal-buffer-size-for-jsps-and-autoflush-property – xdhmoore Jun 06 '18 at 20:27
0

The only reliable way to do this is to create a proxy HttpServletResponse object that caches the response. You'd need to give the uPortal this proxy instead of the actual HttpServletResponse, and only send the output using the real response once the processing completes / send redirect if the processing fails.

It is HTTP protocol design limitation that you cannot send HTTP redirect once output was started.

Other possible ways rely on HTML or Javascript redirects, but since you write that the error may happen at any moment, it would be difficult to print it out in a way that the browsers would reliably interpret it as redirect.

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43