diff --git a/src/Nancy/Owin/NancyMiddleware.cs b/src/Nancy/Owin/NancyMiddleware.cs index 5076a4a81e..125ebdda2d 100644 --- a/src/Nancy/Owin/NancyMiddleware.cs +++ b/src/Nancy/Owin/NancyMiddleware.cs @@ -80,7 +80,11 @@ public static MidFunc UseNancy(NancyOptions options = null) var url = CreateUrl(owinRequestHost, owinRequestScheme, owinRequestPathBase, owinRequestPath, owinRequestQueryString); - var nancyRequestStream = new RequestStream(owinRequestBody, ExpectedLength(owinRequestHeaders), StaticConfiguration.DisableRequestStreamSwitching ?? false); + var expectedLength = ExpectedLength(owinRequestHeaders); + // If length is 0 just use empty memory stream; as there is no body + var nancyRequestStream = (expectedLength == 0) ? + (Stream)new MemoryStream() : + new RequestStream(owinRequestBody, expectedLength ?? 0, StaticConfiguration.DisableRequestStreamSwitching ?? false); var nancyRequest = new Request( owinRequestMethod, @@ -193,14 +197,24 @@ private static ClaimsPrincipal GetUser(IDictionary environment) return null; } - private static long ExpectedLength(IDictionary headers) + private static long? ExpectedLength(IDictionary headers) { var header = GetHeader(headers, "Content-Length"); + if (string.IsNullOrWhiteSpace(header)) - return 0; + { + header = GetHeader(headers, "Transfer-Encoding"); + if (string.IsNullOrWhiteSpace(header)) + { + // No content-length or transfer-encoding means the length is definately 0 + return 0; + } + // Has transfer-encoding, length is unknown + return null; + } - int contentLength; - return int.TryParse(header, NumberStyles.Any, CultureInfo.InvariantCulture, out contentLength) ? contentLength : 0; + // If length cannot be converted to an int, treat it as unknown + return int.TryParse(header, NumberStyles.Any, CultureInfo.InvariantCulture, out int contentLength) ? contentLength : (long?)null; } ///