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. 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. 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 reponses, 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.