Skip to content

Commit 869599e

Browse files
authored
Merge pull request vert-x3#2712 from vert-x3/client-form-submission-refactor
Client form submission refactor
2 parents 40034f5 + 40f3593 commit 869599e

4 files changed

Lines changed: 36 additions & 433 deletions

File tree

vertx-web-client/src/main/java/io/vertx/ext/web/client/impl/HttpContext.java

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,9 @@
1515
*/
1616
package io.vertx.ext.web.client.impl;
1717

18-
import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder;
1918
import io.vertx.core.*;
2019
import io.vertx.core.buffer.Buffer;
21-
import io.vertx.core.http.HttpClientRequest;
22-
import io.vertx.core.http.HttpClientResponse;
23-
import io.vertx.core.http.HttpHeaders;
24-
import io.vertx.core.http.RequestOptions;
20+
import io.vertx.core.http.*;
2521
import io.vertx.core.internal.http.HttpClientInternal;
2622
import io.vertx.core.internal.ContextInternal;
2723
import io.vertx.core.internal.PromiseInternal;
@@ -433,19 +429,42 @@ private void handlePrepareRequest() {
433429
if (body instanceof Pipe) {
434430
//
435431
} else if (body instanceof MultipartForm) {
436-
MultipartFormUpload multipartForm;
432+
ClientForm form;
437433
try {
438434
boolean multipart = "multipart/form-data".equals(contentType);
439-
HttpPostRequestEncoder.EncoderMode encoderMode = this.request.multipartMixed() ? HttpPostRequestEncoder.EncoderMode.RFC1738 : HttpPostRequestEncoder.EncoderMode.HTML5;
440-
multipartForm = new MultipartFormUpload(context, (MultipartForm) this.body, multipart, encoderMode);
441-
this.body = multipartForm.pipe();
435+
if (multipart) {
436+
ClientMultipartForm multipartForm = ClientMultipartForm.multipartForm();
437+
multipartForm.mixed(request.multipartMixed());
438+
form = multipartForm;
439+
} else {
440+
form = ClientForm.form();
441+
}
442+
form.charset(((MultipartForm)body).getCharset());
443+
((MultipartForm)body).forEach(part -> {
444+
if (part.isAttribute()) {
445+
form.attribute(part.name(), part.value());
446+
} else {
447+
ClientMultipartForm multipartForm = (ClientMultipartForm) form;
448+
if (part.isText()) {
449+
if (part.pathname() != null) {
450+
multipartForm.textFileUpload(part.name(), part.filename(), part.mediaType(), part.pathname());
451+
} else {
452+
multipartForm.textFileUpload(part.name(), part.filename(), part.mediaType(), part.content());
453+
}
454+
} else {
455+
if (part.pathname() != null) {
456+
multipartForm.binaryFileUpload(part.name(), part.filename(), part.mediaType(), part.pathname());
457+
} else {
458+
multipartForm.binaryFileUpload(part.name(), part.filename(), part.mediaType(), part.content());
459+
}
460+
}
461+
}
462+
});
463+
this.body = form;
442464
} catch (Exception e) {
443465
fail(e);
444466
return;
445467
}
446-
for (Map.Entry<String, String> header : multipartForm.headers()) {
447-
requestOptions.putHeader(header.getKey(), header.getValue());
448-
}
449468
} else if (body == null && "application/json".equals(contentType)) {
450469
body = Buffer.buffer("null");
451470
} else if (body instanceof JsonObject) {
@@ -530,6 +549,8 @@ private void doSendRequest(HttpClientRequest request) {
530549
request.reset(0L, ar2.cause());
531550
}
532551
});
552+
} else if (bodyToSend instanceof ClientForm) {
553+
request.send((ClientForm) bodyToSend);
533554
} else {
534555
Buffer buffer = (Buffer) bodyToSend;
535556
request.send(buffer);

vertx-web-client/src/main/java/io/vertx/ext/web/client/impl/MultipartFormUpload.java

Lines changed: 0 additions & 268 deletions
This file was deleted.

0 commit comments

Comments
 (0)