Skip to content

Commit

Permalink
Merge pull request #650 from Particular/removing-binary-utf-headers
Browse files Browse the repository at this point in the history
Removing binary utf8 preamble.
  • Loading branch information
adamralph authored Nov 30, 2016
2 parents 6e0ad91 + efc782b commit 93f3988
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
13 changes: 13 additions & 0 deletions src/ServiceInsight/ExtensionMethods/BytesExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace ServiceInsight.ExtensionMethods
{
using System.Linq;

public static class BytesExtensions
{
public static bool StartsWith(this byte[] source, byte[] comparison) =>
source != null &&
comparison != null &&
source.Length >= comparison.Length &&
source.Take(comparison.Length).SequenceEqual(comparison);
}
}
7 changes: 3 additions & 4 deletions src/ServiceInsight/Saga/JsonPropertiesHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace ServiceInsight.Saga
{
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
Expand All @@ -9,11 +8,11 @@ class JsonPropertiesHelper
{
static readonly IList<string> StandardKeys = new List<string> { "$type", "Id", "Originator", "OriginalMessageId" };

public static IList<KeyValuePair<string, string>> ProcessValues(string stateAfterChange, Func<string, string> cleanup) => ProcessValues(cleanup(stateAfterChange));

static IList<KeyValuePair<string, string>> ProcessValues(string stateAfterChange) => JsonConvert.DeserializeObject<Dictionary<string, object>>(stateAfterChange)
public static IList<KeyValuePair<string, string>> ProcessValues(string stateAfterChange) => JsonConvert.DeserializeObject<Dictionary<string, object>>(stateAfterChange)
.Where(m => StandardKeys.All(s => s != m.Key))
.Select(f => new KeyValuePair<string, string>(f.Key, f.Value == null ? string.Empty : f.Value.ToString()))
.ToList();

public static IList<KeyValuePair<string, string>> ProcessArray(string stateAfterChange) => ProcessValues(stateAfterChange.TrimStart('[').TrimEnd(']'));
}
}
2 changes: 1 addition & 1 deletion src/ServiceInsight/Saga/SagaUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void UpdateValues()
return;
}

Values = JsonPropertiesHelper.ProcessValues(StateAfterChange, s => s.TrimStart('[').TrimEnd(']'))
Values = JsonPropertiesHelper.ProcessArray(StateAfterChange)
.Select(v => new SagaUpdatedValue(InitiatingMessage.MessageType, v.Key, v.Value))
.ToList();
}
Expand Down
25 changes: 19 additions & 6 deletions src/ServiceInsight/ServiceControl/DefaultServiceControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using RestSharp.Contrib;
using RestSharp.Deserializers;
using Serilog;
using ServiceInsight.ExtensionMethods;
using ServiceInsight.Framework.Events;
using ServiceInsight.Framework.MessageDecoders;
using ServiceInsight.Framework.Settings;
Expand All @@ -26,7 +27,7 @@
public class DefaultServiceControl : IServiceControl
{
static ILogger anotarLogger = Log.ForContext<IServiceControl>();
static string byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
static byte[] byteOrderMarkUtf8 = Encoding.UTF8.GetPreamble();

const string ConversationEndpoint = "conversations/{0}";
const string EndpointsEndpoint = "endpoints";
Expand Down Expand Up @@ -149,9 +150,7 @@ public IEnumerable<KeyValuePair<string, string>> GetMessageData(SagaMessage mess
return Enumerable.Empty<KeyValuePair<string, string>>();
}

return body.StartsWith("<?xml") ?
GetXmlData(body) :
JsonPropertiesHelper.ProcessValues(body, CleanupBodyString);
return body.StartsWith("<?xml") ? GetXmlData(body) : JsonPropertiesHelper.ProcessValues(body);
}

public void LoadBody(StoredMessage message)
Expand All @@ -171,7 +170,7 @@ public void LoadBody(StoredMessage message)
switch (response.StatusCode)
{
case HttpStatusCode.OK:
presentationBody.Text = CleanupBodyString(response.Content);
presentationBody.Text = response.Content;
break;
case HttpStatusCode.NoContent:
presentationBody.Hint = PresentationHint.NoContent;
Expand Down Expand Up @@ -289,6 +288,8 @@ T Execute<T>(RestRequestWithCache request, Func<IRestResponse, T> selector, stri

var response = restClient.Execute(request);

CleanResponse(response);

var data = default(T);

switch (cacheStyle)
Expand Down Expand Up @@ -376,6 +377,8 @@ T Execute<T, T2>(RestRequestWithCache request, Func<IRestResponse<T2>, T> select

var response = restClient.Execute<T2>(request);

CleanResponse(response);

var data = default(T);

switch (cacheStyle)
Expand Down Expand Up @@ -480,7 +483,17 @@ IEnumerable<KeyValuePair<string, string>> GetXmlData(string bodyString)
return new List<KeyValuePair<string, string>>();
}

static string CleanupBodyString(string bodyString) => bodyString.StartsWith(byteOrderMarkUtf8) ? bodyString.Remove(0, byteOrderMarkUtf8.Length) : bodyString;
static void CleanResponse(IRestResponse response)
{
if (response.RawBytes.StartsWith(byteOrderMarkUtf8))
{
var strippedBytes = response.RawBytes
.Skip(byteOrderMarkUtf8.Length)
.ToArray();

response.Content = new UTF8Encoding(false).GetString(strippedBytes);
}
}

void LogRequest(RestRequestWithCache request)
{
Expand Down
1 change: 1 addition & 0 deletions src/ServiceInsight/ServiceInsight.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@
<DependentUpon>DiagramLegendView.xaml</DependentUpon>
</Compile>
<Compile Include="DiagramLegend\DiagramLegendViewModel.cs" />
<Compile Include="ExtensionMethods\BytesExtensions.cs" />
<Compile Include="ExtensionMethods\JObjectExtensions.cs" />
<Compile Include="Framework\Behaviors\CommandParameterHelper.cs" />
<Compile Include="Framework\WorkNotifier.cs" />
Expand Down

0 comments on commit 93f3988

Please sign in to comment.