public void TestServerDirectoryFileDump()
        {
            string pathQuery = Guid.NewGuid().ToString();
            string tempDir   = Path.Combine(Path.GetTempPath(), pathQuery);

            Directory.CreateDirectory(tempDir);

            string loremIpsum = "Lorem ipsum dolor sit amet";

            StringBuilder sb = new StringBuilder(loremIpsum.Length * 1024);

            for (int i = 0; i < 102400; i++)
            {
                sb.Append(loremIpsum);
            }

            File.WriteAllText(Path.Combine(tempDir, "file.txt"), sb.ToString());

            MockDeaClient.SetResponse(tempDir);

            MockDeaClient client = new MockDeaClient();

            CloudFoundry.WinDEA.DirectoryServer.DirectoryServer server = new CloudFoundry.WinDEA.DirectoryServer.DirectoryServer();
            server.Start("127.0.0.1", DirectoryConfiguration.ReadConfig(), client);

            string output = DownloadString(string.Format("http://127.0.0.1:{0}/{1}", DirectoryConfiguration.ReadConfig().DirectoryServer.V2Port, "file.txt"));

            server.Stop();

            Assert.IsFalse(string.IsNullOrWhiteSpace(output));

            Assert.IsTrue(output == sb.ToString());
        }
        private void WriteFile(string path, bool tail, HttpListenerContext context)
        {
            try
            {
                if (tail)
                {
                    Logger.Debug("Directory server tailing file '{0}'", path);
                    string mimeType = DetectContentType(path);

                    context.Response.StatusCode  = (int)System.Net.HttpStatusCode.OK;
                    context.Response.ContentType = mimeType;

                    StreamHandler streamHandler = new StreamHandler(path, TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(this.streamingTimeout), context);
                    streamHandler.Start();
                }
                else
                {
                    DumpFile(path, context);
                }
            }
            catch (Exception ex)
            {
                DirectoryServer.WriteServerError(ex.Message, context);
            }
        }
        /// <summary>
        /// If validation with the DEA is successful, the HTTP request is served.
        /// Otherwise, the same HTTP response from the DEA is served as response to
        /// the HTTP request.
        /// </summary>
        /// <param name="listenerContext">Http context to respond to.</param>
        private void ServeHttp(object listenerContext)
        {
            try
            {
                HttpListenerContext context = (HttpListenerContext)listenerContext;

                Uri uri = context.Request.Url;

                PathLookupResponse response = this.deaClient.LookupPath(uri);

                if (!string.IsNullOrWhiteSpace(response.Error))
                {
                    Logger.Warning(Strings.ErrorInLookupPath, response.Error);
                    DirectoryServer.WriteServerError(Strings.WinDEADidNotRespondProperly, context);
                }
                else
                {
                    var queryString = string.Join(string.Empty, uri.PathAndQuery.Split('?').Skip(1));
                    NameValueCollection queryStrings = System.Web.HttpUtility.ParseQueryString(queryString);
                    bool tail = queryStrings.AllKeys.Select(key => queryStrings.GetValues(key).Contains("tail")).Any(v => v);

                    Logger.Debug("Directory Server file request is: {0}; path is {1}", uri, queryStrings["path"]);

                    this.ListPath(response.Path, tail, context);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Directory Server - there was an error serving http: {0}", ex.ToString());
            }
        }
 /// <summary>
 /// Lists directory, or writes file contents in the HTTP response as per the
 /// the response received from the DEA. If the "tail" parameter is part of
 /// the HTTP request, then the file contents are streamed through chunked
 /// HTTP transfer encoding. Otherwise, the entire file is dumped in the HTTP
 /// response.
 /// Writes appropriate errors and status codes in the HTTP response if there is
 /// a problem in reading the file or directory.
 /// </summary>
 /// <param name="path">The path.</param>
 /// <param name="tail">if set to <c>true</c> it means we tail the file live.</param>
 /// <param name="context">Http context to respond to.</param>
 private void ListPath(string path, bool tail, HttpListenerContext context)
 {
     if (File.Exists(path))
     {
         this.WriteFile(path, tail, context);
     }
     else if (Directory.Exists(path))
     {
         DirectoryServer.ListDir(path, context);
     }
     else
     {
         Logger.Warning(Strings.PathNotFound, path);
         DirectoryServer.WriteEntityNotFound(context);
     }
 }
        public void TestServerDirectoryDirList()
        {
            string pathQuery = Guid.NewGuid().ToString();
            string tempDir = Path.Combine(Path.GetTempPath(), pathQuery);

            Directory.CreateDirectory(tempDir);
            Directory.CreateDirectory(Path.Combine(tempDir, "dir1"));
            Directory.CreateDirectory(Path.Combine(tempDir, "dir2"));

            string loremIpsum = "Lorem ipsum dolor sit amet";

            File.WriteAllText(Path.Combine(tempDir, "file1.txt"), loremIpsum);

            StringBuilder sb = new StringBuilder(loremIpsum.Length * 1024);
            for (int i = 0; i < 1024; i++)
            {
                sb.Append(loremIpsum);
            }

            File.WriteAllText(Path.Combine(tempDir, "file2.txt"), sb.ToString());

            MockDeaClient.SetResponse(Path.GetTempPath());

            MockDeaClient client = new MockDeaClient();
            CloudFoundry.WinDEA.DirectoryServer.DirectoryServer server = new CloudFoundry.WinDEA.DirectoryServer.DirectoryServer();
            server.Start("127.0.0.1", DirectoryConfiguration.ReadConfig(), client);

            string output = DownloadString(string.Format("http://127.0.0.1:{0}/{1}", DirectoryConfiguration.ReadConfig().DirectoryServer.V2Port, pathQuery));

            server.Stop();

            Assert.IsFalse(string.IsNullOrWhiteSpace(output));

            string expectedOutput = string.Format(@"dir1/                                        -
            dir2/                                        -
            file1.txt                                  {0}B
            file2.txt                               {1}K
            ", loremIpsum.Length, ((1024.0 * loremIpsum.Length) / 1024).ToString("0.00"));

            Assert.IsTrue(output == expectedOutput);
        }
        public void TestServerDirectoryFileTail()
        {
            string pathQuery = Guid.NewGuid().ToString();
            string tempDir   = Path.Combine(Path.GetTempPath(), pathQuery);

            Directory.CreateDirectory(tempDir);

            string loremIpsum = "Lorem ipsum dolor sit amet";

            string filePath = Path.Combine(tempDir, "file.txt");

            File.WriteAllText(filePath, loremIpsum);

            MockDeaClient.SetResponse(tempDir);

            MockDeaClient client = new MockDeaClient();

            CloudFoundry.WinDEA.DirectoryServer.DirectoryServer server = new CloudFoundry.WinDEA.DirectoryServer.DirectoryServer();
            DEAElement config = DirectoryConfiguration.ReadConfig();

            config.DirectoryServer.StreamingTimeoutMS = 5000;
            server.Start("127.0.0.1", config, client);

            Random rnd = new Random();

            string returnBytes = string.Empty;
            string sentBytes   = string.Empty;
            int    readCount   = 0;

            string appendChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

            Thread newThread = new Thread(() =>
            {
                byte[] randomBytes = new byte[rnd.Next(100)];
                rnd.NextBytes(randomBytes);

                File.AppendAllText(filePath, ASCIIEncoding.ASCII.GetString(randomBytes));

                HttpWebResponse response = null;
                HttpWebRequest request   = (HttpWebRequest)HttpWebRequest.Create(string.Format("http://127.0.0.1:{0}/{1}?tail", DirectoryConfiguration.ReadConfig().DirectoryServer.V2Port, "file.txt"));
                response = (HttpWebResponse)request.GetResponse();

                Stream responseStream = response.GetResponseStream();

                byte[] responseBytes = new byte[200];

                int read;

                do
                {
                    read         = responseStream.Read(responseBytes, 0, responseBytes.Length);
                    returnBytes += ASCIIEncoding.ASCII.GetString(responseBytes, 0, read);
                    readCount++;
                }while (read != 0);
            });

            Thread writerThread = new Thread(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    Thread.Sleep(50);

                    string toWrite = string.Empty;

                    for (int j = 0; j < rnd.Next(100); j++)
                    {
                        toWrite += appendChars[rnd.Next(appendChars.Length)];
                    }


                    File.AppendAllText(filePath, toWrite);
                    sentBytes += toWrite;
                }
            });

            newThread.Start();
            Thread.Sleep(1000);
            writerThread.Start();

            writerThread.Join();
            newThread.Join();

            server.Stop();

            Assert.AreEqual(sentBytes, returnBytes);
            Assert.IsTrue(readCount > 1);
        }
        public void TestServerDirectoryFileTail()
        {
            string pathQuery = Guid.NewGuid().ToString();
            string tempDir = Path.Combine(Path.GetTempPath(), pathQuery);

            Directory.CreateDirectory(tempDir);

            string loremIpsum = "Lorem ipsum dolor sit amet";

            string filePath = Path.Combine(tempDir, "file.txt");

            File.WriteAllText(filePath, loremIpsum);

            MockDeaClient.SetResponse(tempDir);

            MockDeaClient client = new MockDeaClient();
            CloudFoundry.WinDEA.DirectoryServer.DirectoryServer server = new CloudFoundry.WinDEA.DirectoryServer.DirectoryServer();
            DEAElement config = DirectoryConfiguration.ReadConfig();
            config.DirectoryServer.StreamingTimeoutMS = 5000;
            server.Start("127.0.0.1", config, client);

            Random rnd = new Random();

            string returnBytes = string.Empty;
            string sentBytes = string.Empty;
            int readCount = 0;

            string appendChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

            Thread newThread = new Thread(() =>
            {
                byte[] randomBytes = new byte[rnd.Next(100)];
                rnd.NextBytes(randomBytes);

                File.AppendAllText(filePath, ASCIIEncoding.ASCII.GetString(randomBytes));

                HttpWebResponse response = null;
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format("http://127.0.0.1:{0}/{1}?tail", DirectoryConfiguration.ReadConfig().DirectoryServer.V2Port, "file.txt"));
                response = (HttpWebResponse)request.GetResponse();

                Stream responseStream = response.GetResponseStream();

                byte[] responseBytes = new byte[200];

                int read;

                do
                {
                    read = responseStream.Read(responseBytes, 0, responseBytes.Length);
                    returnBytes += ASCIIEncoding.ASCII.GetString(responseBytes, 0, read);
                    readCount ++;
                }
                while (read != 0);
            });

            Thread writerThread = new Thread(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    Thread.Sleep(50);

                    string toWrite = string.Empty;

                    for (int j = 0; j < rnd.Next(100); j++)
                    {
                        toWrite += appendChars[rnd.Next(appendChars.Length)];
                    }

                    File.AppendAllText(filePath, toWrite);
                    sentBytes += toWrite;
                }
            });

            newThread.Start();
            Thread.Sleep(1000);
            writerThread.Start();

            writerThread.Join();
            newThread.Join();

            server.Stop();

            Assert.AreEqual(sentBytes, returnBytes);
            Assert.IsTrue(readCount > 1);
        }