Ejemplo n.º 1
0
 public void RespondRequest(Request request)
 {
     byte[] response;
     if (request.Requested.HttpMethod == "GET")
     {
         try
         {
             request.Response.StatusCode = (int)HttpStatusCode.OK;
             if (request.Requested.Cookies.Count == 0)
             {
                 request.Response.SetCookie(new Cookie("session", Security.GetUniqueKey()));
             }
             const string path = @"M:\YandexDisk\Projects\In progress\SignUp Service\WebSite";
             var          url  = request.Requested.RawUrl;
             if (url == "/")
             {
                 response = Encoding.UTF8.GetBytes(File.ReadAllText(path + @"\index.html"));
                 request.Response.ContentType = "text/html; charset=UTF-8";
             }
             else
             {
                 response = Encoding.UTF8.GetBytes(File.ReadAllText(path + url.Replace('/', '\\')));
                 request.Response.ContentType = $"{MimeMapping.GetMimeMapping(url)}; charset=UTF-8";
             }
         }
         catch (FileNotFoundException)
         {
             request.Response.StatusCode = (int)HttpStatusCode.NotFound;
             response = new byte[0];
         }
     }
     else if (request.Requested.HttpMethod == "POST")
     {
         request.Response.StatusCode = (int)HttpStatusCode.OK;
         RequestContext ctx         = request.GetPostRequestData();
         string         requestText = JSON.Stringify(ctx);
         response = Encoding.UTF8.GetBytes(requestText);
         request.Response.ContentType = "application/json";
     }
     else //Unexpected HTTP method
     {
         request.Response.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
         response = new byte[0];
     }
     request.Response.ContentLength64 = response.Length;
     using (Stream output = request.Response.OutputStream)
     {
         output.Write(response, 0, response.Length);
     }
     Task.Run(() => onRequestHandled());
 }