Redirect webpage after sending some content

I work with a web framework (uPortal) that handles errors by simply throwing an exception and then hanging. Frames work by rendering XML in HTML. When an exception occurs, the browser receives the displayed content until the XML template element fails, and then the browser just sits and waits for a timeout. Our theory of the team is that content is sent before an error occurs, which surprised me. The other frameworks I've worked with seem to finish rendering before submitting the content.

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

My only thought is to add some javascript at the top of the page and try to change the structure's behavior to quickly fail and close the connection, and add the </body> and </html> tags when an error occurs. Then the aforementioned javascript will run on pageload and detect if there is all the page content and direct the client side if not. Perhaps he can look for a special hidden div at the bottom of the page.

Are there any examples of solutions that solve this problem in different ways, or people using a similar structure working around this problem?

+5
source share
5 answers

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

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

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

If you can capture the output in the buffer when you process the error, you can decide to send HTTP redirects to the browser and destroy the output buffer to this point.

As with other frameworks, in PHP you can simply enable output buffering with ob_start (), which will not start sending content until the request is complete.

+1
source

I do not know this structure, but

In http, each response has a response code associated with it. Since the page has already been partially transmitted / displayed, a status code (usually "200") has already been sent (and received).

It is not possible for the browser to accept a different response code (for example, “301” for redirection) for the same answer! Also, the server cannot send another response code, because the original response code has already been sent 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 used components of the framework / server, or this was done intentionally, at the risk of the situation you are in now ...

0
source

to redirect the page you need to set the redirect information in the header. but you can write a headline as soon as you start writing content (maybe the heading has already been received by the client by the time you are competing with writing the entire document)

But you can do it differently 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
source

An HTTP response consists of headers and optional response content. When you start writing a response to a socket connection, you cannot return it. In your example: if you encounter an error in the middle of creating content, you cannot add a redirect header - the header is already recorded.

The above statement is not entirely true: in HTTP chunked transfer encoding, the response is sent in separate pieces. The last fragment may have an optional trailer containing object header fields and theoretically a redirect header. But if you can use this mechanism, this is another question. For example, a servlet container may use encoding with an encoding, but it does not give you an API to install a trailer.

But the recording should not start immediately: for example, HttpServletResponse maintains a buffer for the content of the response. If you set the headers and start writing content, the buffer is full, and you can still reset the response and start over. But as soon as the buffer overflows the response, it is written to the connection, and now the HttpServletResponse is executed.

Such a mechanism gives you the opportunity to deal with errors during the generation of content that occur when the response has not yet been completed: just reset, reply and send an error message. You can check your structure if it supports such a mechanism. But obviously this is not a solution to the big answers.

The second way to avoid mistakes while creating content is to simply make sure that they cannot happen. First, collect all your data necessary for the answer (for example, by making unsafe database calls), then in the second step, create the answer - the second step should now not be interrupted (unless you have errors in your code).

You already mentioned the third way to resolve the error, because the client deactivates the response and takes some actions that detect its errors (for example, by including a script in the generated HTML response).

0
source

The only reliable way to do this is to create an HttpServletResponse proxy server that caches the response. You will need to give uPortal this proxy instead of the actual HttpServletResponse and send only the result with a real response after the completion of processing / sending the redirect, if the processing fails.

The restriction on the use of the HTTP protocol does not allow sending HTTP redirects after the exit has started.

Other possible methods depend on redirecting HTML or Javascript, but since you are writing that an error can occur at any time, it would be difficult to print it in such a way that browsers reliably interpret it as a redirect.

0
source

Source: https://habr.com/ru/post/1243799/


All Articles