Commit f6b5b7ff authored by Florian Hockmann's avatar Florian Hockmann

Check validity of HTTP requests

parent 0cd9a9c3
...@@ -470,6 +470,38 @@ namespace PcapDotNet.Packets.Test ...@@ -470,6 +470,38 @@ namespace PcapDotNet.Packets.Test
Assert.IsNotNull(new HttpRequestMethod(HttpRequestKnownMethod.Unknown)); Assert.IsNotNull(new HttpRequestMethod(HttpRequestKnownMethod.Unknown));
} }
[TestMethod]
public void HttpMinimalValidRequestTest()
{
var packet = BuildPacket("UnknownMethod / HTTP/1.0\r\n\r\n");
Assert.IsTrue(packet.Ethernet.IpV4.Tcp.Http.IsValid);
}
[TestMethod]
public void HttpRequestEmptyRequestUriNotAllowed()
{
var packet = BuildPacket("GET HTTP/1.1\r\n\r\n");
Assert.IsFalse(packet.Ethernet.IpV4.Tcp.Http.IsValid);
}
[TestMethod]
public void HttpRequestMissingVersionNumberNotAllowed()
{
var packet = BuildPacket("GET / HTTP/\r\n\r\n");
Assert.IsFalse(packet.Ethernet.IpV4.Tcp.Http.IsValid);
}
[TestMethod]
public void HttpRequestMissingVersionNotAllowed()
{
var packet = BuildPacket("GET /\r\n\r\n");
Assert.IsFalse(packet.Ethernet.IpV4.Tcp.Http.IsValid);
}
private static void TestHttpRequest(string httpString, string expectedMethodString = null, string expectedUri = null, HttpVersion expectedVersion = null, HttpHeader expectedHeader = null, string expectedBodyString = null) 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)); Datagram expectedBody = expectedBodyString == null ? null : new Datagram(Encoding.ASCII.GetBytes(expectedBodyString));
......
...@@ -47,6 +47,18 @@ namespace PcapDotNet.Packets.Http ...@@ -47,6 +47,18 @@ namespace PcapDotNet.Packets.Http
}; };
} }
/// <summary>
/// An HTTP Request is valid if it contains a method, an URI, and a version.
/// </summary>
protected override bool CalculateIsValid()
{
if (_isValid == null)
{
_isValid = Method != null && !string.IsNullOrEmpty(Uri) && Version != null;
}
return _isValid.Value;
}
internal HttpRequestDatagram(byte[] buffer, int offset, int length) internal HttpRequestDatagram(byte[] buffer, int offset, int length)
: this(buffer, offset, Parse(buffer, offset, length)) : this(buffer, offset, Parse(buffer, offset, length))
{ {
...@@ -99,5 +111,7 @@ namespace PcapDotNet.Packets.Http ...@@ -99,5 +111,7 @@ namespace PcapDotNet.Packets.Http
{ {
return header.ContentLength != null; return header.ContentLength != null;
} }
private bool? _isValid;
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment