Skip to content

Commit

Permalink
Funqy - Read the body using the Vert.x body handler instead of its ow…
Browse files Browse the repository at this point in the history
…n handler.

As the body may have already been read.
  • Loading branch information
cescoffier authored and gsmet committed Jan 5, 2024
1 parent df01080 commit e2ae818
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.quarkus.funqy.runtime.bindings.http.FunqyHttpBindingRecorder;
import io.quarkus.jackson.runtime.ObjectMapperProducer;
import io.quarkus.vertx.core.deployment.CoreVertxBuildItem;
import io.quarkus.vertx.http.deployment.RequireBodyHandlerBuildItem;
import io.quarkus.vertx.http.deployment.RouteBuildItem;
import io.quarkus.vertx.http.runtime.HttpBuildTimeConfig;
import io.vertx.core.Handler;
Expand All @@ -40,6 +41,15 @@ public void markObjectMapper(BuildProducer<UnremovableBeanBuildItem> unremovable
new UnremovableBeanBuildItem.BeanClassNameExclusion(ObjectMapperProducer.class.getName())));
}

@BuildStep
public RequireBodyHandlerBuildItem requestBodyHandler(List<FunctionBuildItem> functions) {
if (functions.isEmpty()) {
return null;
}
// Require the body handler if there are functions as they may require the HTTP body
return new RequireBodyHandlerBuildItem();
}

@BuildStep()
@Record(STATIC_INIT)
public void staticInit(FunqyHttpBindingRecorder binding,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,25 @@ public void handle(RoutingContext routingContext) {
dispatch(routingContext, invoker, finalInput);
});
} else if (routingContext.request().method() == HttpMethod.POST) {
routingContext.request().bodyHandler(buff -> {
Object input = null;
if (buff.length() > 0) {
ByteBufInputStream in = new ByteBufInputStream(buff.getByteBuf());
ObjectReader reader = (ObjectReader) invoker.getBindingContext().get(ObjectReader.class.getName());
try {
input = reader.readValue((InputStream) in);
} catch (Exception e) {
log.error("Failed to unmarshal input", e);
routingContext.fail(400);
return;
}
var buff = routingContext.getBody();
Object input = null;
if (buff != null && buff.length() > 0) {
ByteBufInputStream in = new ByteBufInputStream(buff.getByteBuf());
ObjectReader reader = (ObjectReader) invoker.getBindingContext().get(ObjectReader.class.getName());
try {
input = reader.readValue((InputStream) in);
} catch (Exception e) {
log.error("Failed to unmarshal input", e);
routingContext.fail(400);
return;
}
}
Object finalInput = input;
executor.execute(new Runnable() {
@Override
public void run() {
VertxRequestHandler.this.dispatch(routingContext, invoker, finalInput);
}
Object finalInput = input;
executor.execute(() -> {
dispatch(routingContext, invoker, finalInput);
});
});
} else {
routingContext.fail(405);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1291,10 +1291,10 @@ public void initChannel(VirtualChannel ch) throws Exception {

Http1xServerConnection conn = new Http1xServerConnection(
() -> {
ContextInternal internal = (ContextInternal) VertxContext
ContextInternal duplicated = (ContextInternal) VertxContext
.getOrCreateDuplicatedContext(rootContext);
setContextSafe(internal, true);
return internal;
setContextSafe(duplicated, true);
return duplicated;
},
null,
new HttpServerOptions(),
Expand Down

0 comments on commit e2ae818

Please sign in to comment.