From 1df69b0b6594bd4a9bf27516d3fd994c4c93d129 Mon Sep 17 00:00:00 2001 From: cogwheel0 <172976095+cogwheel0@users.noreply.github.com> Date: Mon, 8 Sep 2025 13:18:38 +0530 Subject: [PATCH] fix: custom headers with sockets --- lib/core/services/socket_service.dart | 37 +++++++++++++++++-- .../auth/views/server_connection_page.dart | 18 ++++++--- lib/shared/widgets/conduit_components.dart | 10 +++++ 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/lib/core/services/socket_service.dart b/lib/core/services/socket_service.dart index 24e55b3..e7c856a 100644 --- a/lib/core/services/socket_service.dart +++ b/lib/core/services/socket_service.dart @@ -52,10 +52,41 @@ class SocketService { .setTimeout(20000) .setPath(path); + // Merge Authorization (if any) with user-defined custom headers for the + // Socket.IO handshake. Avoid overriding reserved headers. + final Map extraHeaders = {}; if (authToken != null && authToken!.isNotEmpty) { - builder - .setAuth({'token': authToken}) - .setExtraHeaders({'Authorization': 'Bearer $authToken'}); + extraHeaders['Authorization'] = 'Bearer $authToken'; + builder.setAuth({'token': authToken}); + } + if (serverConfig.customHeaders.isNotEmpty) { + final reserved = { + 'authorization', + 'content-type', + 'accept', + // Socket/WebSocket reserved or managed by client/runtime + 'host', + 'origin', + 'connection', + 'upgrade', + 'sec-websocket-key', + 'sec-websocket-version', + 'sec-websocket-extensions', + 'sec-websocket-protocol', + }; + serverConfig.customHeaders.forEach((key, value) { + final lower = key.toLowerCase(); + if (!reserved.contains(lower) && value.isNotEmpty) { + // Do not overwrite Authorization we already set from authToken + if (lower == 'authorization' && extraHeaders.containsKey('Authorization')) { + return; + } + extraHeaders[key] = value; + } + }); + } + if (extraHeaders.isNotEmpty) { + builder.setExtraHeaders(extraHeaders); } _socket = io.io(base, builder.build()); diff --git a/lib/features/auth/views/server_connection_page.dart b/lib/features/auth/views/server_connection_page.dart index def2905..e6f27c6 100644 --- a/lib/features/auth/views/server_connection_page.dart +++ b/lib/features/auth/views/server_connection_page.dart @@ -674,11 +674,19 @@ class _ServerConnectionPageState extends ConsumerState { ), child: Row( children: [ - ConduitBadge( - text: entry.key, - backgroundColor: context.conduitTheme.buttonPrimary.withValues(alpha: 0.1), - textColor: context.conduitTheme.buttonPrimary, - isCompact: true, + // Make the header badge flexible and truncate long text + Flexible( + fit: FlexFit.loose, + child: ConduitBadge( + text: entry.key, + backgroundColor: + context.conduitTheme.buttonPrimary.withValues(alpha: 0.1), + textColor: context.conduitTheme.buttonPrimary, + isCompact: true, + maxLines: 1, + overflow: TextOverflow.ellipsis, + softWrap: false, + ), ), const SizedBox(width: Spacing.md), Expanded( diff --git a/lib/shared/widgets/conduit_components.dart b/lib/shared/widgets/conduit_components.dart index 66ffbdb..ede4ea2 100644 --- a/lib/shared/widgets/conduit_components.dart +++ b/lib/shared/widgets/conduit_components.dart @@ -562,6 +562,10 @@ class ConduitBadge extends StatelessWidget { final Color? backgroundColor; final Color? textColor; final bool isCompact; + // Optional text behavior controls for truncation/wrapping + final int? maxLines; + final TextOverflow? overflow; + final bool? softWrap; const ConduitBadge({ super.key, @@ -569,6 +573,9 @@ class ConduitBadge extends StatelessWidget { this.backgroundColor, this.textColor, this.isCompact = false, + this.maxLines, + this.overflow, + this.softWrap, }); @override @@ -592,6 +599,9 @@ class ConduitBadge extends StatelessWidget { color: textColor ?? context.conduitTheme.buttonPrimary, fontWeight: FontWeight.w600, ), + maxLines: maxLines, + overflow: overflow, + softWrap: softWrap, ), ); }