Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #1883, MultipartMixed grows cache indefinitely #1887

Merged
merged 2 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 46 additions & 7 deletions src/Microsoft.OData.Core/MediaTypeUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ internal static class MediaTypeUtils
/// <summary>
/// Max size to cache match info.
/// </summary>
private const int MatchInfoCacheMaxSize = 256;
private const int MatchInfoCacheInitialSize = 256;

/// <summary>
/// Concurrent cache to cache match info.
/// </summary>
private static MatchInfoConcurrentCache MatchInfoCache = new MatchInfoConcurrentCache(MatchInfoCacheMaxSize);
private static MatchInfoConcurrentCache MatchInfoCache = new MatchInfoConcurrentCache(MatchInfoCacheInitialSize);

/// <summary>UTF-8 encoding, without the BOM preamble.</summary>
/// <remarks>
Expand Down Expand Up @@ -413,6 +413,15 @@ internal static ODataFormat GetFormatFromContentType(string contentTypeName, ODa
throw new ODataContentTypeException(Strings.MediaTypeUtils_CannotDetermineFormatFromContentType(str, contentTypeName));
}

/// <summary>
/// Internal method for testing membership of the cache
/// </summary>
/// <returns>The ContentTypes in the cache in the MediaInfoCache</returns>
internal static IEnumerable<string> GetCacheKeys()
{
return MatchInfoCache.GetKeys();
}

/// <summary>
/// Parses the specified content type header into a media type instance.
/// </summary>
Expand Down Expand Up @@ -908,7 +917,7 @@ public MatchInfoCacheKey(ODataMediaTypeResolver resolver, ODataPayloadKind paylo
{
this.MediaTypeResolver = resolver;
this.PayloadKind = payloadKind;
this.ContentTypeName = contentTypeName;
this.ContentTypeName = RemoveBoundary(contentTypeName);
}

/// <summary>
Expand All @@ -924,7 +933,7 @@ public MatchInfoCacheKey(ODataMediaTypeResolver resolver, ODataPayloadKind paylo
/// <summary>
/// Name of content type.
/// </summary>
private string ContentTypeName { get; set; }
internal string ContentTypeName { get; set; }

/// <summary>
/// Returns a value indicating whether this instance is equal to a specified object.
Expand Down Expand Up @@ -967,6 +976,27 @@ public override int GetHashCode()
int result = this.MediaTypeResolver.GetHashCode() ^ this.PayloadKind.GetHashCode();
return this.ContentTypeName != null ? result ^ this.ContentTypeName.GetHashCode() : result;
}

/// <summary>
/// Multipart/mixed content types have a boundary that varies by request.
/// It should not be used as part of the format key.
/// </summary>
/// <param name="contentTypeName"></param>
/// <returns></returns>
private static string RemoveBoundary(string contentTypeName)
{
if (contentTypeName.StartsWith("multipart", StringComparison.OrdinalIgnoreCase))
Copy link
Member

@xuzhg xuzhg Sep 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json batch also has the "mulitpart"? #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No; json uses application/json, which does not have a boundary parameter.

The boundary issue is specific to multipart/mixed, which uses a "boundary" mime type parameter to distinguish requests within the multipart/mixed batch payload.


In reply to: 492449803 [](ancestors = 492449803)

{
// our formatters don't care about parameters for multipart, so just strip them all out for simplicity
int parameter = contentTypeName.IndexOf(';');
if (parameter > 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume there will be only 1 ';' in the string, so rather than index and then substring, can we do in 1 pass? may be by using a stringbuilder

{
return contentTypeName.Substring(0, parameter);
}
}

return contentTypeName;
}
}

/// <summary>
Expand All @@ -982,10 +1012,10 @@ private sealed class MatchInfoConcurrentCache
/// <summary>
/// Constructor.
/// </summary>
/// <param name="maxSize">Max size of the elements that the cache can contain.</param>
public MatchInfoConcurrentCache(int maxSize)
/// <param name="initialSize">Initial size of the cache.</param>
public MatchInfoConcurrentCache(int initialSize)
{
this.dict = new ConcurrentDictionary<MatchInfoCacheKey, MediaTypeMatchInfo>(4, maxSize);
this.dict = new ConcurrentDictionary<MatchInfoCacheKey, MediaTypeMatchInfo>(4, initialSize);
}

/// <summary>
Expand Down Expand Up @@ -1018,6 +1048,15 @@ public void Add(MatchInfoCacheKey key, MediaTypeMatchInfo value)
this.dict.TryAdd(key, value);
}
}

/// <summary>
/// Internal method for validating expected elements in the cache
/// </summary>
/// <returns>ContentType of items in the cache</returns>
internal IEnumerable<string> GetKeys()
{
return this.dict.Keys.Select(k => k.ContentTypeName);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,20 @@ public void AlterContentTypeForJsonPaddingIfNeededShouldFailIfAppJsonIsNotAtStar
Action target = () => MediaTypeUtils.AlterContentTypeForJsonPadding(original);
target.Throws<ODataException>(Strings.ODataMessageWriter_JsonPaddingOnInvalidContentType("tricky/application/json"));
}

[Fact]
public void MultipartBoundariesShouldNotGrowCache()
{
for (int i = 1; i < 100; i++)
{
ODataMediaType mediaType;
Encoding encoding;
ODataPayloadKind payloadKind;
MediaTypeUtils.GetFormatFromContentType(string.Format("multipart/mixed;boundary={0}", Guid.NewGuid()), new ODataPayloadKind[] { ODataPayloadKind.Batch }, ODataMediaTypeResolver.GetMediaTypeResolver(null), out mediaType, out encoding, out payloadKind);
}

Assert.True(MediaTypeUtils.GetCacheKeys().Count(k => k.StartsWith("multipart/mixed")) == 1, "Multiple multipart/mixed keys in cache");
}
}

internal class TestMediaTypeWithFormat
Expand Down