Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix URL encoding for events
Browse files Browse the repository at this point in the history
erri120 committed Dec 17, 2024

Verified

This commit was signed with the committer’s verified signature.
erri120 erri120
1 parent 201e01c commit 550b959
Showing 3 changed files with 41 additions and 4 deletions.
14 changes: 13 additions & 1 deletion src/NexusMods.Telemetry/EventDefinition.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Net;
using System.Text;
using JetBrains.Annotations;

namespace NexusMods.Telemetry;
@@ -8,4 +10,14 @@ namespace NexusMods.Telemetry;
/// <param name="Category">The event category</param>
/// <param name="Action">The event action</param>
[PublicAPI]
public record EventDefinition(string Category, string Action);
public record EventDefinition(string Category, string Action)
{
internal byte[] SafeCategory { get; } = Encode(Category);
internal byte[] SafeAction { get; } = Encode(Action);

private static byte[] Encode(string value)
{
var bytes = Encoding.UTF8.GetBytes(value);
return WebUtility.UrlEncodeToBytes(bytes, offset: 0, count: bytes.Length);
}
};
10 changes: 10 additions & 0 deletions src/NexusMods.Telemetry/EventMetadata.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Diagnostics;
using System.Net;
using System.Text;
using JetBrains.Annotations;

namespace NexusMods.Telemetry;
@@ -41,4 +43,12 @@ public EventMetadata(string? name, TimeProvider? timeProvider = null)
/// Checks whether the struct wasn't default initialized.
/// </summary>
public bool IsValid() => Name is not null || CurrentTime != default(TimeOnly);

internal byte[] SafeName => Name is null ? [] : Encode(Name);

private static byte[] Encode(string value)
{
var bytes = Encoding.UTF8.GetBytes(value);
return WebUtility.UrlEncodeToBytes(bytes, offset: 0, count: bytes.Length);
}
}
21 changes: 18 additions & 3 deletions src/NexusMods.Telemetry/EventSender.cs
Original file line number Diff line number Diff line change
@@ -163,14 +163,29 @@ private static void SerializeEvent(IBufferWriter<byte> writer, ValueTuple<EventD
}

sb.Append("&e_c="); // Event category
sb.Append(definition.Category);
{
var input = definition.SafeCategory.AsSpan();
var destination = sb.GetSpan(sizeHint: input.Length);
input.CopyTo(destination);
sb.Advance(input.Length);
}

sb.Append("&e_a="); // Event action
sb.Append(definition.Action);
{
var input = definition.SafeAction.AsSpan();
var destination = sb.GetSpan(sizeHint: input.Length);
input.CopyTo(destination);
sb.Advance(input.Length);

}

if (metadata.Name is not null)
{
sb.Append("&e_n="); // Event name
sb.Append(metadata.Name);
var input = metadata.SafeName.AsSpan();
var destination = sb.GetSpan(sizeHint: input.Length);
input.CopyTo(destination);
sb.Advance(input.Length);
}

sb.Append("&h="); // The current hour (local time)

0 comments on commit 550b959

Please sign in to comment.