Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -65,6 +66,10 @@ public class AiRequestTransformerPlugin extends AbstractShenyuPlugin {

private static final Logger LOG = LoggerFactory.getLogger(AiRequestTransformerPlugin.class);

private static final Pattern REQUEST_LINE_PATTERN = Pattern.compile("^(GET|POST|PUT|DELETE|PATCH|OPTIONS|HEAD)\\s.*\\sHTTP/1.1$");

private static final Pattern REQUEST_PATH_PATTERN = Pattern.compile("^/[a-zA-Z0-9/_\\-]*$");

private final List<HttpMessageReader<?>> messageReaders;

private final AiModelFactoryRegistry aiModelFactoryRegistry;
Expand Down Expand Up @@ -247,7 +252,7 @@ private static Mono<ServerWebExchange> rewriteRequestPath(final ServerWebExchang
return Mono.just(exchange);
}

if (!newPath.matches("^/[a-zA-Z0-9/_\\-]*$")) {
if (!REQUEST_PATH_PATTERN.matcher(newPath).matches()) {
LOG.warn("Extracted path contains invalid characters: {}, Will continue to use the original path.", newPath);
return Mono.just(exchange);
}
Expand Down Expand Up @@ -283,7 +288,7 @@ static HttpHeaders extractHeadersFromAiResponse(final String aiResponse) {
boolean headerSectionStarted = false;
while (Objects.nonNull(line = reader.readLine())) {
if (!headerSectionStarted) {
if (line.startsWith("HTTP/1.1") || line.matches("^(GET|POST|PUT|DELETE|PATCH|OPTIONS|HEAD)\\s.*\\sHTTP/1.1$")) {
if (line.startsWith("HTTP/1.1") || REQUEST_LINE_PATTERN.matcher(line).matches()) {
headerSectionStarted = true;
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.GZIPInputStream;
Expand All @@ -72,6 +73,8 @@ public class AiResponseTransformerPlugin extends AbstractShenyuPlugin {

private static final Logger LOG = LoggerFactory.getLogger(AiResponseTransformerPlugin.class);

private static final Pattern REQUEST_LINE_PATTERN = Pattern.compile("^(GET|POST|PUT|DELETE|PATCH|OPTIONS|HEAD)\\s.*\\sHTTP/1.1$");

private final List<HttpMessageReader<?>> messageReaders;

private final AiModelFactoryRegistry aiModelFactoryRegistry;
Expand Down Expand Up @@ -259,7 +262,7 @@ static HttpHeaders extractHeadersFromAiResponse(final String aiResponse) {
boolean headerSectionStarted = false;
while (Objects.nonNull(line = reader.readLine())) {
if (!headerSectionStarted) {
if (line.startsWith("HTTP/1.1") || line.matches("^(GET|POST|PUT|DELETE|PATCH|OPTIONS|HEAD)\\s.*\\sHTTP/1.1$")) {
if (line.startsWith("HTTP/1.1") || REQUEST_LINE_PATTERN.matcher(line).matches()) {
headerSectionStarted = true;
continue;
}
Expand Down
Loading