public void Init()
        {
            //Init...
            _sys     = new StandardFileSystem();
            _sys     = StandardFileSystem.Create("FSROOT");
            _service = new FilesWebService(_sys);
            _request = new CS422.WebRequest();

            //Set up the filesystem
            //Create Files
            File.Create(_rootPath + "/file1").Close();
            File.Create(_rootPath + "/file2").Close();

            //Create Directory
            Directory.CreateDirectory(_rootPath + "/Dir1");

            //Create subDirs
            Directory.CreateDirectory(_rootPath + "/Dir1/SubDir1");
            Directory.CreateDirectory(_rootPath + "/Dir1/SubDir2");
            Directory.CreateDirectory(_rootPath + "/Dir1/SubDir1/DoubleSubDir1");

            //Create subFiles
            File.Create(_rootPath + "/Dir1/subfile1").Close();
            File.Create(_rootPath + "/Dir1/SubDir1/doubleSubFile1").Close();

            Console.WriteLine("Setup Complete");
        }
        public void Init()
        {
            //Init...
            _sys     = new StandardFileSystem();
            _sys     = StandardFileSystem.Create("FSROOT");
            _service = new FilesWebService(_sys);
            _request = new CS422.WebRequest();
            _root    = _sys.GetRoot();

            //Set up the filesystem
            //Create Files
            FileStream file = File.Create(_rootPath + "/file1");
            String     s    = "this is a file with content";

            byte[] buffer = Encoding.ASCII.GetBytes(s);
            file.Write(buffer, 0, s.Length);
            file.Close();
            File.Create(_rootPath + "/file2").Close();

            //Create Directory
            Directory.CreateDirectory(_rootPath + "/Dir1");

            //Create subDirs
            Directory.CreateDirectory(_rootPath + "/Dir1/SubDir1");
            Directory.CreateDirectory(_rootPath + "/Dir1/SubDir2");
            Directory.CreateDirectory(_rootPath + "/Dir1/SubDir1/DoubleSubDir1");

            //Create subFiles
            File.Create(_rootPath + "/Dir1/subfile1").Close();
            File.Create(_rootPath + "/Dir1/SubDir1/doubleSubFile1").Close();

            Console.WriteLine("Setup Complete");
        }
Example #3
0
        public void FormatResponseTest()
        {
            DemoService service = new DemoService();

            CS422.WebRequest request = new CS422.WebRequest();
            request.Method      = "GET";
            request.URI         = "/";
            request.HTTPVersion = "HTTP/1.1";

            TcpListener listener = new TcpListener(IPAddress.Any, 8000);

            listener.Start();
            Console.WriteLine("listening...");
            TcpClient client = new TcpClient();

            client = listener.AcceptTcpClient();
            Console.WriteLine("Accepted connection");
            //Console.WriteLine("waiting...");
            //Thread.Sleep(100000);

            request.StreamRef = client.GetStream();
            //int bodySize = (int)request.Body.Length;
            service.Handler(request);
            Console.WriteLine("here");

            Assert.NotNull(service);
            // Assert.AreEqual(result, _formattedString);
            Console.WriteLine("closing connection..");
            client.Close();
            //listener.Close();
            listener.Stop();
            //Assert.Fail();
        }
Example #4
0
        public void initialization()
        {
            _request             = new CS422.WebRequest();
            _request.Method      = "GET";
            _request.URI         = "/";
            _request.HTTPVersion = "HTTP/1.1";
            _request.Body        = new MemoryStream();
            int bodySize = (int)_request.Body.Length;

            _responseTemplate = "{0} {1} {2}\r\n" +
                                "Content-Type: text/html\r\n" +
                                "Content-Length: {3}\r\n" +
                                "\r\n\r\n" +
                                "{4}";
        }
Example #5
0
        public static void threadDoWork()
        {
            TcpClient client   = null;
            bool      firstRun = true;

            CS422.WebRequest request = null;
            while (client != null || firstRun == true)
            {
                if (firstRun == true)
                {
                    firstRun = false;
                }

                client = cList.Take();
                if (client != null)
                {
                    request = WebServer.BuildRequest(client);
                    if (request != null)
                    {
                        // good request and client so just find a handler for it

//						for(int i = 0; i < services.Count; i++)
//						{
//							if(request.requestTarget.StartsWith(services[i].ServiceURI))
//							{
//								services[i].Handler(request);
//								break;
//							}
//						}
                        foreach (var x in services)
                        {
                            if (request.requestTarget.StartsWith(x.ServiceURI))
                            {
                                x.Handler(request);
                                break;
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("bad request, connection closed");
                        client.GetStream().Close();
                        client.Close();
                    }
                }
            }
        }
Example #6
0
        private void SendFileContent(File422 file, WebRequest req)
        {
            byte[]        buffer = new byte[1024]; //1kb buffer
            StringBuilder sb     = new StringBuilder();
            string        contentType;             //default contentType
            string        status = "200 Success";  //default status
            int           initialPos;
            long          contentLength, startByte = 0;

            //find extension
            if (!exts.TryGetValue(Path.GetExtension(file.Name).ToLower(),
                                  out contentType))
            {
                //default
                contentType = "application/octet-stream";
            }

            using (Stream fs = file.OpenReadOnly()){
                contentLength = fs.Length;
                //if client sent a range request.
                if (req.Headers.ContainsKey("range"))
                {
                    var t = req.GetRangeHeader(fs.Length);
                    //find range
                    if (t == null)
                    {
                        string pageHTML = "<html><h1>416 REQUESTED RANGE NOT SATISFIABLE</h1></html>";
                        req.WriteRangeNotSatisfiableResponse(pageHTML,
                                                             fs.Length.ToString());
                        return;
                    }
                    status        = "206 Partial Content";
                    startByte     = t.Item1;                 //start offset byte
                    contentLength = (t.Item2 - t.Item1) + 1; //because contentLength is the length, not last byte.
                }

                sb.Append("HTTP/1.1 " + status + "\r\n");
                sb.Append("Content-Length: " + contentLength + "\r\n");
                sb.Append("Content-Type: " + contentType + "\r\n");

                //we need this so that the file downloads, instead
                //of trying to switch views.
                sb.Append("Content-Disposition: attachment; filename=\"" +
                          file.Name + "\"\r\n");

                sb.Append("\r\n");
                initialPos = sb.Length;

                ASCIIEncoding.ASCII.GetBytes(sb.ToString()).CopyTo(buffer, 0);

                if (req.Headers.ContainsKey("range"))
                {
                    int totalBytesRead;
                    int bytesRead = 0;

                    //seek to startbyte.
                    fs.Seek(startByte, SeekOrigin.Begin);

                    //our initial read has to be the smaller of one of these 2.
                    int initialRead = ((buffer.Length - initialPos) < contentLength)
                        ? buffer.Length - initialPos
                        : (int)contentLength; // if (buffer.Length - initialPos) >= cL

                    totalBytesRead = fs.Read(buffer, initialPos, initialRead);

                    //Console.WriteLine(ASCIIEncoding.ASCII.GetString(buffer));

                    //has to be what we had initially plus what we just read.
                    req.WriteResponse(buffer, initialPos + initialRead);
                    Array.Clear(buffer, 0, buffer.Length);

                    //if we still have not read up to content length, keep reading.
                    if (totalBytesRead < contentLength)
                    {
                        int subsequentRead = (buffer.Length < contentLength)
                            ? buffer.Length
                            : (int)contentLength; // if (buffer.Length - initialPos) >= cL

                        //keep track of previous total bytes
                        int prevTotalBytesRead = totalBytesRead;

                        while ((bytesRead = fs.Read(buffer, 0, subsequentRead)) != 0 &&
                               (totalBytesRead += bytesRead) < contentLength)
                        {
                            prevTotalBytesRead = totalBytesRead;

                            req.WriteResponse(buffer, bytesRead);
                            Array.Clear(buffer, 0, buffer.Length);
                        }

                        if (totalBytesRead >= contentLength)
                        {
                            //we subtract the value of totalBytes right before it was more than contentLength,
                            //from content length (contentLength - prevTotalBytesRead)
                            //this gives us the last bit we need to write to achieve the range requested's length.
                            req.WriteResponse(buffer, (int)contentLength - prevTotalBytesRead);
                        }
                    }
                }
                else
                {
                    fs.Read(buffer, initialPos, buffer.Length - initialPos);
                    req.WriteResponse(buffer);

                    while (fs.Read(buffer, 0, buffer.Length) != 0)
                    {
                        req.WriteResponse(buffer);
                        Array.Clear(buffer, 0, buffer.Length);
                    }
                }

                req.CloseResponse();
            }
        }
Example #7
0
 public override void Handler(WebRequest req)
 {
     req.WriteHTMLResponse(String.Format(RESPONSE_TEMPLATE, req.Method, req.RequestTarget, RESPONSE_TEMPLATE.Length + "11404808".Length, "11404808"));
 }