An error in the middle of a record for processing output

I am creating httpserver as part of my java academic course, The server should only support basic GET and POST requests.

I was wondering if there is an elegant way to deal with the error that it has in the middle for writing the contents of the stream of HTML files (and after I already sent the response headers) to the output of the HttpServer stream.

In an elegant way, I refer to displaying or redirecting the user to the error page "Internal Server Error".

I tried forwarding the HTTP response headers with error code 501, but java throws an exception that states that headers have already been sent ...

One fix would be to read the contents of the file into memory and only then send the headers and contents, but other problems may arise, and in addition, I do not want to load huge files into memory before sending them as an answer.

+4
source share
2 answers

Once the response status is sent by wire, it cannot be changed. Therefore, if you sent a 200 OK response, you cannot change your mind after that. As you have discovered, this presents a problem in the case of errors that occur in the middle of the response.

As far as I know, the only thing you can do is send a response. See Section 3.6.1 of RFC 2616:

Code coding changes the body of the message to transmit it as a series of pieces, each with its own size indicator, followed by an EXTRA trailer containing the object header fields. This allows you to transfer dynamically generated content along with the information necessary for the recipient to confirm that he received the full message.

The purpose of this trailer is to provide information about an entity that cannot be calculated before sending the entity body. However, section 7.1 allows you to include any title in this trailer:

The header extension mechanism allows additional header fields of the object to be determined without changing the protocol, but these fields cannot be assumed that the recipient can be recognized. An unrecognized field header MUST be ignored by the recipient and transparent proxies MUST be sent.

Therefore, while you can signal that an error has occurred in the middle of the response, it should be indicated between the two parts how this is signaled. You cannot, in general, use any method that you can assume that the client will understand as an error signaling.

Terminating the connection prematurely in a message with the Content-length header is an option, but it is explicitly forbidden:

When the Content-Length is indicated in the message where the message body is allowed, its field value MUST exactly match the number of OCTETs in the message body. HTTP / 1.1 user agents MUST notify the user when an incorrect length is received and detected.

However, while the server should not send a message shorter than it advertises, the client should check this error condition and report as such (and proxies may even cache this partial response).

+4
source

In an elegant way, I refer to displaying or redirecting the user to the Error Page "Internal Server Error".

If you cannot send a success response, how are you going to send another answer? All you can do is register it and forget about it.

0
source

All Articles