From 6e8c0cae42899e6bd5c7888bad9ab9a17cd549b0 Mon Sep 17 00:00:00 2001 From: Kazuya Fujimoto <> Date: Wed, 25 Mar 2026 21:22:20 +0900 Subject: [PATCH] fix: Omit default port from Host header in MqttServerWs2Connection Only include the port in the Host header when it differs from the scheme's default port (443 for wss, 80 for ws). This fixes 403 Forbidden errors from AWS IoT Core and other servers that normalize the Host header by stripping default ports. Co-Authored-By: Claude Opus 4.6 (1M context) --- .../server/mqtt_client_mqtt_server_ws2_connection.dart | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/src/connectionhandling/server/mqtt_client_mqtt_server_ws2_connection.dart b/lib/src/connectionhandling/server/mqtt_client_mqtt_server_ws2_connection.dart index 40c5d43..82077de 100644 --- a/lib/src/connectionhandling/server/mqtt_client_mqtt_server_ws2_connection.dart +++ b/lib/src/connectionhandling/server/mqtt_client_mqtt_server_ws2_connection.dart @@ -278,7 +278,9 @@ class MqttServerWs2Connection extends MqttServerWsConnection { final c = Completer(); const endL = '\r\n'; final path = '${uri.path}?${uri.query}'; - final host = '${uri.host}:${uri.port.toString()}'; + final isDefaultPort = (uri.scheme == 'wss' && uri.port == 443) || + (uri.scheme == 'ws' && uri.port == 80); + final host = isDefaultPort ? uri.host : '${uri.host}:${uri.port}'; final now = DateTime.now().millisecondsSinceEpoch; final key = 'mqtt-$now'; final key64 = base64.encode(utf8.encode(key));