Beispiel #1
0
        private void PutHandler(WebRequest req)
        {
            //NOTE use Uri.UnescapeDataString to convert the escaped string
            //to it's unescaped representation.
            //i.e if we get http://localhost:4220/%20test/
            //would become http://localhost:4220/ test/
            string[] names = Uri.UnescapeDataString(req.RequestTarget).Split(new char[] { '/' },
                                                                             StringSplitOptions.RemoveEmptyEntries);

            //first name is files.
            string currString = "";

            Dir422 currNode = _fs.GetRoot(); //root is what we chose, the client knows it as "files".
            Dir422 nextNode = null;

            for (int i = 1; i < names.Length; i++)
            {
                currString = names[i];
                if (i == names.Length - 1) // the c in /a/b/c
                //put the file.
                {
                    byte[] buffer       = new byte[1024];
                    Stream concatStream = req.Body;
                    int    read         = 0;

                    string path = _fs.GetRoot().Name + BuildPath(currNode) + "/" + names[i];

                    //Don't allow overwriting existing files or files with \ character
                    if (currNode.ContainsFile(names[i], false) || names[i].Contains("\\"))
                    {
                        return;
                    }

                    using (FileStream newFile = File.OpenWrite(path))
                    {
                        while ((read = concatStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            newFile.Write(buffer, 0, read);
                            Array.Clear(buffer, 0, buffer.Length); //reset buffer for next read.
                        }
                    }
                }
                else if ((nextNode = currNode.GetDir(currString)) != null)
                {
                    //go to next dir to repeat iteration.
                    currNode = nextNode;
                }
                else
                {
                    return;
                }
            }

            string htmlString = BuildDirHTML(currNode);

            //req.WriteHTMLResponse(htmlString);
            req.WriteRegularResponse(htmlString);
        }