Esempio n. 1
0
        private void RespondWithFile(File422 file, WebRequest req)
        {
            Stream fs = file.OpenReadOnly();

            if (req.Headers.ContainsKey("range"))
            {
                WriteWithRangeHeader(file, fs, req);
                return;
            }

            // Send response code and headers
            string response = "HTTP/1.1 200 OK\r\n" +
                              "Content-Length: " + fs.Length + "\r\n" +
                              "Content-Type: " + file.GetContentType() + "\r\n\r\n";

            byte[] responseBytes = Encoding.ASCII.GetBytes(response);
            req.WriteResponse(responseBytes, 0, responseBytes.Length);

            while (true)
            {
                byte[] buf  = new byte[4096];
                int    read = fs.Read(buf, 0, buf.Length);
                if (read == 0)
                {
                    break;
                }
                req.WriteResponse(buf, 0, read);
            }
            fs.Close();
        }
Esempio n. 2
0
        /// <summary>
        /// Writes the with range header.
        /// </summary>
        private void WriteWithRangeHeader(File422 file, Stream fs, WebRequest req)
        {
            String[] ranges = req.Headers["range"].Trim().Substring("byte= ".Length).Split('-');

            long from;

            Int64.TryParse(ranges[0], out from);
            if (fs.Length <= from)
            {
                string invalid = "HTTP/1.1 416 Requested Range Not Satisfiable\r\n\r\n";
                byte[] invalidResponseBytes = Encoding.ASCII.GetBytes(invalid);
                req.WriteResponse(invalidResponseBytes, 0, invalidResponseBytes.Length);
            }

            long to;

            if (ranges[1] != string.Empty)
            {
                Int64.TryParse(ranges[1], out to);
            }
            else
            {
                to = fs.Length - 1;
            }

            // Send response code and headers
            string response = "HTTP/1.1 206 Partial content\r\n" +
                              "Content-Range: bytes " + from.ToString() + "-" + to.ToString() + "/" + fs.Length.ToString() + "\r\n" +
                              "Content-Length: " + (to + 1 - from).ToString() + "\r\n" +
                              "Content-Type: " + file.GetContentType() + "\r\n\r\n";

            byte[] responseBytes = Encoding.ASCII.GetBytes(response);
            req.WriteResponse(responseBytes, 0, responseBytes.Length);


            fs.Seek(from, SeekOrigin.Begin);
            while (true)
            {
                byte[] buf  = new byte[4096];
                int    read = 0;
                read = fs.Read(buf, 0, buf.Length);
                if (read == 0)
                {
                    break;
                }
                req.WriteResponse(buf, 0, read);
                if (fs.Position >= to)
                {
                    break;
                }
            }
            fs.Close();
        }