Skip to content

Commit

Permalink
- Header dictionary has a class for itself now, and some tests
Browse files Browse the repository at this point in the history
- Some additions to OwinContext
- Much improved StatsLogger and StatsPageMiddleware (still public)
- ShuntMiddleware added to map requests to a different IAppBuilder based on request path matching
- License added (BSD License)
  • Loading branch information
mzabani committed Nov 21, 2013
1 parent a78ac7e commit c5bd6c7
Show file tree
Hide file tree
Showing 19 changed files with 595 additions and 75 deletions.
26 changes: 26 additions & 0 deletions COPYING
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright (c) 2013, Marcelo Zabani
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
1 change: 1 addition & 0 deletions Fos.Tests/Fos.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<Compile Include="Logger\NormalConnectionClosing.cs" />
<Compile Include="SocketTests.cs" />
<Compile Include="Logger\OneRequestTestLogger.cs" />
<Compile Include="ResponseAndRequestHeaders.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Fos.Tests/FragmentedResponseStreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void OnFirstWriteAndOnStreamFillEvents()
int numFilledStreams = 5;
int chunkSize = 65535 * numFilledStreams + 1;
byte[] hugeChunk = new byte[chunkSize];
Assert.AreEqual(null, lastFilledStream);
Assert.IsNull(lastFilledStream);
Assert.AreEqual(0, numStreamFills);
s.Write(hugeChunk, 0, chunkSize);
Assert.AreEqual(numFilledStreams, numStreamFills);
Expand Down
36 changes: 31 additions & 5 deletions Fos.Tests/Logger/AbruptConnectionClosing.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Owin;
using NUnit.Framework;
using FastCgiNet;
using Fos;
using Fos.Logging;
using System.Net.Sockets;
Expand All @@ -26,18 +27,43 @@ public void CloseConnectionAbruptlyBeforeSendingAnyRecord()
sock.Close();
}

//TODO: Can't we do better than this?
System.Threading.Thread.Sleep(10);
}

Assert.AreEqual(true, logger.ConnectionWasReceived);
Assert.AreEqual(false, logger.ConnectionClosedNormally);
Assert.AreEqual(null, logger.RequestInfo);
Assert.AreEqual(true, logger.ConnectionClosedAbruptlyWithoutAnyRequestInfo);
Assert.IsTrue(logger.ConnectionWasReceived);
Assert.IsFalse(logger.ConnectionClosedNormally);
Assert.IsNull(logger.RequestInfo);
Assert.IsTrue(logger.ConnectionClosedAbruptlyWithoutAnyRequestInfo);
}

[Ignore]
[Test]
public void CloseConnectionAbruptlyAfterSendingBeginRequestRecord()
{
var logger = new OneRequestTestLogger();

using (var server = GetHelloWorldBoundServer())
{
server.SetLogger(logger);
server.Start(true);

// Just connect and quit
using (var sock = ConnectAndGetSocket())
{
var beginReq = new BeginRequestRecord(1);
var req = new Request(sock, beginReq);
req.Send(beginReq);
sock.Close();
}

//TODO: Can't we do better than this?
System.Threading.Thread.Sleep(10);
}

Assert.IsTrue(logger.ConnectionWasReceived);
Assert.IsFalse(logger.ConnectionClosedNormally);
Assert.IsNotNull(logger.RequestInfo);
Assert.IsFalse(logger.ConnectionClosedAbruptlyWithoutAnyRequestInfo);
}

[Ignore]
Expand Down
14 changes: 7 additions & 7 deletions Fos.Tests/Logger/NormalConnectionClosing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public void StartAndStop()
server.SetLogger(logger);
server.Start(true);

Assert.AreEqual(true, logger.ServerWasStarted);
Assert.AreEqual(false, logger.ServerWasStopped);
Assert.IsTrue(logger.ServerWasStarted);
Assert.IsFalse(logger.ServerWasStopped);
}

Assert.AreEqual(true, logger.ServerWasStopped);
Assert.AreEqual(false, logger.ConnectionWasReceived);
Assert.IsTrue(logger.ServerWasStopped);
Assert.IsFalse(logger.ConnectionWasReceived);
}

[Test]
Expand Down Expand Up @@ -63,10 +63,10 @@ public void CheckBasicData()
browser.ExecuteRequest("http://localhost/", "GET");
}

Assert.AreEqual(true, logger.ConnectionWasReceived);
Assert.AreEqual(true, logger.ConnectionClosedNormally);
Assert.IsTrue(logger.ConnectionWasReceived);
Assert.IsTrue(logger.ConnectionClosedNormally);
Assert.That(logger.RequestInfo != null && logger.RequestInfo.RelativePath == "/" && logger.RequestInfo.ResponseStatusCode== 200);
Assert.AreEqual(false, logger.ConnectionClosedAbruptlyWithoutAnyRequestInfo);
Assert.IsFalse(logger.ConnectionClosedAbruptlyWithoutAnyRequestInfo);
}
}
}
6 changes: 3 additions & 3 deletions Fos.Tests/OwinContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@ public void PartialContextNoMethod()
{
var ctx = new OwinContext("1.0", TokenSource.Token);

Assert.AreEqual(false, ctx.HttpMethodDefined);
Assert.IsFalse(ctx.HttpMethodDefined);
}

[Test]
public void PartialContextNoUri()
{
var ctx = new OwinContext("1.0", TokenSource.Token);

Assert.AreEqual(false, ctx.RelativePathDefined);
Assert.IsFalse(ctx.RelativePathDefined);
}

[Test]
public void PartialContextNoResponse()
{
var ctx = new OwinContext("1.0", TokenSource.Token);

Assert.AreEqual(false, ctx.SomeResponseExists);
Assert.IsFalse(ctx.SomeResponseExists);
}
}
}
37 changes: 37 additions & 0 deletions Fos.Tests/ResponseAndRequestHeaders.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using NUnit.Framework;
using Fos;

namespace Fos.Tests
{
[TestFixture]
public class ResponseAndRequestHeaders
{
[Test]
public void HeadersValuesAreArrayCopies()
{
var headerDict = new HeaderDictionary();
headerDict.Add("Name", new string[1] { "Value" });

string[] copy = headerDict["Name"];

Assert.AreEqual("Value", copy [0]);

// Change and make sure it is still the same in the original!
copy [0] = "Something different";

string[] copy2 = headerDict["Name"];
Assert.AreEqual("Value", copy2 [0]);
}

[Test]
public void OrdinalInsensitiveKeyComparison()
{
var headerDict = new HeaderDictionary();
headerDict.Add("Name", new string[1] { "Value" });

Assert.Throws<ArgumentException>(() => headerDict.Add("nAmE", new string[1] { "whatever" }));
}
}
}

2 changes: 1 addition & 1 deletion Fos/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.

[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.1.0.0")]

// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.
Expand Down
4 changes: 4 additions & 0 deletions Fos/Fos.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
<Compile Include="Logging\StatsLogger.cs" />
<Compile Include="Logging\IServerLogger.cs" />
<Compile Include="Logging\RequestInfo.cs" />
<Compile Include="Logging\StatsPageMiddleware.cs" />
<Compile Include="Logging\ApplicationError.cs" />
<Compile Include="Middleware\ShuntMiddleware.cs" />
<Compile Include="Owin\HeaderDictionary.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Fos/FosSelfHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public bool IsRunning
/// <summary>
/// Starts this FastCgi server! This method only returns when the server is ready to accept connections.
/// </summary>
/// <param name="background">True if this method starts the server without blocking, false to block.</param>
/// <param name="background">True to start the server without blocking, false to block.</param>
public void Start(bool background)
{
AppBuilder = new FCgiAppBuilder(OnAppDisposal.Token);
Expand Down
15 changes: 14 additions & 1 deletion Fos/Listener/SocketListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,18 @@ private void Work()
}
catch (Exception e)
{
var sockEx = e as SocketException;
if (sockEx != null)
{
// Connection reset is a very rude way of the other side closing the connection, but it could happen.
if (sockEx.SocketErrorCode == SocketError.ConnectionReset)
continue;

// Interrupted can also happen
else if (sockEx.SocketErrorCode == SocketError.Interrupted)
continue;
}

if (Logger != null)
Logger.LogServerError(e, "Exception would end the data receiving loop. This is extremely bad. Please file a bug report.");
}
Expand Down Expand Up @@ -270,7 +282,8 @@ void BeginAcceptNewConnections(Socket listenSocket)
}
catch (Exception e)
{
//TODO: Handle it
if (Logger != null)
Logger.LogSocketError(listenSocket, e, "Error when accepting connection on the listen socket.");
}
}

Expand Down
34 changes: 34 additions & 0 deletions Fos/Logging/ApplicationError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;

namespace Fos.Logging
{
public class ApplicationError
{
public string HttpMethod { get; private set; }
public string RelativePath { get; private set; }

/// <summary>
/// All request cookies concatenaded as one big string.
/// </summary>
public string Cookies { get; private set; }

public Exception Error { get; private set; }

/// <summary>
/// The time when the application error happened, in UTC.
/// </summary>
public DateTime When { get; private set; }

internal ApplicationError(string verb, string relativePath, Exception e)
{
HttpMethod = verb;
RelativePath = relativePath;
Error = e;

//TODO: Cookies

When = DateTime.UtcNow;
}
}
}

Loading

0 comments on commit c5bd6c7

Please sign in to comment.