Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Microsoft.AspNet.Server.Kestrel/Http/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public Connection(ListenerContext context, UvStreamHandle socket) : base(context

_connectionId = Interlocked.Increment(ref _lastConnectionId);

_rawSocketInput = new SocketInput(Memory2);
_rawSocketOutput = new SocketOutput(Thread, _socket, _connectionId, Log);
_rawSocketInput = new SocketInput(InputMemory);
_rawSocketOutput = new SocketOutput(OutputMemory, Thread, _socket, _connectionId, Log);
}

public void Start()
Expand Down Expand Up @@ -89,7 +89,7 @@ public void Start()

private void ApplyConnectionFilter()
{
var filteredStreamAdapter = new FilteredStreamAdapter(_filterContext.Connection, Memory2, Log);
var filteredStreamAdapter = new FilteredStreamAdapter(_filterContext.Connection, InputMemory, Log);

SocketInput = filteredStreamAdapter.SocketInput;
SocketOutput = filteredStreamAdapter.SocketOutput;
Expand Down
28 changes: 15 additions & 13 deletions src/Microsoft.AspNet.Server.Kestrel/Http/Frame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
{
public partial class Frame : FrameContext, IFrameControl
{
private const string _endLine = "\r\n";
private const string _endDelimiter = ": ";

private static readonly Encoding _ascii = Encoding.ASCII;
private static readonly ArraySegment<byte> _endChunkBytes = CreateAsciiByteArraySegment("\r\n");
private static readonly ArraySegment<byte> _endChunkedResponseBytes = CreateAsciiByteArraySegment("0\r\n\r\n");
private static readonly ArraySegment<byte> _continueBytes = CreateAsciiByteArraySegment("HTTP/1.1 100 Continue\r\n\r\n");
private static readonly ArraySegment<byte> _emptyData = new ArraySegment<byte>(new byte[0]);
private static readonly byte[] _hex = Encoding.ASCII.GetBytes("0123456789abcdef");

private readonly object _onStartingSync = new Object();
private readonly object _onCompletedSync = new Object();
private readonly object _onStartingSync = new object();
private readonly object _onCompletedSync = new object();
private readonly FrameRequestHeaders _requestHeaders = new FrameRequestHeaders();
private readonly byte[] _nullBuffer = new byte[4096];
private readonly FrameResponseHeaders _responseHeaders = new FrameResponseHeaders();

private List<KeyValuePair<Func<object, Task>, object>> _onStarting;
Expand Down Expand Up @@ -197,8 +201,10 @@ public async Task RequestProcessingAsync()

await ProduceEnd();

// Finish reading the request body in case the app did not.
await RequestBody.CopyToAsync(Stream.Null);
while (await RequestBody.ReadAsync(_nullBuffer, 0, _nullBuffer.Length) != 0)
{
// Finish reading the request body in case the app did not.
}
}

terminated = !_keepAlive;
Expand Down Expand Up @@ -492,8 +498,7 @@ private Tuple<ArraySegment<byte>, IDisposable> CreateResponseHeader(
writer.Write(HttpVersion);
writer.Write(' ');
writer.Write(status);
writer.Write('\r');
writer.Write('\n');
writer.Write(_endLine);

var hasConnection = false;
var hasTransferEncoding = false;
Expand Down Expand Up @@ -521,11 +526,9 @@ private Tuple<ArraySegment<byte>, IDisposable> CreateResponseHeader(
foreach (var value in header.Value)
{
writer.Write(header.Key);
writer.Write(':');
writer.Write(' ');
writer.Write(_endDelimiter);
writer.Write(value);
writer.Write('\r');
writer.Write('\n');
writer.Write(_endLine);

if (isConnection && value.IndexOf("close", StringComparison.OrdinalIgnoreCase) != -1)
{
Expand Down Expand Up @@ -572,8 +575,7 @@ private Tuple<ArraySegment<byte>, IDisposable> CreateResponseHeader(
}
else
{
writer.Write('\r');
writer.Write('\n');
writer.Write(_endLine);
}
writer.Flush();
return new Tuple<ArraySegment<byte>, IDisposable>(writer.Buffer, writer);
Expand Down Expand Up @@ -751,7 +753,7 @@ public static bool TakeMessageHeaders(SocketInput input, FrameRequestHeaders req
var value = beginValue.GetString(endValue);
if (wrapping)
{
value = value.Replace("\r\n", " ");
value = value.Replace(_endLine, " ");
}

consumed = scan;
Expand Down
13 changes: 9 additions & 4 deletions src/Microsoft.AspNet.Server.Kestrel/Http/ListenerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ public class ListenerContext : ServiceContext
{
public ListenerContext()
{
Memory2 = new MemoryPool2();
InputMemory = new MemoryPool2();
OutputMemory = new MemoryPool2();
}

public ListenerContext(ServiceContext serviceContext)
: base(serviceContext)
{
Memory2 = new MemoryPool2();
InputMemory = new MemoryPool2();
OutputMemory = new MemoryPool2();
}

public ListenerContext(ListenerContext listenerContext)
Expand All @@ -26,7 +28,8 @@ public ListenerContext(ListenerContext listenerContext)
ServerAddress = listenerContext.ServerAddress;
Thread = listenerContext.Thread;
Application = listenerContext.Application;
Memory2 = listenerContext.Memory2;
InputMemory = listenerContext.InputMemory;
OutputMemory = listenerContext.OutputMemory;
Log = listenerContext.Log;
}

Expand All @@ -36,6 +39,8 @@ public ListenerContext(ListenerContext listenerContext)

public Func<Frame, Task> Application { get; set; }

public MemoryPool2 Memory2 { get; set; }
public MemoryPool2 InputMemory { get; set; }

public MemoryPool2 OutputMemory { get; set; }
}
}
9 changes: 6 additions & 3 deletions src/Microsoft.AspNet.Server.Kestrel/Http/ListenerPrimary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ abstract public class ListenerPrimary : Listener

// this message is passed to write2 because it must be non-zero-length,
// but it has no other functional significance
private readonly ArraySegment<ArraySegment<byte>> _dummyMessage = new ArraySegment<ArraySegment<byte>>(new[] { new ArraySegment<byte>(new byte[] { 1, 2, 3, 4 }) });
private readonly byte[] _dummyBuffer = new byte[] { 1, 2, 3, 4 };

protected ListenerPrimary(ServiceContext serviceContext) : base(serviceContext)
{
Expand Down Expand Up @@ -79,14 +79,17 @@ protected override void DispatchConnection(UvStreamHandle socket)
}
else
{
var msg = MemoryPoolBlock2.Create(new ArraySegment<byte>(_dummyBuffer), IntPtr.Zero, null, null);
msg.End = msg.Start + _dummyBuffer.Length;

var dispatchPipe = _dispatchPipes[index];
var write = new UvWriteReq(Log);
write.Init(Thread.Loop);
write.Write2(
dispatchPipe,
_dummyMessage,
new ArraySegment<MemoryPoolBlock2>(new[] { msg }),
socket,
(write2, status, error, state) =>
(write2, status, error, bytesWritten, state) =>
{
write2.Dispose();
((UvStreamHandle)state).Dispose();
Expand Down
20 changes: 7 additions & 13 deletions src/Microsoft.AspNet.Server.Kestrel/Http/MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Collections.Concurrent;

namespace Microsoft.AspNet.Server.Kestrel.Http
{
Expand Down Expand Up @@ -95,29 +95,23 @@ public void FreeSegment(ArraySegment<byte> segment)

class Pool<T>
{
private readonly Stack<T[]> _stack = new Stack<T[]>();
private readonly object _sync = new object();
private readonly ConcurrentQueue<T[]> _queue = new ConcurrentQueue<T[]>();

public T[] Alloc(int size)
{
lock (_sync)
T[] item;
if (_queue.TryDequeue(out item))
{
if (_stack.Count != 0)
{
return _stack.Pop();
}
return item;
}
return new T[size];
}

public void Free(T[] value, int limit)
{
lock (_sync)
if (_queue.Count < limit)
{
if (_stack.Count < limit)
{
_stack.Push(value);
}
_queue.Enqueue(value);
}
}
}
Expand Down
34 changes: 12 additions & 22 deletions src/Microsoft.AspNet.Server.Kestrel/Http/MemoryPoolTextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Microsoft.AspNet.Server.Kestrel.Http
{
public class MemoryPoolTextWriter : TextWriter
public class MemoryPoolTextWriter : IDisposable
{
private readonly IMemoryPool _memory;

Expand Down Expand Up @@ -39,35 +39,25 @@ public ArraySegment<byte> Buffer
}
}

public override Encoding Encoding
public Encoding Encoding
{
get
{
return Encoding.UTF8;
}
}

protected override void Dispose(bool disposing)
public void Dispose()
{
try
if (_textArray != null)
{
if (disposing)
{
if (_textArray != null)
{
_memory.FreeChar(_textArray);
_textArray = null;
}
if (_dataArray != null)
{
_memory.FreeByte(_dataArray);
_dataArray = null;
}
}
_memory.FreeChar(_textArray);
_textArray = null;
}
finally
if (_dataArray != null)
{
base.Dispose(disposing);
_memory.FreeByte(_dataArray);
_dataArray = null;
}
}

Expand Down Expand Up @@ -107,7 +97,7 @@ private void Grow(int minimumAvailable)
_dataArray = newArray;
}

public override void Write(char value)
public void Write(char value)
{
if (_textLength == _textEnd)
{
Expand All @@ -121,7 +111,7 @@ public override void Write(char value)
_textArray[_textEnd++] = value;
}

public override void Write(string value)
public void Write(string value)
{
var sourceIndex = 0;
var sourceLength = value.Length;
Expand All @@ -144,7 +134,7 @@ public override void Write(string value)
}
}

public override void Flush()
public void Flush()
{
while (_textBegin != _textEnd)
{
Expand Down
Loading