Beispiel #1
0
 public void d_CreateContainsFile()
 {
     //Dir422 dir = new StdFSDir(rootDir, true);
     //dir.CreateFile("fooFile");
     //Assert.AreEqual(true, dir.ContainsFile("fooFile", false));
     root.CreateFile("fooFile");
     Assert.AreEqual(true, root.ContainsFile("fooFile", false));
 }
Beispiel #2
0
        public void StdFSDirContainsFileNonRecursiveTest()
        {
            Dir422 c = root.GetDir("one").GetDir("a").GetDir("b").GetDir("c");

            root.CreateFile("at_root.txt");
            c.CreateFile("at_ones_c.txt");

            Assert.True(root.ContainsFile("at_root.txt", false));
            Assert.False(root.ContainsFile("at_ones_c.txt", false));
        }
Beispiel #3
0
        public virtual bool ContainsFile(string fileName, bool recursive)
        {
            File422 file = GetFile(fileName);

            if (file != null)
            {
                return(true);
            }

            if (!recursive)
            {
                return(false);
            }

            IList <Dir422> directories = GetDirs();

            for (int i = 0; i < directories.Count(); i++)
            {
                Dir422 dir = directories [i];

                if (dir.ContainsFile(fileName, true))
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #4
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);
        }
Beispiel #5
0
        public void f_GetDirMakeFile()
        {
            //Dir422 dir = new StdFSDir(rootDir + "/FooDir");
            //Dir422 BarDir = dir.GetDir("BarDir");
            //BarDir.CreateFile("barFile2");
            //Assert.AreEqual(true, BarDir.ContainsFile("barFile2", true));
            Dir422 FooDir = root.GetDir("FooDir");
            Dir422 BarDir = FooDir.GetDir("BarDir");

            BarDir.CreateFile("barFile2");
            Assert.AreEqual(true, BarDir.ContainsFile("barFile2", true));
        }
Beispiel #6
0
        /*
         * Don’t allow overwriting existing files
         * Allow all (non-existing-file-overwriting) uploads provided the file name doesn’t contain reserve characters (/ and \)
         * Files can be uploaded to any directory that the service shares
         * Files must be uploaded into the current directory that the user is seeing the listing for within the browser
         * There is no limit on file sizes or content
         * You only need to support uploading 1 file at a time (1 per page refresh, as described below)
         * You do NOT need to support cross-origin requests or the OPTIONS method, which will have been discussed in class
         *
         */
        void handlePUT(WebRequest req)
        {
            string[] path = req.URI.Split('/');

            Dir422 currentDir = fileSystem.GetRoot();

            // Don't include last element because thats the file name
            for (int i = 2; i < path.Length - 1; i++)
            {
                // Fix '/' at end of URI
                if (path[i] != "")
                {
                    currentDir = currentDir.GetDir(path[i]);
                }
            }

            // The file already exists
            if (currentDir.ContainsFile(path[path.Length - 1], false))
            {
                req.WriteNotFoundResponse("File exists");
            }
            // Make file
            else
            {
                File422 file = currentDir.CreateFile(path[path.Length - 1]);

                using (Stream stream = file.OpenReadWrite())
                {
                    byte[] buffer = new byte[1024 * 8];
                    int    bytesRead = 0;
                    long   size = long.Parse(req.Headers["content-length"]), totalRead = 0;

                    while (totalRead < size)
                    {
                        bytesRead = req.Body.Read(buffer, 0, (size - totalRead > buffer.Length) ? buffer.Length : (int)(size - totalRead));

                        totalRead += bytesRead;

                        stream.Write(buffer, 0, (bytesRead < size) ? bytesRead : (int)size);
                    }
                }

                req.WriteHTMLResponse("file uploaded!");
            }
        }
Beispiel #7
0
        private void PUTHandler(WebRequest req, string name, Dir422 dir)
        {
            if (dir.ContainsFile(name, false) || name.Contains("\\") || name.Contains("/"))
            {
                string bodyMessage = "Invalid: File already exists or filename is invalid";
                string template    = "HTTP/1.1 400 Bad Request\r\n" +
                                     "Content-Type: text/html\r\n" +
                                     "Content-Length: {0}\r\n\r\n" +
                                     "{1}";

                string response      = string.Format(template, Encoding.ASCII.GetBytes(bodyMessage).Length, bodyMessage);
                byte[] responseBytes = Encoding.ASCII.GetBytes(response);
                req.WriteResponse(responseBytes, 0, responseBytes.Length);
                return;
            }

            File422    file = dir.CreateFile(name);
            FileStream fs   = (FileStream)file.OpenReadWrite();

            long fileSize = 0;

            Int64.TryParse(req.BodySize, out fileSize);
            int totalread = 0;

            while (true)
            {
                byte[] buf  = new byte[4096];
                int    read = req.Body.Read(buf, 0, buf.Length);
                fs.Write(buf, 0, read);
                totalread += read;
                if ((read == 0) || read < buf.Length && totalread >= fileSize)
                {
                    break;
                }
            }
            fs.Dispose(); fs.Close();
            req.WriteHTMLResponse("Successfully uploaded file: " + name);
        }
Beispiel #8
0
        public void RunTest(FileSys422 mySys)
        {
            Dir422 root = mySys.GetRoot();

            //We should not be able to go above our root.
            Assert.IsNull(root.Parent);

            // Checking that we do not have a file
            Assert.IsFalse(root.ContainsFile("NewFile.txt", false));
            //create the file
            root.CreateFile("NewFile.txt");
            // Check that we can find it.
            Assert.IsTrue(root.ContainsFile("NewFile.txt", false));

            // Same with directory
            Assert.IsFalse(root.ContainsDir("SubDir", false));
            Dir422 subDir = root.CreateDir("SubDir");

            Assert.IsTrue(root.ContainsDir("SubDir", false));

            //Creating a file in a sub dir
            subDir.CreateFile("subText.txt");

            // Testing the recursive methods on files
            Assert.IsFalse(root.ContainsFile("subText.txt", false));
            Assert.IsTrue(root.ContainsFile("subText.txt", true));

            //Testing recurcive method on dirs
            subDir.CreateDir("newSubDir");

            Assert.IsFalse(root.ContainsDir("newSubDir", false));
            Assert.IsTrue(root.ContainsDir("newSubDir", true));

            //Checking getDir
            Dir422 recivedDir = root.GetDir("InvalidDir");

            Assert.IsNull(recivedDir);
            recivedDir = root.GetDir("SubDir");
            Assert.AreEqual("SubDir", recivedDir.Name);

            // Checking that if a file does not exist we return null,
            // otherwise we recived the file we wanted.
            File422 recidedFile = root.GetFile("InvalidFile");

            Assert.IsNull(recidedFile);
            recidedFile = root.GetFile("NewFile.txt");
            Assert.AreEqual("NewFile.txt", recidedFile.Name);

            //Checking the name validation function.
            // All of these methods use the same Validate Name method.
            Assert.IsNull(subDir.CreateFile("file/New.txt"));
            Assert.IsNull(subDir.CreateDir("file/New"));

            string bufString = "hello world";

            byte[] buff        = ASCIIEncoding.ASCII.GetBytes(bufString);
            var    writeStream = recidedFile.OpenReadWrite();

            writeStream.Write(buff, 0, 11);

            var readStream = recidedFile.OpenReadOnly();

            Assert.IsNull(readStream);

            writeStream.Dispose();

            readStream = recidedFile.OpenReadOnly();
            Assert.IsNotNull(readStream);

            //First read 'hello ' from each stream
            byte[] readBuf = new byte[6];
            readStream.Read(readBuf, 0, 6);
            Assert.AreEqual("hello ", ASCIIEncoding.ASCII.GetString(readBuf));

            //Having two streams open for read
            var readStream2 = recidedFile.OpenReadOnly();

            Assert.IsNotNull(readStream2);

            byte[] readBuf2 = new byte[6];
            readStream2.Read(readBuf2, 0, 6);
            Assert.AreEqual("hello ", ASCIIEncoding.ASCII.GetString(readBuf2));

            //Next read 'world' from each stream
            readBuf = new byte[5];
            readStream.Read(readBuf, 0, 5);
            Assert.AreEqual("world", ASCIIEncoding.ASCII.GetString(readBuf));

            readBuf2 = new byte[5];
            readStream2.Read(readBuf2, 0, 5);
            Assert.AreEqual("world", ASCIIEncoding.ASCII.GetString(readBuf2));

            //try to open a stream to write while there are streams open for read
            writeStream = recidedFile.OpenReadWrite();
            Assert.IsNull(writeStream);

            //Close streams and try again
            readStream.Close();
            readStream2.Close();

            writeStream = recidedFile.OpenReadWrite();
            Assert.IsNotNull(writeStream);
        }
Beispiel #9
0
        public override void Handler(WebRequest Req)
        {
            Dir422 Root = m_FS.GetRoot();

            //Remove Last / here!!!!
            if (Req.URI.LastIndexOf('/') == Req.URI.Length - 1)
            {
                Req.URI = Req.URI.Substring(0, Req.URI.Length - 1);
            }
            //int x = 0;


            //1. Percent-decode URI.
            // THIS NOT GOOD ENOUGH!!!

            Req.URI = Utility.PercentDecode(Req.URI);
            //string test = Req.URI.Replace ("%20", " ");;

            // Set name of requested file||dir.
            string uriName = Utility.NameFromPath(Req.URI);

            //2. If it refers to file somewhere in the shared folder.
            if (Root.ContainsFile(uriName, true))
            {
                string path = Req.URI.Substring(0, Req.URI.LastIndexOf("/"));

                Dir422     Dir          = Utility.TraverseToDir(Root, path);
                StdFSFile  File         = (StdFSFile)Dir.GetFile(uriName);
                FileStream MyFileStream = (FileStream)File.OpenReadOnly();

                if (Req.headers.ContainsKey("Range"))
                {
                    // process partial response here
                    Console.WriteLine("Process Partial Request");
                    int x = 0;
                }
                else
                {
                    string contentType = Utility.ContentType(uriName);
                    string response    = Utility.BuildFileResponseString(
                        MyFileStream.Length.ToString(), contentType);

                    //if (contentType != "video/mp4")
                    //{
                    byte[] sendResponseString = Encoding.ASCII.GetBytes(response);
                    Req.bodyRequest.Write(sendResponseString, 0, sendResponseString.Length);
                    //}

                    //byte[] sendResponseString = Encoding.ASCII.GetBytes(response);

                    int    read = 0;
                    byte[] send = new byte[7500];

                    while (read < MyFileStream.Length)
                    {
                        read = read + MyFileStream.Read(send, 0, send.Length);
                        Req.bodyRequest.Write(send, 0, send.Length);
                    }
                }
            }

            //3. Else if it refers to a folder somewhere in the shared folder.
            //	 Or is the shared directory
            else if (Root.ContainsDir(uriName, true) || Req.URI == ServiceURI || uriName == "")
            {
                if (Req.URI == ServiceURI || uriName == "")
                {
                    string dirHTMLListing = BuildDirHTML(Root);

                    byte[] sendResponseString = Encoding.ASCII.GetBytes(dirHTMLListing);
                    Req.bodyRequest.Write(sendResponseString, 0, sendResponseString.Length);

                    //Req.WriteHTMLResponse (dirHTMLListing);
                }
                else
                {
                    Dir422 Dir            = Utility.TraverseToDir(Root, Req.URI);
                    string dirHTMLListing = BuildDirHTML(Dir);

                    byte[] sendResponseString = Encoding.ASCII.GetBytes(dirHTMLListing);
                    Req.bodyRequest.Write(sendResponseString, 0, sendResponseString.Length);

                    //Req.WriteHTMLResponse (dirHTMLListing);
                }
            }

            //4.Else it’s a bad URI.
            else
            {
                Req.WriteNotFoundResponse("File or directory not found");
            }
        }
 public void c_CreateContainFile()
 {
     root.CreateFile("testFile");
     Assert.AreEqual(true, root.ContainsFile("testFile", false));
 }