Beispiel #1
0
        private HttpResponse HandleGetRequest(HttpRequest request)
        {
            HttpResponse response   = new HttpResponse("200", "OK");
            FileHandle   fileHandle = new FileHandle();
            string       path       = fileHandle.PathWithRoot(fileHandle.ComputePath(request.Values["Route"]));

            if (File.Exists(path + ".txt"))
            {
                response.AddPayload(File.ReadAllText(path + ".txt"));
            }
            else if (Directory.Exists(path) && path == fileHandle.Root)
            {
                response.AddPayload(File.ReadAllText(Path.Join(fileHandle.Root, "help.txt")));
            }
            else if (Directory.Exists(path))
            {
                string[] files = fileHandle.GetFiles(path);

                if (files.Length == 0)
                {
                    response.AddPayload("No messages available");
                }
                else
                {
                    response.AddPayload($"Found messages: {files.Length}\n");

                    foreach (string filePath in files)
                    {
                        int id = Convert.ToInt32(Path.GetFileNameWithoutExtension(filePath));
                        response.AddPayload($"Message {id}:\n");
                        response.AddPayload(File.ReadAllText(filePath));
                        response.AddPayload("\n\n");
                    }
                }
            }
            else
            {
                response.Status        = "404";
                response.StatusMessage = "Not Found";
            }

            return(response);
        }
Beispiel #2
0
        private HttpResponse HandleDeleteRequest(HttpRequest request)
        {
            HttpResponse response   = new HttpResponse("200", "OK");
            FileHandle   fileHandle = new FileHandle();
            string       path       = fileHandle.PathWithRoot(fileHandle.ComputePath(request.Values["Route"]));

            if (File.Exists(path + ".txt"))
            {
                response.AddPayload("Deleted Successfully\n");
                File.Delete(path + ".txt");
            }
            else
            {
                response.StatusMessage = "Not Found";
                response.Status        = "404";
            }

            return(response);
        }