Skip to content

Commit

Permalink
- Tons of bug fixes
Browse files Browse the repository at this point in the history
- New HeaderWriter class to move some code out of FosRequest
- Sanitized the Listener's loop quite a bit
- Changes to adapt to FastCgiNet
  • Loading branch information
mzabani committed Nov 27, 2013
1 parent 98f9319 commit 31548f7
Show file tree
Hide file tree
Showing 21 changed files with 481 additions and 311 deletions.
4 changes: 2 additions & 2 deletions Fos.Tests/Browser/Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Browser
private Socket SocketToUse;

protected FastCgiStream ResponseStream;
protected SocketRequest Request;
protected ApplicationSocketRequest Request;
protected ushort RequestId
{
get
Expand All @@ -34,7 +34,7 @@ protected void SendBeginRequest(Socket sock)
var beginRequestRecord = new BeginRequestRecord(requestId);
beginRequestRecord.ApplicationMustCloseConnection = true;
beginRequestRecord.Role = Role.Responder;
Request = new SocketRequest(sock, beginRequestRecord, true);
Request = new ApplicationSocketRequest(sock, beginRequestRecord);
Request.Send(beginRequestRecord);
}

Expand Down
9 changes: 4 additions & 5 deletions Fos.Tests/Browser/BrowserResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public BrowserResponse(Stream response)
while ((line = reader.ReadLine()) != string.Empty)
{
spaceIdx = line.IndexOf(' ');
Console.WriteLine(line);
string headerName = line.Substring(0, spaceIdx - 1);
string headerValue = line.Substring(spaceIdx + 1);
Console.WriteLine("{0}: {1}", headerName, headerValue);
//Console.WriteLine("{0}: {1}", headerName, headerValue);
Headers.Add(headerName, headerValue);
}


Console.WriteLine ("4");

// TODO: Why do we need to do this? Something is very strange here!
Expand All @@ -54,10 +54,9 @@ public BrowserResponse(Stream response)
Console.Write ((char)buf[i]);
ResponseBody.Write(buf, 0, bytesRead);
}

ResponseBody.Position = 0;
response.Dispose();
}

ResponseBody.Position = 0;
}

public void Dispose()
Expand Down
4 changes: 3 additions & 1 deletion Fos.Tests/Fos.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
<Compile Include="SocketTests.cs" />
<Compile Include="Logger\OneRequestTestLogger.cs" />
<Compile Include="ResponseAndRequestHeaders.cs" />
<Compile Include="StdoutStream.cs" />
<Compile Include="Streams\StdoutStream.cs" />
<Compile Include="Streams\HeaderWriter.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
Expand All @@ -72,5 +73,6 @@
<Folder Include="Middleware\" />
<Folder Include="Browser\" />
<Folder Include="Logger\" />
<Folder Include="Streams\" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion Fos.Tests/Logger/AbruptConnectionClosing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void CloseConnectionAbruptlyAfterSendingBeginRequestRecord()
var beginReq = new BeginRequestRecord(1);
using (var req = new FosRequest(sock, logger))
{
req.SetBeginRequest(beginReq);
req.AddReceivedRecord(beginReq);
req.Send(beginReq);
}
}
Expand Down
9 changes: 9 additions & 0 deletions Fos.Tests/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ namespace Fos.Tests
[TestFixture]
public class Startup
{
[Test]
public void CloseAndFlush()
{
var memStream = new System.IO.MemoryStream();

memStream.Close();
memStream.Flush();
}

[Test]
public void StartAndDispose()
{
Expand Down
31 changes: 31 additions & 0 deletions Fos.Tests/Streams/HeaderWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Linq;
using NUnit.Framework;
using Fos;
using Fos.Streams;
using System.IO;

namespace Fos.Tests
{
[TestFixture]
public class HeaderWriterTests
{
[Test]
public void OnFirstWriteAndOnStreamFillEvents()
{
byte[] buf = new byte[1024];
using (var s = new MemoryStream(buf))
{
using (var writer = new HeaderWriter(s))
{
writer.Write("Status", "200");
writer.WriteLine();
}
}

int writtenLength = "Status: 200\r\n\r\n".Length;
var contents = System.Text.ASCIIEncoding.ASCII.GetString(buf, 0, writtenLength);
Assert.AreEqual("Status: 200\r\n\r\n", contents);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Linq;
using NUnit.Framework;
using Fos;
using Fos.Listener;
using Fos.Streams;
using System.IO;

namespace Fos.Tests
Expand All @@ -26,9 +26,7 @@ public void OnFirstWriteAndOnStreamFillEvents()
numFirstWrites++; };

int numStreamFills = 0;
Stream lastFilledStream = null;
s.OnStreamFill += (filledStream) => {
lastFilledStream = filledStream;
s.OnStreamFill += () => {
numStreamFills++;
};

Expand All @@ -43,7 +41,6 @@ public void OnFirstWriteAndOnStreamFillEvents()
int numFilledStreams = 5;
int chunkSize = 65535 * numFilledStreams + 1;
byte[] hugeChunk = new byte[chunkSize];
Assert.IsNull(lastFilledStream);
Assert.AreEqual(0, numStreamFills);
s.Write(hugeChunk, 0, chunkSize);
Assert.AreEqual(numFilledStreams, numStreamFills);
Expand Down
7 changes: 5 additions & 2 deletions Fos/Fos.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
<Compile Include="CustomPages\EmptyResponsePage.cs" />
<Compile Include="CustomPages\ApplicationErrorPage.cs" />
<Compile Include="CustomPages\ICustomPage.cs" />
<Compile Include="Listener\ByteReaderAndRequest.cs" />
<Compile Include="Listener\SocketListener.cs" />
<Compile Include="Listener\FosRequest.cs" />
<Compile Include="Logging\IServerLogger.cs" />
Expand All @@ -60,7 +59,10 @@
<Compile Include="Owin\FosAppBuilder.cs" />
<Compile Include="Middleware\FosOwinRoot.cs" />
<Compile Include="SocketHelper.cs" />
<Compile Include="Listener\FosStdoutStream.cs" />
<Compile Include="Streams\FosStdoutStream.cs" />
<Compile Include="Listener\RecordFactoryAndRequest.cs" />
<Compile Include="Streams\HeaderWriter.cs" />
<Compile Include="Streams\NonEndingStdoutSocketStream.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
Expand All @@ -69,6 +71,7 @@
<Folder Include="Logging\" />
<Folder Include="CustomPages\" />
<Folder Include="Logging\StatsLogging\" />
<Folder Include="Streams\" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
2 changes: 1 addition & 1 deletion Fos/FosSelfHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private void OnReceiveStdin(FosRequest req, StdinRecord rec)
//TODO: The task may have failed or something else happened, verify
// Remember that connections closed by the other side abruptly have already
// been closed by the listener loop, so we shouldn't call Request.CloseSocket() here again
if (t.Result)
if (req.ApplicationMustCloseConnection)
{
req.Dispose();
}
Expand Down
34 changes: 0 additions & 34 deletions Fos/Listener/ByteReaderAndRequest.cs

This file was deleted.

Loading

0 comments on commit 31548f7

Please sign in to comment.