/// <summary> /// Diffs the request or response headers /// </summary> /// <param name="result"></param> /// <param name="firstText"></param> /// <param name="secondText"></param> /// <param name="firstBasePos">The starting position for the first text relative to the entire request/response text</param> /// <param name="secondBasePos">The starting position for the second text relative to the entire request/response text</param> /// <returns></returns> private RequestsDifferResult DiffHeaders(RequestsDifferResult result, string firstText, string secondText, int firstBasePos, int secondBasePos) { //next diff the headers HeadersDiffCollection firstHeaders = new HeadersDiffCollection(); HeadersDiffCollection secondHeaders = new HeadersDiffCollection(); firstHeaders.BasePosition = firstBasePos; secondHeaders.BasePosition = secondBasePos; firstHeaders.Import(firstText); secondHeaders.Import(secondText); GenericDiffer headersDiffer = new GenericDiffer(); headersDiffer.Properties.Sorted = true; headersDiffer.Properties.CaseInSensitiveSort = true; headersDiffer.AddTask(firstHeaders); headersDiffer.AddTask(secondHeaders); headersDiffer.DoDiff(); //obtain the resulting differences and add them to the result result = AppendDiffs(result, headersDiffer); return(result); }
/// <summary> /// Imports a string containing Http Headers to a collection of HttpDiffObjects /// </summary> /// <param name="text">Request or response text</param> /// <exception cref="DiffException"></exception> public override void Import(string text) { _items = new List <IDiffObject>(); _differences = new HeadersDiffCollection(); _commonElements = new HeadersDiffCollection(); //the headers start after the first new line (following the request line) int nlIndex = text.IndexOf('\n') + 1; _basePosition += nlIndex; int headersEnd = text.IndexOf("\r\n\r\n"); if (headersEnd == -1) { headersEnd = text.IndexOf("\n\n"); if (headersEnd == -1) { headersEnd = text.Length; } } string headersText = String.Empty; if (headersEnd > nlIndex) { headersText = text.Substring(nlIndex, headersEnd - nlIndex); } int i, n = headersText.Length; StringBuilder currLine = new StringBuilder(ESTIMATED_LINE_SIZE); long pos = _basePosition; for (i = 0; i < n; i++) { if (headersText[i] == '\r' || headersText[i] == '\n') { string val = currLine.ToString(); if (val != String.Empty) { AddNewItem(headersText, i, pos, val); currLine = new StringBuilder(ESTIMATED_LINE_SIZE); } pos = _basePosition + i + 1; } else { currLine.Append(headersText[i]); } } //if the string finished without a \n if (currLine.Length > 0) { AddNewItem(headersText, n - 2, n - currLine.Length + _basePosition, currLine.ToString()); } }