Skip to content

Commit

Permalink
Allow the overriding of the Allowed Encodings.
Browse files Browse the repository at this point in the history
  • Loading branch information
HarryCordewener committed Jan 4, 2024
1 parent 065f853 commit cc15b60
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public partial class TelnetInterpreter

private bool charsetOffered = false;

private Func<IEnumerable<EncodingInfo>> AllowedEncodings { get; set; } = Encoding.GetEncodings;

private Func<IEnumerable<EncodingInfo>, IOrderedEnumerable<Encoding>> _charsetOrder = (x) => x.Select(y => y.GetEncoding()).OrderBy(z => z.EncodingName);

public Lazy<byte[]> SupportedCharacterSets { get; }
Expand Down Expand Up @@ -205,7 +207,7 @@ private async Task CompleteCharsetAsync(StateMachine<State, Trigger>.Transition

_Logger.Debug("Charsets offered to us: {@charsetResultDebug}", charsetsOffered);

var encodingDict = Encoding.GetEncodings().ToDictionary(x => x.GetEncoding().WebName);
var encodingDict = AllowedEncodings().ToDictionary(x => x.GetEncoding().WebName);
var offeredEncodingInfo = charsetsOffered.Select(x => { try { return encodingDict[Encoding.GetEncoding(x).WebName]; } catch { return null; } }).Where(x => x != null);
var preferredEncoding = _charsetOrder(offeredEncodingInfo);
var chosenEncoding = preferredEncoding.FirstOrDefault();
Expand Down Expand Up @@ -273,7 +275,7 @@ private async Task WillingCharsetAsync()
/// </summary>
private async Task OnDoCharsetAsync(StateMachine<State, Trigger>.Transition _)
{
_Logger.Debug("Charsets String: {CharsetList}", ";" + string.Join(";", _charsetOrder(Encoding.GetEncodings()).Select(x => x.WebName)));
_Logger.Debug("Charsets String: {CharsetList}", ";" + string.Join(";", _charsetOrder(AllowedEncodings()).Select(x => x.WebName)));
await CallbackNegotiationAsync(SupportedCharacterSets.Value);
charsetOffered = true;
}
Expand All @@ -286,7 +288,7 @@ private byte[] CharacterSets()
{
byte[] pre = [(byte)Trigger.IAC, (byte)Trigger.SB, (byte)Trigger.CHARSET, (byte)Trigger.REQUEST];
byte[] post = [(byte)Trigger.IAC, (byte)Trigger.SE];
byte[] defaultCharsets = ascii.GetBytes($";{string.Join(";", _charsetOrder(Encoding.GetEncodings()).Select(x => x.WebName))}");
byte[] defaultCharsets = ascii.GetBytes($";{string.Join(";", _charsetOrder(AllowedEncodings()).Select(x => x.WebName))}");
return [.. pre, .. defaultCharsets, .. post];
}
}
Expand Down

0 comments on commit cc15b60

Please sign in to comment.