26
26
import java .io .ByteArrayInputStream ;
27
27
import java .net .URI ;
28
28
import java .net .URLEncoder ;
29
+ import java .net .http .HttpRequest .BodyPublisher ;
29
30
import java .net .http .HttpRequest .BodyPublishers ;
30
31
import java .util .List ;
31
32
import java .util .Objects ;
@@ -72,12 +73,11 @@ public java.net.http.HttpRequest createRequest(HttpRequest req) {
72
73
break ;
73
74
74
75
case POST :
75
- // Copy the content into a byte array to avoid reading the content inputstream multiple times.
76
- builder = builder .POST (BodyPublishers .ofByteArray (Contents .bytes (req .getContent ())));
76
+ builder = builder .POST (notChunkingBodyPublisher (req ));
77
77
break ;
78
78
79
79
case PUT :
80
- builder = builder .PUT (BodyPublishers . ofByteArray ( Contents . bytes ( req . getContent ()) ));
80
+ builder = builder .PUT (notChunkingBodyPublisher ( req ));
81
81
break ;
82
82
83
83
default :
@@ -103,6 +103,27 @@ public java.net.http.HttpRequest createRequest(HttpRequest req) {
103
103
return builder .build ();
104
104
}
105
105
106
+ /**
107
+ * Some drivers do not support chunked transport, we ensure the http client is not using chunked
108
+ * transport. This is done by using a BodyPublisher with a known size, in best case without
109
+ * wasting memory by buffering the request.
110
+ *
111
+ * @return a BodyPublisher with a known size
112
+ */
113
+ private BodyPublisher notChunkingBodyPublisher (HttpRequest req ) {
114
+ String length = req .getHeader ("content-length" );
115
+
116
+ if (length == null ) {
117
+ // read the data into a byte array to know the length
118
+ return BodyPublishers .ofByteArray (Contents .bytes (req .getContent ()));
119
+ }
120
+
121
+ // we know the length of the request and use it
122
+ BodyPublisher chunking = BodyPublishers .ofInputStream (req .getContent ());
123
+
124
+ return BodyPublishers .fromPublisher (chunking , Long .parseLong (length ));
125
+ }
126
+
106
127
private String getRawUrl (URI baseUrl , String uri ) {
107
128
String rawUrl ;
108
129
if (uri .startsWith ("ws://" ) || uri .startsWith ("wss://" ) ||
0 commit comments