1818
1919namespace nanoFramework . WebServer
2020{
21+ /// <summary>
22+ /// This class instantiates a web server.
23+ /// </summary>
2124 public class WebServer : IDisposable
2225 {
2326 /// <summary>
@@ -41,8 +44,8 @@ public class WebServer : IDisposable
4144
4245 private bool _cancel = false ;
4346 private Thread _serverThread = null ;
44- private ArrayList _callbackRoutes ;
45- private HttpListener _listener ;
47+ private readonly ArrayList _callbackRoutes ;
48+ private readonly HttpListener _listener ;
4649
4750 #endregion
4851
@@ -101,16 +104,13 @@ public static UrlParameter[] DecodeParam(string parameter)
101104 {
102105 UrlParameter [ ] retParams = null ;
103106 int i = parameter . IndexOf ( ParamStart ) ;
104- int j = i ;
105107 int k ;
106108
107109 if ( i >= 0 )
108110 {
109- //look at the number of = and ;
110-
111111 while ( ( i < parameter . Length ) || ( i == - 1 ) )
112112 {
113- j = parameter . IndexOf ( ParamEqual , i ) ;
113+ int j = parameter . IndexOf ( ParamEqual , i ) ;
114114 if ( j > i )
115115 {
116116 //first param!
@@ -161,13 +161,19 @@ public static UrlParameter[] DecodeParam(string parameter)
161161 #region Constructors
162162
163163 /// <summary>
164- /// Instantiates a new webserver .
164+ /// Instantiates a new web server .
165165 /// </summary>
166166 /// <param name="port">Port number to listen on.</param>
167- /// <param name="timeout">Timeout to listen and respond to a request in millisecond .</param>
167+ /// <param name="protocol"><see cref="HttpProtocol"/> version to use with web server .</param>
168168 public WebServer ( int port , HttpProtocol protocol ) : this ( port , protocol , null )
169169 { }
170170
171+ /// <summary>
172+ /// Instantiates a new web server.
173+ /// </summary>
174+ /// <param name="port">Port number to listen on.</param>
175+ /// <param name="protocol"><see cref="HttpProtocol"/> version to use with web server.</param>
176+ /// <param name="controllers">Controllers to use with this web server.</param>
171177 public WebServer ( int port , HttpProtocol protocol , Type [ ] controllers )
172178 {
173179 _callbackRoutes = new ArrayList ( ) ;
@@ -222,7 +228,7 @@ public WebServer(int port, HttpProtocol protocol, Type[] controllers)
222228 }
223229 }
224230
225- _callbackRoutes . Add ( callbackRoutes ) ; ;
231+ _callbackRoutes . Add ( callbackRoutes ) ;
226232 }
227233 }
228234 }
@@ -246,14 +252,14 @@ public WebServer(int port, HttpProtocol protocol, Type[] controllers)
246252
247253 private Authentication ExtractAuthentication ( string strAuth )
248254 {
249- const string None = "None" ;
250- const string Basic = "Basic" ;
251- const string ApiKey = "ApiKey" ;
255+ const string _none = "None" ;
256+ const string _basic = "Basic" ;
257+ const string _apiKey = "ApiKey" ;
252258
253- Authentication authentication = null ;
254- if ( strAuth . IndexOf ( None ) == 0 )
259+ Authentication authentication ;
260+ if ( strAuth . IndexOf ( _none ) == 0 )
255261 {
256- if ( strAuth . Length == None . Length )
262+ if ( strAuth . Length == _none . Length )
257263 {
258264 authentication = new Authentication ( ) ;
259265 }
@@ -262,16 +268,16 @@ private Authentication ExtractAuthentication(string strAuth)
262268 throw new ArgumentException ( $ "Authentication attribute None can only be used alone") ;
263269 }
264270 }
265- else if ( strAuth . IndexOf ( Basic ) == 0 )
271+ else if ( strAuth . IndexOf ( _basic ) == 0 )
266272 {
267- if ( strAuth . Length == Basic . Length )
273+ if ( strAuth . Length == _basic . Length )
268274 {
269275 authentication = new Authentication ( ( NetworkCredential ) null ) ;
270276 }
271277 else
272278 {
273279 var sep = strAuth . IndexOf ( ':' ) ;
274- if ( sep == Basic . Length )
280+ if ( sep == _basic . Length )
275281 {
276282 var space = strAuth . IndexOf ( ' ' ) ;
277283 if ( space < 0 )
@@ -289,16 +295,16 @@ private Authentication ExtractAuthentication(string strAuth)
289295 }
290296 }
291297 }
292- else if ( strAuth . IndexOf ( ApiKey ) == 0 )
298+ else if ( strAuth . IndexOf ( _apiKey ) == 0 )
293299 {
294- if ( strAuth . Length == ApiKey . Length )
300+ if ( strAuth . Length == _apiKey . Length )
295301 {
296302 authentication = new Authentication ( string . Empty ) ;
297303 }
298304 else
299305 {
300306 var sep = strAuth . IndexOf ( ':' ) ;
301- if ( sep == ApiKey . Length )
307+ if ( sep == _apiKey . Length )
302308 {
303309 var key = strAuth . Substring ( sep + 1 ) ;
304310 authentication = new Authentication ( key ) ;
@@ -422,38 +428,30 @@ public static void OutputHttpCode(HttpListenerResponse response, HttpStatusCode
422428 public static void SendFileOverHTTP ( HttpListenerResponse response , StorageFile strFilePath , string contentType = "" )
423429 {
424430 contentType = contentType == "" ? GetContentTypeFromFileName ( strFilePath . FileType ) : contentType ;
431+ IBuffer readBuffer = FileIO . ReadBuffer ( strFilePath ) ;
432+ long fileLength = readBuffer . Length ;
425433
426- try
427- {
428- IBuffer readBuffer = FileIO . ReadBuffer ( strFilePath ) ;
429- long fileLength = readBuffer . Length ;
430-
431- response . ContentType = contentType ;
432- response . ContentLength64 = fileLength ;
433- // Now loops sending all the data.
434+ response . ContentType = contentType ;
435+ response . ContentLength64 = fileLength ;
436+ // Now loops sending all the data.
434437
435- byte [ ] buf = new byte [ MaxSizeBuffer ] ;
436- using ( DataReader dataReader = DataReader . FromBuffer ( readBuffer ) )
438+ byte [ ] buf = new byte [ MaxSizeBuffer ] ;
439+ using ( DataReader dataReader = DataReader . FromBuffer ( readBuffer ) )
440+ {
441+ for ( long bytesSent = 0 ; bytesSent < fileLength ; )
437442 {
438- for ( long bytesSent = 0 ; bytesSent < fileLength ; )
439- {
440- // Determines amount of data left.
441- long bytesToRead = fileLength - bytesSent ;
442- bytesToRead = bytesToRead < MaxSizeBuffer ? bytesToRead : MaxSizeBuffer ;
443- // Reads the data.
444- dataReader . ReadBytes ( buf ) ;
445- // Writes data to browser
446- response . OutputStream . Write ( buf , 0 , ( int ) bytesToRead ) ;
447- // allow some time to physically send the bits. Can be reduce to 10 or even less if not too much other code running in parallel
448- // Updates bytes read.
449- bytesSent += bytesToRead ;
450- }
443+ // Determines amount of data left.
444+ long bytesToRead = fileLength - bytesSent ;
445+ bytesToRead = bytesToRead < MaxSizeBuffer ? bytesToRead : MaxSizeBuffer ;
446+ // Reads the data.
447+ dataReader . ReadBytes ( buf ) ;
448+ // Writes data to browser
449+ response . OutputStream . Write ( buf , 0 , ( int ) bytesToRead ) ;
450+ // allow some time to physically send the bits. Can be reduce to 10 or even less if not too much other code running in parallel
451+ // Updates bytes read.
452+ bytesSent += bytesToRead ;
451453 }
452454 }
453- catch ( Exception e )
454- {
455- throw e ;
456- }
457455 }
458456
459457 /// <summary>
@@ -466,32 +464,24 @@ public static void SendFileOverHTTP(HttpListenerResponse response, StorageFile s
466464 public static void SendFileOverHTTP ( HttpListenerResponse response , string fileName , byte [ ] content , string contentType = "" )
467465 {
468466 contentType = contentType == "" ? GetContentTypeFromFileName ( fileName . Substring ( fileName . LastIndexOf ( '.' ) ) ) : contentType ;
467+ response . ContentType = contentType ;
468+ response . ContentLength64 = content . Length ;
469469
470- try
471- {
472- response . ContentType = contentType ;
473- response . ContentLength64 = content . Length ;
474-
475- // Now loop to send all the data.
470+ // Now loop to send all the data.
476471
477- for ( long bytesSent = 0 ; bytesSent < content . Length ; )
478- {
479- // Determines amount of data left
480- long bytesToSend = content . Length - bytesSent ;
481- bytesToSend = bytesToSend < MaxSizeBuffer ? bytesToSend : MaxSizeBuffer ;
472+ for ( long bytesSent = 0 ; bytesSent < content . Length ; )
473+ {
474+ // Determines amount of data left
475+ long bytesToSend = content . Length - bytesSent ;
476+ bytesToSend = bytesToSend < MaxSizeBuffer ? bytesToSend : MaxSizeBuffer ;
482477
483- // Writes data to output stream
484- response . OutputStream . Write ( content , ( int ) bytesSent , ( int ) bytesToSend ) ;
478+ // Writes data to output stream
479+ response . OutputStream . Write ( content , ( int ) bytesSent , ( int ) bytesToSend ) ;
485480
486- // allow some time to physically send the bits. Can be reduce to 10 or even less if not too much other code running in parallel
481+ // allow some time to physically send the bits. Can be reduce to 10 or even less if not too much other code running in parallel
487482
488- // update bytes sent
489- bytesSent += bytesToSend ;
490- }
491- }
492- catch ( Exception e )
493- {
494- throw e ;
483+ // update bytes sent
484+ bytesSent += bytesToSend ;
495485 }
496486 }
497487
@@ -539,7 +529,9 @@ private void StartListener()
539529 }
540530 }
541531
542- if ( isFound && ( ( route . Method == string . Empty || ( context . Request . HttpMethod == route . Method ) ) ) )
532+ if ( isFound
533+ && ( route . Method == string . Empty
534+ || ( context . Request . HttpMethod == route . Method ) ) )
543535 {
544536 // Starting a new thread to be able to handle a new request in parallel
545537 isRoute = true ;
@@ -562,27 +554,24 @@ private void StartListener()
562554 {
563555 if ( route . Authentication . AuthenticationType == AuthenticationType . Basic )
564556 {
565- var credSite = route . Authentication . Credentials == null ? Credential : route . Authentication . Credentials ;
557+ var credSite = route . Authentication . Credentials ?? Credential ;
566558 var credReq = context . Request . Credentials ;
567- if ( credReq != null )
559+ if ( credReq != null
560+ && ( credSite . UserName == credReq . UserName )
561+ && ( credSite . Password == credReq . Password ) )
568562 {
569- if ( ( credSite . UserName == credReq . UserName ) && ( credSite . Password == credReq . Password ) )
570- {
571- isAuthOk = true ;
572- }
563+ isAuthOk = true ;
573564 }
574565 }
575566 else if ( route . Authentication . AuthenticationType == AuthenticationType . ApiKey )
576567 {
577- var apikeySite = route . Authentication . ApiKey == null ? ApiKey : route . Authentication . ApiKey ;
568+ var apikeySite = route . Authentication . ApiKey ?? ApiKey ;
578569 var apikeyReq = GetApiKeyFromHeaders ( context . Request . Headers ) ;
579570
580- if ( apikeyReq != null )
571+ if ( apikeyReq != null
572+ && apikeyReq == apikeySite )
581573 {
582- if ( apikeyReq == apikeySite )
583- {
584- isAuthOk = true ;
585- }
574+ isAuthOk = true ;
586575 }
587576 }
588577 }
@@ -638,12 +627,10 @@ private void StartListener()
638627 private string GetApiKeyFromHeaders ( WebHeaderCollection headers )
639628 {
640629 var sec = headers . GetValues ( "ApiKey" ) ;
641- if ( sec != null )
630+ if ( sec != null
631+ && sec . Length > 0 )
642632 {
643- if ( sec . Length > 0 )
644- {
645- return sec [ 0 ] ;
646- }
633+ return sec [ 0 ] ;
647634 }
648635
649636 return null ;
0 commit comments