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
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
@@ -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
@@ -0,0 +1,236 @@
/*
* Copyright 2017-2023 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.servlet.http;

import io.micronaut.core.annotation.Experimental;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.io.buffer.ByteBuffer;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Arrays;

/**
* A {@link ByteBuffer} implementation that is backed by a byte array.
*
* @param <T>
*/
@Internal
@Experimental
public class ByteArrayByteBuffer<T> implements ByteBuffer<T> {

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

/**
* Construct a new {@link ByteArrayByteBuffer} for the given bytes.
* @param underlyingBytes the bytes to wrap
*/
public ByteArrayByteBuffer(byte[] underlyingBytes) {
this(underlyingBytes, underlyingBytes.length);
}

/**
* Construct a new {@link ByteArrayByteBuffer} for the given bytes and capacity.
* If capacity is greater than the length of the underlyingBytes, extra bytes will be zeroed out.
* If capacity is less than the length of the underlyingBytes, the underlyingBytes will be truncated.
*
* @param underlyingBytes the bytes to wrap
* @param capacity the capacity of the buffer
*/
public ByteArrayByteBuffer(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 ByteArrayByteBuffer<>(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 ByteArrayByteBuffer<>(Arrays.copyOfRange(underlyingBytes, index, index + 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];
}
}
Original file line number Diff line number Diff line change
@@ -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));
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package io.micronaut.servlet.http

import spock.lang.Specification

import java.nio.charset.StandardCharsets

class ByteArrayByteBufferSpec extends Specification {

void 'test creating a buffer'() {
given:
def bytes = 'abcdefghij'.bytes
def buffer = new ByteArrayByteBuffer(bytes)

expect:
buffer.toByteArray() == bytes
buffer.maxCapacity() == 10

buffer.readableBytes() == 10
buffer.readerIndex() == 0

buffer.writableBytes() == 10
buffer.writerIndex() == 0
}

void 'test copying a buffer'() {
given:
def bytes = 'abcdefghij'.bytes
def buffer = new ByteArrayByteBuffer(bytes)

when:
def sliced = buffer.slice(1, 5)

then:
sliced.toByteArray() == 'bcdef'.bytes

when:
def expanded = new ByteArrayByteBuffer(buffer.toByteArray(), 20)

then:
expanded.readerIndex() == 0
expanded.readableBytes() == 20
expanded.toByteArray() == 'abcdefghij\0\0\0\0\0\0\0\0\0\0'.bytes

when:
def shrunk = new ByteArrayByteBuffer(buffer.toByteArray(), 5)

then:
shrunk.readerIndex() == 0
shrunk.readableBytes() == 5
shrunk.toByteArray() == 'abcde'.bytes
}

void 'test inputstream creation'() {
given:
def bytes = 'abcdefghij'.bytes
def buffer = new ByteArrayByteBuffer(bytes)

when:
def stream = buffer.toInputStream()

then:
stream.readAllBytes() == bytes
}

void 'test writing'() {
given:
def bytes = 'abcdefghij'.bytes
def buffer = new ByteArrayByteBuffer(bytes)

when:
buffer.write('12345'.bytes)

then:
buffer.toByteArray() == '12345fghij'.bytes
buffer.readerIndex() == 0
buffer.readableBytes() == 10

buffer.writerIndex() == 5
buffer.writableBytes() == 5

when:
buffer = new ByteArrayByteBuffer('abcdefghij'.bytes)
buffer.writerIndex(5)
buffer.write('T', StandardCharsets.UTF_8)

then:
buffer.toByteArray() == 'abcdeTghij'.bytes
buffer.readerIndex() == 0
buffer.readableBytes() == 10
buffer.writerIndex() == 6
buffer.writableBytes() == 4
}

void 'test reading'() {
given:
def bytes = 'abcdefghij'.bytes
def buffer = new ByteArrayByteBuffer(bytes)
def target = new byte[5]

when:
buffer.read(target)

then:
target == 'abcde'.bytes
buffer.readerIndex() == 5
buffer.readableBytes() == 5

buffer.writerIndex() == 0
buffer.writableBytes() == 10

when:
target = new byte[5]
buffer = new ByteArrayByteBuffer('abcdefghij'.bytes)
buffer.readerIndex(5)
buffer.read(target, 3, 5)

then:
target == '\0\0\0fg'.bytes
buffer.writerIndex() == 0
buffer.writableBytes() == 10

and: 'we only read 2 elements'
buffer.readerIndex() == 7
buffer.readableBytes() == 3
}
}
Original file line number Diff line number Diff line change
@@ -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;
@@ -36,6 +39,7 @@
import io.micronaut.http.codec.MediaTypeCodecRegistry;
import io.micronaut.http.cookie.Cookies;
import io.micronaut.servlet.http.BodyBuilder;
import io.micronaut.servlet.http.ByteArrayByteBuffer;
import io.micronaut.servlet.http.ServletExchange;
import io.micronaut.servlet.http.ServletHttpRequest;
import io.micronaut.servlet.http.ServletHttpResponse;
@@ -84,7 +88,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;
@@ -99,6 +105,7 @@ public final class DefaultServletHttpRequest<B> extends MutableConvertibleValues
private Supplier<Optional<B>> body;

private boolean bodyIsReadAsync;
private ByteArrayByteBuffer<B> servletByteBuffer;

/**
* Default constructor.
@@ -249,7 +256,7 @@ public String getContextPath() {

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

@Override
@@ -420,6 +427,38 @@ 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 {
if (servletByteBuffer == null) {
this.servletByteBuffer = new ByteArrayByteBuffer<>(delegate.getInputStream().readAllBytes());
}
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);
}

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

include 'servlet-bom'
Original file line number Diff line number Diff line change
@@ -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
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -14,7 +14,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 UndertowHttpServerTestSuite {
}