Skip to content
Open
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
59 changes: 59 additions & 0 deletions src/WcfCoreMtomEncoder.Tests/MtomCharsetTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System.IO;
using System.ServiceModel.Channels;
using System.Text;
using Xunit;

namespace WcfCoreMtomEncoder.Tests
{
public class MtomCharsetTests
{
// Regression coverage for the charset parameter on the application/xop+xml root part.
// A quoted charset (e.g. charset="utf-8") is valid per RFC 7231 3.1.1.1 and is what IRS MeF
// sends; before the fix, GetEncoding was handed the value with the quotes still attached and
// threw: System.ArgumentException: '"utf-8"' is not a supported encoding name.
[Theory]
[InlineData("charset=\"utf-8\"")] // quoted, lowercase -> the reported crash
[InlineData("charset=\"UTF-8\"")] // quoted, uppercase
[InlineData("charset=utf-8")] // unquoted -> must keep working
[InlineData("")] // absent -> must fall back, not NRE
public void ReadMessage_decodes_xop_part_regardless_of_charset_quoting(string charsetParameter)
{
var message = ReadMtomResponse(charsetParameter);

Assert.NotNull(message);
Assert.Equal("Ping", message.GetReaderAtBodyContents().LocalName);
}

// Builds a minimal multipart/related MTOM response whose application/xop+xml root part carries
// the given charset token, then decodes it through MtomMessageEncoder (the path GenTax takes).
private static Message ReadMtomResponse(string charsetParameter)
{
MessageEncoder inner = new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8)
.CreateMessageEncoderFactory().Encoder;
var encoder = new MtomMessageEncoder(inner);

const string boundary = "MIMEBoundary_test";
var httpContentType =
$"multipart/related; boundary=\"{boundary}\"; type=\"application/xop+xml\"; start=\"<root>\"";

var partContentType = "application/xop+xml"
+ (string.IsNullOrEmpty(charsetParameter) ? "" : "; " + charsetParameter)
+ "; type=\"text/xml\"";

var body =
$"--{boundary}\r\n" +
"Content-ID: <root>\r\n" +
$"Content-Type: {partContentType}\r\n" +
"Content-Transfer-Encoding: binary\r\n" +
"\r\n" +
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
"<s:Body><Ping xmlns=\"urn:test\" /></s:Body></s:Envelope>\r\n" +
$"--{boundary}--\r\n";

using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(body)))
{
return encoder.ReadMessage(stream, int.MaxValue, httpContentType);
}
}
}
}
26 changes: 26 additions & 0 deletions src/WcfCoreMtomEncoder.Tests/WcfCoreMtomEncoder.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="System.ServiceModel.Primitives" Version="10.0.652802" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\WcfCoreMtomEncoder\WcfCoreMtomEncoder.csproj" />
</ItemGroup>

</Project>
26 changes: 26 additions & 0 deletions src/WcfCoreMtomEncoder.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,42 @@ VisualStudioVersion = 16.0.28803.156
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WcfCoreMtomEncoder", "WcfCoreMtomEncoder\WcfCoreMtomEncoder.csproj", "{F5F35855-99CB-44E0-9471-BC8BDE86B555}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WcfCoreMtomEncoder.Tests", "WcfCoreMtomEncoder.Tests\WcfCoreMtomEncoder.Tests.csproj", "{53F07002-4799-4535-BE67-B2CEFDB7A50D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F5F35855-99CB-44E0-9471-BC8BDE86B555}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F5F35855-99CB-44E0-9471-BC8BDE86B555}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F5F35855-99CB-44E0-9471-BC8BDE86B555}.Debug|x64.ActiveCfg = Debug|Any CPU
{F5F35855-99CB-44E0-9471-BC8BDE86B555}.Debug|x64.Build.0 = Debug|Any CPU
{F5F35855-99CB-44E0-9471-BC8BDE86B555}.Debug|x86.ActiveCfg = Debug|Any CPU
{F5F35855-99CB-44E0-9471-BC8BDE86B555}.Debug|x86.Build.0 = Debug|Any CPU
{F5F35855-99CB-44E0-9471-BC8BDE86B555}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F5F35855-99CB-44E0-9471-BC8BDE86B555}.Release|Any CPU.Build.0 = Release|Any CPU
{F5F35855-99CB-44E0-9471-BC8BDE86B555}.Release|x64.ActiveCfg = Release|Any CPU
{F5F35855-99CB-44E0-9471-BC8BDE86B555}.Release|x64.Build.0 = Release|Any CPU
{F5F35855-99CB-44E0-9471-BC8BDE86B555}.Release|x86.ActiveCfg = Release|Any CPU
{F5F35855-99CB-44E0-9471-BC8BDE86B555}.Release|x86.Build.0 = Release|Any CPU
{53F07002-4799-4535-BE67-B2CEFDB7A50D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{53F07002-4799-4535-BE67-B2CEFDB7A50D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{53F07002-4799-4535-BE67-B2CEFDB7A50D}.Debug|x64.ActiveCfg = Debug|Any CPU
{53F07002-4799-4535-BE67-B2CEFDB7A50D}.Debug|x64.Build.0 = Debug|Any CPU
{53F07002-4799-4535-BE67-B2CEFDB7A50D}.Debug|x86.ActiveCfg = Debug|Any CPU
{53F07002-4799-4535-BE67-B2CEFDB7A50D}.Debug|x86.Build.0 = Debug|Any CPU
{53F07002-4799-4535-BE67-B2CEFDB7A50D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{53F07002-4799-4535-BE67-B2CEFDB7A50D}.Release|Any CPU.Build.0 = Release|Any CPU
{53F07002-4799-4535-BE67-B2CEFDB7A50D}.Release|x64.ActiveCfg = Release|Any CPU
{53F07002-4799-4535-BE67-B2CEFDB7A50D}.Release|x64.Build.0 = Release|Any CPU
{53F07002-4799-4535-BE67-B2CEFDB7A50D}.Release|x86.ActiveCfg = Release|Any CPU
{53F07002-4799-4535-BE67-B2CEFDB7A50D}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
5 changes: 3 additions & 2 deletions src/WcfCoreMtomEncoder/MtomMessageEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ where ReferenceMatch(reference.Attribute("href"), part)

private static Stream CreateStream(string content, MediaTypeHeaderValue contentType)
{
var encoding = !string.IsNullOrEmpty(contentType.CharSet)
? Encoding.GetEncoding(contentType.CharSet)
var charset = contentType.CharSet?.Trim('"');
var encoding = !string.IsNullOrEmpty(charset)
? Encoding.GetEncoding(charset)
: Encoding.Default;

return new MemoryStream(encoding.GetBytes(content));
Expand Down
3 changes: 2 additions & 1 deletion src/WcfCoreMtomEncoder/MtomPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public string GetStringContentForEncoder(MessageEncoder encoder)
throw new NotSupportedException();
}

var encoding = ContentType.CharSet != null ? Encoding.GetEncoding(ContentType.CharSet) : Encoding.Default;
var charset = ContentType.CharSet?.Trim('"');
var encoding = !string.IsNullOrEmpty(charset) ? Encoding.GetEncoding(charset) : Encoding.Default;

return encoding.GetString(GetRawContent());
}
Expand Down