Exemplo n.º 1
0
        private void RespondWithFile(File422 file, WebRequest req)
        {
            StringBuilder resp       = new StringBuilder();
            string        ext        = Path.GetExtension(file.Name).ToLower();
            var           fileStream = file.OpenReadOnly();
            int           bytesRead  = -1;

            byte[] buffer        = new byte[8192];
            string contentType   = "text/plain";
            string statusCode    = "200 OK";
            long   rangeBegin    = 0;
            long   rangeEnd      = (fileStream.Length - 1);
            long   contentLength = fileStream.Length - 1;

            if (req.getHeader("range") != null)
            {
                statusCode = "206 Partial Content";

                // need to determine what bytes they want
                string   r          = req.getHeader("range");
                string[] firstSplit = r.Split('=');
                //firstSplit[1] now has the range

                string[] range = firstSplit[1].Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);

                //now range should have two pieces if it is a range
                // one piece if it is requesting from front or back of file
                // determine where the - is to figure out which, this is stored in firstSplit[1] still
                long a = 0;
                long b = 0;
                if (range.Length == 2)
                {
                    long.TryParse(range[0], out a);
                    long.TryParse(range[1], out b);
                    rangeBegin = a;
                    rangeEnd   = b;
                }
                else
                {
                    if (firstSplit[1][0] == '-')
                    {
                        //wants the back of the file
                        long.TryParse(range[0], out a);
                        rangeBegin = (fileStream.Length - 1 - a);
                        rangeEnd   = (fileStream.Length - 1);
                    }
                    else
                    {
                        // wants the front of the file
                        long.TryParse(range[0], out a);
                        rangeBegin = a;
                        rangeEnd   = (fileStream.Length - 1);
                    }
                }
                contentLength = rangeEnd - rangeBegin;
            }
            // determine type of file
            if (ext == ".jpeg" || ext == ".jpg")
            {
                contentType = "image/jpeg";
            }
            else if (ext == ".png")
            {
                contentType = "image/png";
            }
            else if (ext == ".pdf")
            {
                contentType = "application/pdf";
            }
            else if (ext == ".mp4")
            {
                contentType = "video/mp4";
            }
            else if (ext == ".txt")
            {
                contentType = "text/plain";
            }
            else if (ext == ".html")
            {
                contentType = "text/html";
            }
            else if (ext == ".xml")
            {
                contentType = "application/xml";
            }

            if (contentLength == 0)
            {
                contentLength = 1;
            }

            resp.Append("HTTP/1.1 " + statusCode + "\r\nContent-Type:" + contentType + "\r\n" + "Content-Length:" + contentLength.ToString() + "\r\nAccept-Ranges: bytes\r\n");

            if (req.getHeader("range") != null)
            {
                // add on content-range
                resp.Append("Content-Range: bytes " + rangeBegin.ToString() + "-" + rangeEnd.ToString() + "/" + fileStream.Length.ToString() + "\r\n");
            }
            resp.Append("\r\n");

            Console.WriteLine("RESPONSE");
            Console.WriteLine(resp.ToString());

            req.WriteFileResponse(Encoding.ASCII.GetBytes(resp.ToString()));

            if (req.getHeader("range") != null)
            {
                fileStream.Position = rangeBegin;
                while (bytesRead != 0)
                {
                    try{
                        int readAmmount = Convert.ToInt32(Math.Max(buffer.Length, fileStream.Position - rangeEnd));
                        bytesRead = fileStream.Read(buffer, 0, readAmmount);
                        req.WriteFileResponse(buffer);
                    }
                    catch {
                    }
                }
            }
            else
            {
                while (bytesRead != 0)
                {
                    try{
                        bytesRead = fileStream.Read(buffer, 0, buffer.Length);
                        req.WriteFileResponse(buffer);
                    }
                    catch {
                    }
                }
            }

            return;
        }