From 2bff13bc427255795902c654f54a35020b10852c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20M=C3=B6rner?= Date: Thu, 29 Feb 2024 13:39:38 +0100 Subject: [PATCH 1/3] removed unnessecary buffered copy added method to deserialize SoapDefinition from stream removed unnessecary StreamReader usage --- .../DocumentationWriter/SoapMethods.cs | 9 ++++ .../MessageEncoder/SoapMessageEncoder.cs | 46 +++++++++---------- src/SoapCore/Meta/BodyWriterExtensions.cs | 10 +--- src/SoapCore/SoapEndpointMiddleware.cs | 7 +-- 4 files changed, 35 insertions(+), 37 deletions(-) diff --git a/src/SoapCore/DocumentationWriter/SoapMethods.cs b/src/SoapCore/DocumentationWriter/SoapMethods.cs index 873282c8..c01127dc 100644 --- a/src/SoapCore/DocumentationWriter/SoapMethods.cs +++ b/src/SoapCore/DocumentationWriter/SoapMethods.cs @@ -49,6 +49,15 @@ public static SoapDefinition DeserializeFromString(string xml) return (SoapDefinition)xs.Deserialize(sr); } + public static SoapDefinition DeserializeFromStream(Stream xml) + { + XmlSerializer xs = new XmlSerializer(typeof(SoapDefinition)); + + xs.UnknownElement += _unknownElementHandler; + + return (SoapDefinition)xs.Deserialize(xml); + } + public string GenerateDocumentation() { #if NETCOREAPP3_1_OR_GREATER diff --git a/src/SoapCore/MessageEncoder/SoapMessageEncoder.cs b/src/SoapCore/MessageEncoder/SoapMessageEncoder.cs index c655f236..448ab142 100644 --- a/src/SoapCore/MessageEncoder/SoapMessageEncoder.cs +++ b/src/SoapCore/MessageEncoder/SoapMessageEncoder.cs @@ -139,35 +139,33 @@ public async Task ReadMessageAsync(Stream stream, int maxSizeOfHeaders, Message message; - using (var ms = new MemoryStream()) - { - await stream.CopyToAsync(ms); - ms.Seek(0, SeekOrigin.Begin); - XmlReader reader; + var ms = new MemoryStream(); + await stream.CopyToAsync(ms); + ms.Seek(0, SeekOrigin.Begin); + XmlReader reader; - var readEncoding = SoapMessageEncoderDefaults.ContentTypeToEncoding(contentType); + var readEncoding = SoapMessageEncoderDefaults.ContentTypeToEncoding(contentType); - if (readEncoding == null) - { - // Fallback to default or writeEncoding - readEncoding = _writeEncoding; - } + if (readEncoding == null) + { + // Fallback to default or writeEncoding + readEncoding = _writeEncoding; + } - var supportXmlDictionaryReader = SoapMessageEncoderDefaults.TryValidateEncoding(readEncoding, out _); + var supportXmlDictionaryReader = SoapMessageEncoderDefaults.TryValidateEncoding(readEncoding, out _); - if (supportXmlDictionaryReader) - { - reader = XmlDictionaryReader.CreateTextReader(ms, readEncoding, ReaderQuotas, dictionaryReader => { }); - } - else - { - var streamReaderWithEncoding = new StreamReader(ms, readEncoding); - var xmlReaderSettings = new XmlReaderSettings() { IgnoreWhitespace = true, DtdProcessing = DtdProcessing.Prohibit, CloseInput = true }; - reader = XmlReader.Create(streamReaderWithEncoding, xmlReaderSettings); - } - - message = Message.CreateMessage(reader, maxSizeOfHeaders, MessageVersion).CreateBufferedCopy(int.MaxValue).CreateMessage(); + if (supportXmlDictionaryReader) + { + reader = XmlDictionaryReader.CreateTextReader(ms, readEncoding, ReaderQuotas, dictionaryReader => { }); } + else + { + var streamReaderWithEncoding = new StreamReader(ms, readEncoding); + var xmlReaderSettings = new XmlReaderSettings() { IgnoreWhitespace = true, DtdProcessing = DtdProcessing.Prohibit, CloseInput = true }; + reader = XmlReader.Create(streamReaderWithEncoding, xmlReaderSettings); + } + + message = Message.CreateMessage(reader, maxSizeOfHeaders, MessageVersion); return message; } diff --git a/src/SoapCore/Meta/BodyWriterExtensions.cs b/src/SoapCore/Meta/BodyWriterExtensions.cs index 512ffbd2..c94a875a 100644 --- a/src/SoapCore/Meta/BodyWriterExtensions.cs +++ b/src/SoapCore/Meta/BodyWriterExtensions.cs @@ -40,11 +40,8 @@ public static bool TryAddSchemaTypeFromXmlSchemaProviderAttribute(this XmlDictio memoryStream.Position = 0; - var streamReader = new StreamReader(memoryStream); - var result = streamReader.ReadToEnd(); - var doc = new XmlDocument(); - doc.LoadXml(result); + doc.Load(memoryStream); doc.DocumentElement.WriteContentTo(writer); return true; @@ -130,11 +127,8 @@ public static bool TryAddSchemaTypeFromXmlSchemaProviderAttribute(this XmlDictio schema.Write(memoryStream); memoryStream.Position = 0; - var streamReader = new StreamReader(memoryStream); - var result = streamReader.ReadToEnd(); - var doc = new XmlDocument(); - doc.LoadXml(result); + doc.Load(memoryStream); doc.DocumentElement.WriteContentTo(writer); return true; diff --git a/src/SoapCore/SoapEndpointMiddleware.cs b/src/SoapCore/SoapEndpointMiddleware.cs index a4a58152..d4b6af77 100644 --- a/src/SoapCore/SoapEndpointMiddleware.cs +++ b/src/SoapCore/SoapEndpointMiddleware.cs @@ -264,10 +264,7 @@ private async Task ProcessMeta(HttpContext httpContext, bool showDocumentation) using var ms = new MemoryStream(); await messageEncoder.WriteMessageAsync(responseMessage, httpContext, ms, _options.IndentWsdl); ms.Position = 0; - using var sr = new StreamReader(ms); - var wsdl = await sr.ReadToEndAsync(); - - var documentation = SoapDefinition.DeserializeFromString(wsdl).GenerateDocumentation(); + var documentation = SoapDefinition.DeserializeFromStream(ms).GenerateDocumentation(); await httpContext.Response.WriteAsync(documentation); @@ -438,7 +435,7 @@ bool TryGetRequestValue(string key, out StringValues value) bodyWriter.WriteBodyContents(dictionaryWriter); dictionaryWriter.Flush(); - await context.Response.WriteAsync(DefaultEncodings.UTF8.GetString(ms.ToArray())); + await ms.CopyToAsync(context.Response.Body); } private Func> MakeProcessorPipe(ISoapMessageProcessor[] soapMessageProcessors, HttpContext httpContext, Func> processMessageFunc) From fbec6cbfaf152c7b512316b8b988dd880d88868b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20M=C3=B6rner?= Date: Thu, 29 Feb 2024 13:53:15 +0100 Subject: [PATCH 2/3] use null-xmlresolver --- src/SoapCore/MessageEncoder/SoapMessageEncoder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SoapCore/MessageEncoder/SoapMessageEncoder.cs b/src/SoapCore/MessageEncoder/SoapMessageEncoder.cs index 448ab142..40aa9681 100644 --- a/src/SoapCore/MessageEncoder/SoapMessageEncoder.cs +++ b/src/SoapCore/MessageEncoder/SoapMessageEncoder.cs @@ -161,7 +161,7 @@ public async Task ReadMessageAsync(Stream stream, int maxSizeOfHeaders, else { var streamReaderWithEncoding = new StreamReader(ms, readEncoding); - var xmlReaderSettings = new XmlReaderSettings() { IgnoreWhitespace = true, DtdProcessing = DtdProcessing.Prohibit, CloseInput = true }; + var xmlReaderSettings = new XmlReaderSettings() { XmlResolver = null, IgnoreWhitespace = true, DtdProcessing = DtdProcessing.Prohibit, CloseInput = true }; reader = XmlReader.Create(streamReaderWithEncoding, xmlReaderSettings); } From 7cd18b524be6bf8b955baf43b182bd743516ab9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20M=C3=B6rner?= Date: Thu, 29 Feb 2024 14:11:15 +0100 Subject: [PATCH 3/3] v1.1.0.46 --- src/SoapCore/SoapCore.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SoapCore/SoapCore.csproj b/src/SoapCore/SoapCore.csproj index 4c81d8d5..f2d01ab6 100644 --- a/src/SoapCore/SoapCore.csproj +++ b/src/SoapCore/SoapCore.csproj @@ -2,7 +2,7 @@ SOAP protocol middleware for ASP.NET Core - 1.1.0.45 + 1.1.0.46 Digital Design netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0;net6.0 SoapCore