Skip to content

Commit

Permalink
Fixing WebRequest hang issue (#2219)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddynaka authored Jan 5, 2021
1 parent fcdd3af commit b5860b1
Showing 1 changed file with 7 additions and 35 deletions.
42 changes: 7 additions & 35 deletions src/Sarif/Core/WebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -112,49 +113,20 @@ private static Uri SynthesizeAbsoluteUriFrom(Uri uri)
return uri;
}

// In this query pattern, note how backtracking is disabled with (?>...) while parsing the
// parameter names and values. A parameter name can't contain a '=', and a parameter value
// can't contain a '&', so it's never necessary to backtrack over an individual parameter
// name or value.
const string QueryPattern =
@"^
\? # A literal '?', followed by zero or more of...
(
(?>
(?<name>[^=]+) # a parameter name (everything that's not an = sign),
)
= # an equal sign, and
(
(?>
(?<value>[^&]*) # the value (everything that's not an '&')...
)
&? # and an '&' (except after the last one).
)
)*
$";

private static readonly Regex s_queryRegex = SarifUtilities.RegexFromPattern(QueryPattern);

private static IDictionary<string, string> ParseParametersFromQueryString(string query)
{
IDictionary<string, string> parameterDictionary = new Dictionary<string, string>();

Match match = s_queryRegex.Match(query);
NameValueCollection nameValueCollection = System.Web.HttpUtility.ParseQueryString(query);

// RFC 3986 does _not_ require a URI's query to consist of key-value pairs. If it
// doesn't, don't fail; just return an empty parameter dictionary.
if (match.Success)
for (int i = 0; i < nameValueCollection.Count; i++)
{
List<string> names = match.Groups["name"].Captures.Cast<Capture>().Select(c => c.Value).ToList();
List<string> values = match.Groups["value"].Captures.Cast<Capture>().Select(c => c.Value).ToList();
string key = nameValueCollection.GetKey(i);
string value = nameValueCollection.GetValues(i).FirstOrDefault();

if (names.Count == values.Count)
if (!string.IsNullOrEmpty(key))
{
parameterDictionary = new Dictionary<string, string>();
for (int i = 0; i < names.Count; ++i)
{
parameterDictionary.Add(names[i], values[i]);
}
parameterDictionary.Add(key, value);
}
}

Expand Down

0 comments on commit b5860b1

Please sign in to comment.