Commit bd000db5 authored by Brickner_cp's avatar Brickner_cp

Warnings, Code Analysis and Documentation. 149 warnings left.

parent f67e42b3
......@@ -72,14 +72,6 @@ namespace PcapDotNet.Base.Test
Assert.IsTrue(sequence.SequenceEqual(new[] {1,2,3}.Concat(4, 5)));
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)]
public void IsEmptyNullTest()
{
Assert.IsFalse(IEnumerableExtensions.IsEmpty<int>(null));
Assert.Fail();
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)]
public void SequenceToStringNullTest()
......
......@@ -13,17 +13,6 @@ namespace PcapDotNet.Base
public static class IEnumerableExtensions
// ReSharper restore InconsistentNaming
{
/// <summary>
/// True iff the sequence has no elements.
/// </summary>
public static bool IsEmpty<T>(this IEnumerable<T> sequence)
{
if (sequence == null)
throw new ArgumentNullException("sequence");
return !sequence.GetEnumerator().MoveNext();
}
/// <summary>
/// Concatenates a sequence with more values.
/// </summary>
......@@ -36,6 +25,26 @@ namespace PcapDotNet.Base
return sequence.Concat((IEnumerable<T>)values);
}
public static long Xor(this IEnumerable<long> sequence)
{
return sequence.Xor(value => value);
}
public static int Xor(this IEnumerable<int> sequence)
{
return sequence.Xor(value => value);
}
public static long Xor<T>(this IEnumerable<T> sequence, Func<T, long> selector)
{
return sequence.Aggregate((long)0, (xorTotal, current) => xorTotal ^ selector(current));
}
public static int Xor<T>(this IEnumerable<T> sequence, Func<T, int> selector)
{
return sequence.Aggregate(0, (xorTotal, current) => xorTotal ^ selector(current));
}
/// <summary>
/// Converts a sequence to a string by converting each element to a string.
/// </summary>
......@@ -148,7 +157,7 @@ namespace PcapDotNet.Base
/// <returns>The hash code created by xoring all the hash codes of the elements in the sequence.</returns>
public static int SequenceGetHashCode<T>(this IEnumerable<T> sequence)
{
return sequence.Aggregate(0, (valueSoFar, element) => valueSoFar ^ element.GetHashCode());
return sequence.Xor(value => value.GetHashCode());
}
/// <summary>
......@@ -160,7 +169,7 @@ namespace PcapDotNet.Base
public static int BytesSequenceGetHashCode(this IEnumerable<byte> sequence)
{
int i = 0;
return sequence.Aggregate(0, (value, b) => value ^ (b << (8 * (i++ % 4))));
return sequence.Xor(b => (b << (8 * (i++ % 4))));
}
/// <summary>
......@@ -172,7 +181,7 @@ namespace PcapDotNet.Base
public static int UShortsSequenceGetHashCode(this IEnumerable<ushort> sequence)
{
int i = 0;
return sequence.Aggregate(0, (value, b) => value ^ (b << (16 * (i++ % 2))));
return sequence.Xor(b => (b << (16 * (i++ % 2))));
}
/// <summary>
......
......@@ -1543,7 +1543,7 @@ namespace PcapDotNet.Core.Test
data.Append(field.Value());
string[] mediaType = fieldShow.Split(new[] {';', ' ', '/'}, StringSplitOptions.RemoveEmptyEntries);
Assert.AreEqual(httpDatagram.Header.ContentType.MediaType, mediaType[0]);
Assert.AreEqual(httpDatagram.Header.ContentType.MediaSubType, mediaType[1]);
Assert.AreEqual(httpDatagram.Header.ContentType.MediaSubtype, mediaType[1]);
int fieldShowParametersStart = fieldShow.IndexOf(';');
if (fieldShowParametersStart == -1)
Assert.IsFalse(httpDatagram.Header.ContentType.Parameters.Any());
......
......@@ -534,7 +534,7 @@ namespace PcapDotNet.Packets.TestUtils
IgmpMessageType.JoinGroupReplyVersion0, IgmpMessageType.LeaveGroupRequestVersion0,
IgmpMessageType.LeaveGroupReplyVersion0, IgmpMessageType.ConfirmGroupRequestVersion0,
IgmpMessageType.ConfirmGroupReplyVersion0,
IgmpMessageType.MulticastTracerouteResponse); // todo support IGMP traceroute http://www.ietf.org/proceedings/48/I-D/idmr-traceroute-ipm-07.txt.
IgmpMessageType.MulticastTraceRouteResponse); // todo support IGMP traceroute http://www.ietf.org/proceedings/48/I-D/idmr-traceroute-ipm-07.txt.
IgmpQueryVersion igmpQueryVersion = IgmpQueryVersion.None;
TimeSpan igmpMaxResponseTime = random.NextTimeSpan(TimeSpan.FromSeconds(0.1), TimeSpan.FromSeconds(256 * 0.1) - TimeSpan.FromTicks(1));
IpV4Address igmpGroupAddress = random.NextIpV4Address();
......@@ -674,7 +674,7 @@ namespace PcapDotNet.Packets.TestUtils
}
routingOffset = 0;
if (!routing.IsEmpty())
if (routing.Any())
{
int routingIndex = random.Next(routing.Length);
for (int i = 0; i != routingIndex; ++i)
......@@ -1105,7 +1105,7 @@ namespace PcapDotNet.Packets.TestUtils
public static HttpField NextHttpField(this Random random, HashSet<string> fieldNames)
{
const string unknownField = "Unknown Name";
List<string> allOptions = new List<string> { unknownField, HttpTransferEncodingField.NameLower, HttpContentLengthField.NameLower, HttpContentTypeField.NameLower };
List<string> allOptions = new List<string> { unknownField, HttpTransferEncodingField.FieldNameUpper, HttpContentLengthField.FieldNameUpper, HttpContentTypeField.FieldNameUpper };
List<string> possibleOptions = new List<string>(allOptions.Count);
foreach (string option in allOptions)
{
......@@ -1124,17 +1124,17 @@ namespace PcapDotNet.Packets.TestUtils
} while (fieldNames.Contains(fieldName));
return new HttpField(fieldName, random.NextHttpFieldValue());
case HttpTransferEncodingField.NameLower:
case HttpTransferEncodingField.FieldNameUpper:
int numTransferCodings = random.Next(1, 10);
List<string> transferCodings = new List<string>(numTransferCodings);
for (int i = 0; i != numTransferCodings; ++i)
transferCodings.Add(random.NextHttpTransferCoding());
return new HttpTransferEncodingField(transferCodings);
case HttpContentLengthField.NameLower:
case HttpContentLengthField.FieldNameUpper:
return new HttpContentLengthField(random.NextUInt(1000));
case HttpContentTypeField.NameLower:
case HttpContentTypeField.FieldNameUpper:
return new HttpContentTypeField(random.NextHttpToken(), random.NextHttpToken(), random.NextHttpFieldParameters());
......@@ -1255,7 +1255,7 @@ namespace PcapDotNet.Packets.TestUtils
if (httpHeader.ContentType != null &&
httpHeader.ContentType.MediaType == "multipart" &&
httpHeader.ContentType.MediaSubType == "byteranges" &&
httpHeader.ContentType.MediaSubtype == "byteranges" &&
httpHeader.ContentType.Parameters["boundary"] != null)
{
......
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Text;
using PcapDotNet.Base;
......@@ -16,6 +17,11 @@ namespace PcapDotNet.Packets
{
public static int Compare(this byte[] array, int offset, byte[] other, int otherOffset, int count)
{
if (array == null)
throw new ArgumentNullException("array");
if (other == null)
throw new ArgumentNullException("other");
for (int i = 0; i != count; ++i)
{
int compare = array[offset + i].CompareTo(other[otherOffset + i]);
......@@ -27,11 +33,17 @@ namespace PcapDotNet.Packets
public static bool SequenceEqual(this byte[] array, int offset, byte[] other, int otherOffset, int count)
{
if (array == null)
throw new ArgumentNullException("array");
return array.Compare(offset, other, otherOffset, count) == 0;
}
public static int Find(this byte[] array, int offset, int count, byte[] other, int otherOffset, int otherCount)
{
if (array == null)
throw new ArgumentNullException("array");
if (otherCount > count)
return array.Length;
......@@ -48,6 +60,11 @@ namespace PcapDotNet.Packets
public static int Find(this byte[] array, int offset, int count, byte[] other)
{
if (array == null)
throw new ArgumentNullException("array");
if (other == null)
throw new ArgumentNullException("other");
return array.Find(offset, count, other, 0, other.Length);
}
......@@ -555,6 +572,9 @@ namespace PcapDotNet.Packets
public static void Write(this byte[] buffer, ref int offset, string value, Encoding encoding)
{
if (encoding == null)
throw new ArgumentNullException("encoding");
buffer.Write(ref offset, encoding.GetBytes(value));
}
......@@ -665,15 +685,15 @@ namespace PcapDotNet.Packets
// buffer.Write(offset, AsciiBytes.LineFeed);
// }
//
public static void WriteCarriageReturnLineFeed(this byte[] buffer, ref int offset)
public static void WriteCarriageReturnLinefeed(this byte[] buffer, ref int offset)
{
buffer.Write(ref offset, AsciiBytes.CarriageReturn);
buffer.Write(ref offset, AsciiBytes.LineFeed);
buffer.Write(ref offset, AsciiBytes.Linefeed);
}
public static void WriteDecimal(this byte[] buffer, ref int offset, uint value)
{
buffer.Write(ref offset, value.ToString(), Encoding.ASCII);
buffer.Write(ref offset, value.ToString(CultureInfo.InvariantCulture), Encoding.ASCII);
}
private static bool IsWrongEndianity(Endianity endianity)
......
......@@ -153,6 +153,9 @@ namespace PcapDotNet.Packets
public string ToString(Encoding encoding)
{
if (encoding == null)
throw new ArgumentNullException("encoding");
return encoding.GetString(Buffer, StartOffset, Length);
}
......
......@@ -12,7 +12,7 @@
public const byte Nine = (byte)'9';
public const byte Delete = 127; // DEL
public const byte CarriageReturn = 13; // CR
public const byte LineFeed = 10; // LF
public const byte Linefeed = 10; // LF
public const byte Space = (byte)' '; // SP
public const byte HorizontalTab = 9; // HT
public const byte DoubleQuotationMark = (byte)'"';
......
using System;
using System.Globalization;
namespace PcapDotNet.Packets.Http
{
......@@ -51,7 +52,7 @@ namespace PcapDotNet.Packets.Http
if (value >= AsciiBytes.UpperA && value <= AsciiBytes.UpperF)
return value - AsciiBytes.UpperA + 10;
throw new ArgumentOutOfRangeException("value", value,
string.Format("Must be a valid ASCII hexadecimal character ({0}-{1}, {2}-{3}, {4}-{5})",
string.Format(CultureInfo.InvariantCulture, "Must be a valid ASCII hexadecimal character ({0}-{1}, {2}-{3}, {4}-{5})",
AsciiBytes.Zero, AsciiBytes.Nine,
AsciiBytes.LowerA, AsciiBytes.LowerF,
AsciiBytes.UpperA, AsciiBytes.UpperF));
......
namespace PcapDotNet.Packets.Http
using System.Globalization;
namespace PcapDotNet.Packets.Http
{
/// <summary>
/// RFC 2616.
......@@ -10,15 +12,15 @@
/// <summary>
/// The field name.
/// </summary>
public const string Name = "Content-Length";
public const string FieldName = "Content-Length";
/// <summary>
/// The field name in lowercase.
/// </summary>
public const string NameLower = "content-length";
public const string FieldNameUpper = "CONTENT-LENGTH";
public HttpContentLengthField(uint contentLength)
:base(Name, contentLength.ToString())
:base(FieldName, contentLength.ToString(CultureInfo.InvariantCulture))
{
ContentLength = contentLength;
}
......@@ -26,7 +28,7 @@
public uint? ContentLength { get; private set;}
internal HttpContentLengthField(byte[] fieldValue)
:base(Name, fieldValue)
:base(FieldName, fieldValue)
{
HttpParser parser = new HttpParser(fieldValue);
uint? contentLength;
......
using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using PcapDotNet.Base;
......@@ -8,14 +9,14 @@ namespace PcapDotNet.Packets.Http
{
public class HttpContentTypeField : HttpField, IEquatable<HttpContentTypeField>
{
public const string Name = "Content-Type";
public const string NameLower = "content-type";
public const string FieldName = "Content-Type";
public const string FieldNameUpper = "CONTENT-TYPE";
public HttpContentTypeField(string mediaType, string mediaSubType, HttpFieldParameters parameters)
:base(Name, string.Format("{0}/{1}{2}", mediaType, mediaSubType, parameters))
public HttpContentTypeField(string mediaType, string mediaSubtype, HttpFieldParameters parameters)
: base(FieldName, string.Format(CultureInfo.InvariantCulture, "{0}/{1}{2}", mediaType, mediaSubtype, parameters))
{
MediaType = mediaType;
MediaSubType = mediaSubType;
MediaSubtype = mediaSubtype;
Parameters = parameters;
}
......@@ -23,12 +24,12 @@ namespace PcapDotNet.Packets.Http
{
return other != null &&
MediaType == other.MediaType &&
MediaSubType == other.MediaSubType &&
MediaSubtype == other.MediaSubtype &&
Parameters.Equals(other.Parameters);
}
public string MediaType { get; private set; }
public string MediaSubType { get; private set; }
public string MediaSubtype { get; private set; }
public HttpFieldParameters Parameters { get; private set;}
public override bool Equals(HttpField other)
......@@ -37,7 +38,7 @@ namespace PcapDotNet.Packets.Http
}
internal HttpContentTypeField(byte[] fieldValue)
: base(Name, fieldValue)
: base(FieldName, fieldValue)
{
string fieldValueString = HttpRegex.GetString(fieldValue);
Match match = _regex.Match(fieldValueString);
......@@ -45,7 +46,7 @@ namespace PcapDotNet.Packets.Http
return;
MediaType = match.Groups[MediaTypeGroupName].Captures.Cast<Capture>().First().Value;
MediaSubType = match.Groups[MediaSubTypeGroupName].Captures.Cast<Capture>().First().Value;
MediaSubtype = match.Groups[MediaSubTypeGroupName].Captures.Cast<Capture>().First().Value;
Parameters = new HttpFieldParameters(match.GroupCapturesValues(HttpRegex.ParameterNameGroupName),
match.GroupCapturesValues(HttpRegex.ParameterValueGroupName));
}
......
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
......@@ -257,12 +258,12 @@ namespace PcapDotNet.Packets.Http
if (contentTypeField != null)
{
if (contentTypeField.MediaType == "multipart" &&
contentTypeField.MediaSubType == "byteranges")
contentTypeField.MediaSubtype == "byteranges")
{
string boundary = contentTypeField.Parameters["boundary"];
if (boundary != null)
{
byte[] lastBoundaryBuffer = Encoding.ASCII.GetBytes(string.Format("\r\n--{0}--", boundary));
byte[] lastBoundaryBuffer = Encoding.ASCII.GetBytes(string.Format(CultureInfo.InvariantCulture, "\r\n--{0}--", boundary));
int lastBoundaryOffset = buffer.Find(offset, length, lastBoundaryBuffer);
int lastBoundaryEnd = lastBoundaryOffset + lastBoundaryBuffer.Length;
return new Datagram(buffer, offset,
......@@ -285,7 +286,10 @@ namespace PcapDotNet.Packets.Http
if (chunkSizeValue == 0)
{
int? endOffset;
HttpHeader trailerHeader = new HttpHeader(GetHeaderFields(out endOffset, buffer, parser.Offset, offset + length - parser.Offset));
// HttpHeader trailerHeader = new HttpHeader(
GetHeaderFields(out endOffset, buffer, parser.Offset, offset + length - parser.Offset)
// )
;
if (endOffset != null)
parser.Skip(endOffset.Value - parser.Offset);
break;
......@@ -305,7 +309,7 @@ namespace PcapDotNet.Packets.Http
datagram.Write(contentBuffer, contentBufferOffset);
contentBufferOffset += datagram.Length;
}
Datagram content = new Datagram(contentBuffer);
// Datagram content = new Datagram(contentBuffer);
return new Datagram(buffer, offset, parser.Offset - offset);
}
......
......@@ -2,6 +2,7 @@
using System.CodeDom;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
......@@ -14,13 +15,16 @@ namespace PcapDotNet.Packets.Http
{
public static HttpField CreateField(string fieldName, byte[] fieldValue)
{
switch (fieldName.ToLowerInvariant())
if (fieldName == null)
throw new ArgumentNullException("fieldName");
switch (fieldName.ToUpperInvariant())
{
case HttpTransferEncodingField.NameLower:
case HttpTransferEncodingField.FieldNameUpper:
return new HttpTransferEncodingField(fieldValue);
case HttpContentLengthField.NameLower:
case HttpContentLengthField.FieldNameUpper:
return new HttpContentLengthField(fieldValue);
case HttpContentTypeField.NameLower:
case HttpContentTypeField.FieldNameUpper:
return new HttpContentTypeField(fieldValue);
default:
......@@ -34,7 +38,7 @@ namespace PcapDotNet.Packets.Http
}
public HttpField(string name, string value, Encoding encoding)
: this(name, encoding.GetBytes(NormalizeValue(value)))
: this(name, encoding == null ? null : encoding.GetBytes(NormalizeValue(value)))
{
}
......@@ -105,7 +109,7 @@ namespace PcapDotNet.Packets.Http
public virtual bool Equals(HttpField other)
{
return other != null && Name.Equals(other.Name, StringComparison.InvariantCultureIgnoreCase) && Value.SequenceEqual(other.Value);
return other != null && Name.Equals(other.Name, StringComparison.OrdinalIgnoreCase) && Value.SequenceEqual(other.Value);
}
public override bool Equals(object obj)
......@@ -113,9 +117,14 @@ namespace PcapDotNet.Packets.Http
return Equals(obj as HttpField);
}
public override int GetHashCode()
{
return Name.ToUpperInvariant().GetHashCode() ^ Value.BytesSequenceGetHashCode();
}
public override string ToString()
{
return string.Format("{0}: {1}", Name, ValueString);
return string.Format(CultureInfo.InvariantCulture, "{0}: {1}", Name, ValueString);
}
internal void Write(byte[] buffer, ref int offset)
......@@ -124,7 +133,7 @@ namespace PcapDotNet.Packets.Http
buffer.Write(ref offset, AsciiBytes.Colon);
buffer.Write(ref offset, AsciiBytes.Space);
buffer.Write(ref offset, Value);
buffer.WriteCarriageReturnLineFeed(ref offset);
buffer.WriteCarriageReturnLinefeed(ref offset);
}
private static readonly Encoding _defaultEncoding = Encoding.GetEncoding(28591);
......
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using PcapDotNet.Base;
......@@ -44,7 +45,7 @@ namespace PcapDotNet.Packets.Http
public bool Equals(HttpFieldParameters other)
{
return _parameters.DictionaryEquals(other._parameters);
return other != null && _parameters.DictionaryEquals(other._parameters);
}
public override bool Equals(object obj)
......@@ -52,6 +53,14 @@ namespace PcapDotNet.Packets.Http
return Equals(obj as HttpFieldParameters);
}
public override int GetHashCode()
{
return
_parameters.Select(pair => new KeyValuePair<string, string>(pair.Key.ToUpperInvariant(), pair.Value))
.OrderBy(pair => pair.Key)
.Xor(pair => pair.Key.GetHashCode() ^ pair.Value.GetHashCode());
}
public override string ToString()
{
if (!this.Any())
......@@ -77,12 +86,12 @@ namespace PcapDotNet.Packets.Http
while (nameEnumerator.MoveNext())
{
if (!valueEnumerator.MoveNext())
throw new ArgumentException(string.Format("more names ({0}) were given than values ({1})", parametersNames.Count(), parametersValues.Count()), "parametersValues");
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "more names ({0}) were given than values ({1})", parametersNames.Count(), parametersValues.Count()), "parametersValues");
_parameters.Add(nameEnumerator.Current, valueEnumerator.Current);
}
if (valueEnumerator.MoveNext())
throw new ArgumentException(string.Format("more values ({0}) were given than names ({1})", parametersValues.Count(), parametersNames.Count()), "parametersNames");
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "more values ({0}) were given than names ({1})", parametersValues.Count(), parametersNames.Count()), "parametersNames");
}
private readonly Dictionary<string, string> _parameters = new Dictionary<string, string>();
......
......@@ -12,7 +12,7 @@ namespace PcapDotNet.Packets.Http
public HttpHeader(IEnumerable<HttpField> fields)
{
_fields = fields.ToDictionary(field => field.Name, field => field, StringComparer.InvariantCultureIgnoreCase);
_fields = fields.ToDictionary(field => field.Name, field => field, StringComparer.OrdinalIgnoreCase);
}
public HttpHeader(params HttpField[] fields)
......@@ -37,7 +37,7 @@ namespace PcapDotNet.Packets.Http
{
get
{
return GetField<HttpTransferEncodingField>(HttpTransferEncodingField.Name);
return GetField<HttpTransferEncodingField>(HttpTransferEncodingField.FieldName);
}
}
......@@ -45,7 +45,7 @@ namespace PcapDotNet.Packets.Http
{
get
{
return GetField<HttpContentLengthField>(HttpContentLengthField.Name);
return GetField<HttpContentLengthField>(HttpContentLengthField.FieldName);
}
}
......@@ -53,7 +53,7 @@ namespace PcapDotNet.Packets.Http
{
get
{
return GetField<HttpContentTypeField>(HttpContentTypeField.Name);
return GetField<HttpContentTypeField>(HttpContentTypeField.FieldName);
}
}
......@@ -68,6 +68,11 @@ namespace PcapDotNet.Packets.Http
return Equals(obj as HttpHeader);
}
public override int GetHashCode()
{
return _fields.Select(pair => pair.Value).SequenceGetHashCode();
}
public override string ToString()
{
return this.SequenceToString("\r\n");
......@@ -85,7 +90,7 @@ namespace PcapDotNet.Packets.Http
internal HttpHeader(IEnumerable<KeyValuePair<string, IEnumerable<byte>>> fields)
{
var mergedFields = new Dictionary<string, IEnumerable<byte>>(StringComparer.InvariantCultureIgnoreCase);
var mergedFields = new Dictionary<string, IEnumerable<byte>>(StringComparer.OrdinalIgnoreCase);
foreach (var field in fields)
{
string fieldName = field.Key;
......@@ -99,7 +104,7 @@ namespace PcapDotNet.Packets.Http
mergedFields[fieldName] = fieldValue.Concat(AsciiBytes.Comma).Concat(field.Value);
}
_fields = mergedFields.ToDictionary(field => field.Key, field => HttpField.CreateField(field.Key, field.Value.ToArray()), StringComparer.InvariantCultureIgnoreCase);
_fields = mergedFields.ToDictionary(field => field.Key, field => HttpField.CreateField(field.Key, field.Value.ToArray()), StringComparer.OrdinalIgnoreCase);
}
public void Write(byte[] buffer, int offset)
......@@ -111,7 +116,7 @@ namespace PcapDotNet.Packets.Http
{
foreach (HttpField field in this)
field.Write(buffer, ref offset);
buffer.WriteCarriageReturnLineFeed(ref offset);
buffer.WriteCarriageReturnLinefeed(ref offset);
}
private T GetField<T>(string fieldName) where T : HttpField
......@@ -123,6 +128,6 @@ namespace PcapDotNet.Packets.Http
}
private static readonly HttpHeader _empty = new HttpHeader();
private readonly Dictionary<string, HttpField> _fields = new Dictionary<string, HttpField>(StringComparer.InvariantCultureIgnoreCase);
private readonly Dictionary<string, HttpField> _fields = new Dictionary<string, HttpField>(StringComparer.OrdinalIgnoreCase);
}
}
......@@ -83,7 +83,7 @@ namespace PcapDotNet.Packets.Http
else if (first == AsciiBytes.CarriageReturn) // CR
{
range = range.Skip(1);
if (range.FirstOrDefault() != AsciiBytes.LineFeed) // CR without LF
if (range.FirstOrDefault() != AsciiBytes.Linefeed) // CR without LF
break;
// CRLF
......@@ -109,7 +109,7 @@ namespace PcapDotNet.Packets.Http
{
fieldContent = new Datagram(_buffer, originalOffset, Offset - originalOffset);
if (IsNext(AsciiBytes.Space) || IsNext(AsciiBytes.CarriageReturn) || IsNext(AsciiBytes.HorizontalTab) || IsNext(AsciiBytes.LineFeed))
if (IsNext(AsciiBytes.Space) || IsNext(AsciiBytes.CarriageReturn) || IsNext(AsciiBytes.HorizontalTab) || IsNext(AsciiBytes.Linefeed))
break;
if (IsNext(AsciiBytes.DoubleQuotationMark))
......@@ -184,12 +184,12 @@ namespace PcapDotNet.Packets.Http
public HttpParser CarriageReturnLineFeed()
{
return Bytes(AsciiBytes.CarriageReturn, AsciiBytes.LineFeed);
return Bytes(AsciiBytes.CarriageReturn, AsciiBytes.Linefeed);
}
public bool IsCarriageReturnLineFeed()
{
return IsNext(AsciiBytes.CarriageReturn) && IsNextNext(AsciiBytes.LineFeed);
return IsNext(AsciiBytes.CarriageReturn) && IsNextNext(AsciiBytes.Linefeed);
}
public HttpParser DecimalNumber(int numDigits, out uint? number)
......
using System.Text;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using PcapDotNet.Base;
......@@ -71,17 +72,17 @@ namespace PcapDotNet.Packets.Http
public static Regex Optional(Regex regex)
{
return Build(Bracket(string.Format("{0}?", regex)));
return Build(Bracket(string.Format(CultureInfo.InvariantCulture, "{0}?", regex)));
}
public static Regex Any(Regex regex)
{
return Build(Bracket(string.Format("{0}*", regex)));
return Build(Bracket(string.Format(CultureInfo.InvariantCulture, "{0}*", regex)));
}
public static Regex AtLeastOne(Regex regex)
{
return Build(Bracket(string.Format("{0}+", regex)));
return Build(Bracket(string.Format(CultureInfo.InvariantCulture, "{0}+", regex)));
}
public static Regex AtLeast(Regex regex, int minCount)
......@@ -90,30 +91,30 @@ namespace PcapDotNet.Packets.Http
return Any(regex);
if (minCount == 1)
return AtLeastOne(regex);
return Build(string.Format("(?:{0}){{{1},}}", regex, minCount));
return Build(string.Format(CultureInfo.InvariantCulture, "(?:{0}){{{1},}}", regex, minCount));
}
public static Regex CommaSeparatedRegex(Regex element, int minCount)
{
Regex linearWhiteSpacesRegex = Any(LinearWhiteSpace);
Regex regex = Concat(Build(string.Format("{0}{1}", linearWhiteSpacesRegex, element)),
AtLeast(Build(string.Format("{0},{1}{2}", linearWhiteSpacesRegex, linearWhiteSpacesRegex, element)), minCount - 1));
Regex regex = Concat(Build(string.Format(CultureInfo.InvariantCulture, "{0}{1}", linearWhiteSpacesRegex, element)),
AtLeast(Build(string.Format(CultureInfo.InvariantCulture, "{0},{1}{2}", linearWhiteSpacesRegex, linearWhiteSpacesRegex, element)), minCount - 1));
return minCount <= 0 ? Optional(regex) : regex;
}
public static Regex Capture(Regex regex, string captureName)
{
return Build(string.Format("(?<{0}>{1})", captureName, regex));
return Build(string.Format(CultureInfo.InvariantCulture, "(?<{0}>{1})", captureName, regex));
}
public static Regex MatchEntire(Regex regex)
{
return Build(string.Format("^{0}$", regex));
return Build(string.Format(CultureInfo.InvariantCulture, "^{0}$", regex));
}
private static string Bracket(string pattern)
{
return string.Format("(?:{0})", pattern);
return string.Format(CultureInfo.InvariantCulture, "(?:{0})", pattern);
}
private static readonly Regex _carriageReturnLineFeed = Build(@"\r\n");
......
......@@ -61,7 +61,7 @@ namespace PcapDotNet.Packets.Http
if (Version == null)
return;
Version.Write(buffer, ref offset);
buffer.WriteCarriageReturnLineFeed(ref offset);
buffer.WriteCarriageReturnLinefeed(ref offset);
}
}
}
\ No newline at end of file
......@@ -14,7 +14,7 @@ namespace PcapDotNet.Packets.Http
public HttpRequestMethod(HttpRequestKnownMethod method)
{
Method = method.ToString().ToUpper();
Method = method.ToString().ToUpperInvariant();
if (!_knownMethods.ContainsKey(Method))
throw new ArgumentException("Invalid known request method given: " + method, "method");
}
......@@ -64,6 +64,11 @@ namespace PcapDotNet.Packets.Http
return Equals(obj as HttpRequestMethod);
}
public override int GetHashCode()
{
return Method.GetHashCode();
}
private static readonly Dictionary<string, HttpRequestKnownMethod> _knownMethods = CreateKnownMethodsTable();
}
}
\ No newline at end of file
......@@ -64,7 +64,7 @@ namespace PcapDotNet.Packets.Http
return;
buffer.Write(ref offset, ReasonPhrase);
buffer.WriteCarriageReturnLineFeed(ref offset);
buffer.WriteCarriageReturnLinefeed(ref offset);
}
}
}
\ No newline at end of file
......@@ -10,12 +10,12 @@ namespace PcapDotNet.Packets.Http
{
public class HttpTransferEncodingField : HttpField, IEquatable<HttpTransferEncodingField>
{
public const string Name = "Transfer-Encoding";
public const string NameLower = "transfer-encoding";
public const string FieldName = "Transfer-Encoding";
public const string FieldNameUpper = "TRANSFER-ENCODING";
private const string RegexTransferCodingGroupName = "TransferCoding";
public HttpTransferEncodingField(IList<string> transferCodings)
:base(Name, transferCodings.SequenceToString(","))
:base(FieldName, transferCodings.SequenceToString(","))
{
SetTransferCodings(transferCodings);
}
......@@ -33,7 +33,8 @@ namespace PcapDotNet.Packets.Http
}
public ReadOnlyCollection<string> TransferCodings{get{return _transferCodings;}}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase")]
private void SetTransferCodings(IList<string> transferCodings)
{
if (transferCodings.Any(coding => coding.Any(c => c.IsUppercaseAlpha())))
......@@ -48,7 +49,7 @@ namespace PcapDotNet.Packets.Http
}
internal HttpTransferEncodingField(byte[] fieldValue)
: base(Name, fieldValue)
: base(FieldName, fieldValue)
{
string fieldValueString = HttpRegex.GetString(fieldValue);
Match match = _regex.Match(fieldValueString);
......
using System;
using System.Globalization;
using System.Text;
using PcapDotNet.Base;
......@@ -28,7 +29,7 @@ namespace PcapDotNet.Packets.Http
public override string ToString()
{
return string.Format("HTTP/{0}.{1}", Major, Minor);
return string.Format(CultureInfo.InvariantCulture, "HTTP/{0}.{1}", Major, Minor);
}
public bool Equals(HttpVersion other)
......
......@@ -229,7 +229,7 @@ namespace PcapDotNet.Packets.Icmp
from attribute in type.GetCustomAttributes<IcmpDatagramRegistrationAttribute>(false)
select attribute;
if (registrationAttributes.IsEmpty())
if (!registrationAttributes.Any())
return null;
return registrationAttributes.First();
......
......@@ -75,6 +75,6 @@ namespace PcapDotNet.Packets.Igmp
/// </summary>
LeaveGroupVersion2 = 0x17,
MulticastTracerouteResponse = 0x1E,
MulticastTraceRouteResponse = 0x1E,
}
}
\ No newline at end of file
......@@ -64,7 +64,7 @@ namespace PcapDotNet.Packets
where attribute.OptionTypeType == typeof(TOptionType)
select attribute;
if (registraionAttributes.IsEmpty())
if (!registraionAttributes.Any())
return null;
return registraionAttributes.First();
......
......@@ -10,6 +10,7 @@
<Rule Id="CA1027" Action="None" />
<Rule Id="CA1028" Action="None" />
<Rule Id="CA1045" Action="None" />
<Rule Id="CA1056" Action="None" />
<Rule Id="CA1501" Action="None" />
<Rule Id="CA1710" Action="None" />
<Rule Id="CA2000" Action="None" />
......
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