Commit ebab5eda authored by FlorianHockmann's avatar FlorianHockmann

Move common behavior up to HttpDatagram

IsValidStart in HttpDatagram now implements the property with the template method pattern. HttpRequestDatagram and HttpResponseDatagram only override CalculateIsValidStart().
parent d7ece16d
...@@ -239,7 +239,23 @@ namespace PcapDotNet.Packets.Http ...@@ -239,7 +239,23 @@ namespace PcapDotNet.Packets.Http
/// </summary> /// </summary>
public Datagram Body { get; private set; } public Datagram Body { get; private set; }
public virtual bool IsValidStart => false; /// <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) internal static HttpDatagram CreateDatagram(byte[] buffer, int offset, int length)
{ {
...@@ -357,5 +373,7 @@ namespace PcapDotNet.Packets.Http ...@@ -357,5 +373,7 @@ namespace PcapDotNet.Packets.Http
} }
private static readonly byte[] _httpSlash = Encoding.ASCII.GetBytes("HTTP/"); private static readonly byte[] _httpSlash = Encoding.ASCII.GetBytes("HTTP/");
private bool? _isValidStart;
} }
} }
\ No newline at end of file
...@@ -50,16 +50,9 @@ namespace PcapDotNet.Packets.Http ...@@ -50,16 +50,9 @@ namespace PcapDotNet.Packets.Http
/// <summary> /// <summary>
/// An HTTP Request has a valid start if it contains a method, an URI, and a version. /// An HTTP Request has a valid start if it contains a method, an URI, and a version.
/// </summary> /// </summary>
public override bool IsValidStart protected override bool CalculateIsValidStart()
{ {
get return Method != null && !string.IsNullOrEmpty(Uri) && Version != null;
{
if (_isValidStart == null)
{
_isValidStart = Method != null && !string.IsNullOrEmpty(Uri) && Version != null;
}
return _isValidStart.Value;
}
} }
internal HttpRequestDatagram(byte[] buffer, int offset, int length) internal HttpRequestDatagram(byte[] buffer, int offset, int length)
...@@ -114,7 +107,5 @@ namespace PcapDotNet.Packets.Http ...@@ -114,7 +107,5 @@ namespace PcapDotNet.Packets.Http
{ {
return header.ContentLength != null; return header.ContentLength != null;
} }
private bool? _isValidStart;
} }
} }
\ No newline at end of file
...@@ -53,16 +53,9 @@ namespace PcapDotNet.Packets.Http ...@@ -53,16 +53,9 @@ namespace PcapDotNet.Packets.Http
/// <summary> /// <summary>
/// A HTTP response has a valid start if it contains a version and a status code. /// A HTTP response has a valid start if it contains a version and a status code.
/// </summary> /// </summary>
public override bool IsValidStart protected override bool CalculateIsValidStart()
{ {
get return Version != null && StatusCode != null;
{
if (_isValidStart == null)
{
_isValidStart = Version != null && StatusCode != null;
}
return _isValidStart.Value;
}
} }
internal HttpResponseDatagram(byte[] buffer, int offset, int length) internal HttpResponseDatagram(byte[] buffer, int offset, int length)
...@@ -119,7 +112,5 @@ namespace PcapDotNet.Packets.Http ...@@ -119,7 +112,5 @@ namespace PcapDotNet.Packets.Http
return false; return false;
return true; return true;
} }
private bool? _isValidStart;
} }
} }
\ 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