aboutsummaryrefslogtreecommitdiff
path: root/SocketHttpListener.Portable
diff options
context:
space:
mode:
authorLuke <luke.pulverenti@gmail.com>2016-11-12 17:49:50 -0500
committerGitHub <noreply@github.com>2016-11-12 17:49:50 -0500
commit6cbebfcc4ecf408f0f8dd9d07579045fb55a782e (patch)
tree8915e9036e301656f3e8da53f8aaab85f4298781 /SocketHttpListener.Portable
parentfc6184b15968fb0541f57f4c461aaf8df25e28e0 (diff)
parent102bbe2beb76fa76b21f4ed3f7c584a58d787204 (diff)
Merge pull request #2284 from MediaBrowser/dev
Dev
Diffstat (limited to 'SocketHttpListener.Portable')
-rw-r--r--SocketHttpListener.Portable/Net/HttpConnection.cs19
-rw-r--r--SocketHttpListener.Portable/Net/HttpListenerRequest.cs11
-rw-r--r--SocketHttpListener.Portable/Net/HttpListenerResponse.cs6
-rw-r--r--SocketHttpListener.Portable/Net/ResponseStream.cs10
4 files changed, 31 insertions, 15 deletions
diff --git a/SocketHttpListener.Portable/Net/HttpConnection.cs b/SocketHttpListener.Portable/Net/HttpConnection.cs
index db34c42185..67dd5c9584 100644
--- a/SocketHttpListener.Portable/Net/HttpConnection.cs
+++ b/SocketHttpListener.Portable/Net/HttpConnection.cs
@@ -23,7 +23,7 @@ namespace SocketHttpListener.Net
StringBuilder current_line;
ListenerPrefix prefix;
RequestStream i_stream;
- ResponseStream o_stream;
+ Stream o_stream;
bool chunked;
int reuses;
bool context_bound;
@@ -204,12 +204,23 @@ namespace SocketHttpListener.Net
return i_stream;
}
- public ResponseStream GetResponseStream()
+ public Stream GetResponseStream(HttpListenerRequest request)
{
// TODO: can we get this stream before reading the input?
if (o_stream == null)
{
- o_stream = new ResponseStream(stream, context.Response, _memoryStreamFactory, _textEncoding);
+ if (context.Response.SendChunked || request == null || request.HasExpect100Continue)
+ {
+ o_stream = new ResponseStream(stream, context.Response, _memoryStreamFactory, _textEncoding);
+ }
+ else
+ {
+ o_stream = stream;
+ using (var headerStream = ResponseStream.GetHeaders(context.Response, _memoryStreamFactory, false))
+ {
+ headerStream.CopyTo(o_stream);
+ }
+ }
}
return o_stream;
}
@@ -479,7 +490,7 @@ namespace SocketHttpListener.Net
{
if (!context.Request.IsWebSocketRequest || force_close)
{
- Stream st = GetResponseStream();
+ Stream st = GetResponseStream(context.Request);
if (st != null)
st.Dispose();
diff --git a/SocketHttpListener.Portable/Net/HttpListenerRequest.cs b/SocketHttpListener.Portable/Net/HttpListenerRequest.cs
index 63d5e510d2..767f1c5425 100644
--- a/SocketHttpListener.Portable/Net/HttpListenerRequest.cs
+++ b/SocketHttpListener.Portable/Net/HttpListenerRequest.cs
@@ -179,16 +179,21 @@ namespace SocketHttpListener.Net
}
}
- if (String.Compare(Headers["Expect"], "100-continue", StringComparison.OrdinalIgnoreCase) == 0)
+ if (HasExpect100Continue)
{
- ResponseStream output = context.Connection.GetResponseStream();
-
+ var output = (ResponseStream)context.Connection.GetResponseStream(this);
+
var _100continue = _textEncoding.GetASCIIEncoding().GetBytes("HTTP/1.1 100 Continue\r\n\r\n");
output.InternalWrite(_100continue, 0, _100continue.Length);
}
}
+ 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 0bc827b5a4..8c610d725a 100644
--- a/SocketHttpListener.Portable/Net/HttpListenerResponse.cs
+++ b/SocketHttpListener.Portable/Net/HttpListenerResponse.cs
@@ -19,7 +19,7 @@ namespace SocketHttpListener.Net
CookieCollection cookies;
WebHeaderCollection headers = new WebHeaderCollection();
bool keep_alive = true;
- ResponseStream output_stream;
+ Stream output_stream;
Version version = HttpVersion.Version11;
string location;
int status_code = 200;
@@ -149,7 +149,7 @@ namespace SocketHttpListener.Net
get
{
if (output_stream == null)
- output_stream = context.Connection.GetResponseStream();
+ output_stream = context.Connection.GetResponseStream(context.Request);
return output_stream;
}
}
@@ -489,7 +489,7 @@ namespace SocketHttpListener.Net
int preamble = encoding.GetPreamble().Length;
if (output_stream == null)
- output_stream = context.Connection.GetResponseStream();
+ output_stream = context.Connection.GetResponseStream(context.Request);
/* Assumes that the ms was at position 0 */
ms.Position = preamble;
diff --git a/SocketHttpListener.Portable/Net/ResponseStream.cs b/SocketHttpListener.Portable/Net/ResponseStream.cs
index 07788ea418..7a6425deac 100644
--- a/SocketHttpListener.Portable/Net/ResponseStream.cs
+++ b/SocketHttpListener.Portable/Net/ResponseStream.cs
@@ -64,7 +64,7 @@ namespace SocketHttpListener.Net
{
disposed = true;
byte[] bytes = null;
- MemoryStream ms = GetHeaders(true);
+ MemoryStream ms = GetHeaders(response, _memoryStreamFactory, false);
bool chunked = response.SendChunked;
if (stream.CanWrite)
{
@@ -102,14 +102,14 @@ namespace SocketHttpListener.Net
base.Dispose(disposing);
}
- MemoryStream GetHeaders(bool closing)
+ internal static MemoryStream GetHeaders(HttpListenerResponse response, IMemoryStreamFactory memoryStreamFactory, bool closing)
{
// SendHeaders works on shared headers
lock (response.headers_lock)
{
if (response.HeadersSent)
return null;
- MemoryStream ms = _memoryStreamFactory.CreateNew();
+ MemoryStream ms = memoryStreamFactory.CreateNew();
response.SendHeaders(closing, ms);
return ms;
}
@@ -137,7 +137,7 @@ namespace SocketHttpListener.Net
throw new ObjectDisposedException(GetType().ToString());
byte[] bytes = null;
- MemoryStream ms = GetHeaders(false);
+ MemoryStream ms = GetHeaders(response, _memoryStreamFactory, false);
bool chunked = response.SendChunked;
if (ms != null)
{
@@ -177,7 +177,7 @@ namespace SocketHttpListener.Net
throw new ObjectDisposedException(GetType().ToString());
byte[] bytes = null;
- MemoryStream ms = GetHeaders(false);
+ MemoryStream ms = GetHeaders(response, _memoryStreamFactory, false);
bool chunked = response.SendChunked;
if (ms != null)
{