示例#1
0
            protected override async Task <HttpResponseMessage> SendAsyncCore(HttpRequestMessage request,
                                                                              CancellationToken cancellationToken)
            {
                ThreadId = Thread.CurrentThread.ManagedThreadId;

                if (Calls == CancelRequestNum && CancellationTokenSource != null)
                {
                    CancellationTokenSource.Cancel();
                }

                // validate the request
                Assert.That(request.RequestUri, Is.EqualTo(DownloadUri));
                Assert.That(request.Headers.Range, Is.EqualTo(new RangeHeaderValue(bytesRead,
                                                                                   bytesRead + ChunkSize - 1)));

                // read the current data from the stream
                var contentLength = StreamContent.Length;

                byte[] buffer      = new byte[ChunkSize];
                int    currentRead = StreamContent.Read(buffer, 0, ChunkSize);
                var    readStream  = new MemoryStream(buffer, 0, currentRead);

                // prepare the response to send back
                var response = new HttpResponseMessage();

                response.Content = new StreamContent(readStream);
                response.Content.Headers.ContentType   = new MediaTypeHeaderValue("image/png");
                response.Content.Headers.ContentLength = currentRead;
                response.Content.Headers.ContentRange  = new ContentRangeHeaderValue(bytesRead,
                                                                                     bytesRead + currentRead - 1, contentLength);

                bytesRead += currentRead;
                return(response);
            }
            protected override Task <HttpResponseMessage> SendAsyncCore(HttpRequestMessage request,
                                                                        CancellationToken cancellationToken)
            {
                TaskCompletionSource <HttpResponseMessage> tcs = new TaskCompletionSource <HttpResponseMessage>();

                if (Calls == CancelRequestNum && CancellationTokenSource != null)
                {
                    CancellationTokenSource.Cancel();
                }

                // validate the request
                Assert.That(request.RequestUri, Is.EqualTo(DownloadUri));
                Assert.That(request.Headers.Range, Is.EqualTo(new RangeHeaderValue(bytesRead,
                                                                                   bytesRead + ChunkSize - 1)));

                // prepare the response to send back
                var response = new HttpResponseMessage();

                response.StatusCode = StatusCode;
                if (StatusCode == HttpStatusCode.OK)
                {
                    // read the current data from the stream
                    var    contentLength = StreamContent.Length;
                    byte[] buffer        = new byte[ChunkSize];
                    int    currentRead   = StreamContent.Read(buffer, 0, ChunkSize);
                    var    readStream    = new MemoryStream(buffer, 0, currentRead);

                    response.Content = new StreamContent(readStream);
                    response.Content.Headers.ContentType   = new MediaTypeHeaderValue("image/png");
                    response.Content.Headers.ContentLength = currentRead;
                    response.Content.Headers.ContentRange  = new ContentRangeHeaderValue(bytesRead,
                                                                                         bytesRead + currentRead - 1, contentLength);

                    bytesRead += currentRead;
                }
                else
                {
                    response.Content = new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes(ErrorResponse)));
                }
                tcs.SetResult(response);
                return(tcs.Task);
            }