Пример #1
0
        public override void Handler(WebRequest req)
        {
            if (req.URI.Length < this.ServiceURI.Length)
            {
                req.URI = this.ServiceURI;
            }

            if (!req.URI.StartsWith(this.ServiceURI))
            {
                throw new InvalidOperationException();
            }



            uriPath = req.URI;

            string[] pieces = req.URI.Substring(ServiceURI.Length).Split('/'); //split up the path by '/' tokens

            if (pieces.Length == 1 && pieces[0] == "")                         //we passed in only the root
            {
                RespondWithList(r_sys.GetRoot(), req);
            }


            for (int x = 0; x < pieces.Length; x++)
            {
                pieces[x] = decode(pieces[x]);
            }


            Dir422 dir = r_sys.GetRoot(); //grab the root of the filesystem

            if (req.httpMethod == "PUT")  //if the method is put.
            {
                foreach (Tuple <string, string> header in req.headers)
                {
                    if (header.Item1 == "Content-Length")
                    {
                        if (Convert.ToInt64(header.Item2) <= 0)
                        {
                            req.WriteInvalidUpload("400");
                            return;
                        }
                    }
                }


                for (int i = 0; i < pieces.Length - 1; i++) //go through the parts of the path
                {
                    dir = dir.getDir(pieces[i]);
                    if (dir == null) //if you encounter a directory that doesn't exist, tell the user that the target they requested is not found and return
                    {
                        req.WriteNotFoundResponse("File not found.\n");
                        return;
                    }
                }



                File422 fileToCreate = dir.GetFile(pieces[pieces.Length - 1]); //grab the last file of the path
                if (fileToCreate == null)
                {
                    string pathName = StandardFileSystem.rootPath;

                    for (int i = 0; i < pieces.Length; i++)
                    {
                        pathName += "/";
                        pathName += pieces[i];
                    }

                    FileStream fs = new FileStream(pathName, FileMode.Create, FileAccess.ReadWrite);


                    int    x           = 0;
                    byte[] bodyBytes   = new byte[4096];
                    string bodyContent = "";

                    x = req.bodyStream.Read(bodyBytes, 0, 4096);
                    fs.Write(bodyBytes, 0, 4096);

                    while (x > 0)
                    {
                        bodyContent += Encoding.ASCII.GetString(bodyBytes);
                        x            = req.bodyStream.Read(bodyBytes, 0, 4096);
                        fs.Write(bodyBytes, 0, 4096);
                    }



                    fs.Close();

                    req.WriteHTMLResponse("200 OK");
                }

                else
                {
                    req.WriteInvalidUpload("File already exists");
                }


                return;
            }


            for (int i = 0; i < pieces.Length - 1; i++) //go through the parts of the path
            {
                dir = dir.getDir(pieces[i]);
                if (dir == null) //if you encounter a directory that doesn't exist, tell the user that the target they requested is not found and return
                {
                    req.WriteNotFoundResponse("File not found.\n");
                    return;
                }
            }

            //we now have the directory of one above the file / directory



            //one piece to process left
            //check if dir is in the last piece we have
            File422 file = dir.GetFile(pieces[pieces.Length - 1]); //grab the last file of the path

            if (file != null)
            {
                RespondWithFile(file, req);
            }
            else
            {
                dir = dir.getDir(pieces[pieces.Length - 1]); //if it wasn't a file, grab it as a dir
                if (dir != null)
                {
                    RespondWithList(dir, req);
                }
                else //if it's null, tell the user it was not found
                {
                    req.WriteNotFoundResponse("Not found\n");
                }
            }
        }