@@ -228,7 +228,7 @@ public WebServer(int port, HttpProtocol protocol, Type[] controllers)
228
228
}
229
229
}
230
230
231
- _callbackRoutes . Add ( callbackRoutes ) ;
231
+ _callbackRoutes . Add ( callbackRoutes ) ;
232
232
}
233
233
}
234
234
}
@@ -495,106 +495,97 @@ private void StartListener()
495
495
new Thread ( ( ) =>
496
496
{
497
497
bool isRoute = false ;
498
- CallbackRoutes route ;
499
- int urlParam ;
498
+ string rawUrl = context . Request . RawUrl ;
499
+
500
+ //This is for handling with transitory or bad requests
501
+ if ( rawUrl == null )
502
+ {
503
+ return ;
504
+ }
505
+
506
+ int urlParam = rawUrl . IndexOf ( ParamStart ) ;
507
+
508
+ // Variables used only within the "for". They are here for performance reasons
500
509
bool isFound ;
510
+ string routeStr ;
501
511
int incForSlash ;
502
512
string toCompare ;
503
- string routeStr ;
504
- string rawUrl ;
513
+ bool mustAuthenticate ;
514
+ bool isAuthOk ;
515
+ //
505
516
506
517
foreach ( var rt in _callbackRoutes )
507
518
{
508
- route = ( CallbackRoutes ) rt ;
509
- urlParam = context . Request . RawUrl . IndexOf ( ParamStart ) ;
510
- isFound = false ;
519
+ CallbackRoutes route = ( CallbackRoutes ) rt ;
520
+
511
521
routeStr = route . Route ;
512
- rawUrl = context . Request . RawUrl ;
513
522
incForSlash = routeStr . IndexOf ( '/' ) == 0 ? 0 : 1 ;
514
523
toCompare = route . CaseSensitive ? rawUrl : rawUrl . ToLower ( ) ;
515
- if ( toCompare . IndexOf ( routeStr ) == incForSlash )
524
+
525
+ if ( urlParam > 0 )
526
+ {
527
+ isFound = urlParam == routeStr . Length + incForSlash ;
528
+ }
529
+ else
530
+ {
531
+ isFound = toCompare . Length == routeStr . Length + incForSlash ;
532
+ }
533
+
534
+ // Matching the route name
535
+ // Matching the method type
536
+ if ( ! isFound ||
537
+ ( toCompare . IndexOf ( routeStr ) != incForSlash ) ||
538
+ ( route . Method != string . Empty && context . Request . HttpMethod != route . Method )
539
+ )
516
540
{
517
- if ( urlParam > 0 )
541
+ continue ;
542
+ }
543
+
544
+ // Starting a new thread to be able to handle a new request in parallel
545
+ isRoute = true ;
546
+
547
+ // Check auth first
548
+ mustAuthenticate = route . Authentication != null && route . Authentication . AuthenticationType != AuthenticationType . None ;
549
+ isAuthOk = false ;
550
+
551
+ if ( mustAuthenticate )
552
+ {
553
+ if ( route . Authentication . AuthenticationType == AuthenticationType . Basic )
518
554
{
519
- if ( urlParam == routeStr . Length + incForSlash )
520
- {
521
- isFound = true ;
522
- }
555
+ var credSite = route . Authentication . Credentials ?? Credential ;
556
+ var credReq = context . Request . Credentials ;
557
+
558
+ isAuthOk = credReq != null
559
+ && ( credSite . UserName == credReq . UserName )
560
+ && ( credSite . Password == credReq . Password ) ;
523
561
}
524
- else
562
+ else if ( route . Authentication . AuthenticationType == AuthenticationType . ApiKey )
525
563
{
526
- if ( toCompare . Length == routeStr . Length + incForSlash )
527
- {
528
- isFound = true ;
529
- }
564
+ var apikeySite = route . Authentication . ApiKey ?? ApiKey ;
565
+ var apikeyReq = GetApiKeyFromHeaders ( context . Request . Headers ) ;
566
+
567
+ isAuthOk = apikeyReq != null
568
+ && apikeyReq == apikeySite ;
530
569
}
570
+ }
531
571
532
- if ( isFound
533
- && ( route . Method == string . Empty
534
- || ( context . Request . HttpMethod == route . Method ) ) )
572
+ if ( mustAuthenticate && isAuthOk )
573
+ {
574
+ route . Callback . Invoke ( null , new object [ ] { new WebServerEventArgs ( context ) } ) ;
575
+ context . Response . Close ( ) ;
576
+ context . Close ( ) ;
577
+ }
578
+ else
579
+ {
580
+ if ( route . Authentication . AuthenticationType == AuthenticationType . Basic )
535
581
{
536
- // Starting a new thread to be able to handle a new request in parallel
537
- isRoute = true ;
538
-
539
- // Check auth first
540
- bool isAuthOk = false ;
541
- if ( route . Authentication != null )
542
- {
543
- if ( route . Authentication . AuthenticationType == AuthenticationType . None )
544
- {
545
- isAuthOk = true ;
546
- }
547
- }
548
- else
549
- {
550
- isAuthOk = true ;
551
- }
552
-
553
- if ( ! isAuthOk )
554
- {
555
- if ( route . Authentication . AuthenticationType == AuthenticationType . Basic )
556
- {
557
- var credSite = route . Authentication . Credentials ?? Credential ;
558
- var credReq = context . Request . Credentials ;
559
- if ( credReq != null
560
- && ( credSite . UserName == credReq . UserName )
561
- && ( credSite . Password == credReq . Password ) )
562
- {
563
- isAuthOk = true ;
564
- }
565
- }
566
- else if ( route . Authentication . AuthenticationType == AuthenticationType . ApiKey )
567
- {
568
- var apikeySite = route . Authentication . ApiKey ?? ApiKey ;
569
- var apikeyReq = GetApiKeyFromHeaders ( context . Request . Headers ) ;
570
-
571
- if ( apikeyReq != null
572
- && apikeyReq == apikeySite )
573
- {
574
- isAuthOk = true ;
575
- }
576
- }
577
- }
578
-
579
- if ( isAuthOk )
580
- {
581
- route . Callback . Invoke ( null , new object [ ] { new WebServerEventArgs ( context ) } ) ;
582
- context . Response . Close ( ) ;
583
- context . Close ( ) ;
584
- }
585
- else
586
- {
587
- if ( route . Authentication . AuthenticationType == AuthenticationType . Basic )
588
- {
589
- context . Response . Headers . Add ( "WWW-Authenticate" , $ "Basic realm=\" Access to { routeStr } \" ") ;
590
- }
591
-
592
- context . Response . StatusCode = ( int ) HttpStatusCode . Unauthorized ;
593
- context . Response . ContentLength64 = 0 ;
594
- context . Response . Close ( ) ;
595
- context . Close ( ) ;
596
- }
582
+ context . Response . Headers . Add ( "WWW-Authenticate" , $ "Basic realm=\" Access to { routeStr } \" ") ;
597
583
}
584
+
585
+ context . Response . StatusCode = ( int ) HttpStatusCode . Unauthorized ;
586
+ context . Response . ContentLength64 = 0 ;
587
+ context . Response . Close ( ) ;
588
+ context . Close ( ) ;
598
589
}
599
590
}
600
591
0 commit comments