@@ -469,7 +469,7 @@ public static void ParseCommand(
469
469
out string path , out Dictionary < string , string > queryString ,
470
470
out Dictionary < string , List < string > > queryStringArray , out string protocol )
471
471
{
472
- string [ ] tmp = command . Split ( ' ' ) ;
472
+ var tmp = command . Split ( ' ' ) ;
473
473
if ( tmp . Length != 3 )
474
474
throw new Exception ( "Invalid HTTP header, command not valid" ) ;
475
475
// handle HTTP method
@@ -480,31 +480,24 @@ public static void ParseCommand(
480
480
throw new Exception ( "Invalid HTTP header, protocol is not HTTP" ) ;
481
481
// handle path
482
482
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 > ( ) ;
487
485
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 ) ;
508
501
}
509
502
}
510
503
@@ -516,24 +509,32 @@ public static void ParseFormUrlEncoded(
516
509
queryString = new Dictionary < string , string > ( ) ;
517
510
queryStringArray = new Dictionary < string , List < string > > ( ) ;
518
511
var tmp = data . Split ( '&' ) ;
512
+
519
513
foreach ( string keyval in tmp )
520
514
{
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 )
526
519
{
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 = "" ;
536
522
}
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 ) ;
537
538
}
538
539
}
539
540
0 commit comments