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

Allow serialization of invalid xml characters #766

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
2 changes: 1 addition & 1 deletion src/SoapCore.Tests/Wsdl/WsdlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ private async Task<string> GetWsdlFromMetaBodyWriter<T>(SoapSerializer serialize
var bodyWriter = serializer == SoapSerializer.DataContractSerializer
? new MetaWCFBodyWriter(service, baseUrl, "BasicHttpBinding", false) as BodyWriter
: new MetaBodyWriter(service, baseUrl, xmlNamespaceManager, "BasicHttpBinding", new[] { MessageVersion.None }) as BodyWriter;
var encoder = new SoapMessageEncoder(MessageVersion.Soap12WSAddressingAugust2004, System.Text.Encoding.UTF8, XmlDictionaryReaderQuotas.Max, false, true);
var encoder = new SoapMessageEncoder(MessageVersion.Soap12WSAddressingAugust2004, System.Text.Encoding.UTF8, XmlDictionaryReaderQuotas.Max, false, true, false);
var responseMessage = Message.CreateMessage(encoder.MessageVersion, null, bodyWriter);
responseMessage = new MetaMessage(responseMessage, service, xmlNamespaceManager, "BasicHttpBinding", false);

Expand Down
10 changes: 7 additions & 3 deletions src/SoapCore/MessageEncoder/SoapMessageEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ public class SoapMessageEncoder
private readonly bool _omitXmlDeclaration;
private readonly bool _indentXml;
private readonly bool _supportXmlDictionaryReader;
private readonly bool _checkXmlCharacters;

public SoapMessageEncoder(MessageVersion version, Encoding writeEncoding, XmlDictionaryReaderQuotas quotas, bool omitXmlDeclaration, bool indentXml)
public SoapMessageEncoder(MessageVersion version, Encoding writeEncoding, XmlDictionaryReaderQuotas quotas, bool omitXmlDeclaration, bool indentXml, bool checkXmlCharacters)
{
_indentXml = indentXml;
_omitXmlDeclaration = omitXmlDeclaration;
_checkXmlCharacters = checkXmlCharacters;
if (writeEncoding == null)
{
throw new ArgumentNullException(nameof(writeEncoding));
Expand Down Expand Up @@ -153,7 +155,8 @@ public virtual async Task WriteMessageAsync(Message message, PipeWriter pipeWrit
OmitXmlDeclaration = _optimizeWriteForUtf8 && _omitXmlDeclaration, //can only omit if utf-8
Indent = _indentXml,
Encoding = _writeEncoding,
CloseOutput = true
CloseOutput = true,
CheckCharacters = _checkXmlCharacters
});

using var xmlWriter = XmlDictionaryWriter.CreateDictionaryWriter(xmlTextWriter);
Expand Down Expand Up @@ -183,7 +186,8 @@ public virtual Task WriteMessageAsync(Message message, Stream stream)
OmitXmlDeclaration = _optimizeWriteForUtf8 && _omitXmlDeclaration, //can only omit if utf-8,
Indent = _indentXml,
Encoding = _writeEncoding,
CloseOutput = false
CloseOutput = false,
CheckCharacters = _checkXmlCharacters
});

using var xmlWriter = XmlDictionaryWriter.CreateDictionaryWriter(xmlTextWriter);
Expand Down
6 changes: 6 additions & 0 deletions src/SoapCore/SoapCoreOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ public class SoapCoreOptions
/// </summary>
public bool IndentXml { get; set; } = true;

/// <summary>
/// Gets or sets a value indicating whether to check to make sure that the XmlOutput doesn't contain invalid characters
/// <para>Defaults to true</para>
/// </summary>
public bool CheckXmlCharacters { get; set; } = true;

/// <summary>
/// Gets or sets an collection of Xml Namespaces to override the default prefix for.
/// </summary>
Expand Down
11 changes: 7 additions & 4 deletions src/SoapCore/SoapEndpointMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public SoapEndpointMiddleware(ILogger<SoapEndpointMiddleware<T_MESSAGE>> logger,

for (var i = 0; i < options.EncoderOptions.Length; i++)
{
_messageEncoders[i] = new SoapMessageEncoder(options.EncoderOptions[i].MessageVersion, options.EncoderOptions[i].WriteEncoding, options.EncoderOptions[i].ReaderQuotas, options.OmitXmlDeclaration, options.IndentXml);
_messageEncoders[i] = new SoapMessageEncoder(options.EncoderOptions[i].MessageVersion, options.EncoderOptions[i].WriteEncoding, options.EncoderOptions[i].ReaderQuotas, options.OmitXmlDeclaration, options.IndentXml, options.CheckXmlCharacters);
}
}

Expand Down Expand Up @@ -686,9 +686,12 @@ private async Task<Message> WriteErrorResponseMessage(
var faultExceptionTransformer = serviceProvider.GetRequiredService<IFaultExceptionTransformer>();
var faultMessage = faultExceptionTransformer.ProvideFault(exception, messageEncoder.MessageVersion, requestMessage, xmlNamespaceManager);

httpContext.Response.ContentType = httpContext.Request.ContentType;
httpContext.Response.Headers["SOAPAction"] = faultMessage.Headers.Action;
httpContext.Response.StatusCode = statusCode;
if(!httpContext.Response.HasStarted)
{
httpContext.Response.ContentType = httpContext.Request.ContentType;
httpContext.Response.Headers["SOAPAction"] = faultMessage.Headers.Action;
httpContext.Response.StatusCode = statusCode;
}

SetHttpResponse(httpContext, faultMessage);
if (messageEncoder.MessageVersion.Addressing == AddressingVersion.WSAddressing10)
Expand Down
9 changes: 8 additions & 1 deletion src/SoapCore/SoapOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public class SoapOptions

public bool IndentXml { get; set; } = true;

/// <summary>
/// Gets or sets a value indicating whether to check to make sure that the XmlOutput doesn't contain invalid characters
/// <para>Defaults to true</para>
/// </summary>
public bool CheckXmlCharacters { get; set; } = true;

public XmlNamespaceManager XmlNamespacePrefixOverrides { get; set; }
public WsdlFileOptions WsdlFileOptions { get; set; }

Expand All @@ -66,7 +72,8 @@ public static SoapOptions FromSoapCoreOptions(SoapCoreOptions opt, Type serviceT
OmitXmlDeclaration = opt.OmitXmlDeclaration,
IndentXml = opt.IndentXml,
XmlNamespacePrefixOverrides = opt.XmlNamespacePrefixOverrides,
WsdlFileOptions = opt.WsdlFileOptions
WsdlFileOptions = opt.WsdlFileOptions,
CheckXmlCharacters = opt.CheckXmlCharacters
};

#pragma warning disable CS0612 // Type or member is obsolete
Expand Down