3434import java .util .concurrent .TimeUnit ;
3535import lombok .extern .slf4j .Slf4j ;
3636import org .apache .http .HttpEntity ;
37+ import org .apache .http .ParseException ;
3738import org .apache .http .client .config .RequestConfig ;
3839import org .apache .http .client .methods .CloseableHttpResponse ;
3940import org .apache .http .client .methods .HttpPost ;
@@ -141,15 +142,28 @@ public OpenCliResult collect(OpenCliCollectRequest request, long submittedAtNano
141142 try (CloseableHttpResponse response = client .execute (post )) {
142143 int status = response .getStatusLine ().getStatusCode ();
143144 observedStatus = status ;
144- Optional <String > body = readBoundedBody (response .getEntity (), maxResponseBytes );
145+ HttpEntity entity = response .getEntity ();
146+ Optional <byte []> body = readBoundedBody (entity , maxResponseBytes );
145147 if (post .isAborted () || deadline - System .nanoTime () <= 0 ) {
146148 throw new OpenCliRemoteTimeoutException (timeout , true , correlationId );
147149 }
148- String respBody = body .orElseThrow (
150+ byte [] responseBytes = body .orElseThrow (
149151 () -> new OpenCliRemoteResponseLimitException (status , maxResponseBytes , correlationId ));
150- if (status < 200 || status >= 300 ) {
151- log .warn ("Agent HTTP non-2xx correlationId={} status={} responseChars={}" ,
152- correlationId , status , respBody .length ());
152+ boolean successfulStatus = status >= 200 && status < 300 ;
153+ String respBody ;
154+ try {
155+ respBody = decodeBody (entity , responseBytes );
156+ } catch (IllegalArgumentException | ParseException e ) {
157+ log .warn ("Agent content type invalid correlationId={} status={}" , correlationId , status );
158+ if (successfulStatus ) {
159+ throw new OpenCliRemoteProtocolException ("Invalid agent response content type" , correlationId ,
160+ failureSnapshot (status , Outcome .PROTOCOL_FAILURE , null ));
161+ }
162+ respBody = null ;
163+ }
164+ if (!successfulStatus ) {
165+ log .warn ("Agent HTTP non-2xx correlationId={} status={} responseBytes={}" ,
166+ correlationId , status , responseBytes .length );
153167 throw new OpenCliExecutableFailureException ("Agent HTTP " + status + " correlationId=" + correlationId ,
154168 null , failureSnapshot (status , Outcome .HTTP_FAILURE , respBody ));
155169 }
@@ -225,9 +239,9 @@ public void close() {
225239 }
226240 }
227241
228- private static Optional <String > readBoundedBody (HttpEntity entity , int limit ) throws IOException {
242+ private static Optional <byte [] > readBoundedBody (HttpEntity entity , int limit ) throws IOException {
229243 if (Objects .isNull (entity )) {
230- return Optional .of ("" );
244+ return Optional .of (new byte [ 0 ] );
231245 }
232246 // The response owns the stream. Closing the stream itself can drain an oversized entity.
233247 InputStream input = entity .getContent ();
@@ -245,9 +259,13 @@ private static Optional<String> readBoundedBody(HttpEntity entity, int limit) th
245259 }
246260 body .write (buffer , 0 , read );
247261 }
262+ return Optional .of (body .toByteArray ());
263+ }
264+
265+ private static String decodeBody (HttpEntity entity , byte [] body ) {
248266 ContentType type = ContentType .get (entity );
249267 Charset charset = Objects .nonNull (type ) ? type .getCharset () : null ;
250- return Optional . of (body . toString ( Objects .nonNull (charset ) ? charset . name () : StandardCharsets .UTF_8 . name ()) );
268+ return new String (body , Objects .nonNull (charset ) ? charset : StandardCharsets .UTF_8 );
251269 }
252270
253271 private int resolveTimeoutMillis () {
0 commit comments