4040import org .springframework .core .io .buffer .DataBuffer ;
4141import org .springframework .core .io .buffer .DataBufferUtils ;
4242import org .springframework .http .HttpHeaders ;
43+ import org .springframework .http .HttpStatus ;
4344import org .springframework .http .codec .HttpMessageReader ;
4445import org .springframework .http .server .reactive .ServerHttpResponseDecorator ;
4546import org .springframework .lang .NonNull ;
5859import java .util .List ;
5960import java .util .Objects ;
6061import java .util .Optional ;
62+ import java .util .regex .Matcher ;
6163import java .util .regex .Pattern ;
6264import java .util .stream .Stream ;
6365import java .util .zip .GZIPInputStream ;
@@ -76,6 +78,8 @@ public class AiResponseTransformerPlugin extends AbstractShenyuPlugin {
7678
7779 private static final Pattern REQUEST_LINE_PATTERN = Pattern .compile ("^(GET|POST|PUT|DELETE|PATCH|OPTIONS|HEAD)\\ s.*\\ sHTTP/1.1$" );
7880
81+ private static final Pattern STATUS_LINE_PATTERN = Pattern .compile ("^HTTP/1\\ .[01]\\ s+(\\ d{3})(?:\\ s+.*)?$" );
82+
7983 private static final ObjectMapper MAPPER = new ObjectMapper ();
8084
8185 private final List <HttpMessageReader <?>> messageReaders ;
@@ -168,32 +172,53 @@ public static HttpHeaders extractHeadersFromAiResponse(final String aiResponse)
168172 return AiResponseParser .extractHeadersFromAiResponse (aiResponse );
169173 }
170174
175+ /**
176+ * For unit test.
177+ *
178+ * @param aiResponse the AI response
179+ * @return the extracted status code, or {@code null} when the response has no usable status line
180+ */
181+ public static Integer extractStatusFromAiResponse (final String aiResponse ) {
182+ return AiResponseParser .extractStatusFromAiResponse (aiResponse );
183+ }
184+
171185 /**
172186 * Utility class for parsing AI responses.
173187 */
174188 private static class AiResponseParser {
175-
189+
176190 /**
177- * Extract body from AI response .
191+ * Remove the surrounding markdown code block markers, if any .
178192 *
179193 * @param aiResponse the AI response
180- * @return the extracted body
194+ * @return the response without code block markers
181195 */
182- static String extractBodyFromAiResponse (final String aiResponse ) {
183- if (Objects .isNull (aiResponse ) || aiResponse .isEmpty ()) {
184- return null ;
185- }
186-
196+ static String stripCodeBlockMarkers (final String aiResponse ) {
187197 String cleanedResponse = aiResponse ;
188198 if (cleanedResponse .contains ("```" )) {
189199 int startIndex = cleanedResponse .indexOf ("```" );
190200 int endIndex = cleanedResponse .lastIndexOf ("```" );
191-
201+
192202 if (startIndex != -1 && endIndex != -1 && endIndex > startIndex ) {
193203 cleanedResponse = cleanedResponse .substring (startIndex + 3 , endIndex ).trim ();
194204 LOG .debug ("Removed code block markers, cleaned response: {}" , cleanedResponse );
195205 }
196206 }
207+ return cleanedResponse ;
208+ }
209+
210+ /**
211+ * Extract body from AI response.
212+ *
213+ * @param aiResponse the AI response
214+ * @return the extracted body
215+ */
216+ static String extractBodyFromAiResponse (final String aiResponse ) {
217+ if (Objects .isNull (aiResponse ) || aiResponse .isEmpty ()) {
218+ return null ;
219+ }
220+
221+ String cleanedResponse = stripCodeBlockMarkers (aiResponse );
197222
198223 if (cleanedResponse .startsWith ("{" ) && cleanedResponse .endsWith ("}" )
199224 || cleanedResponse .startsWith ("[" ) && cleanedResponse .endsWith ("]" )) {
@@ -249,23 +274,14 @@ static HttpHeaders extractHeadersFromAiResponse(final String aiResponse) {
249274 return headers ;
250275 }
251276
252- String cleanedResponse = aiResponse ;
253- if (cleanedResponse .contains ("```" )) {
254- int startIndex = cleanedResponse .indexOf ("```" );
255- int endIndex = cleanedResponse .lastIndexOf ("```" );
256-
257- if (startIndex != -1 && endIndex != -1 && endIndex > startIndex ) {
258- cleanedResponse = cleanedResponse .substring (startIndex + 3 , endIndex ).trim ();
259- LOG .debug ("Removed code block markers for header extraction, cleaned response: {}" , cleanedResponse );
260- }
261- }
277+ String cleanedResponse = stripCodeBlockMarkers (aiResponse );
262278
263279 try (BufferedReader reader = new BufferedReader (new StringReader (cleanedResponse ))) {
264280 String line ;
265281 boolean headerSectionStarted = false ;
266282 while (Objects .nonNull (line = reader .readLine ())) {
267283 if (!headerSectionStarted ) {
268- if (line .startsWith ( "HTTP/1.1" ) || REQUEST_LINE_PATTERN .matcher (line ).matches ()) {
284+ if (STATUS_LINE_PATTERN . matcher ( line .trim ()). matches ( ) || REQUEST_LINE_PATTERN .matcher (line . trim () ).matches ()) {
269285 headerSectionStarted = true ;
270286 continue ;
271287 }
@@ -286,6 +302,44 @@ static HttpHeaders extractHeadersFromAiResponse(final String aiResponse) {
286302 }
287303 return headers ;
288304 }
305+
306+ /**
307+ * Extract the status code from the status line of an AI response.
308+ * The status line is only accepted as the first non-empty line, so a status-looking
309+ * header value further down cannot change the response status.
310+ *
311+ * @param aiResponse the AI response
312+ * @return the extracted status code, or {@code null} when there is no valid status line
313+ */
314+ static Integer extractStatusFromAiResponse (final String aiResponse ) {
315+ if (Objects .isNull (aiResponse ) || aiResponse .isEmpty ()) {
316+ return null ;
317+ }
318+
319+ String cleanedResponse = stripCodeBlockMarkers (aiResponse );
320+ try (BufferedReader reader = new BufferedReader (new StringReader (cleanedResponse ))) {
321+ String line ;
322+ while (Objects .nonNull (line = reader .readLine ())) {
323+ if (line .trim ().isEmpty ()) {
324+ continue ;
325+ }
326+ Matcher matcher = STATUS_LINE_PATTERN .matcher (line .trim ());
327+ if (!matcher .matches ()) {
328+ return null ;
329+ }
330+ int statusCode = Integer .parseInt (matcher .group (1 ));
331+ // HttpStatus resolves only codes defined by the HTTP specification.
332+ if (Objects .isNull (HttpStatus .resolve (statusCode ))) {
333+ LOG .warn ("AI response contains an invalid HTTP status code: {}" , statusCode );
334+ return null ;
335+ }
336+ return statusCode ;
337+ }
338+ } catch (IOException | NumberFormatException e ) {
339+ LOG .warn ("AI response transformer plugin: extract status from AiResponse fail" );
340+ }
341+ return null ;
342+ }
289343 }
290344
291345 static class AiResponseTransformerDecorator extends ServerHttpResponseDecorator {
@@ -358,21 +412,7 @@ public Mono<Void> writeWith(@NonNull final Publisher<? extends DataBuffer> body)
358412 .filter (Objects ::nonNull )
359413 .collect (StringBuilder ::new , StringBuilder ::append )
360414 .map (StringBuilder ::toString )
361- .flatMap (aiResponse -> {
362-
363- HttpHeaders newHeaders = extractHeadersFromAiResponse (aiResponse );
364- String newBody = extractBodyFromAiResponse (aiResponse );
365-
366- if (Objects .nonNull (newBody ) && !newBody .isEmpty ()) {
367- this .getHeaders ().clear ();
368- this .getHeaders ().putAll (newHeaders );
369- LOG .debug ("Returning transformed response body: {}" , newBody );
370- return WebFluxResultUtils .result (this .exchange , newBody .getBytes (StandardCharsets .UTF_8 ));
371- } else {
372- LOG .warn ("response body is empty or null, returning original response: {}" , finalResponseBody );
373- return WebFluxResultUtils .result (this .exchange , finalResponseBody .getBytes (StandardCharsets .UTF_8 ));
374- }
375- })
415+ .flatMap (aiResponse -> applyTransformedResponse (aiResponse , finalResponseBody ))
376416 .onErrorResume (throwable -> {
377417 LOG .error ("response transformation failed" , throwable );
378418 LOG .info ("Returning original response due to error: {}" , finalResponseBody );
@@ -389,6 +429,36 @@ public Mono<Void> writeAndFlushWith(@NonNull final Publisher<? extends Publisher
389429 return writeWith (Flux .from (body ).concatMap (Flux ::from ));
390430 }
391431
432+ private Mono <Void > applyTransformedResponse (final String aiResponse , final String originalResponseBody ) {
433+ HttpHeaders newHeaders = extractHeadersFromAiResponse (aiResponse );
434+ String newBody = extractBodyFromAiResponse (aiResponse );
435+ Integer newStatus = extractStatusFromAiResponse (aiResponse );
436+
437+ if (isBodylessStatus (newStatus )) {
438+ // 204/304 must not carry a body, so the generated status and headers are applied as-is
439+ this .getHeaders ().clear ();
440+ this .getHeaders ().putAll (newHeaders );
441+ this .getHeaders ().remove (HttpHeaders .CONTENT_TYPE );
442+ this .getHeaders ().remove (HttpHeaders .CONTENT_LENGTH );
443+ this .getHeaders ().remove (HttpHeaders .TRANSFER_ENCODING );
444+ this .setStatusCode (HttpStatus .valueOf (newStatus ));
445+ LOG .debug ("Returning transformed bodyless response with status: {}" , newStatus );
446+ return super .writeWith (Flux .empty ());
447+ }
448+ if (Objects .nonNull (newBody ) && !newBody .isEmpty ()) {
449+ this .getHeaders ().clear ();
450+ this .getHeaders ().putAll (newHeaders );
451+ if (Objects .nonNull (newStatus )) {
452+ this .setStatusCode (HttpStatus .valueOf (newStatus ));
453+ }
454+ LOG .debug ("Returning transformed response body: {}" , newBody );
455+ return WebFluxResultUtils .result (this .exchange , newBody .getBytes (StandardCharsets .UTF_8 ));
456+ }
457+ // the generated response has no usable body, so the original response (and its status) is kept
458+ LOG .warn ("response body is empty or null, returning original response: {}" , originalResponseBody );
459+ return WebFluxResultUtils .result (this .exchange , originalResponseBody .getBytes (StandardCharsets .UTF_8 ));
460+ }
461+
392462 private String extractBodyFromAiResponse (final String aiResponse ) {
393463 if (Objects .isNull (aiResponse ) || aiResponse .isEmpty ()) {
394464 LOG .warn ("response is null or empty" );
@@ -404,6 +474,14 @@ private String extractBodyFromAiResponse(final String aiResponse) {
404474 return result ;
405475 }
406476
477+ private static boolean isBodylessStatus (final Integer statusCode ) {
478+ if (Objects .isNull (statusCode )) {
479+ return false ;
480+ }
481+ HttpStatus status = HttpStatus .resolve (statusCode );
482+ return HttpStatus .NO_CONTENT .equals (status ) || HttpStatus .NOT_MODIFIED .equals (status );
483+ }
484+
407485 private HttpHeaders extractHeadersFromAiResponse (final String aiResponse ) {
408486 if (Objects .isNull (aiResponse ) || aiResponse .isEmpty ()) {
409487 LOG .warn ("AI response is null or empty for header extraction" );
0 commit comments