Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-java): pin the default transport to HTTP/1.1

Yudhi Armyndharis42810614f7d6
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5fd710d39..2311903be 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -70,6 +70,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Cap the total bytes one storage import extracts at `STORAGE_IMPORT_MAX_TOTAL_BYTES`.
- Close the Baileys session proxy's fetch dispatcher when the session disconnects, logs out or is destroyed.
- Java SDK (next SDK release after 0.5.1): the request timeout also bounds the response body, so a stalled body raises `OpenWATimeoutError`.
+- Java SDK (next SDK release after 0.5.1): the default transport speaks HTTP/1.1, so calls to a gateway over plain `http://` no longer fail with `HTTP/1.1 header parser received no bytes`.
- Keep a new dashboard template draft when an earlier template save finishes.
- Stop the dashboard Chats page from marking the open chat read on a newly selected session.
- Refuse bulk batches once shutdown begins, and answer `503` for a batch saved during shutdown instead of sending it.
diff --git a/sdk/java/src/main/java/com/rmyndharis/openwa/http/DefaultHttpTransport.java b/sdk/java/src/main/java/com/rmyndharis/openwa/http/DefaultHttpTransport.java
index 2763293dc..c4842cc1d 100644
--- a/sdk/java/src/main/java/com/rmyndharis/openwa/http/DefaultHttpTransport.java
+++ b/sdk/java/src/main/java/com/rmyndharis/openwa/http/DefaultHttpTransport.java
@@ -17,7 +17,13 @@ public final class DefaultHttpTransport implements HttpTransport {
private final HttpClient client;
public DefaultHttpTransport() {
- this(HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NEVER).build());
+ // HTTP/1.1 explicitly: the client defaults to HTTP/2 and, on a cleartext connection, asks to
+ // upgrade to it. A gateway answers that request with nothing at all (its HTTP server hands an
+ // Upgrade request to Socket.IO), so every call over http:// failed before it was ever sent.
+ this(HttpClient.newBuilder()
+ .version(HttpClient.Version.HTTP_1_1)
+ .followRedirects(HttpClient.Redirect.NEVER)
+ .build());
}
/** Lets a test observe whether an exchange was started. */
diff --git a/sdk/java/src/test/java/com/rmyndharis/openwa/http/DefaultHttpTransportTest.java b/sdk/java/src/test/java/com/rmyndharis/openwa/http/DefaultHttpTransportTest.java
index c1c74d306..15f14f4ef 100644
--- a/sdk/java/src/test/java/com/rmyndharis/openwa/http/DefaultHttpTransportTest.java
+++ b/sdk/java/src/test/java/com/rmyndharis/openwa/http/DefaultHttpTransportTest.java
@@ -1,6 +1,7 @@
package com.rmyndharis.openwa.http;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -11,11 +12,16 @@ import com.rmyndharis.openwa.OpenWAClient;
import com.rmyndharis.openwa.errors.OpenWAApiError;
import com.rmyndharis.openwa.errors.OpenWATimeoutError;
import com.sun.net.httpserver.HttpServer;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.net.Authenticator;
import java.net.CookieHandler;
+import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ProxySelector;
+import java.net.ServerSocket;
+import java.net.Socket;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
@@ -23,6 +29,8 @@ import java.time.DateTimeException;
import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
+import java.nio.charset.StandardCharsets;
+import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
@@ -213,6 +221,57 @@ class DefaultHttpTransportTest {
assertEquals(0, recording.started.get());
}
+ @Test
+ @Timeout(30)
+ void speaksHttp1ToAServerThatAnswersNothingToAnH2cUpgrade() throws Exception {
+ // The gateway's HTTP server hands a request that asks to upgrade to Socket.IO, which answers
+ // nothing when the path is not its own. The JDK client asks for h2c on a cleartext connection
+ // unless it is pinned to HTTP/1.1, and com.sun.net.httpserver ignores the header, so only a
+ // raw socket reproduces the server.
+ try (ServerSocket raw = new ServerSocket(0, 50, InetAddress.getByName("127.0.0.1"))) {
+ Thread serving = new Thread(() -> {
+ try {
+ while (true) {
+ try (Socket socket = raw.accept()) {
+ String head = readHead(socket.getInputStream()).toLowerCase(Locale.ROOT);
+ if (!head.contains("upgrade: h2c")) {
+ socket.getOutputStream().write((
+ "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n"
+ + "Content-Length: 2\r\nConnection: close\r\n\r\n{}")
+ .getBytes(StandardCharsets.US_ASCII));
+ }
+ }
+ }
+ } catch (IOException closed) {
+ // the test ended
+ }
+ });
+ serving.setDaemon(true);
+ serving.start();
+ OpenWAClient plain = new OpenWAClient(ClientConfig.builder()
+ .baseUrl("http://127.0.0.1:" + raw.getLocalPort())
+ .apiKey("owa_k1_x")
+ .timeout(Duration.ofSeconds(5))
+ .build());
+
+ assertDoesNotThrow(() -> plain.requestVoid(HttpMethod.GET, "/api/health", null, null));
+ }
+ }
+
+ private static String readHead(InputStream in) throws IOException {
+ ByteArrayOutputStream head = new ByteArrayOutputStream();
+ int last4 = 0;
+ int b;
+ while ((b = in.read()) != -1) {
+ head.write(b);
+ last4 = (last4 << 8) | b;
+ if (last4 == 0x0d0a0d0a) {
+ break;
+ }
+ }
+ return head.toString(StandardCharsets.US_ASCII);
+ }
+
@Test
void passesThroughOnlyTheFailuresHttpClientSendDoes() {
IOException io = new IOException("reset");