Browse Source

commit: error handling

HOME 4 years ago
parent
commit
e575baf754
1 changed files with 26 additions and 16 deletions
  1. 26 16
      HttpServer/HttpProgram.cs

+ 26 - 16
HttpServer/HttpProgram.cs

@@ -77,27 +77,37 @@ namespace HttpServer
 
                     var requestPath = request.Url.LocalPath.ToLower();
 
-                    if (requestPath == "/boot/ipxe")
+                    try
                     {
-                        Console.WriteLine(" Send init script");
-                        var buffer = Encoding.ASCII.GetBytes(IpxeScriptManager.GetInitScript(Properties.Settings.Default.ListenPrefix));
-                        context.Response.OutputStream.Write(buffer, 0, buffer.Length);
-                        context.Response.Close();
+                        if (requestPath == "/boot/ipxe")
+                        {
+                            Console.WriteLine(" Send init script");
+                            var buffer =
+                                Encoding.ASCII.GetBytes(
+                                    IpxeScriptManager.GetInitScript(Properties.Settings.Default.ListenPrefix));
+                            context.Response.OutputStream.Write(buffer, 0, buffer.Length);
+                        }
+                        else if (requestPath == "/boot/ipxe/script")
+                        {
+                            var queryString = HttpUtility.ParseQueryString(request.Url.Query);
+                            var mac = queryString["mac"].ToUpper();
+
+                            Console.WriteLine($" MAC:{mac}, send boot script");
+
+                            var buffer = Encoding.ASCII.GetBytes(IpxeScriptManager.GetScript(mac));
+                            context.Response.OutputStream.Write(buffer, 0, buffer.Length);
+                        }
+                        else
+                        {
+                            context.Response.StatusCode = 404;
+                        }
                     }
-                    else if (requestPath == "/boot/ipxe/script")
+                    catch (Exception e)
                     {
-                        var queryString = HttpUtility.ParseQueryString(request.Url.Query);
-                        var mac = queryString["mac"].ToUpper();
-
-                        Console.WriteLine($" MAC:{mac}, send boot script");
-
-                        var buffer = Encoding.ASCII.GetBytes(IpxeScriptManager.GetScript(mac));
-                        context.Response.OutputStream.Write(buffer, 0, buffer.Length);
-                        context.Response.Close();
+                        Console.WriteLine(e);
                     }
-                    else
+                    finally
                     {
-                        context.Response.StatusCode = 404;
                         context.Response.Close();
                     }
                 }