Beispiel #1
0
 public CopyToAsyncOperation(WebResponseStream parent,
                             Stream destination, int bufferSize,
                             CancellationToken cancellationToken)
     : base(parent, cancellationToken)
 {
     this.destination = destination;
     buffer           = new byte [bufferSize];
 }
Beispiel #2
0
 protected Operation(WebResponseStream parent,
                     CancellationToken cancellationToken)
 {
     cts = CancellationTokenSource.CreateLinkedTokenSource(
         cancellationToken);
     cts.Token.Register(() => parent.OnCanceled());
     tcs = new TaskCompletionSource <T> ();
 }
Beispiel #3
0
 public ReadAsyncOperation(WebResponseStream parent,
                           byte[] buffer, int offset, int count,
                           CancellationToken cancellationToken)
     : base(parent, cancellationToken)
 {
     this.buffer      = buffer;
     this.bufferIndex = offset;
     this.bufferCount = count;
 }
Beispiel #4
0
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (responseStream != null)
         {
             responseStream.Dispose();
         }
         responseStream = null;
     }
 }
Beispiel #5
0
 internal Content(WebResponseStream stream)
     : base(stream)
 {
     this.responseStream = stream;
 }
Beispiel #6
0
        async Task <HttpResponseMessage> ProcessRequest(HttpRequestMessage request,
                                                        CFHTTPMessage message,
                                                        WebRequestStream body,
                                                        bool retryWithCredentials,
                                                        CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            WebResponseStream stream;

            if (body != null)
            {
                stream = WebResponseStream.Create(message, body);
            }
            else
            {
                stream = WebResponseStream.Create(message);
            }
            if (stream == null)
            {
                throw new HttpRequestException(string.Format(
                                                   "Failed to create web request for '{0}'.",
                                                   request.RequestUri)
                                               );
            }

            stream.Stream.ShouldAutoredirect          = AllowAutoRedirect;
            stream.Stream.AttemptPersistentConnection = GetKeepAlive(request);

            var response = await stream.Open(
                WorkerThread, cancellationToken).ConfigureAwait(false);

            var status = (HttpStatusCode)response.ResponseStatusCode;

            if (retryWithCredentials && (body == null) &&
                (status == HttpStatusCode.Unauthorized) ||
                (status == HttpStatusCode.ProxyAuthenticationRequired))
            {
                if (HandleAuthentication(request.RequestUri, message, response))
                {
                    stream.Dispose();
                    return(await ProcessRequest(
                               request, message, null, false, cancellationToken));
                }
            }

            // The Content object takes ownership of the stream, so we don't
            // dispose it here.

            var retval = new HttpResponseMessage();

            retval.StatusCode   = response.ResponseStatusCode;
            retval.ReasonPhrase = GetReasonPhrase(response);
            retval.Version      = response.Version;

            var content = new Content(stream);

            retval.Content = content;

            DecodeHeaders(response, retval, content);
            return(retval);
        }