Permalink
Browse files

Merge pull request #85 from FlorianHockmann/master

Add validity checks for HTTP datagrams
  • Loading branch information...
2 parents 0cd9a9c + e9071e5 commit 57a21ac515bc81cb5b94f49e8ae95d6ab5506e92 @bricknerb bricknerb committed on GitHub Jan 27, 2017
@@ -108,6 +108,7 @@ public void RandomHttpTest()
if (httpLayer.Header != null)
{
Assert.AreEqual(httpLayer.Header.GetHashCode(), httpDatagram.Header.GetHashCode());
+ Assert.IsTrue(httpDatagram.IsValidStart, "IsValidStart");
foreach (var field in httpLayer.Header)
Assert.IsFalse(field.Equals("abc"));
@@ -470,6 +471,86 @@ public void HttpRequestMethodBadKnownTest()
Assert.IsNotNull(new HttpRequestMethod(HttpRequestKnownMethod.Unknown));
}
+ [TestMethod]
+ public void HttpIsValidStartRequestValidTest()
+ {
+ var packet = BuildPacket("UnknownMethod / HTTP/1.0\r\n\r\n");
+
+ Assert.IsTrue(packet.Ethernet.IpV4.Tcp.Http.IsValidStart);
+ }
+
+ [TestMethod]
+ public void HttpIsValidStartRequestWithMissingUriNotAllowedTest()
+ {
+ var packet = BuildPacket("GET HTTP/1.1\r\n\r\n");
+
+ Assert.IsFalse(packet.Ethernet.IpV4.Tcp.Http.IsValidStart);
+ }
+
+ [TestMethod]
+ public void HttpIsValidStartRequestWithMissingVersionNumberNotAllowedTest()
+ {
+ var packet = BuildPacket("GET / HTTP/\r\n\r\n");
+
+ Assert.IsFalse(packet.Ethernet.IpV4.Tcp.Http.IsValidStart);
+ }
+
+ [TestMethod]
+ public void HttpIsValidStartRequestWithMissingVersionNotAllowedTest()
+ {
+ var packet = BuildPacket("GET /\r\n\r\n");
+
+ Assert.IsFalse(packet.Ethernet.IpV4.Tcp.Http.IsValidStart);
+ }
+
+ [TestMethod]
+ public void HttpIsValidStartRequestWithMissingMethodNotAllowedTest()
+ {
+ var packet = BuildPacket(" / HTTP/1.0\r\n\r\n");
+
+ Assert.IsFalse(packet.Ethernet.IpV4.Tcp.Http.IsValidStart);
+ }
+
+ [TestMethod]
+ public void HttpIsValidStartResponseValidTest()
+ {
+ var packet = BuildPacket("HTTP/1.0 200 OK\r\n\r\n");
+
+ Assert.IsTrue(packet.Ethernet.IpV4.Tcp.Http.IsValidStart);
+ }
+
+ [TestMethod]
+ public void HttpIsValidStartResponseMinimalValidTest()
+ {
+ var packet = BuildPacket("HTTP/1.0 200 \r\n\r\n");
+
+ Assert.IsTrue(packet.Ethernet.IpV4.Tcp.Http.IsValidStart);
+ }
+
+ [TestMethod]
+ public void HttpIsValidStartResponseWithMissingVersionNumberNotAllowedTest()
+ {
+ var packet = BuildPacket("HTTP/ 200 OK\r\n\r\n");
+
+ Assert.IsFalse(packet.Ethernet.IpV4.Tcp.Http.IsValidStart);
+ }
+
+ [TestMethod]
+ public void HttpIsValidStartResponseWithMissingVersionNotAllowed()
+ {
+ var packet = BuildPacket(" 200 OK\r\n\r\n");
+
+ Assert.IsFalse(packet.Ethernet.IpV4.Tcp.Http.IsValidStart);
+ }
+
+ [TestMethod]
+ public void HttpIsValidStartResponseWithMissingStatusCodeNotAllowedTest()
+ {
+ var packet = BuildPacket("HTTP/1.0 OK \r\n\r\n");
+
+ Assert.IsFalse(packet.Ethernet.IpV4.Tcp.Http.IsValidStart);
+ }
+
private static void TestHttpRequest(string httpString, string expectedMethodString = null, string expectedUri = null, HttpVersion expectedVersion = null, HttpHeader expectedHeader = null, string expectedBodyString = null)
{
Datagram expectedBody = expectedBodyString == null ? null : new Datagram(Encoding.ASCII.GetBytes(expectedBodyString));
@@ -239,6 +239,24 @@ internal class ParseInfoBase
/// </summary>
public Datagram Body { get; private set; }
+ /// <summary>
+ /// True if this datagram contains a valid start for an HTTP message.
+ /// </summary>
+ public bool IsValidStart
+ {
+ get
+ {
+ if (_isValidStart == null)
+ _isValidStart = CalculateIsValidStart();
+ return _isValidStart.Value;
+ }
+ }
+
+ /// <summary>
+ /// Calculate whether the HTTP datagram has a valid start.
+ /// </summary>
+ protected abstract bool CalculateIsValidStart();
+
internal static HttpDatagram CreateDatagram(byte[] buffer, int offset, int length)
{
if (length >= _httpSlash.Length && buffer.SequenceEqual(offset, _httpSlash, 0, _httpSlash.Length))
@@ -355,5 +373,7 @@ private static Datagram ParseChunkedBody(byte[] buffer, int offset, int length)
}
private static readonly byte[] _httpSlash = Encoding.ASCII.GetBytes("HTTP/");
+
+ private bool? _isValidStart;
}
}
@@ -47,6 +47,14 @@ public override ILayer ExtractLayer()
};
}
+ /// <summary>
+ /// An HTTP Request has a valid start if it contains a method, an URI, and a version.
+ /// </summary>
+ protected override bool CalculateIsValidStart()
+ {
+ return Method != null && !string.IsNullOrEmpty(Uri) && Version != null;
+ }
+
internal HttpRequestDatagram(byte[] buffer, int offset, int length)
: this(buffer, offset, Parse(buffer, offset, length))
{
@@ -50,6 +50,14 @@ public override ILayer ExtractLayer()
};
}
+ /// <summary>
+ /// An HTTP response has a valid start if it contains a version and a status code.
+ /// </summary>
+ protected override bool CalculateIsValidStart()
+ {
+ return Version != null && StatusCode != null;
+ }
+
internal HttpResponseDatagram(byte[] buffer, int offset, int length)
: this(buffer, offset, Parse(buffer, offset, length))
{

0 comments on commit 57a21ac

Please sign in to comment.