コード例 #1
0
        private Dir422 TraverseToDirectory(Dir422 currentLocation, string[] uriPieces, WebRequest req)
        {
            File422 fileAtRoot = null;

            //Iterate over all path parts until we get to the file or directory wanted
            for (int item = 0; item < uriPieces.Length - 1; item++)
            {
                fileAtRoot = currentLocation.GetFile(uriPieces[item]);

                if (fileAtRoot == null)
                {
                    currentLocation = currentLocation.GetDir(uriPieces[item]);

                    if (currentLocation == null)
                    {
                        req.WriteNotFoundResponse("<html>404 File Not Found<br>" + "</html>");
                    }
                }
                else
                {
                    break;
                }
            }

            return(currentLocation);
        }
コード例 #2
0
        private string BuildPath(Dir422 dir, File422 file)
        {
            Dir422 tempDir;

            //Check if we are building a path for a file or a directory
            if (file != null)
            {
                tempDir = file.Parent;
            }
            else
            {
                tempDir = dir;
            }

            List <string> pathItems = new List <string>();

            if (tempDir.Parent != null)
            {
                //Work our way up the tree
                while (tempDir.Parent.Parent != null)
                {
                    pathItems.Add(tempDir.Name);
                    tempDir = tempDir.Parent;
                }
            }

            string path = "";

            //Are they accessing an item in the root directory?
            if (pathItems.Count > 0)
            {
                path = "/files/" + pathItems[pathItems.Count - 1];

                for (int i = pathItems.Count - 2; i > -1; i--)
                {
                    path += "/" + pathItems[i];
                }
            }
            else
            {
                if (file != null)
                {
                    path = "/files/" + file.Name;
                }
                else
                {
                    path = "/files/" + dir.Name;
                }
            }

            return(path);
        }
コード例 #3
0
        public override File422 CreateFile(string fileName)
        {
            if (!CheckValidName(fileName))
            {
                return(null);
            }

            File422 newFile = GetFile(fileName);

            if (newFile == null)
            {
                newFile = new MemFSFile(fileName, this);
                _files.Add(newFile);
            }

            return(newFile);
        }
コード例 #4
0
        //Create a new file. If the file exists, set the length to 0.
        public override File422 CreateFile(string fileName)
        {
            if (!CheckValidName(fileName))
            {
                return(null);
            }

            File422    newFile       = null;
            FileStream newFileStream = null;

            if (File.Exists(fileName))
            {
                try
                {
                    newFile       = GetFile(fileName);
                    newFileStream = (FileStream)newFile.OpenReadWrite();

                    //Remove all existing contents
                    newFileStream.SetLength(0);
                    newFileStream.Close();
                }
                catch (Exception e)
                {
                    newFile = null;
                }
            }
            else
            {
                try
                {
                    newFileStream = File.Create(_path + "/" + fileName);
                    newFile       = new StdFSFile(_path + "/" + fileName);
                    newFileStream.Close();
                }
                catch (Exception e)
                {
                    newFile = null;
                }
            }

            return(newFile);
        }
コード例 #5
0
 public virtual bool Contains(File422 file)
 {
     return(Contains(file.Parent));
 }
コード例 #6
0
        public override void Handler(WebRequest req)
        {
            //If the request URI doesn't start with the proper service URI
            if (!req.GetUri().StartsWith(this.ServiceURI, StringComparison.CurrentCulture))
            {
                throw new InvalidOperationException();
            }

            //Get Uri pieces
            string[] uriPieces = GetUriPeices(req);

            //Decode path
            uriPieces = DecodePath(uriPieces);

            //Get the root directory
            Dir422 currentLocation = FileSystem.GetRoot();

            //Handle PUT requests
            if (req.GetHTTPType() == "PUT")
            {
                FileStream newFileStream = null;

                try
                {
                    currentLocation = TraverseToDirectory(currentLocation, uriPieces, req);

                    if (currentLocation.ContainsFile(uriPieces[uriPieces.Length - 1], false) == true)
                    {
                        req.WriteNotFoundResponse("<html>File Already Exists!</html>");
                    }
                    else
                    {
                        File422 newFile = currentLocation.CreateFile(uriPieces[uriPieces.Length - 1]);
                        newFileStream = (FileStream)newFile.OpenReadWrite();

                        CopyBytes(req.GetBody(), newFileStream, req.GetBody().Length);

                        newFileStream.Close();

                        string response = "<html>File Uploaded!</html>";
                        string header   = string.Format(putHeaderTemplate, req.GetHTTPVersion(), "text/html", response.Length);

                        req.WriteHTMLResponse(header + response);
                    }
                }
                catch (Exception e)
                {
                    if (newFileStream != null)
                    {
                        newFileStream.Close();
                    }
                }
            }
            //Handle GET requests
            else
            {
                //If we're at the root directory, respond with a list
                if (uriPieces.Length == 0)
                {
                    string response = BuildDirHTML(FileSystem.GetRoot());

                    string header = string.Format(dirHeaderTemplate, req.GetHTTPVersion(), "text/html", response.Length);

                    req.WriteHTMLResponse(header + response);
                }
                else
                {
                    currentLocation = TraverseToDirectory(currentLocation, uriPieces, req);

                    //If the wanted item is a file, respond with a file
                    if (currentLocation.GetFile(uriPieces[uriPieces.Length - 1]) != null)
                    {
                        //If we have a range header, we need to use a partial response header
                        if (req.GetHeaders().ContainsKey("range"))
                        {
                            string header = GetRangeHeader(req, currentLocation, uriPieces);

                            req.WriteHTMLResponse(header);
                        }
                        //Write repsonse for displaying file contents
                        else
                        {
                            Stream selectedFile = currentLocation.GetFile(uriPieces[uriPieces.Length - 1]).OpenReadOnly();
                            string header       = string.Format(fileHeaderTemplate, req.GetHTTPVersion(), GetContentType(currentLocation.GetFile(uriPieces[uriPieces.Length - 1]).Name), selectedFile.Length);
                            selectedFile.Close();

                            req.WriteHTMLResponse(header);
                        }

                        BuildFileHTML(currentLocation.GetFile(uriPieces[uriPieces.Length - 1]), req);
                    }
                    //If the wanted item is a directory, respond with a directory listing
                    else
                    {
                        //Write response for displaying directory
                        if (currentLocation.GetDir(uriPieces[uriPieces.Length - 1]) != null)
                        {
                            string response = BuildDirHTML(currentLocation.GetDir(uriPieces[uriPieces.Length - 1]));

                            string header = string.Format(dirHeaderTemplate, req.GetHTTPVersion(), "text/html", response.ToString().Length);

                            req.WriteHTMLResponse(header + response);
                        }
                        //Request item could not be found
                        else
                        {
                            req.WriteNotFoundResponse("<html>404 File Not Found<br>" + "</html>");
                        }
                    }
                }
            }
        }
コード例 #7
0
        private void BuildFileHTML(File422 File, WebRequest req)
        {
            long start = 0,
                 end   = -1;

            Stream fileStream = File.OpenReadOnly();

            //Do we have a range header? Set the bounds
            if (req.GetHeaders().ContainsKey("range"))
            {
                string value = "";
                req.GetHeaders().TryGetValue("range", out value);

                if (value != "")
                {
                    value = value.Substring(6);
                    string[] nums = value.Split('-');
                    long.TryParse(nums[0], out start);
                    long.TryParse(nums[1], out end);

                    for (int i = 0; i < nums.Length; i++)
                    {
                        if (nums[i].Contains("\r\n"))
                        {
                            nums[i] = nums[i].Substring(0, nums[i].LastIndexOf("\r\n"));
                        }
                    }

                    if (end == 0 && nums[1] == string.Empty)
                    {
                        end     = fileStream.Length;
                        nums[1] = end.ToString();
                    }

                    fileStream.Seek(start, SeekOrigin.Begin);

                    if (end > fileStream.Length)
                    {
                        end = (int)fileStream.Length;
                    }

                    if (end < 0)
                    {
                        start = 0;
                        end   = 0;
                    }

                    if (start > fileStream.Length)
                    {
                        start = 0;
                        end   = 0;
                    }

                    if (start < 0)
                    {
                        start = 0;
                    }
                }
            }
            //No range header? Set the bounds to 0 an length
            else
            {
                start = 0;
                end   = (int)fileStream.Length;
            }

            CopyBytes(fileStream, req.GetBody(), end - start);

            fileStream.Close();
        }