private static int GetMediaTypeWithQualityLength( string input, int start, out MediaTypeSegmentWithQuality result) { result = MediaType.CreateMediaTypeSegmentWithQuality(input, start); if (result.MediaType.HasValue) { return(result.MediaType.Length); } else { return(0); } }
private static int GetMediaTypeWithQualityLength( string input, int start, out MediaTypeSegmentWithQuality result) { result = MediaType.CreateMediaTypeSegmentWithQuality(input, start); if (result.MediaType.HasValue) { return result.MediaType.Length; } else { return 0; } }
public void ParseAcceptHeader_InvalidTokenAtEnd() { // Arrange var expected = new MediaTypeSegmentWithQuality[0]; var input = "*/*:"; // Act var parsed = AcceptHeaderParser.ParseAcceptHeader(new List <string>() { input }); // Assert Assert.Equal(expected, parsed); }
public void ParseAcceptHeader_DelimiterAtStart() { // Arrange var expected = new MediaTypeSegmentWithQuality[0]; var input = ",;:"; // Act var parsed = AcceptHeaderParser.ParseAcceptHeader(new List <string>() { input }); // Assert Assert.Equal(expected, parsed); }
public void ParseAcceptHeader_ValidMediaType_FollowedBySemicolon() { // Arrange var expected = new MediaTypeSegmentWithQuality[0]; var input = "*/*Content-Type;application/json"; // Act var parsed = AcceptHeaderParser.ParseAcceptHeader(new List <string>() { input }); // Assert Assert.Equal(expected, parsed); }
public void ParseAcceptHeader_InvalidTokenAtStart() { // Arrange var expected = new MediaTypeSegmentWithQuality[0]; var input = ":;:"; // Act var parsed = AcceptHeaderParser.ParseAcceptHeader(new List <string>() { input }); // Assert expected.Should().Equal(parsed); }
public void ParseAcceptHeader_ValidMediaType_FollowedByNondelimiter() { // Arrange var expected = new MediaTypeSegmentWithQuality[0]; var input = "*/*Content-Type:application/json"; // Act var parsed = AcceptHeaderParser.ParseAcceptHeader(new List <string>() { input }); // Assert expected.Should().Equal(parsed); }
public void ParseAcceptHeader_ValidMediaType_FollowedByWhitespace() { // Arrange var expected = new MediaTypeSegmentWithQuality[] { new MediaTypeSegmentWithQuality(new StringSegment("application/json"), 1.0), }; var input = "*/*Content-Type application/json"; // Act var parsed = AcceptHeaderParser.ParseAcceptHeader(new List <string>() { input }); // Assert Assert.Equal(expected, parsed); }
public void ParseAcceptHeader_ValidMediaType_FollowedByComma() { // Arrange var expected = new MediaTypeSegmentWithQuality[] { new MediaTypeSegmentWithQuality(new StringSegment("*/*Content-Type"), 1.0), new MediaTypeSegmentWithQuality(new StringSegment("application/json"), 1.0), }; var input = "*/*Content-Type,application/json"; // Act var parsed = AcceptHeaderParser.ParseAcceptHeader(new List <string>() { input }); // Assert expected.Should().Equal(parsed); }
private static bool TryParseValue(string value, ref int index, out MediaTypeSegmentWithQuality parsedValue) { parsedValue = default(MediaTypeSegmentWithQuality); // The accept header may be added multiple times to the request/response message. E.g. // Accept: text/xml; q=1 // Accept: // Accept: text/plain; q=0.2 // In this case, do not fail parsing in case one of the values is the empty string. if (string.IsNullOrEmpty(value) || (index == value.Length)) { return true; } var separatorFound = false; var currentIndex = GetNextNonEmptyOrWhitespaceIndex(value, index, out separatorFound); if (currentIndex == value.Length) { index = currentIndex; return true; } MediaTypeSegmentWithQuality result; var length = GetMediaTypeWithQualityLength(value, currentIndex, out result); if (length == 0) { return false; } currentIndex = currentIndex + length; currentIndex = GetNextNonEmptyOrWhitespaceIndex(value, currentIndex, out separatorFound); // If we've not reached the end of the string, then we must have a separator. // E. g application/json, text/plain <- We must be at ',' otherwise, we've failed parsing. if (!separatorFound && (currentIndex < value.Length)) { return false; } index = currentIndex; parsedValue = result; return true; }
private static bool TryParseValue(string value, ref int index, out MediaTypeSegmentWithQuality parsedValue) { parsedValue = default(MediaTypeSegmentWithQuality); // The accept header may be added multiple times to the request/response message. E.g. // Accept: text/xml; q=1 // Accept: // Accept: text/plain; q=0.2 // In this case, do not fail parsing in case one of the values is the empty string. if (string.IsNullOrEmpty(value) || (index == value.Length)) { return(true); } var currentIndex = GetNextNonEmptyOrWhitespaceIndex(value, index, out _); if (currentIndex == value.Length) { index = currentIndex; return(true); } // We deliberately want to ignore media types that we are not capable of parsing. // This is due to the fact that some browsers will send invalid media types like // ; q=0.9 or */;q=0.2, etc. // In this scenario, our recovery action consists of advancing the pointer to the // next separator and moving on. // In case we don't find the next separator, we simply advance the cursor to the // end of the string to signal that we are done parsing. var result = default(MediaTypeSegmentWithQuality); int length; try { length = GetMediaTypeWithQualityLength(value, currentIndex, out result); } catch { length = 0; } if (length == 0) { // The parsing failed. currentIndex = value.IndexOf(',', currentIndex); if (currentIndex == -1) { index = value.Length; return(false); } index = currentIndex; return(false); } currentIndex = currentIndex + length; currentIndex = GetNextNonEmptyOrWhitespaceIndex(value, currentIndex, out var separatorFound); // If we've not reached the end of the string, then we must have a separator. // E. g application/json, text/plain <- We must be at ',' otherwise, we've failed parsing. if (!separatorFound && (currentIndex < value.Length)) { index = currentIndex; return(false); } index = currentIndex; parsedValue = result; return(true); }