Esempio n. 1
0
 public static ServerReply DELETE(RequestKontext req)
 {
     if (req.Options.Contains("/") == false)
     {
         return(BadRequest(req));
     }
     string[] frag = req.Options.Split('/');
     if (frag[1] == "messages" && frag.Length == 3)
     {
         if (Convert.ToInt32(frag[2]) <= Directory.GetFiles(pfad).Length&& Convert.ToInt32(frag[2]) > 0 && frag[2] != "") //if the index doesn't pass the number of messages
         {
             int    index         = Convert.ToInt32(frag[2]) - 1;
             string file_on_index = Convert.ToString(Directory.GetFiles(pfad).GetValue(index));
             File.Delete(file_on_index);
             return(new ServerReply(req.Protocol, "200 OK", "", "text"));
         }
         else
         {
             return(OuttaRange(req));
         }
     }
     else
     {
         return(BadRequest(req));
     }
 }
Esempio n. 2
0
        public static ServerReply PUT(RequestKontext req)
        {
            if (req.Options.Contains("/") == false)
            {
                return(BadRequest(req));
            }
            string[] frag = req.Options.Split('/');
            if (frag[1] == "messages" && frag.Length == 3)
            {
                if (Convert.ToInt32(frag[2]) <= Directory.GetFiles(pfad).Length&& Convert.ToInt32(frag[2]) > 0 && frag[2] != "")
                {
                    if (req.Body[0] != '\n')
                    {
                        string file_on_index;
                        int    index = Convert.ToInt32(frag[2]) - 1;
                        file_on_index = Convert.ToString(Directory.GetFiles(pfad).GetValue(index));
                        File.WriteAllText(file_on_index, req.Body);
                        return(new ServerReply(req.Protocol, "200 OK", "", "text"));
                    }

                    else
                    {
                        return(BadRequest(req));
                    }
                }
                else
                {
                    return(OuttaRange(req));
                }
            }
            else
            {
                return(BadRequest(req));
            }
        }
Esempio n. 3
0
        public static ServerReply POST(RequestKontext req)
        {
            if (req.Options.Contains("/") == false)
            {
                return(BadRequest(req));
            }
            string[] frag         = req.Options.Split('/');
            int      num_of_files = Directory.GetFiles(pfad).Length;

            if (frag[1] == "messages" && frag.Length == 2)
            {
                if (req.Body == "")
                {
                    return(BadRequest(req));
                }
                else
                {
                    string name = "Message " + Convert.ToString(num_of_files + 1) + ".txt";
                    using (File.Create(pfad + name))
                    {
                    }

                    File.WriteAllText(pfad + name, req.Body);
                    return(new ServerReply(req.Protocol, "200 OK", Convert.ToString(num_of_files + 1), "text"));
                }
            }
            else
            {
                return(BadRequest(req));
            }
        }
Esempio n. 4
0
        public void ClientHandler(TcpClient client)
        {
            StreamReader stream = new StreamReader(client.GetStream()); // Stream mit den Request/CLient infos
            String       msg    = "";                                   // Die Infos werden hier gespeichert

            //StreamWrite, Flush -> senden, despose -> schließen

            if (stream == null)
            {
                throw new NullReferenceException("NULL VALUE!");
            }

            while (stream.Peek() != -1)          //Peek returned den nächsten Charakter oder -1 wenn keine mehr vorhanden sind
            {
                msg += stream.ReadLine() + "\n"; //Hier wird der Stream in dem string gespeichert
            }

            RequestKontext req   = RequestKontext.GetRequest(msg);
            ServerReply    reply = ServerReply.HandlingRequest(req);

            using (StreamWriter writer = new StreamWriter(client.GetStream())) //using für Idisposable
            {
                writer.Write($"{reply.Protocoll} {reply.Status}\r\n");
                writer.Write($"Content-Type: {reply.ContentType}\r\n");
                writer.Write($"Content-Length: {reply.Data.Length}\r\n");
                writer.Write("\r\n");
                writer.Write($"{reply.Data}\r\n");
                writer.Write("\r\n\r\n");
            }
        }
Esempio n. 5
0
        public static ServerReply HandlingRequest(RequestKontext req)
        {
            pfad = "C:\\Users\\titto\\Desktop\\3.Semester\\Software Engineering\\RestServer\\Restful API Ue1\\Messages\\";
            if (req == null)
            {
                return(BadRequest(req));
            }

            //Header Informationen vom  client werden ausgegeben
            Console.WriteLine($"Type: {req.Type}");
            Console.WriteLine($"Request: {req.Options}");
            Console.WriteLine($"Protocoll: {req.Protocol}");

            foreach (HeaderInfo tmp in RequestKontext.HeaderInformation)
            {
                Console.WriteLine($"{tmp.key}:{tmp.value}");
            }
            ///
            if (req.Type == "GET")
            {
                return(GET(req));
            }

            else if (req.Type == "POST")
            {
                return(POST(req));
            }
            else if (req.Type == "DELETE")
            {
                return(DELETE(req));
            }
            else if (req.Type == "PUT")
            {
                return(PUT(req));
            }
            else
            {
                return(new ServerReply(req.Protocol, "405 Method Not Allowed", "", "text"));
            }
        }
Esempio n. 6
0
        public static ServerReply GET(RequestKontext req)
        {
            if (req.Options.Contains("/") == false)    //Get - localhost:123123/messages/1
            {
                return(new ServerReply(req.Protocol, "404 Not Found", "", "text"));
            }
            string[] frag = req.Options.Split('/'); //---/messages
            if (frag[1] == "messages" && frag.Length == 2)
            {
                string   name;
                string[] messages = Directory.GetFiles(pfad); //geting all messages (with full path)
                //Converting String array into String
                StringBuilder tmp = new StringBuilder();
                foreach (string value in messages)
                {
                    name = value.Replace(pfad, "");
                    tmp.Append("Name: ");
                    tmp.Append(name);
                    tmp.Append("\n");
                    tmp.Append("Nachricht:\n");
                    //getting the message out of the txt file
                    using (var streamReader = new StreamReader(value, Encoding.UTF8))
                    {
                        tmp.Append(streamReader.ReadToEnd());
                    }
                    tmp.Append("\n\n");
                }
                string _return = tmp.ToString();
                return(new ServerReply(req.Protocol, "200 OK", _return, "text"));
            }
            else if (frag[1] == "messages" && frag.Length == 3 && frag[2] != "")                                                 // frag[0]="", frag[1]="messages", frag[3]=integer
            {
                if (Convert.ToInt32(frag[2]) <= Directory.GetFiles(pfad).Length&& frag[2] != "" && Convert.ToInt32(frag[2]) > 0) //if the index doesn't pass the number of messages
                {
                    string        file_on_index;
                    int           index = Convert.ToInt32(frag[2]) - 1;
                    StringBuilder tmp   = new StringBuilder();
                    file_on_index = Convert.ToString(Directory.GetFiles(pfad).GetValue(index));
                    tmp.Append("Name: ");
                    tmp.Append(file_on_index.Remove(0, pfad.Length));
                    tmp.Append("\n");
                    tmp.Append("Nachricht:\n");
                    using (var streamReader = new StreamReader(file_on_index, Encoding.UTF8))
                    {
                        tmp.Append(streamReader.ReadToEnd());
                    }


                    string _return = Convert.ToString(tmp);

                    return(new ServerReply(req.Protocol, "200 OK", _return, "text"));
                }

                else
                {
                    return(OuttaRange(req));
                }
            }
            else
            {
                return(BadRequest(req));
            }
        }
Esempio n. 7
0
 public static ServerReply OuttaRange(RequestKontext req)
 {
     return(new ServerReply(req.Protocol, "416 Range Not Satisfiable", "", "text"));
 }
Esempio n. 8
0
 public static ServerReply BadRequest(RequestKontext req)
 {
     return(new ServerReply(req.Protocol, "400 Bad Request", "", "text"));
 }