fix: custom headers with sockets

This commit is contained in:
cogwheel0
2025-09-08 13:18:38 +05:30
parent 3ba3e34938
commit 1df69b0b65
3 changed files with 57 additions and 8 deletions

View File

@@ -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<String, String> 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());

View File

@@ -674,11 +674,19 @@ class _ServerConnectionPageState extends ConsumerState<ServerConnectionPage> {
),
child: Row(
children: [
ConduitBadge(
// 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),
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(

View File

@@ -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,
),
);
}