forked from elastic/apm-agent-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Access Request.InputStream only when SOAP header present
This commit fixes a bug where accessing the Request.InputStream causes the entrie request to be buffered in memory. This has two repercussions: 1. For many large requests, it can potentially cause out of memory 2. It can interfere with a custom IHostBufferPolicySelector used in web API to determine whether to buffer the input stream. With this fix, the input stream is accessed only when a SOAP content-type is present and has not already been read bufferless. Fixes elastic#1113
- Loading branch information
Showing
8 changed files
with
191 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
test/Elastic.Apm.AspNetFullFramework.Tests/Soap/SoapRequestTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Licensed to Elasticsearch B.V under | ||
// one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Elastic.Apm.AspNetFullFramework.Tests.Soap | ||
{ | ||
[Collection(Consts.AspNetFullFrameworkTestsCollection)] | ||
public class SoapRequestTests : TestsBase | ||
{ | ||
public SoapRequestTests(ITestOutputHelper xUnitOutputHelper) | ||
: base(xUnitOutputHelper) { } | ||
|
||
/// <summary> | ||
/// Tests that the reading of the input stream to get the action name for a SOAP 1.2 request | ||
/// does not cause an exception to be thrown when the framework deserializes the input stream | ||
/// to parse the parameters for the web method. | ||
/// </summary> | ||
[AspNetFullFrameworkFact] | ||
public async Task Name_Should_Should_Not_Throw_Exception_When_Asmx_Soap12_Request() | ||
{ | ||
var pathData = SampleAppUrlPaths.CallSoapServiceProtocolV12; | ||
var action = "Input"; | ||
|
||
var input = @"This is the input"; | ||
var request = new HttpRequestMessage(HttpMethod.Post, pathData.Uri) | ||
{ | ||
Content = new StringContent($@"<?xml version=""1.0"" encoding=""utf-8""?> | ||
<soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope""> | ||
<soap12:Body> | ||
<{action} xmlns=""http://tempuri.org/""> | ||
<input>{input}</input> | ||
</{action}> | ||
</soap12:Body> | ||
</soap12:Envelope>", Encoding.UTF8, "application/soap+xml") | ||
}; | ||
|
||
request.Headers.Accept.Clear(); | ||
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*")); | ||
|
||
var response = await HttpClient.SendAsync(request); | ||
response.IsSuccessStatusCode.Should().BeTrue(); | ||
|
||
var responseText = await response.Content.ReadAsStringAsync(); | ||
responseText.Should().Contain(input); | ||
|
||
await WaitAndCustomVerifyReceivedData(receivedData => | ||
{ | ||
receivedData.Transactions.Count.Should().Be(1); | ||
var transaction = receivedData.Transactions.First(); | ||
transaction.Name.Should().Be($"POST {pathData.Uri.AbsolutePath} {action}"); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Licensed to Elasticsearch B.V under | ||
// one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using System.IO; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Elastic.Apm.Tests.TestHelpers; | ||
using FluentAssertions; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Elastic.Apm.AspNetFullFramework.Tests | ||
{ | ||
[Collection(Consts.AspNetFullFrameworkTestsCollection)] | ||
public class WebApiTests : TestsBase | ||
{ | ||
public WebApiTests(ITestOutputHelper xUnitOutputHelper) : base(xUnitOutputHelper) { } | ||
|
||
// https://github.com/elastic/apm-agent-dotnet/issues/1113 | ||
[AspNetFullFrameworkFact] | ||
public async Task MultipartData_Should_Not_Throw() | ||
{ | ||
var pathData = SampleAppUrlPaths.WebApiPage; | ||
using var request = new HttpRequestMessage(HttpMethod.Post, pathData.Uri); | ||
|
||
using var plainInputTempFile = TempFile.CreateWithContents("this is plain input"); | ||
using var jsonTempFile = TempFile.CreateWithContents("{\"input\":\"this is json input\"}"); | ||
using var multiPartContent = new MultipartFormDataContent | ||
{ | ||
{ new StreamContent(new FileStream(plainInputTempFile.Path, FileMode.Open, FileAccess.Read)), "plain", "plain" }, | ||
{ new StreamContent(new FileStream(jsonTempFile.Path, FileMode.Open, FileAccess.Read)), "json", "json" }, | ||
}; | ||
|
||
request.Content = multiPartContent; | ||
using var response = await HttpClient.SendAsync(request).ConfigureAwait(false); | ||
response.IsSuccessStatusCode.Should().BeTrue(); | ||
} | ||
} | ||
} |