Skip to content

Commit

Permalink
Correct line endings. Prepping for ResponseBuilder.
Browse files Browse the repository at this point in the history
  • Loading branch information
statianzo committed Aug 30, 2011
1 parent 3146932 commit 1ee4a03
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 54 deletions.
29 changes: 29 additions & 0 deletions Fleck.sln
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,35 @@ Global
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = Samples\ConsoleApp\ConsoleApp.csproj
Policies = $0
$0.TextStylePolicy = $1
$1.FileWidth = 120
$1.NoTabsAfterNonTabs = True
$1.EolMarker = Windows
$1.inheritsSet = VisualStudio
$1.inheritsScope = text/plain
$1.scope = text/x-csharp
$0.CSharpFormattingPolicy = $2
$2.BeforeMethodDeclarationParentheses = False
$2.BeforeMethodCallParentheses = False
$2.BeforeConstructorDeclarationParentheses = False
$2.BeforeIndexerDeclarationBracket = False
$2.BeforeDelegateDeclarationParentheses = False
$2.AfterDelegateDeclarationParameterComma = True
$2.NewParentheses = False
$2.SpacesBeforeBrackets = False
$2.inheritsSet = Mono
$2.inheritsScope = text/x-csharp
$2.scope = text/x-csharp
$0.DotNetNamingPolicy = $3
$3.DirectoryNamespaceAssociation = PrefixedHierarchical
$3.ResourceNamePolicy = FileFormatDefault
$0.TextStylePolicy = $4
$4.FileWidth = 120
$4.TabWidth = 4
$4.inheritsSet = Mono
$4.inheritsScope = text/plain
$4.scope = text/plain
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
1 change: 0 additions & 1 deletion Samples/ConsoleApp/ConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Fleck.Samples.ConsoleApp</RootNamespace>
<AssemblyName>Fleck.Samples.ConsoleApp</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
Expand Down
4 changes: 1 addition & 3 deletions src/Fleck.Tests/Fleck.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Fleck.Tests</RootNamespace>
<AssemblyName>Fleck.Tests</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Expand Down Expand Up @@ -41,8 +40,7 @@
<Reference Include="Moq, Version=4.0.10827.0, Culture=neutral, PublicKeyToken=69f491c39445e920">
<HintPath>..\..\libs\TestingLibs\Moq.dll</HintPath>
</Reference>
<Reference Include="nunit.framework, Version=2.5.5.10112, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77">
<SpecificVersion>False</SpecificVersion>
<Reference Include="nunit.framework">
<HintPath>..\..\libs\TestingLibs\nunit.framework.dll</HintPath>
</Reference>
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/Fleck/Fleck.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Fleck</RootNamespace>
<AssemblyName>Fleck</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<FileUpgradeFlags>
</FileUpgradeFlags>
Expand Down Expand Up @@ -54,8 +53,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core">
</Reference>
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="DataFrame.cs" />
Expand All @@ -73,6 +71,8 @@
<Compile Include="WebSocketServer.cs" />
<Compile Include="WebsocketHttpRequest.cs" />
<Compile Include="RequestParser.cs" />
<Compile Include="Interfaces\IRequestParser.cs" />
<Compile Include="Interfaces\IResponseBuilder.cs" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
Expand Down
4 changes: 3 additions & 1 deletion src/Fleck/HandshakeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using Fleck.Interfaces;

namespace Fleck
{
Expand All @@ -14,14 +15,15 @@ public HandshakeHandler(string origin, string location, string scheme)
Origin = origin;
Location = location;
Scheme = scheme;
RequestParser = new RequestParser();
}

public string Scheme { get; set; }
public string Origin { get; set; }
public string Location { get; set; }
public ClientHandshake ClientHandshake { get; set; }
public Action<ClientHandshake> OnSuccess { get; set; }

public IRequestParser RequestParser { get; set; }

public void Shake(ISocket socket)
{
Expand Down
11 changes: 11 additions & 0 deletions src/Fleck/Interfaces/IRequestParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace Fleck.Interfaces
{
public interface IRequestParser
{
bool IsComplete(ArraySegment<byte> bytes);
WebSocketHttpRequest Parse(ArraySegment<byte> bytes);
}
}

10 changes: 10 additions & 0 deletions src/Fleck/Interfaces/IResponseBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace Fleck.Interfaces
{
public interface IResponseBuilder
{

}
}

91 changes: 46 additions & 45 deletions src/Fleck/RequestParser.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
using System;
using System.Text;
using System.Text.RegularExpressions;

namespace Fleck
{
public class RequestParser
{
const string pattern = @"^(?<method>[^\s]+)\s(?<path>[^\s]+)\sHTTP\/1\.1\r\n" + // request line
@"((?<field_name>[^:\r\n]+):\s(?<field_value>[^\r\n]+)\r\n)+" + //headers
@"\r\n" + //newline
@"(?<body>.+)?";

private static readonly Regex _regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);

public bool IsComplete(ArraySegment<byte> bytes)
{
var requestString = Encoding.UTF8.GetString(bytes.Array, bytes.Offset, bytes.Count);
return _regex.IsMatch(requestString);
}

public WebSocketHttpRequest Parse(ArraySegment<byte> bytes)
{
var body = Encoding.UTF8.GetString(bytes.Array, bytes.Offset, bytes.Count);
Match match = _regex.Match(body);

var request = new WebSocketHttpRequest{
Method = match.Groups["method"].Value,
Path = match.Groups["path"].Value,
Body = match.Groups["body"].Value
};

var fields = match.Groups["field_name"].Captures;
var values = match.Groups["field_value"].Captures;
for (var i = 0; i < fields.Count; i++) {
var name = fields[i].ToString();
var value = values[i].ToString();
request.Headers[name] = value;
}

return request;
}
}
}

using System;
using System.Text;
using System.Text.RegularExpressions;
using Fleck.Interfaces;

namespace Fleck
{
public class RequestParser : IRequestParser
{
const string pattern = @"^(?<method>[^\s]+)\s(?<path>[^\s]+)\sHTTP\/1\.1\r\n" + // request line
@"((?<field_name>[^:\r\n]+):\s(?<field_value>[^\r\n]+)\r\n)+" + //headers
@"\r\n" + //newline
@"(?<body>.+)?";

private static readonly Regex _regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);

public bool IsComplete(ArraySegment<byte> bytes)
{
var requestString = Encoding.UTF8.GetString(bytes.Array, bytes.Offset, bytes.Count);
return _regex.IsMatch(requestString);
}

public WebSocketHttpRequest Parse(ArraySegment<byte> bytes)
{
var body = Encoding.UTF8.GetString(bytes.Array, bytes.Offset, bytes.Count);
Match match = _regex.Match(body);

var request = new WebSocketHttpRequest{
Method = match.Groups["method"].Value,
Path = match.Groups["path"].Value,
Body = match.Groups["body"].Value
};

var fields = match.Groups["field_name"].Captures;
var values = match.Groups["field_value"].Captures;
for (var i = 0; i < fields.Count; i++) {
var name = fields[i].ToString();
var value = values[i].ToString();
request.Headers[name] = value;
}

return request;
}
}
}

2 changes: 1 addition & 1 deletion src/Fleck/SocketWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Task Authenticate(X509Certificate2 certificate, Action callback, Action<E
_stream = ssl;
Func<AsyncCallback, object, IAsyncResult> begin =
(cb, s) => ssl.BeginAuthenticateAsServer(certificate, false, SslProtocols.Tls, false, cb, s);

Task task = Task.Factory.FromAsync(begin, ssl.EndAuthenticateAsServer, null);
task.ContinueWith(t => callback(), TaskContinuationOptions.NotOnFaulted);
task.ContinueWith(t => error(t.Exception), TaskContinuationOptions.OnlyOnFaulted);
Expand Down

0 comments on commit 1ee4a03

Please sign in to comment.