Skip to content

Commit

Permalink
Merge pull request #56 from ILepekhov/feature/reducing-memory-allocat…
Browse files Browse the repository at this point in the history
…ions-in-TusHeaders

Reduced memory allocations while checking if a custom header is not reserved.
  • Loading branch information
bluetianx authored Feb 14, 2024
2 parents 69341bb + 37c42c5 commit b830127
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 51 deletions.
17 changes: 8 additions & 9 deletions src/BirdMessenger/Abstractions/TusRequestOptionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Net.Http;
using System.Threading.Tasks;
using BirdMessenger.Constants;
using BirdMessenger.Delegates;
using BirdMessenger.Events;
using BirdMessenger.Infrastructure;

Expand All @@ -16,12 +15,12 @@ public TusRequestOptionBase()
{
HttpHeaders = new Dictionary<string, string>();
}

/// <summary>
/// invoke before sending http request to server
/// </summary>
public Func<PreSendRequestEvent,Task>? OnPreSendRequestAsync { get; set; }

/// <summary>
/// add additional http headers
/// </summary>
Expand All @@ -32,7 +31,7 @@ public TusRequestOptionBase()
internal void AddCustomHttpHeaders(HttpRequestMessage httpRequestMessage)
{
ValidateHttpHeaders();

if (HttpHeaders is not null && HttpHeaders.Any())
{
foreach (var key in HttpHeaders.Keys)
Expand All @@ -41,10 +40,10 @@ internal void AddCustomHttpHeaders(HttpRequestMessage httpRequestMessage)
}
}
}


/// <summary>
///
///
/// </summary>
/// <returns></returns>
private void ValidateHttpHeaders()
Expand All @@ -53,12 +52,12 @@ private void ValidateHttpHeaders()
{
foreach (var headerKey in HttpHeaders.Keys)
{
if (TusHeaders.TusReservedWords.Contains(headerKey.ToLower()))
if (TusHeaders.IsReserved(headerKey))
{
throw new TusException($"HttpHeader can not contain tus Reserved word:{headerKey}");
throw new TusException($"HttpHeader can not contain tus Reserved word: {headerKey}");
}
}
}
}

}
}
90 changes: 48 additions & 42 deletions src/BirdMessenger/Constants/TusHeaders.cs
Original file line number Diff line number Diff line change
@@ -1,50 +1,56 @@
using System;
using System.Collections.Generic;

namespace BirdMessenger.Constants;

internal static class TusHeaders
public static class TusHeaders
{
private static readonly HashSet<string> TusReservedWords = new(StringComparer.OrdinalIgnoreCase);

static TusHeaders()
{
TusReservedWords.Add(UploadLength.ToLower());
TusReservedWords.Add(UploadOffset.ToLower());
TusReservedWords.Add(UploadMetadata.ToLower());
TusReservedWords.Add(UploadDeferLength.ToLower());
TusReservedWords.Add(ContentType.ToLower());
TusReservedWords.Add(UploadChecksum.ToLower());
TusReservedWords.Add(TusResumable.ToLower());
TusReservedWords.Add(UploadConcat.ToLower());
TusReservedWords.Add(TusVersion.ToLower());
TusReservedWords.Add(TusMaxSize.ToLower());
TusReservedWords.Add(TusExtension.ToLower());
TusReservedWords.Add(UploadLength);
TusReservedWords.Add(UploadOffset);
TusReservedWords.Add(UploadMetadata);
TusReservedWords.Add(UploadDeferLength);
TusReservedWords.Add(ContentType);
TusReservedWords.Add(UploadChecksum);
TusReservedWords.Add(TusResumable);
TusReservedWords.Add(UploadConcat);
TusReservedWords.Add(TusVersion);
TusReservedWords.Add(TusMaxSize);
TusReservedWords.Add(TusExtension);
}

public const string UploadLength = "Upload-Length";

public const string UploadOffset = "Upload-Offset";

public const string UploadMetadata = "Upload-Metadata";

public const string TusResumable = "Tus-Resumable";

public const string Location = "Location";

public const string UploadDeferLength = "Upload-Defer-Length";

public const string ContentType = "Content-Type";

public const string UploadChecksum = "Upload-Checksum";

public const string UploadConcat = "Upload-Concat";

public const string UploadContentTypeValue= "application/offset+octet-stream";

public const string TusVersion = "Tus-Version";

public const string TusMaxSize = "Tus-Max-Size";

public const string TusExtension = "Tus-Extension";

public static bool IsReserved(string headerName)
{
return string.IsNullOrWhiteSpace(headerName) is false &&
TusReservedWords.Contains(headerName);
}
internal static readonly HashSet<string> TusReservedWords = new();

internal const string UploadLength = "Upload-Length";

internal const string UploadOffset = "Upload-Offset";

internal const string UploadMetadata = "Upload-Metadata";

internal const string TusResumable = "Tus-Resumable";

internal const string Location = "Location";

internal const string UploadDeferLength = "Upload-Defer-Length";

internal const string ContentType = "Content-Type";

internal const string UploadChecksum = "Upload-Checksum";

internal const string UploadConcat = "Upload-Concat";

internal const string UploadContentTypeValue= "application/offset+octet-stream";

internal const string TusVersion = "Tus-Version";

internal const string TusMaxSize = "Tus-Max-Size";

internal const string TusExtension = "Tus-Extension";


}
}

0 comments on commit b830127

Please sign in to comment.