From fb8b67d2859975bd4827f04ac4776709a70cf877 Mon Sep 17 00:00:00 2001 From: ruri Date: Thu, 19 Mar 2020 01:50:29 +0100 Subject: [PATCH] Improved string escaping and added it to multipart stringcontents --- OpenBullet/Views/Main/HitsDB.xaml.cs | 6 ++--- RuriLib/Blocks/BlockTCP.cs | 2 +- RuriLib/Extensions.cs | 33 +++++++++++++++++++++++++++ RuriLib/Functions/Requests/Request.cs | 4 ++-- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/OpenBullet/Views/Main/HitsDB.xaml.cs b/OpenBullet/Views/Main/HitsDB.xaml.cs index 51c6b6f..235fdc9 100644 --- a/OpenBullet/Views/Main/HitsDB.xaml.cs +++ b/OpenBullet/Views/Main/HitsDB.xaml.cs @@ -1,5 +1,6 @@ using Microsoft.Win32; using OpenBullet.ViewModels; +using RuriLib; using RuriLib.Models; using System; using System.Collections.Generic; @@ -188,8 +189,7 @@ namespace OpenBullet.Views.Main { try { - Selected.CopyToClipboard(hit => hit.ToFormattedString((sender as MenuItem).Header.ToString() - .Replace(@"\r\n", "\r\n").Replace(@"\n", "\n").Replace(@"\t", "\t"))); + Selected.CopyToClipboard(hit => hit.ToFormattedString((sender as MenuItem).Header.ToString().Unescape())); } catch (Exception ex) { @@ -239,7 +239,7 @@ namespace OpenBullet.Views.Main { try { - Selected.SaveToFile(GetSaveFile(), hit => hit.ToFormattedString((sender as MenuItem).Header.ToString().Replace(@"\r\n", "\r\n").Replace(@"\n", "\n"))); + Selected.SaveToFile(GetSaveFile(), hit => hit.ToFormattedString((sender as MenuItem).Header.ToString().Unescape())); } catch (Exception ex) { diff --git a/RuriLib/Blocks/BlockTCP.cs b/RuriLib/Blocks/BlockTCP.cs index 59e5e09..1288c24 100644 --- a/RuriLib/Blocks/BlockTCP.cs +++ b/RuriLib/Blocks/BlockTCP.cs @@ -247,7 +247,7 @@ namespace RuriLib var msg = ReplaceValues(Message, data); byte[] b = { }; - var payload = Encoding.ASCII.GetBytes(msg.Replace(@"\r\n", "\r\n")); + var payload = Encoding.ASCII.GetBytes(msg.Unescape()); // Manual implementation of the WebSocket frame if (WebSocket) diff --git a/RuriLib/Extensions.cs b/RuriLib/Extensions.cs index 6987162..0336cb6 100644 --- a/RuriLib/Extensions.cs +++ b/RuriLib/Extensions.cs @@ -29,4 +29,37 @@ namespace RuriLib } } } + + /// + /// Extension methods for strings. + /// + public static class StringExtensions + { + /// + /// Replaces literal values of \n, \r\n and \t with the actual escape codes. + /// + /// The string to unescape + /// Whether to unescape both \n and \r\n with the Environment.NewLine + /// The string with unescaped escape sequences. + public static string Unescape(this string str, bool useEnvNewLine = false) + { + var stringBuilder = new StringBuilder(str) + .Replace(@"\t", "\t"); + + if (useEnvNewLine) + { + stringBuilder + .Replace(@"\n", Environment.NewLine) + .Replace(@"\r\n", Environment.NewLine); + } + else + { + stringBuilder + .Replace(@"\n", "\n") + .Replace(@"\r\n", "\r\n"); + } + + return stringBuilder.ToString(); + } + } } diff --git a/RuriLib/Functions/Requests/Request.cs b/RuriLib/Functions/Requests/Request.cs index ebaf550..9f02145 100644 --- a/RuriLib/Functions/Requests/Request.cs +++ b/RuriLib/Functions/Requests/Request.cs @@ -74,7 +74,7 @@ namespace RuriLib.Functions.Requests HttpMethod method = HttpMethod.POST, bool encodeContent = false, List log = null) { this.contentType = contentType; - var pData = Regex.Replace(postData, @"(?