forked from statianzo/Fleck
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correct line endings. Prepping for ResponseBuilder.
- Loading branch information
Showing
9 changed files
with
104 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
|
||
namespace Fleck.Interfaces | ||
{ | ||
public interface IResponseBuilder | ||
{ | ||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters