use of io.netty.handler.codec.http.multipart.HttpPostRequestEncoder in project esa-restclient by esastack.
the class MultipartWriterTest method testWriteAndFlushHttp1.
// //////*********************************HTTP1 MULTIPART WRITER**************************************////////
@Test
void testWriteAndFlushHttp1() throws IOException {
final MultipartWriter writer = MultipartWriter.singleton();
final EmbeddedChannel channel = new EmbeddedChannel();
final File file = File.createTempFile("httpclient-", ".tmp");
file.deleteOnExit();
try {
try (FileOutputStream out = new FileOutputStream(file)) {
final byte[] data = new byte[4 * 1024 * 1024];
ThreadLocalRandom.current().nextBytes(data);
out.write(data);
}
final io.esastack.httpclient.core.MultipartRequest request = client.post("http://127.0.0.1/abc").multipart().file("file", file).attr("key1", "value1");
final ExecContext ctx = ExecContextUtil.newAs();
final ChannelFuture end = writer.writeAndFlush(request, channel, ctx, channel.newPromise(), false, HttpVersion.HTTP_1_1, false);
channel.flush();
HttpRequest req = channel.readOutbound();
then(req.method()).isSameAs(HttpMethod.POST);
then(req.headers().get(io.esastack.commons.net.http.HttpHeaderNames.CONTENT_TYPE).contains(io.esastack.commons.net.http.HttpHeaderValues.MULTIPART_FORM_DATA)).isTrue();
then(req.headers().get(HttpHeaderNames.HOST)).isEqualTo("127.0.0.1");
then(req.protocolVersion()).isSameAs(HttpVersion.HTTP_1_1);
HttpPostRequestEncoder chunked = channel.readOutbound();
then(chunked).isNotNull();
then(chunked.isChunked()).isTrue();
then(chunked.isMultipart()).isTrue();
then(chunked.getBodyListAttributes().size()).isEqualTo(2);
then(end.isDone() && end.isSuccess()).isTrue();
} finally {
file.delete();
}
}
use of io.netty.handler.codec.http.multipart.HttpPostRequestEncoder in project esa-restclient by esastack.
the class MultipartWriter method encodeAndWrite1.
private static void encodeAndWrite1(io.esastack.httpclient.core.HttpRequest request, HttpRequest request0, Channel channel, ExecContext execCtx, ChannelPromise headFuture, ChannelPromise endPromise) throws IOException {
try {
final HttpPostRequestEncoder encoder = buildEncoder(request0, request);
// Finalize request
final HttpRequest finalizedRequest = encoder.finalizeRequest();
// Considering 100-expect-continue, We must write request immediately.
if (LoggerUtils.logger().isDebugEnabled()) {
LoggerUtils.logger().debug("Send Request:\n" + finalizedRequest.headers());
}
channel.write(request0, headFuture);
final Runnable writeContent = () -> {
if (encoder.isChunked()) {
// Write content chunk by chunk
channel.writeAndFlush(encoder, endPromise);
} else {
// FullHttpRequest
LastHttpContent last = new DefaultLastHttpContent(((FullHttpRequest) finalizedRequest).content());
last.trailingHeaders().add(((FullHttpRequest) finalizedRequest).trailingHeaders());
channel.writeAndFlush(last, endPromise);
}
};
if (writeContentNow(execCtx, request)) {
writeContent.run();
} else {
channel.flush();
execCtx.set100ContinueCallback(() -> Utils.runInChannel(channel, writeContent));
}
// Now no more use of file representation (and list of HttpData)
doClean(endPromise, encoder);
} catch (Exception ex) {
FACTORY.cleanRequestHttpData(request0);
throw new IOException(ex);
}
}
use of io.netty.handler.codec.http.multipart.HttpPostRequestEncoder in project esa-restclient by esastack.
the class MultipartWriter method encodeAndWrite2.
private void encodeAndWrite2(io.esastack.httpclient.core.HttpRequest request, Channel channel, Http2ConnectionHandler handler, int streamId, HttpRequest request0, ExecContext execCtx, boolean uriEncodeEnabled, ChannelPromise headFuture, ChannelPromise endPromise) throws IOException {
try {
final HttpPostRequestEncoder encoder = buildEncoder(request0, request);
final HttpRequest finalizedRequest = encoder.finalizeRequest();
final ChannelFuture future = checkAndWriteH2Headers(channel, handler, // Note that request.headers is same as request.headers.
toHttp2Headers(request, (Http1HeadersImpl) request.headers(), uriEncodeEnabled), streamId, false, headFuture);
if (future.isDone() && !future.isSuccess()) {
endPromise.setFailure(future.cause());
return;
}
final Runnable writeContent = () -> {
// case 1: FullHttpRequest
if (!encoder.isChunked()) {
handler.writeData(streamId, ((FullHttpRequest) finalizedRequest).content(), true, endPromise);
channel.flush();
} else {
// case 2: chunked data
channel.writeAndFlush(new Http2ChunkedInput(new HttpContentToByteBuf(encoder), streamId), endPromise);
}
};
// Considering 100-expect-continue, We must write request immediately.
if (!writeContentNow(execCtx, request)) {
channel.flush();
execCtx.set100ContinueCallback(() -> Utils.runInChannel(channel, writeContent));
} else {
writeContent.run();
}
// Now no more use of file representation (and list of HttpData)
doClean(endPromise, encoder);
} catch (Exception ex) {
FACTORY.cleanRequestHttpData(request0);
throw new IOException(ex);
}
}
use of io.netty.handler.codec.http.multipart.HttpPostRequestEncoder in project djl-serving by deepjavalibrary.
the class ModelServerTest method testInvocationsMultipart.
private void testInvocationsMultipart(Channel channel) throws InterruptedException, HttpPostRequestEncoder.ErrorDataEncoderException, IOException {
reset();
DefaultFullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/invocations?model_name=mlp");
ByteBuf content = Unpooled.buffer(testImage.length);
content.writeBytes(testImage);
HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(req, true);
encoder.addBodyAttribute("test", "test");
MemoryFileUpload body = new MemoryFileUpload("data", "0.png", "image/png", null, null, testImage.length);
body.setContent(content);
encoder.addBodyHttpData(body);
channel.writeAndFlush(encoder.finalizeRequest());
if (encoder.isChunked()) {
channel.writeAndFlush(encoder).sync();
}
latch.await();
Type type = new TypeToken<List<Classification>>() {
}.getType();
List<Classification> classifications = JsonUtils.GSON.fromJson(result, type);
assertEquals(classifications.get(0).getClassName(), "0");
}
use of io.netty.handler.codec.http.multipart.HttpPostRequestEncoder in project micronaut-core by micronaut-projects.
the class DefaultHttpClient method buildMultipartRequest.
private HttpPostRequestEncoder buildMultipartRequest(MutableHttpRequest clientHttpRequest, Object bodyValue) throws HttpPostRequestEncoder.ErrorDataEncoderException {
HttpDataFactory factory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE);
io.netty.handler.codec.http.HttpRequest request = NettyHttpRequestBuilder.toHttpRequest(clientHttpRequest);
HttpPostRequestEncoder postRequestEncoder = new HttpPostRequestEncoder(factory, request, true, CharsetUtil.UTF_8, HttpPostRequestEncoder.EncoderMode.HTML5);
if (bodyValue instanceof MultipartBody.Builder) {
bodyValue = ((MultipartBody.Builder) bodyValue).build();
}
if (bodyValue instanceof MultipartBody) {
final MultipartBody multipartBody = (MultipartBody) bodyValue;
postRequestEncoder.setBodyHttpDatas(multipartBody.getData(new MultipartDataFactory<InterfaceHttpData>() {
@NonNull
@Override
public InterfaceHttpData createFileUpload(@NonNull String name, @NonNull String filename, @NonNull MediaType contentType, @Nullable String encoding, @Nullable Charset charset, long length) {
return factory.createFileUpload(request, name, filename, contentType.toString(), encoding, charset, length);
}
@NonNull
@Override
public InterfaceHttpData createAttribute(@NonNull String name, @NonNull String value) {
return factory.createAttribute(request, name, value);
}
@Override
public void setContent(InterfaceHttpData fileUploadObject, Object content) throws IOException {
if (fileUploadObject instanceof FileUpload) {
FileUpload fu = (FileUpload) fileUploadObject;
if (content instanceof InputStream) {
fu.setContent((InputStream) content);
} else if (content instanceof File) {
fu.setContent((File) content);
} else if (content instanceof byte[]) {
final ByteBuf buffer = Unpooled.wrappedBuffer((byte[]) content);
fu.setContent(buffer);
}
}
}
}));
} else {
throw new MultipartException(String.format("The type %s is not a supported type for a multipart request body", bodyValue.getClass().getName()));
}
return postRequestEncoder;
}
Aggregations