New TIdHTTP flags and OnChunkReceived event

Three new flag have been added to the TIdHTTP.HTTPOptions property:

  • hoNoParseXmlCharset: when receiving an XML response with an “application/…” Content-Type, the TIdHTTP.Response.Charset property will be set to the charset declared in the XML’s prolog, unless this flag is enabled, in which case TIdHTTP.Response.Charset will be set to the charset specified in the HTTP response’s Content-Type header instead.
  • hoWantProtocolErrorContent: when an HTTP error response is received, TIdHTTP normally reads and discards the response’s message body and then raises EIdHTTPProtocolException (the message body is available in the EIdHTTPProtocolException.ErrorMessage property). If the hoNoProtocolErrorException flag is enabled, or the ResponseCode number is specified in the request’s AIgnoreReplies parameter, then no EIdHTTPProtocolException is raised, as the caller would like to process the ResponseCode manually. Normally TIdHTTP would still discard the message body, though. If this new flag is enabled, the message body will no longer be discarded, it will be saved in the caller’s target TStream like a successful response would be. This flag is disabled by default to preserve existing behavior to discard error message bodies.
  • hoNoReadChunked: similar to the hoNoReadMultipartMIME, flag, this flag specifies whether TIdHTTP should read and parse the body content of a “chunked” response into the target TStream or to exit immediately and let the caller read the content manually from the TIdHTTP.IOHandler directly (see UPDATE further below). By default, this flag is disabled to preserve existing behavior to have TIdHTTP read the content into the target TStream.

Also, a new OnChunkedReceived event has been added to TIdHTTP. When TIdHTTP is reading a “chunked” response, this event will be called for each chunk that is successfully read. The event handler may alter the chunk data before it is written, or set the chunk’s size to 0 to prevent it from being written, to the target TStream.

As with the hoNoReadMultipartMIME flag, you can use the TIdHTTP.OnHeadersAvailable event to set the hoNoReadChunked flag, or the OnChunkReceived event handler, on a per-response basis after examining the HTTP response headers before the response body content is read. That way, you can dynamically decide whether to enable the flag/event on certain responses, or to disable them on other responses, depending on how and when you want to parse the chunk data – in real-time while the response is still being received, or from a local TStream after the response has been completed.

UPDATE March 24, 2021: in earlier versions of TIdHTTP, the TCP connection would be closed when TIdHTTP is done processing a response if an HTTP keep-alive is not being used.  When the hoNoReadChunked or hoNoReadMultipartMIME flag is enabled and an HTTP keep-alive is not used, this would cause a premature closure of the connection before user code can read the chunked/mime data from the IOHandler.  As of GitHub checkin 0F1D4047, this has now been corrected, so that the connection is no longer closed if the hoNoReadChunked flag is enabled and the response is chunked, or the hoNoReadMultipartMIME flag is enabled and the response is MIME, regardless of whether or not an HTTP keep-alive is used.