Commit a78426d3 authored by Florian Hockmann's avatar Florian Hockmann

Check validity of HTTP responses

parent f6b5b7ff
......@@ -502,6 +502,46 @@ namespace PcapDotNet.Packets.Test
Assert.IsFalse(packet.Ethernet.IpV4.Tcp.Http.IsValid);
}
[TestMethod]
public void HttpValidResponseTest()
{
var packet = BuildPacket("HTTP/1.0 200 OK\r\n\r\n");
Assert.IsTrue(packet.Ethernet.IpV4.Tcp.Http.IsValid);
}
[TestMethod]
public void HttpMinimalResponseTest()
{
var packet = BuildPacket("HTTP/1.0 200 \r\n\r\n");
Assert.IsTrue(packet.Ethernet.IpV4.Tcp.Http.IsValid);
}
[TestMethod]
public void HttpResponseMissingVersionNumberNotAllowed()
{
var packet = BuildPacket("HTTP/ 200 OK\r\n\r\n");
Assert.IsFalse(packet.Ethernet.IpV4.Tcp.Http.IsValid);
}
[TestMethod]
public void HttpResponseMissingVersionNotAllowed()
{
var packet = BuildPacket(" 200 OK\r\n\r\n");
Assert.IsFalse(packet.Ethernet.IpV4.Tcp.Http.IsValid);
}
[TestMethod]
public void HttpResponseMissingStatusCodeNotAllowed()
{
var packet = BuildPacket("HTTP/1.0 OK \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)
{
Datagram expectedBody = expectedBodyString == null ? null : new Datagram(Encoding.ASCII.GetBytes(expectedBodyString));
......
......@@ -50,6 +50,18 @@ namespace PcapDotNet.Packets.Http
};
}
/// <summary>
/// A HTTP response is valid if it contains a version and a status code.
/// </summary>
protected override bool CalculateIsValid()
{
if (_isValid == null)
{
_isValid = Version != null && StatusCode != null;
}
return _isValid.Value;
}
internal HttpResponseDatagram(byte[] buffer, int offset, int length)
: this(buffer, offset, Parse(buffer, offset, length))
{
......@@ -104,5 +116,7 @@ namespace PcapDotNet.Packets.Http
return false;
return true;
}
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