hapifhir / hapi-hl7v2

282 stars 137 forks source link

Cannot decode a response with CHUNKED encoding #131

Open EugeneHryz opened 3 months ago

EugeneHryz commented 3 months ago

Version: 2.5.1

When application receives a response with header Transfer-Encoding: chunked, then HAPI is unable to decode it. Here is the exception:

ca.uhn.hl7v2.hoh.api.DecodeException: Failed to decode CHUNKED encoding
    at ca.uhn.hl7v2.hoh.encoder.AbstractHl7OverHttpDecoder.readBytesChunked(AbstractHl7OverHttpDecoder.java:249)
    at ca.uhn.hl7v2.hoh.encoder.AbstractHl7OverHttpDecoder.doReadContentsFromInputStreamAndDecode(AbstractHl7OverHttpDecoder.java:222)
    at ca.uhn.hl7v2.hoh.encoder.AbstractHl7OverHttpDecoder.readHeadersAndContentsFromInputStreamAndDecode(AbstractHl7OverHttpDecoder.java:537)
    at ca.uhn.hl7v2.hoh.raw.client.AbstractRawClient.doSendAndReceiveInternal(AbstractRawClient.java:159)
    at ca.uhn.hl7v2.hoh.raw.client.AbstractRawClient.sendAndReceive(AbstractRawClient.java:279)
    at ca.uhn.hl7v2.hoh.raw.client.HohRawClientSimple.sendAndReceive(HohRawClientSimple.java:55)
    at ca.uhn.hl7v2.hoh.hapi.client.AbstractClient.sendAndReceiveMessage(AbstractClient.java:161)
    ...
    ...
Caused by: java.net.SocketException: Received EOF from input stream
    at ca.uhn.hl7v2.hoh.encoder.AbstractHl7OverHttpDecoder.readLine(AbstractHl7OverHttpDecoder.java:580)
    at ca.uhn.hl7v2.hoh.encoder.AbstractHl7OverHttpDecoder.readLine(AbstractHl7OverHttpDecoder.java:543)
    at ca.uhn.hl7v2.hoh.encoder.AbstractHl7OverHttpDecoder.readBytesChunked(AbstractHl7OverHttpDecoder.java:247)

I think the issue is in the class AbstractHl7OverHttpDecoder in method readBytesChunked(InputStream). It seems it reads the length of the first chunk, and then instead of reading just that amount of bytes, it reads the whole response body. And when it tries to read the length of second chunk, the stream has already been read (Receives EOF of stream).