private int ComputeContentLength() { int contentLength = 0; for (int i = 0; i < StartRangeBytes.Length; i++) { contentLength += Convert.ToInt32(EndRangeBytes[i] - StartRangeBytes[i]) + 1; if (IsMultipartRequest) { contentLength += MultipartBoundary.Length + ResourceMimeType.Length + StartRangeBytes[i].ToString().Length + EndRangeBytes[i].ToString().Length + ResourceLength.ToString().Length + 49; // Length needed for multipart header } } if (IsMultipartRequest) { contentLength += MultipartBoundary.Length + 8; // Length of dash and line break } return(contentLength); }
/// <summary> /// Send the requested resource to the HTTP response stream. /// </summary> private void ReturnChunkedResponse() { HttpResponse response = _context.Response; byte[] buffer = new byte[BufferSize]; using (Stream fs = GetResourceStream()) { for (int i = 0; i < StartRangeBytes.Length; i++) { // Position the stream at the starting byte fs.Seek(StartRangeBytes[i], SeekOrigin.Begin); long bytesToReadRemaining = EndRangeBytes[i] - StartRangeBytes[i] + 1; // Output multipart boundary, if needed if (IsMultipartRequest) { response.Output.Write("--" + MultipartBoundary); response.Output.Write(String.Format("{0}: {1}", HttpHeaderContentType, ResourceMimeType)); response.Output.Write(String.Format("{0}: bytes {1}-{2}/{3}", HttpHeaderContentRange, StartRangeBytes[i].ToString(), EndRangeBytes[i].ToString(), ResourceLength.ToString() ) ); response.Output.WriteLine(); } // Stream out the requested chunks for the current range request while (bytesToReadRemaining > 0) { if (response.IsClientConnected) { int chunkSize = fs.Read(buffer, 0, BufferSize < bytesToReadRemaining ? BufferSize : Convert.ToInt32(bytesToReadRemaining)); response.OutputStream.Write(buffer, 0, chunkSize); bytesToReadRemaining -= chunkSize; response.Flush(); #if DEBUG //Thread.Sleep(DebuggingSleepTime); #endif } else { // Client disconnected - quit return; } } } } }