Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit 03bcaf1

Browse files
committed
improve parsing HTTP GET parameters to support badly encoded UII component with values containing special char =
1 parent 24ea5fa commit 03bcaf1

File tree

1 file changed

+40
-39
lines changed

1 file changed

+40
-39
lines changed

Erasme.Http/Erasme.Http/HttpUtility.cs

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ public static void ParseCommand(
469469
out string path, out Dictionary<string,string> queryString,
470470
out Dictionary<string,List<string>> queryStringArray, out string protocol)
471471
{
472-
string[] tmp = command.Split(' ');
472+
var tmp = command.Split(' ');
473473
if(tmp.Length != 3)
474474
throw new Exception("Invalid HTTP header, command not valid");
475475
// handle HTTP method
@@ -480,31 +480,24 @@ public static void ParseCommand(
480480
throw new Exception("Invalid HTTP header, protocol is not HTTP");
481481
// handle path
482482
fullPath = HttpUtility.UrlDecode(tmp[1]);
483-
tmp = tmp[1].Split('?');
484-
path = HttpUtility.UrlDecode(tmp[0]);
485-
// handle GET parameters
486-
queryString = new Dictionary<string,string>();
483+
484+
queryString = new Dictionary<string, string>();
487485
queryStringArray = new Dictionary<string, List<string>>();
488-
if(tmp.Length > 1) {
489-
tmp = tmp[1].Split('&');
490-
foreach(string keyval in tmp) {
491-
var tmp2 = keyval.Split('=');
492-
var key = HttpUtility.UrlDecode(tmp2[0]);
493-
if (tmp2.Length == 1)
494-
queryString[key] = null;
495-
else if (tmp2.Length == 2)
496-
{
497-
if (key.EndsWith("[]", StringComparison.InvariantCulture))
498-
{
499-
key = key.Substring(0, key.Length - 2);
500-
if (!queryStringArray.ContainsKey(key))
501-
queryStringArray[key] = new List<string>();
502-
queryStringArray[key].Add(HttpUtility.UrlDecode(tmp2[1]));
503-
}
504-
else
505-
queryString[key] = HttpUtility.UrlDecode(tmp2[1]);
506-
}
507-
}
486+
487+
var pos = tmp[1].IndexOf('?');
488+
if (pos < 0)
489+
{
490+
path = HttpUtility.UrlDecode(tmp[1]);
491+
}
492+
else if (pos == 0)
493+
{
494+
path = "";
495+
}
496+
else
497+
{
498+
path = HttpUtility.UrlDecode(tmp[1].Substring(0, pos));
499+
// handle GET parameters
500+
ParseFormUrlEncoded(tmp[1].Substring(pos+1), out queryString, out queryStringArray);
508501
}
509502
}
510503

@@ -516,24 +509,32 @@ public static void ParseFormUrlEncoded(
516509
queryString = new Dictionary<string, string>();
517510
queryStringArray = new Dictionary<string, List<string>>();
518511
var tmp = data.Split('&');
512+
519513
foreach (string keyval in tmp)
520514
{
521-
var tmp2 = keyval.Split('=');
522-
var key = HttpUtility.UrlDecode(tmp2[0]);
523-
if (tmp2.Length == 1)
524-
queryString[key] = null;
525-
else if (tmp2.Length == 2)
515+
string key = null;
516+
string val = "";
517+
var pos = keyval.IndexOf('=');
518+
if (pos < 0)
526519
{
527-
if (key.EndsWith("[]", StringComparison.InvariantCulture))
528-
{
529-
key = key.Substring(0, key.Length - 2);
530-
if (!queryStringArray.ContainsKey(key))
531-
queryStringArray[key] = new List<string>();
532-
queryStringArray[key].Add(HttpUtility.UrlDecode(tmp2[1]));
533-
}
534-
else
535-
queryString[key] = HttpUtility.UrlDecode(tmp2[1]);
520+
key = HttpUtility.UrlDecode(keyval);
521+
val = "";
536522
}
523+
else if (pos > 0)
524+
{
525+
key = HttpUtility.UrlDecode(keyval.Substring(0, pos));
526+
val = HttpUtility.UrlDecode(keyval.Substring(pos + 1));
527+
}
528+
529+
if (key.EndsWith("[]", StringComparison.InvariantCulture))
530+
{
531+
key = key.Substring(0, key.Length - 2);
532+
if (!queryStringArray.ContainsKey(key))
533+
queryStringArray[key] = new List<string>();
534+
queryStringArray[key].Add(val);
535+
}
536+
else
537+
queryString[key] = HttpUtility.UrlDecode(val);
537538
}
538539
}
539540

0 commit comments

Comments
 (0)