From 38c216a61a3dc389d1ddab2a37b4b032a6de13d0 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 12 Nov 2016 19:30:03 -0500 Subject: [PATCH 1/2] update response streams --- SocketHttpListener.Portable/Net/HttpConnection.cs | 6 +++--- SocketHttpListener.Portable/Net/HttpListenerRequest.cs | 9 ++------- SocketHttpListener.Portable/Net/HttpListenerResponse.cs | 4 ++-- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/SocketHttpListener.Portable/Net/HttpConnection.cs b/SocketHttpListener.Portable/Net/HttpConnection.cs index 67dd5c958..d9fd97ed2 100644 --- a/SocketHttpListener.Portable/Net/HttpConnection.cs +++ b/SocketHttpListener.Portable/Net/HttpConnection.cs @@ -204,12 +204,12 @@ namespace SocketHttpListener.Net return i_stream; } - public Stream GetResponseStream(HttpListenerRequest request) + public Stream GetResponseStream(bool isExpect100Continue = false) { // TODO: can we get this stream before reading the input? if (o_stream == null) { - if (context.Response.SendChunked || request == null || request.HasExpect100Continue) + if (context.Response.SendChunked || isExpect100Continue) { o_stream = new ResponseStream(stream, context.Response, _memoryStreamFactory, _textEncoding); } @@ -490,7 +490,7 @@ namespace SocketHttpListener.Net { if (!context.Request.IsWebSocketRequest || force_close) { - Stream st = GetResponseStream(context.Request); + Stream st = GetResponseStream(); if (st != null) st.Dispose(); diff --git a/SocketHttpListener.Portable/Net/HttpListenerRequest.cs b/SocketHttpListener.Portable/Net/HttpListenerRequest.cs index 767f1c542..811cc6ddb 100644 --- a/SocketHttpListener.Portable/Net/HttpListenerRequest.cs +++ b/SocketHttpListener.Portable/Net/HttpListenerRequest.cs @@ -179,9 +179,9 @@ namespace SocketHttpListener.Net } } - if (HasExpect100Continue) + if (String.Compare(Headers["Expect"], "100-continue", StringComparison.OrdinalIgnoreCase) == 0) { - var output = (ResponseStream)context.Connection.GetResponseStream(this); + var output = (ResponseStream)context.Connection.GetResponseStream(true); var _100continue = _textEncoding.GetASCIIEncoding().GetBytes("HTTP/1.1 100 Continue\r\n\r\n"); @@ -189,11 +189,6 @@ namespace SocketHttpListener.Net } } - public bool HasExpect100Continue - { - get { return String.Compare(Headers["Expect"], "100-continue", StringComparison.OrdinalIgnoreCase) == 0; } - } - static bool MaybeUri(string s) { int p = s.IndexOf(':'); diff --git a/SocketHttpListener.Portable/Net/HttpListenerResponse.cs b/SocketHttpListener.Portable/Net/HttpListenerResponse.cs index 8c610d725..93358cae4 100644 --- a/SocketHttpListener.Portable/Net/HttpListenerResponse.cs +++ b/SocketHttpListener.Portable/Net/HttpListenerResponse.cs @@ -149,7 +149,7 @@ namespace SocketHttpListener.Net get { if (output_stream == null) - output_stream = context.Connection.GetResponseStream(context.Request); + output_stream = context.Connection.GetResponseStream(); return output_stream; } } @@ -489,7 +489,7 @@ namespace SocketHttpListener.Net int preamble = encoding.GetPreamble().Length; if (output_stream == null) - output_stream = context.Connection.GetResponseStream(context.Request); + output_stream = context.Connection.GetResponseStream(); /* Assumes that the ms was at position 0 */ ms.Position = preamble; From 3e06bda46b2569c1df47d9ab0c7a763dcf3b1451 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 12 Nov 2016 20:53:51 -0500 Subject: [PATCH 2/2] update components --- .../HttpResponseExtensionsInternal.cs | 4 +--- .../Net/HttpConnection.cs | 18 +++++---------- .../Net/HttpListenerResponse.cs | 23 +------------------ 3 files changed, 8 insertions(+), 37 deletions(-) diff --git a/ServiceStack/HttpResponseExtensionsInternal.cs b/ServiceStack/HttpResponseExtensionsInternal.cs index 318d62429..f78647721 100644 --- a/ServiceStack/HttpResponseExtensionsInternal.cs +++ b/ServiceStack/HttpResponseExtensionsInternal.cs @@ -161,10 +161,8 @@ namespace ServiceStack var responseText = result as string; if (responseText != null) { - if (response.ContentType == null || response.ContentType == "text/html") - response.ContentType = defaultContentType; - var bytes = Encoding.UTF8.GetBytes(responseText); + response.SetContentLength(bytes.Length); await response.OutputStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false); return; } diff --git a/SocketHttpListener.Portable/Net/HttpConnection.cs b/SocketHttpListener.Portable/Net/HttpConnection.cs index d9fd97ed2..b09d02254 100644 --- a/SocketHttpListener.Portable/Net/HttpConnection.cs +++ b/SocketHttpListener.Portable/Net/HttpConnection.cs @@ -209,7 +209,7 @@ namespace SocketHttpListener.Net // TODO: can we get this stream before reading the input? if (o_stream == null) { - if (context.Response.SendChunked || isExpect100Continue) + if (context.Response.SendChunked || isExpect100Continue || context.Response.ContentLength64 <= 0) { o_stream = new ResponseStream(stream, context.Response, _memoryStreamFactory, _textEncoding); } @@ -438,7 +438,9 @@ namespace SocketHttpListener.Net str = String.Format("

{0}

", description); byte[] error = context.Response.ContentEncoding.GetBytes(str); - response.Close(error, false); + response.ContentLength64 = error.Length; + response.OutputStream.Write(error, 0, (int)error.Length); + response.Close(); } catch { @@ -492,7 +494,9 @@ namespace SocketHttpListener.Net { Stream st = GetResponseStream(); if (st != null) + { st.Dispose(); + } o_stream = null; } @@ -514,16 +518,6 @@ namespace SocketHttpListener.Net if (!force_close && context.Request.FlushInput()) { - if (chunked && context.Response.ForceCloseChunked == false) - { - // Don't close. Keep working. - reuses++; - Unbind(); - Init(); - BeginReadRequest(); - return; - } - reuses++; Unbind(); Init(); diff --git a/SocketHttpListener.Portable/Net/HttpListenerResponse.cs b/SocketHttpListener.Portable/Net/HttpListenerResponse.cs index 93358cae4..fb3bc2bdb 100644 --- a/SocketHttpListener.Portable/Net/HttpListenerResponse.cs +++ b/SocketHttpListener.Portable/Net/HttpListenerResponse.cs @@ -30,8 +30,6 @@ namespace SocketHttpListener.Net internal bool HeadersSent; internal object headers_lock = new object(); - bool force_close_chunked; - private readonly ILogger _logger; private readonly ITextEncoding _textEncoding; @@ -50,11 +48,6 @@ namespace SocketHttpListener.Net } } - internal bool ForceCloseChunked - { - get { return force_close_chunked; } - } - public Encoding ContentEncoding { get @@ -327,7 +320,7 @@ namespace SocketHttpListener.Net headers.Add(name, value); } - void Close(bool force) + private void Close(bool force) { if (force) { @@ -345,20 +338,6 @@ namespace SocketHttpListener.Net Close(false); } - public void Close(byte[] responseEntity, bool willBlock) - { - if (disposed) - return; - - if (responseEntity == null) - throw new ArgumentNullException("responseEntity"); - - //TODO: if willBlock -> BeginWrite + Close ? - ContentLength64 = responseEntity.Length; - OutputStream.Write(responseEntity, 0, (int)content_length); - Close(false); - } - public void Redirect(string url) { StatusCode = 302; // Found