Skip to content

Commit

Permalink
Merge pull request #2712 from vert-x3/client-form-submission-refactor
Browse files Browse the repository at this point in the history
Client form submission refactor
  • Loading branch information
vietj authored Jan 23, 2025
2 parents 40034f5 + 40f3593 commit 869599e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 433 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@
*/
package io.vertx.ext.web.client.impl;

import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder;
import io.vertx.core.*;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpClientResponse;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.RequestOptions;
import io.vertx.core.http.*;
import io.vertx.core.internal.http.HttpClientInternal;
import io.vertx.core.internal.ContextInternal;
import io.vertx.core.internal.PromiseInternal;
Expand Down Expand Up @@ -433,19 +429,42 @@ private void handlePrepareRequest() {
if (body instanceof Pipe) {
//
} else if (body instanceof MultipartForm) {
MultipartFormUpload multipartForm;
ClientForm form;
try {
boolean multipart = "multipart/form-data".equals(contentType);
HttpPostRequestEncoder.EncoderMode encoderMode = this.request.multipartMixed() ? HttpPostRequestEncoder.EncoderMode.RFC1738 : HttpPostRequestEncoder.EncoderMode.HTML5;
multipartForm = new MultipartFormUpload(context, (MultipartForm) this.body, multipart, encoderMode);
this.body = multipartForm.pipe();
if (multipart) {
ClientMultipartForm multipartForm = ClientMultipartForm.multipartForm();
multipartForm.mixed(request.multipartMixed());
form = multipartForm;
} else {
form = ClientForm.form();
}
form.charset(((MultipartForm)body).getCharset());
((MultipartForm)body).forEach(part -> {
if (part.isAttribute()) {
form.attribute(part.name(), part.value());
} else {
ClientMultipartForm multipartForm = (ClientMultipartForm) form;
if (part.isText()) {
if (part.pathname() != null) {
multipartForm.textFileUpload(part.name(), part.filename(), part.mediaType(), part.pathname());
} else {
multipartForm.textFileUpload(part.name(), part.filename(), part.mediaType(), part.content());
}
} else {
if (part.pathname() != null) {
multipartForm.binaryFileUpload(part.name(), part.filename(), part.mediaType(), part.pathname());
} else {
multipartForm.binaryFileUpload(part.name(), part.filename(), part.mediaType(), part.content());
}
}
}
});
this.body = form;
} catch (Exception e) {
fail(e);
return;
}
for (Map.Entry<String, String> header : multipartForm.headers()) {
requestOptions.putHeader(header.getKey(), header.getValue());
}
} else if (body == null && "application/json".equals(contentType)) {
body = Buffer.buffer("null");
} else if (body instanceof JsonObject) {
Expand Down Expand Up @@ -530,6 +549,8 @@ private void doSendRequest(HttpClientRequest request) {
request.reset(0L, ar2.cause());
}
});
} else if (bodyToSend instanceof ClientForm) {
request.send((ClientForm) bodyToSend);
} else {
Buffer buffer = (Buffer) bodyToSend;
request.send(buffer);
Expand Down

This file was deleted.

Loading

0 comments on commit 869599e

Please sign in to comment.