Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: Attempt to get FullHttpRequest support in Servlet requests #471

Merged
merged 5 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
@@ -0,0 +1,74 @@
package io.micronaut.servlet.jetty.filters

import io.micronaut.context.annotation.Property
import io.micronaut.context.annotation.Requires
import io.micronaut.http.HttpRequest
import io.micronaut.http.HttpResponse
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Body
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Header
import io.micronaut.http.annotation.Post
import io.micronaut.http.annotation.RequestFilter
import io.micronaut.http.annotation.ServerFilter
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.http.client.exceptions.HttpClientResponseException
import io.micronaut.http.filter.FilterContinuation
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import jakarta.inject.Singleton
import spock.lang.Specification

import java.nio.charset.StandardCharsets

@MicronautTest
@Property(name= "spec.name", value = RequestFilterBodySpec.SPEC_NAME)
class RequestFilterBodySpec extends Specification {

static final String SPEC_NAME = "RequestFilterBodySpec"

@Inject
@Client("/")
HttpClient client

@Inject
MyServerFilter filter

void 'test body access'() {
when:
def post = HttpRequest.POST("/request-filter/binding", "{\"foo\":10}").contentType(MediaType.APPLICATION_JSON_TYPE)
def response = client.toBlocking().retrieve(post)

then:
filter.events == ['binding application/json {"foo":10}']
response == 'application/json {"foo":10}'
}

@ServerFilter
@Singleton
@Requires(property = "spec.name", value = RequestFilterBodySpec.SPEC_NAME)
static class MyServerFilter {

List<String> events = []

@RequestFilter("/request-filter/binding")
void requestFilterBinding(
@Header String contentType,
@Body byte[] bytes,
FilterContinuation<HttpResponse<?>> continuation) {
events.add("binding " + contentType + " " + new String(bytes, StandardCharsets.UTF_8))
continuation.proceed()
}
}

@Controller
@Requires(property = "spec.name", value = SPEC_NAME)
static class MyController {

@Post("/request-filter/binding")
String requestFilterBinding(@Header String contentType, @Body byte[] bytes) {
contentType + " " + new String(bytes, StandardCharsets.UTF_8)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ public List<ConversionError> getConversionErrors() {
final T converted = conversionService.convertRequired(publisher, type);
return () -> Optional.of(converted);
}
if (type.isArray()) {
if (type.isAssignableFrom(byte[].class)) {
byte[] content = inputStream.readAllBytes();
return () -> Optional.of((T) content);
} else if (type.isArray()) {
Class<?> componentType = type.getComponentType();
List<T> content = (List<T>) codec.decode(Argument.listOf(componentType), inputStream);
Object[] array = content.toArray((Object[]) Array.newInstance(componentType, 0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.convert.value.MutableConvertibleValues;
import io.micronaut.core.convert.value.MutableConvertibleValuesMap;
import io.micronaut.core.execution.ExecutionFlow;
import io.micronaut.core.io.buffer.ByteBuffer;
import io.micronaut.core.type.Argument;
import io.micronaut.core.util.ArrayUtils;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.core.util.StringUtils;
import io.micronaut.core.util.SupplierUtil;
import io.micronaut.http.FullHttpRequest;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.HttpMethod;
import io.micronaut.http.HttpParameters;
Expand All @@ -52,8 +55,10 @@
import reactor.core.publisher.Sinks;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -84,7 +89,9 @@ public final class DefaultServletHttpRequest<B> extends MutableConvertibleValues
ServletHttpRequest<HttpServletRequest, B>,
MutableConvertibleValues<Object>,
ServletExchange<HttpServletRequest, HttpServletResponse>,
StreamedServletMessage<B, byte[]> {
StreamedServletMessage<B, byte[]>,
FullHttpRequest<B> {

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

private final ConversionService conversionService;
Expand All @@ -99,6 +106,7 @@ public final class DefaultServletHttpRequest<B> extends MutableConvertibleValues
private Supplier<Optional<B>> body;

private boolean bodyIsReadAsync;
private ServletByteBuffer<B> servletByteBuffer;

/**
* Default constructor.
Expand Down Expand Up @@ -249,7 +257,7 @@ public String getContextPath() {

@Override
public InputStream getInputStream() throws IOException {
return delegate.getInputStream();
return servletByteBuffer != null ? servletByteBuffer.toInputStream() : delegate.getInputStream();
}

@Override
Expand Down Expand Up @@ -420,6 +428,227 @@ public void onError(Throwable t) {
bodyContent.subscribe(s);
}

@Override
public boolean isFull() {
return !bodyIsReadAsync;
}

@Override
public ByteBuffer<?> contents() {
if (bodyIsReadAsync) {
if (LOG.isDebugEnabled()) {
LOG.debug("Body is read asynchronously, cannot get contents");
}
return null;
}
try {
this.servletByteBuffer = new ServletByteBuffer<>(getInputStream().readAllBytes());
timyates marked this conversation as resolved.
Show resolved Hide resolved
return servletByteBuffer;
} catch (IOException e) {
throw new IllegalStateException("Error getting all body contents", e);
}
}

@Override
public ExecutionFlow<ByteBuffer<?>> bufferContents() {
ByteBuffer<?> contents = contents();
if (contents == null) {
return null;
}
return ExecutionFlow.just(contents);
}

private final class ServletByteBuffer<T> implements ByteBuffer<T> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not we move this class to servlet-core so that we are able to use it in the serverless implementations?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe extract it into its own file and annotate it with @Experimental

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 1bb046f

Which isn't showing up in the PR for unknown reasons... github status is lying I suspect


private final byte[] underlyingBytes;
private int readerIndex;
private int writerIndex;

private ServletByteBuffer(byte[] underlyingBytes) {
this(underlyingBytes, underlyingBytes.length);
}

private ServletByteBuffer(byte[] underlyingBytes, int capacity) {
if (capacity < underlyingBytes.length) {
this.underlyingBytes = Arrays.copyOf(underlyingBytes, capacity);
} else if (capacity > underlyingBytes.length) {
this.underlyingBytes = new byte[capacity];
System.arraycopy(underlyingBytes, 0, this.underlyingBytes, 0, underlyingBytes.length);
} else {
this.underlyingBytes = underlyingBytes;
}
}

@Override
public T asNativeBuffer() {
throw new IllegalStateException("Not supported");
}

@Override
public int readableBytes() {
return underlyingBytes.length - readerIndex;
}

@Override
public int writableBytes() {
return underlyingBytes.length - writerIndex;
}

@Override
public int maxCapacity() {
return underlyingBytes.length;
}

@Override
public ByteBuffer capacity(int capacity) {
return new ServletByteBuffer<>(underlyingBytes, capacity);
}

@Override
public int readerIndex() {
return readerIndex;
}

@Override
public ByteBuffer readerIndex(int readPosition) {
this.readerIndex = Math.min(readPosition, underlyingBytes.length - 1);
return this;
}

@Override
public int writerIndex() {
return writerIndex;
}

@Override
public ByteBuffer writerIndex(int position) {
this.writerIndex = Math.min(position, underlyingBytes.length - 1);
return this;
}

@Override
public byte read() {
return underlyingBytes[readerIndex++];
}

@Override
public CharSequence readCharSequence(int length, Charset charset) {
String s = new String(underlyingBytes, readerIndex, length, charset);
readerIndex += length;
return s;
}

@Override
public ByteBuffer read(byte[] destination) {
int count = Math.min(readableBytes(), destination.length);
System.arraycopy(underlyingBytes, readerIndex, destination, 0, count);
readerIndex += count;
return this;
}

@Override
public ByteBuffer read(byte[] destination, int offset, int length) {
int count = Math.min(readableBytes(), Math.min(destination.length - offset, length));
System.arraycopy(underlyingBytes, readerIndex, destination, offset, count);
readerIndex += count;
return this;
}

@Override
public ByteBuffer write(byte b) {
underlyingBytes[writerIndex++] = b;
return this;
}

@Override
public ByteBuffer write(byte[] source) {
int count = Math.min(writableBytes(), source.length);
System.arraycopy(source, 0, underlyingBytes, writerIndex, count);
writerIndex += count;
return this;
}

@Override
public ByteBuffer write(CharSequence source, Charset charset) {
write(source.toString().getBytes(charset));
return this;
}

@Override
public ByteBuffer write(byte[] source, int offset, int length) {
int count = Math.min(writableBytes(), length);
System.arraycopy(source, offset, underlyingBytes, writerIndex, count);
writerIndex += count;
return this;
}

@Override
public ByteBuffer write(ByteBuffer... buffers) {
for (ByteBuffer<?> buffer : buffers) {
write(buffer.toByteArray());
}
return this;
}

@Override
public ByteBuffer write(java.nio.ByteBuffer... buffers) {
for (java.nio.ByteBuffer buffer : buffers) {
write(buffer.array());
}
return this;
}

@Override
public ByteBuffer slice(int index, int length) {
return new ServletByteBuffer<>(Arrays.copyOf(underlyingBytes, length), length);
}

@Override
public java.nio.ByteBuffer asNioBuffer() {
return java.nio.ByteBuffer.wrap(underlyingBytes, readerIndex, readableBytes());
}

@Override
public java.nio.ByteBuffer asNioBuffer(int index, int length) {
return java.nio.ByteBuffer.wrap(underlyingBytes, index, length);
}

@Override
public InputStream toInputStream() {
return new ByteArrayInputStream(underlyingBytes, readerIndex, readableBytes());
}

@Override
public OutputStream toOutputStream() {
throw new IllegalStateException("Not implemented");
}

@Override
public byte[] toByteArray() {
return Arrays.copyOfRange(underlyingBytes, readerIndex, readableBytes());
}

@Override
public String toString(Charset charset) {
return new String(underlyingBytes, readerIndex, readableBytes(), charset);
}

@Override
public int indexOf(byte b) {
for (int i = readerIndex; i < underlyingBytes.length; i++) {
if (underlyingBytes[i] == b) {
return i;
}
}
return -1;
}

@Override
public byte getByte(int index) {
return underlyingBytes[index];
}
}

/**
* The servlet request headers.
*/
Expand Down
1 change: 0 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ micronautBuild {
importMicronautCatalog("micronaut-serde")
importMicronautCatalog("micronaut-session")
importMicronautCatalog("micronaut-validation")
importMicronautCatalog("micronaut-logging")
}

include 'servlet-bom'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"io.micronaut.http.server.tck.tests.filter.ClientResponseFilterTest", // responseFilterThrowableParameter fails under Graal https://ge.micronaut.io/s/ufuhtbe5sgmxi
"io.micronaut.http.server.tck.tests.codec.JsonCodecAdditionalTypeTest", // remove once this pr is merged https://github.com/micronaut-projects/micronaut-core/pull/9308/files
"io.micronaut.http.server.tck.tests.constraintshandler.ControllerConstraintHandlerTest", // Bug accessing request body in @OnError
"io.micronaut.http.server.tck.tests.filter.RequestFilterTest", // Temporary disable tests until we get requestFilterBinding separated in the TCK (as it's testing anetty feature)
})
public class JettyHttpServerTestSuite {
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"io.micronaut.http.server.tck.tests.filter.ClientResponseFilterTest", // responseFilterThrowableParameter fails under Graal https://ge.micronaut.io/s/ufuhtbe5sgmxi
"io.micronaut.http.server.tck.tests.codec.JsonCodecAdditionalTypeTest", // remove once this pr is merged https://github.com/micronaut-projects/micronaut-core/pull/9308/files
"io.micronaut.http.server.tck.tests.constraintshandler.ControllerConstraintHandlerTest", // Bug accessing request body in @OnError
"io.micronaut.http.server.tck.tests.filter.RequestFilterTest", // Temporary disable tests until we get requestFilterBinding separated in the TCK (as it's testing anetty feature)
})
public class TomcatHttpServerTestSuite {
}
Loading