Example #1
0
        /// <summary>
        /// Main response callback, invoked once we have first Response packet from
        /// server.  This is where we initiate the actual file transfer, reading from
        /// a stream.
        /// </summary>
        public static void RespCallback(IAsyncResult asyncResult)
        {
            try
            {
                WebRequestState reqState    = ((WebRequestState)(asyncResult.AsyncState));
                WebRequest      req         = reqState.request;
                string          statusDescr = "";
                //string contentLength = "";

                // HTTP
                HttpWebResponse resp = ((HttpWebResponse)(req.EndGetResponse(asyncResult)));
                reqState.response   = resp;
                statusDescr         = resp.StatusDescription;
                reqState.totalBytes = reqState.response.ContentLength;
                //contentLength = reqState.response.ContentLength.ToString();   // # bytes

                // Set up a stream, for reading response data into it
                Stream responseStream = reqState.response.GetResponseStream();
                reqState.streamResponse = responseStream;

                // Begin reading contents of the response data
                responseStream.BeginRead(reqState.bufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallback), reqState);

                return;
            }
            catch
            {
            }
        }
Example #2
0
        public IAsyncResult BeginSend(Uri endpoint, IWebProxy proxy, KeyValuePair <string, string>[] headers, byte[] report, AsyncCallback callback, object state)
        {
            var request = (HttpWebRequest)System.Net.WebRequest.Create(endpoint);

#if !NETSTANDARD1_3
            request.KeepAlive = false;
#endif
            request.Method      = "POST";
            request.ContentType = "application/json";
            if (proxy != null)
            {
                request.Proxy = proxy;
            }
            if (headers != null)
            {
                foreach (var header in headers)
                {
                    request.Headers[header.Key] = header.Value;
                }
            }
            request.Headers["Bugsnag-Sent-At"] = DateTime.UtcNow.ToString("o", CultureInfo.InvariantCulture);
            var internalState = new WebRequestState(callback, state, endpoint, report, request);
            var asyncResult   = request.BeginGetRequestStream(new AsyncCallback(WriteCallback), internalState);
            return(new WebRequestAsyncResult(asyncResult, internalState));
        }
        private async Task <HttpActionStatus> GetResponseAsync(Request request, WebRequestState state)
        {
            try
            {
                state.Response = (HttpWebResponse)await state.Request.GetResponseAsync().ConfigureAwait(false);

                state.ResponseStream = state.Response.GetResponseStream();
                return(HttpActionStatus.Success);
            }
            catch (WebException error)
            {
                var status = HandleWebException(request, state, error);

                // HttpWebRequest reacts to response codes like 404 or 500 with an exception with ProtocolError status:
                if (status == HttpActionStatus.ProtocolError)
                {
                    state.Response       = (HttpWebResponse)error.Response;
                    state.ResponseStream = state.Response.GetResponseStream();
                    return(HttpActionStatus.Success);
                }

                return(status);
            }
            catch (Exception error)
            {
                LogUnknownException(error);
                return(HttpActionStatus.UnknownFailure);
            }
        }
Example #4
0
        /// <summary>
        /// Main callback invoked in response to the Stream.BeginRead method, when we have some data.
        /// </summary>
        private static void ReadCallback(IAsyncResult asyncResult)
        {
            try
            {
                // Will be either HttpWebRequestState or FtpWebRequestState
                WebRequestState reqState = ((WebRequestState)(asyncResult.AsyncState));

                Stream responseStream = reqState.streamResponse;

                // Get results of read operation
                int bytesRead = responseStream.EndRead(asyncResult);
                //Console.WriteLine(bytesRead);

                // Got some data, need to read more
                if (bytesRead > 0)
                {
                    // Report some progress, including total # bytes read, % complete, and transfer rate
                    reqState.bytesRead += bytesRead;
                    //double pctComplete = ((double)reqState.bytesRead / (double)reqState.totalBytes) * 100.0f;

                    // Note: bytesRead/totalMS is in bytes/ms.  Convert to kb/sec.
                    //TimeSpan totalTime = DateTime.Now - reqState.transferStart;
                    //double kbPerSec = (reqState.bytesRead * 1000.0f) / (totalTime.TotalMilliseconds * 1024.0f);

                    if (reqState.bufferData == null)
                    {
                        reqState.bufferData = new byte[bytesRead];
                        System.Buffer.BlockCopy(reqState.bufferRead, 0, reqState.bufferData, 0, bytesRead);
                    }
                    else
                    {
                        byte[] rv = new byte[bytesRead + reqState.bufferData.Length];
                        System.Buffer.BlockCopy(reqState.bufferData, 0, rv, 0, reqState.bufferData.Length);
                        System.Buffer.BlockCopy(reqState.bufferRead, 0, rv, reqState.bufferData.Length, bytesRead);

                        reqState.bufferData = rv;
                        rv = null;
                    }

                    //Console.WriteLine(Encoding.ASCII.GetString(reqState.bufferRead));
                    //reqState.progCB(reqState.bytesRead, pctComplete, kbPerSec);

                    // Kick off another read
                    responseStream.BeginRead(reqState.bufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallback), reqState);
                    return;
                }

                // EndRead returned 0, so no more data to be read
                else
                {
                    responseStream.Close();
                    reqState.response.Close();
                    reqState.doneCB();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Example #5
0
        internal virtual WebResponse GetResponse(WebRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            HttpWebRequest request2 = request as HttpWebRequest;
            TimeoutState   state    = null;

            if ((request2 != null) && (request2.Timeout > 0))
            {
                state = new TimeoutState(request2);
            }
            this._webRequest = request;
            WebRequestState state2 = new WebRequestState(request);
            IAsyncResult    result = request.BeginGetResponse(new AsyncCallback(WebRequestPSCmdlet.ResponseCallback), state2);

            if (state != null)
            {
                ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(WebRequestPSCmdlet.TimeoutCallback), state, state.httpRequest.Timeout, true);
            }
            state2.waithandle.WaitOne(-1, false);
            this._webRequest = null;
            if (state2.webException == null)
            {
                return(state2.response);
            }
            if (((state != null) && state.abort) && state2.webException.Status.Equals(WebExceptionStatus.RequestCanceled))
            {
                throw new WebException(WebCmdletStrings.RequestTimeout, WebExceptionStatus.Timeout);
            }
            throw state2.webException;
        }
        private HttpActionStatus HandleWebException(Request request, WebRequestState state, WebException error)
        {
            switch (error.Status)
            {
            case WebExceptionStatus.ConnectFailure:
            case WebExceptionStatus.KeepAliveFailure:
            case WebExceptionStatus.ConnectionClosed:
            case WebExceptionStatus.PipelineFailure:
            case WebExceptionStatus.NameResolutionFailure:
            case WebExceptionStatus.ProxyNameResolutionFailure:
            case WebExceptionStatus.SecureChannelFailure:
                LogConnectionFailure(request, error);
                return(HttpActionStatus.ConnectionFailure);

            case WebExceptionStatus.SendFailure:
                LogWebException(request, error);
                return(HttpActionStatus.SendFailure);

            case WebExceptionStatus.ReceiveFailure:
                LogWebException(request, error);
                return(HttpActionStatus.ReceiveFailure);

            case WebExceptionStatus.RequestCanceled: return(HttpActionStatus.RequestCanceled);

            case WebExceptionStatus.Timeout: return(HttpActionStatus.Timeout);

            case WebExceptionStatus.ProtocolError: return(HttpActionStatus.ProtocolError);

            default:
                LogWebException(request, error);
                return(HttpActionStatus.UnknownFailure);
            }
        }
        private async Task <bool> HandleHttpErrorResponseAsync(WebRequestState state, HttpResponseMessage httpErrorResponse, HttpClientResponseData responseData, CancellationToken cancellationToken)
        {
            HttpStatusCode         statusCode;
            AmazonServiceException errorResponseException = null;

            UnmarshallerContext errorContext = null;

            statusCode = httpErrorResponse.StatusCode;
            state.Metrics.AddProperty(Metric.StatusCode, statusCode);
            string redirectedLocation = responseData.GetHeaderValue("location");

            state.Metrics.AddProperty(Metric.RedirectLocation, redirectedLocation);

            var responseStream = await responseData.ResponseBody.OpenResponseAsync()
                                 .ConfigureAwait(continueOnCapturedContext: false);

            errorContext = state.Unmarshaller.CreateContext(responseData,
                                                            Config.LogResponse || Config.ReadEntireResponse || AWSConfigs.LoggingConfig.LogResponses != ResponseLoggingOption.Never,
                                                            responseStream,
                                                            state.Metrics);
            errorResponseException = state.Unmarshaller.UnmarshallException(errorContext, null, statusCode);
            if (Config.LogResponse || AWSConfigs.LoggingConfig.LogResponses != ResponseLoggingOption.Never)
            {
                this.logger.Error(errorResponseException, "Received error response: [{0}]", errorContext.ResponseBody);
            }
            state.Metrics.AddProperty(Metric.AWSRequestID, errorResponseException.RequestId);
            state.Metrics.AddProperty(Metric.AWSErrorCode, errorResponseException.ErrorCode);

            if (CanRetry(state))
            {
                if (isTemporaryRedirect(statusCode, redirectedLocation))
                {
                    this.logger.DebugFormat("Request {0} is being redirected to {1}.", state.Request.RequestName, redirectedLocation);
                    state.Request.Endpoint = new Uri(redirectedLocation);
                    return(true);
                }
                else if (ShouldRetry(statusCode, this.Config, errorResponseException, state.RetriesAttempt))
                {
                    this.logger.DebugFormat("Retry number {0} for request {1}.", state.RetriesAttempt, state.Request.RequestName);
                    pauseExponentially(state);
                    cancellationToken.ThrowIfCancellationRequested();
                    return(true);
                }
            }

            if (errorResponseException != null)
            {
                this.logger.Error(errorResponseException, "Error making request {0}.", state.Request.RequestName);
                state.Metrics.AddProperty(Metric.Exception, errorResponseException);
                throw errorResponseException;
            }


            AmazonServiceException excep = new AmazonServiceException("Unable to make request", null, statusCode);

            this.logger.Error(excep, "Error making request {0}.", state.Request.RequestName);
            state.Metrics.AddProperty(Metric.Exception, excep);
            throw excep;
        }
        private static bool NeedToReadResponseBody(Request request, WebRequestState state)
        {
            if (request.Method == RequestMethods.Head)
            {
                return(false);
            }

            // ContentLength can be -1 in case server returns content without setting the header. It is a default on HttpWebRequest level.
            return(state.Response.ContentLength != 0);
        }
Example #9
0
        public RssItem[] EndGetRss(IAsyncResult asyncResult)
        {
            CustomAsyncResult <WebRequestState> myAsyncResult = (CustomAsyncResult <WebRequestState>)asyncResult;
            WebRequestState myState = myAsyncResult.AdditionalData;

            var result = RssHelper.ConvertXmlToRss(XElement.Load(new XmlTextReader(CompleteGetUrl(asyncResult))), myState.Count).ToArray();

            WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
            return(result);
        }
Example #10
0
        internal virtual WebResponse GetResponse(WebRequest request)
        {
            if (null == request)
            {
                throw new ArgumentNullException("request");
            }

            // Construct TimeoutState
            HttpWebRequest httpRequest  = request as HttpWebRequest;
            TimeoutState   timeoutState = null;

            if (httpRequest != null && httpRequest.Timeout > 0)
            {
                timeoutState = new TimeoutState(httpRequest);
            }

            // Construct WebRequestState
            _webRequest = request;
            WebRequestState requestState = new WebRequestState(request);

            // Call asynchronous GetResponse
            IAsyncResult asyncResult = (IAsyncResult)request.BeginGetResponse(new AsyncCallback(ResponseCallback), requestState);

            // Set timeout if necessary
            if (timeoutState != null)
            {
                ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), timeoutState, timeoutState.httpRequest.Timeout, true);
            }

            // Wait on signal
            requestState.waithandle.WaitOne(-1, false);
            requestState.waithandle.Close();
            _webRequest = null;

            // The current thread will be waked up in three cases:
            //    1. the EngGetResponse is done. In this case, we EITHER get the response (requestState.response != null),
            //       OR a WebException is raised (requestState.webException != null).
            //    2. the ^C is typed, a PipelineStoppedException is raised. StopProcessing will abort the request. In this
            //       case, there will be a WebException with status 'RequestCancelled'.
            //    3. the time is up. The TimeoutCallback method will abort the request. In this case, there will also be a
            //       WebException with status 'RequestCancelled' and timeoutState.abort will be true.
            if (requestState.webException != null)
            {
                // Case 3. We wrap the exception to be 'Timeout' WebException
                if (timeoutState != null && timeoutState.abort && requestState.webException.Status.Equals(WebExceptionStatus.RequestCanceled))
                {
                    throw new WebException(WebCmdletStrings.RequestTimeout, WebExceptionStatus.Timeout);
                }

                // Case 1 or 2
                throw requestState.webException;
            }

            return(requestState.response);
        }
Example #11
0
 private bool RetryOrThrow(WebRequestState state, Exception exception)
 {
     if (CanRetry(state) && state.RetriesAttempt < Config.MaxErrorRetry)
     {
         return(true);
     }
     else
     {
         throw new AmazonServiceException(exception);
     }
 }
        private bool ResponseBodyIsTooLarge(WebRequestState state)
        {
            var size  = Math.Max(state.Response.ContentLength, state.BodyStream?.Length ?? 0L);
            var limit = Settings.MaxResponseBodySize ?? long.MaxValue;

            if (size > limit)
            {
                LogResponseBodyTooLarge(size, limit);
            }

            return(size > limit);
        }
        private Task <T> InvokeHelper <T>(WebRequestState state, CancellationToken cancellationToken) where T : AmazonWebServiceResponse
        {
            if (state.RetriesAttempt == 0 || Config.ResignRetries)
            {
                SignRequest(state);
            }
            if (state.RetriesAttempt > 0)
            {
                HandleRetry(state);
            }

            return(InvokeConfiguredRequest <T>(state, cancellationToken));
        }
Example #14
0
        private async Task <AmazonWebServiceResponse> HandleHttpContentAsync(WebRequestState state, HttpResponseMessage httpResponse)
        {
            LogResponse(state.Metrics, state.Request, httpResponse.StatusCode);
            AmazonWebServiceResponse response     = null;
            HttpClientResponseData   responseData = new HttpClientResponseData(httpResponse);
            UnmarshallerContext      context      = null;

            try
            {
                var responseStream = await responseData.OpenResponseAsync()
                                     .ConfigureAwait(continueOnCapturedContext: false);

                context = state.Unmarshaller.CreateContext(responseData,
                                                           this.SupportResponseLogging &&
                                                           (Config.LogResponse || Config.ReadEntireResponse || AWSConfigs.ResponseLogging != ResponseLoggingOption.Never),
                                                           responseStream,
                                                           state.Metrics);

                using (state.Metrics.StartEvent(Metric.ResponseUnmarshallTime))
                {
                    response = state.Unmarshaller.Unmarshall(context);
                }
                context.ValidateCRC32IfAvailable();

                var contentHeaders = httpResponse.Content.Headers as HttpContentHeaders;
                if (contentHeaders != null)
                {
                    response.ContentLength  = contentHeaders.ContentLength.GetValueOrDefault();
                    response.HttpStatusCode = httpResponse.StatusCode;
                }

                if (response.ResponseMetadata != null)
                {
                    state.Metrics.AddProperty(Metric.AWSRequestID, response.ResponseMetadata.RequestId);
                }
                LogFinishedResponse(state.Metrics, context, response.ContentLength);

                return(response);
            }
            finally
            {
                if (!state.Unmarshaller.HasStreamingProperty)
                {
                    httpResponse.Dispose();
                }

                ProcessResponseHandlers(response, state.Request, responseData);
            }
        }
Example #15
0
        void HttpGetCallback(IAsyncResult asyncResult)
        {
            WebRequestState myState = (WebRequestState)asyncResult.AsyncState;

            try
            {
                myState.Response = (HttpWebResponse)myState.Request.EndGetResponse(asyncResult);
            }
            catch (WebException e)
            {
                myState.Error    = e;
                myState.Response = (HttpWebResponse)e.Response;
            }
            myState.UserCallback(new CustomAsyncResult <WebRequestState>(asyncResult, myState));
        }
Example #16
0
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        WebRequestState  webRequestState  = ContainerHome.Container.Resolve <WebRequestState>();
        ActionDescriptor actionDescriptor = filterContext.ActionDescriptor;

        if (actionDescriptor.IsDefined(typeof(StaffUserAuthorizeAttribute), true))
        {
            return;
        }
        if (!webRequestState.IsAuthenticated || webRequestState.ClientUser == null)
        {
            filterContext.Result = new HttpUnauthorizedResult();
            return;             // 401, always show log-in page
        }
    }
        /// <inheritdoc />
        public async Task <Response> SendAsync(Request request, TimeSpan?connectionTimeout, TimeSpan timeout, CancellationToken cancellationToken)
        {
            if (timeout.TotalMilliseconds < 1)
            {
                LogRequestTimeout(request, timeout);
                return(new Response(ResponseCode.RequestTimeout));
            }

            var state = new WebRequestState(timeout);

            using (var timeoutCancellation = new CancellationTokenSource())
            {
                var timeoutTask   = Task.Delay(state.TimeRemaining, timeoutCancellation.Token);
                var senderTask    = SendInternalAsync(request, state, connectionTimeout, cancellationToken);
                var completedTask = await Task.WhenAny(timeoutTask, senderTask).ConfigureAwait(false);

                if (completedTask is Task <Response> taskWithResponse)
                {
                    timeoutCancellation.Cancel();
                    return(taskWithResponse.GetAwaiter().GetResult());
                }

                // If the completed task can not be cast to Task<Response>, it means that timeout task has completed.
                state.CancelRequest();
                LogRequestTimeout(request, timeout);

                // Wait a little for canceled sending task completion before returning result:
                var senderTaskContinuation = senderTask.ContinueWith(
                    t =>
                {
                    if (t.IsCompleted)
                    {
                        t.GetAwaiter().GetResult().Dispose();
                    }
                });

                var abortWaitingDelay = Task.Delay(Settings.RequestAbortTimeout);

                await Task.WhenAny(senderTaskContinuation, abortWaitingDelay).ConfigureAwait(false);

                if (!senderTask.IsCompleted)
                {
                    LogFailedToWaitForRequestAbort();
                }

                return(responseFactory.BuildResponse(ResponseCode.RequestTimeout, state));
            }
        }
Example #18
0
        /// <summary>
        /// Call back method for BeginGetResponse
        /// </summary>
        /// <param name="asyncResult"></param>
        private static void ResponseCallback(IAsyncResult asyncResult)
        {
            WebRequestState myRequestState = (WebRequestState)asyncResult.AsyncState;

            try
            {
                myRequestState.response = myRequestState.request.EndGetResponse(asyncResult);
            }
            catch (WebException ex)
            {
                myRequestState.response     = null;
                myRequestState.webException = ex;
            }
            finally
            {
                myRequestState.waithandle.Set();
            }
        }
    private void StartRequest(WebRequest webRequest)
    {
        Debug.Log("TransfluentMethod." + m_type + ": Starting WebRequest");

        m_isResponseCompleted = false;

        m_webRequestState = new WebRequestState();
        m_webRequestState.m_webRequest = webRequest;

        m_webRequestState.m_asyncResult = (IAsyncResult)m_webRequestState.m_webRequest.BeginGetResponse(new AsyncCallback(ResponseCallback), m_webRequestState);

        ThreadPool.RegisterWaitForSingleObject(m_webRequestState.m_asyncResult.AsyncWaitHandle,
                                               new WaitOrTimerCallback(ScanTimeoutCallback),
                                               m_webRequestState,
                                               (m_timeoutThreshold * 1000), // obviously because this is in miliseconds
                                               true
                                               );
    }
        private static async Task SendLargeBufferedBodyAsync(Content content, WebRequestState state)
        {
            using (BufferPool.Default.Rent(Constants.BufferSize, out var buffer))
            {
                var index = content.Offset;
                var end   = content.Offset + content.Length;

                while (index < end)
                {
                    var size = Math.Min(buffer.Length, end - index);

                    Buffer.BlockCopy(content.Buffer, index, buffer, 0, size);

                    await state.RequestStream.WriteAsync(buffer, 0, size).ConfigureAwait(false);

                    index += size;
                }
            }
        }
        private bool NeedToStreamResponseBody(WebRequestState state)
        {
            try
            {
                var contentLength = null as long?;

                if (state.Response.ContentLength >= 0)
                {
                    contentLength = state.Response.ContentLength;
                }

                return(Settings.UseResponseStreaming(contentLength));
            }
            catch (Exception error)
            {
                log.Error(error);
                return(false);
            }
        }
 private bool RetryOrThrow(WebRequestState state, Exception exception)
 {
     if (CanRetry(state) && state.RetriesAttempt < Config.MaxErrorRetry)
     {
         this.logger.DebugFormat("Retry number {0} for request {1}.", state.RetriesAttempt, state.Request.RequestName);
         return(true);
     }
     else
     {
         var webException = exception.InnerException as WebException;
         if (webException != null)
         {
             var errorMessage = string.Format(CultureInfo.InvariantCulture,
                                              "Encountered a WebException ({0}), the request cannot be retried. Either the maximum number of retries has been exceeded ({1}/{2}) or the request is using a non-seekable stream.",
                                              webException.Status, state.RetriesAttempt, Config.MaxErrorRetry);
             throw new AmazonServiceException(errorMessage, exception);
         }
         throw new AmazonServiceException(exception);
     }
 }
    private void ScanTimeoutCallback(object state, bool timedOut)
    {
        if (timedOut)
        {
            WebRequestState requestState = (WebRequestState)state;
            if (requestState != null)
            {
                requestState.m_webRequest.Abort();
            }

            Debug.LogError("TransfluentMethod." + m_type + ": Request timed out");
        }
        else
        {
            RegisteredWaitHandle registeredWaitHandle = (RegisteredWaitHandle)state;
            if (registeredWaitHandle != null)
            {
                registeredWaitHandle.Unregister(null);
            }
        }
    }
Example #24
0
        private IAsyncResult StartGetUrl(string url, int cacheDuration, int count, AsyncCallback wcfCallback, object wcfState)
        {
            /// If the url already exists in cache then there's no need to fetch it from the source.
            /// We can just return the response immediately from cache
            if (!ConstantHelper.DisableCache && Services.Get <ICache>().Contains(url))
            {
                WebRequestState myState = new WebRequestState(wcfCallback, wcfState)
                {
                    Url           = url,
                    CacheDuration = cacheDuration,
                    ContentType   = WebOperationContext.Current.IncomingRequest.ContentType,
                    Count         = count
                };

                // Trigger the completion of the request immediately
                var completedState = new CompletedAsyncResult <WebRequestState>(myState, wcfState);
                wcfCallback(completedState);
                return(completedState);
            }
            else
            {
                /// The content does not exist in cache and we need to get it from the
                /// original source
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.Method = "GET";
                WebRequestState myState = new WebRequestState(wcfCallback, wcfState)
                {
                    Request       = request,
                    ContentType   = WebOperationContext.Current.IncomingRequest.ContentType,
                    Url           = url,
                    CacheDuration = cacheDuration,
                    Count         = count
                };
                IAsyncResult asyncResult = request.BeginGetResponse(new AsyncCallback(HttpGetCallback), myState);
                return(new CustomAsyncResult <WebRequestState>(asyncResult, myState));
            }
        }
Example #25
0
        private IAsyncResult StartGetUrl(string url, int cacheDuration, int count, AsyncCallback wcfCallback, object wcfState)
        {
            /// If the url already exists in cache then there's no need to fetch it from the source.
            /// We can just return the response immediately from cache
            if (!ConstantHelper.DisableCache && Services.Get<ICache>().Contains(url))
            {
                WebRequestState myState = new WebRequestState(wcfCallback, wcfState)
                {
                    Url = url,
                    CacheDuration = cacheDuration,
                    ContentType = WebOperationContext.Current.IncomingRequest.ContentType,
                    Count = count
                };

                // Trigger the completion of the request immediately 
                var completedState = new CompletedAsyncResult<WebRequestState>(myState, wcfState);
                wcfCallback(completedState);
                return completedState;
            }
            else
            {
                /// The content does not exist in cache and we need to get it from the
                /// original source 
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.Method = "GET";
                WebRequestState myState = new WebRequestState(wcfCallback, wcfState)
                {
                    Request = request,
                    ContentType = WebOperationContext.Current.IncomingRequest.ContentType,
                    Url = url,
                    CacheDuration = cacheDuration,
                    Count = count
                };
                IAsyncResult asyncResult = request.BeginGetResponse(new AsyncCallback(HttpGetCallback), myState);
                return new CustomAsyncResult<WebRequestState>(asyncResult, myState);
            }
        }
        protected Task <Res> Invoke <T, Req, Res>(Req request, IMarshaller <T, Req> marshaller, ResponseUnmarshaller unmarshaller, AbstractAWSSigner signer, CancellationToken cancellationToken = default(CancellationToken))
            where T : IRequest
            where Req : AmazonWebServiceRequest
            where Res : AmazonWebServiceResponse
        {
            ProcessPreRequestHandlers(request);

            IRequest irequest = marshaller.Marshall(request);

            WebRequestState state = new WebRequestState(irequest, unmarshaller, signer);

            state.Metrics.StartEvent(Metric.ClientExecuteTime);

            irequest.Endpoint = DetermineEndpoint(irequest);
            if (Config.LogMetrics)
            {
                state.Metrics.IsEnabled = true;
                state.Metrics.AddProperty(Metric.ServiceName, irequest.ServiceName);
                state.Metrics.AddProperty(Metric.ServiceEndpoint, irequest.Endpoint);
                state.Metrics.AddProperty(Metric.MethodName, irequest.RequestName);
            }
            ConfigureRequest(state);
            return(InvokeHelper <Res>(state, cancellationToken));
        }
        private async Task <HttpActionStatus> ReadResponseBodyAsync(Request request, WebRequestState state, CancellationToken cancellationToken)
        {
            try
            {
                var contentLength = (int)state.Response.ContentLength;
                if (contentLength > 0)
                {
                    state.BodyBuffer = Settings.BufferFactory(contentLength);

                    var totalBytesRead = 0;

                    // If a contentLength-sized buffer won't end up in LOH, it can be used directly to work with socket.
                    // Otherwise, it's better to use a small temporary buffer from a pool because the passed reference will be stored for a long time due to Keep-Alive.
                    if (contentLength < Constants.LOHObjectSizeThreshold)
                    {
                        while (totalBytesRead < contentLength)
                        {
                            var bytesToRead = Math.Min(contentLength - totalBytesRead, Constants.BufferSize);
                            var bytesRead   = await state.ResponseStream.ReadAsync(state.BodyBuffer, totalBytesRead, bytesToRead, cancellationToken)
                                              .ConfigureAwait(false);

                            if (bytesRead == 0)
                            {
                                break;
                            }

                            totalBytesRead += bytesRead;
                        }
                    }
                    else
                    {
                        using (BufferPool.Default.Rent(Constants.BufferSize, out var buffer))
                        {
                            while (totalBytesRead < contentLength)
                            {
                                var bytesToRead = Math.Min(contentLength - totalBytesRead, buffer.Length);
                                var bytesRead   = await state.ResponseStream.ReadAsync(buffer, 0, bytesToRead).ConfigureAwait(false);

                                if (bytesRead == 0)
                                {
                                    break;
                                }

                                Buffer.BlockCopy(buffer, 0, state.BodyBuffer, totalBytesRead, bytesRead);

                                totalBytesRead += bytesRead;
                            }
                        }
                    }

                    if (totalBytesRead < contentLength)
                    {
                        throw new EndOfStreamException($"Response stream ended prematurely. Read only {totalBytesRead} byte(s), but Content-Length specified {contentLength}.");
                    }

                    state.BodyBufferLength = totalBytesRead;
                }
                else
                {
                    state.BodyStream = new MemoryStream();

                    using (BufferPool.Default.Rent(Constants.BufferSize, out var buffer))
                    {
                        while (true)
                        {
                            var bytesRead = await state.ResponseStream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);

                            if (bytesRead == 0)
                            {
                                break;
                            }

                            state.BodyStream.Write(buffer, 0, bytesRead);

                            if (ResponseBodyIsTooLarge(state))
                            {
                                state.CancelRequestAttempt();
                                state.BodyStream = null;
                                return(HttpActionStatus.InsufficientStorage);
                            }
                        }
                    }
                }

                return(HttpActionStatus.Success);
            }
            catch (Exception error)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(HttpActionStatus.RequestCanceled);
                }

                LogReceiveBodyFailure(request, error);
                return(HttpActionStatus.ReceiveFailure);
            }
        }
Example #28
0
 internal WebRequestEventArgs(WebRequestState state)
 {
     State = state;
 }
Example #29
0
        private void GetResponseCallback(object aResult)
        {
            WebRequestState wreq = aResult as WebRequestState;
            Artwork artwork = wreq.Artwork;

            try
            {
                HttpWebResponse wresp = (HttpWebResponse)wreq.Request.GetResponse();
                try
                {
                    Stream stream = wresp.GetResponseStream();
                    if (stream != null)
                    {
                        try
                        {
                            Bitmap image = new Bitmap(stream);

                            try
                            {
                                Image scaledImage = ScaleImage(image);
                                Image reflectedImage = ReflectImage(scaledImage);

                                artwork.Image = scaledImage;
                                artwork.ImageReflected = reflectedImage;
                                artwork.Error = false;
                            }
                            catch (Exception e)
                            {
                                DownloadError(artwork, e.Message);
                                return;
                            }

                            image.Dispose();

                            if (EventUpdated != null)
                            {
                                EventUpdated(this, new EventArgsArtwork(artwork));
                            }

                            RequestCompleted(artwork);

                            return;
                        }
                        catch (Exception e)
                        {
                            DownloadError(artwork, e.Message);
                            return;
                        }
                        finally
                        {
                            stream.Close();
                        }
                    }
                }
                catch (Exception e)
                {
                    DownloadError(artwork, e.Message);
                    return;
                }
                finally
                {
                    if (wresp != null)
                    {
                        wresp.Close();
                    }
                }
            }
            catch (WebException e)
            {
                DownloadError(artwork, e.Message);
                return;
            }

            DownloadError(artwork, "Unknown error");
        }
Example #30
0
 internal virtual WebResponse GetResponse(WebRequest request)
 {
     if (request == null)
     {
         throw new ArgumentNullException("request");
     }
     HttpWebRequest request2 = request as HttpWebRequest;
     TimeoutState state = null;
     if ((request2 != null) && (request2.Timeout > 0))
     {
         state = new TimeoutState(request2);
     }
     this._webRequest = request;
     WebRequestState state2 = new WebRequestState(request);
     IAsyncResult result = request.BeginGetResponse(new AsyncCallback(WebRequestPSCmdlet.ResponseCallback), state2);
     if (state != null)
     {
         ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(WebRequestPSCmdlet.TimeoutCallback), state, state.httpRequest.Timeout, true);
     }
     state2.waithandle.WaitOne(-1, false);
     this._webRequest = null;
     if (state2.webException == null)
     {
         return state2.response;
     }
     if (((state != null) && state.abort) && state2.webException.Status.Equals(WebExceptionStatus.RequestCanceled))
     {
         throw new WebException(WebCmdletStrings.RequestTimeout, WebExceptionStatus.Timeout);
     }
     throw state2.webException;
 }
Example #31
0
        /// <summary>
        /// If a stream download is in progress using HttpWebRequest, then this method will flag 
        /// that request to discard its download stream. After this call, no more data is 
        /// pushed into TSDemux or audio/video buffers. Also no new stream download is started 
        /// after this method is called and until ResumeDownloads is called.
        /// </summary>
        public void AbortStreamDownloads()
        {
            lock (_requestLock)
            {
                _downloadAbortInProgress = true;

                if (_currentWebRequestState != null)
                {
                    _currentWebRequestState.DiscardData = true;
                    _currentWebRequestState.WebRequest.Abort();
                    _currentWebRequestState = null;
                }

                if (null != _asyncResult)
                {
                    _asyncResult.Abort();
                    _asyncResult = null;
                }
            }
        }
Example #32
0
        /// <summary>
        /// Internal worker
        /// </summary>
        private void BeginLoadingNextStreamWorker()
        {
            _asyncResult.Purpose = HLSCallbackPurpose.WaitForStream;

            Uri uri = CurrentStream.Uri;
            HLSTrace.WriteLine("Downloading {0}", uri.ToString());

            _currentMediaSequenceNumber = CurrentStream.SequenceNumber;

            lock (_requestLock)
            {
                //Debug.Assert(_asyncResult != null, "_asyncResult cannot be null");

                if (_downloadAbortInProgress)
                {
                    HLSTrace.WriteLine("Aborting downloading of URI {0}", uri.ToString());
                    return;
                }

                HttpWebRequest request = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(uri);

                request.AllowReadStreamBuffering = false;
                //request.CookieContainer = _playlist.CookieContainer;

                // uses xbox sliveright specific http header to disable http cache in xbox,
                // for video playback, unlikely users will  watch the same clip again, also cached content
                // make bandwiddth measurement unusable.
                request.Headers["x-ms-bypassclientsidecache"] = "1";

                _currentWebRequestState = new WebRequestState();
                _currentWebRequestState.WebRequest = request;
                _currentWebRequestState.DiscardData = false;
                _currentWebRequestState.AsyncResult = _asyncResult;
                _currentWebRequestState.StartTime = DateTime.Now;

                request.BeginGetResponse(new AsyncCallback(DataStreamResponseReceived), _currentWebRequestState);
            }
        }
        internal virtual WebResponse GetResponse(WebRequest request)
        {
            if (null == request) { throw new ArgumentNullException("request"); }

            // Construct TimeoutState
            HttpWebRequest httpRequest = request as HttpWebRequest;
            TimeoutState timeoutState = null;
            if (httpRequest != null && httpRequest.Timeout > 0)
            {
                timeoutState = new TimeoutState(httpRequest);
            }

            // Construct WebRequestState
            _webRequest = request;
            WebRequestState requestState = new WebRequestState(request);

            // Call asynchronous GetResponse
            IAsyncResult asyncResult = (IAsyncResult)request.BeginGetResponse(new AsyncCallback(ResponseCallback), requestState);
            // Set timeout if necessary
            if (timeoutState != null)
            {
                ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), timeoutState, timeoutState.httpRequest.Timeout, true);
            }

            // Wait on signal
            requestState.waithandle.WaitOne(-1, false);
            requestState.waithandle.Close();
            _webRequest = null;

            // The current thread will be waked up in three cases:
            //    1. the EngGetResponse is done. In this case, we EITHER get the response (requestState.response != null),
            //       OR a WebException is raised (requestState.webException != null).
            //    2. the ^C is typed, a PipelineStoppedException is raised. StopProcessing will abort the request. In this
            //       case, there will be a WebException with status 'RequestCancelled'.
            //    3. the time is up. The TimeoutCallback method will abort the request. In this case, there will also be a
            //       WebException with status 'RequestCancelled' and timeoutState.abort will be true.
            if (requestState.webException != null)
            {
                // Case 3. We wrap the exception to be 'Timeout' WebException
                if (timeoutState != null && timeoutState.abort && requestState.webException.Status.Equals(WebExceptionStatus.RequestCanceled))
                {
                    throw new WebException(WebCmdletStrings.RequestTimeout, WebExceptionStatus.Timeout);
                }

                // Case 1 or 2
                throw requestState.webException;
            }

            return (requestState.response);
        }
 private async Task SendBodyFromContentProducerAsync(IContentProducer contentProducer, WebRequestState state, CancellationToken cancellationToken)
 {
     var wrapper = new BufferingStreamWrapper(state.RequestStream);
     await contentProducer.ProduceAsync(wrapper, cancellationToken).ConfigureAwait(false);
 }
    private void StartRequest(WebRequest webRequest)
    {
        Debug.Log("TransfluentMethod." + m_type + ": Starting WebRequest");

        m_isResponseCompleted = false;

        m_webRequestState = new WebRequestState();
        m_webRequestState.m_webRequest = webRequest;

        m_webRequestState.m_asyncResult = (IAsyncResult)m_webRequestState.m_webRequest.BeginGetResponse(new AsyncCallback(ResponseCallback), m_webRequestState);

        ThreadPool.RegisterWaitForSingleObject(m_webRequestState.m_asyncResult.AsyncWaitHandle,
            new WaitOrTimerCallback(ScanTimeoutCallback),
            m_webRequestState,
            (m_timeoutThreshold * 1000), // obviously because this is in miliseconds
            true
            );
    }
        private async Task <T> InvokeConfiguredRequest <T>(WebRequestState state, CancellationToken cancellationToken) where T : AmazonWebServiceResponse
        {
            int  currentRetries   = state.RetriesAttempt;
            T    response         = null;
            bool shouldRetry      = false;
            bool responseReceived = false;
            HttpResponseMessage    responseMessage = null;
            HttpClientResponseData responseData    = null;
            var requestMessage = ConfigureRequestMessage(state);

            try
            {
                try
                {
                    SetContent(requestMessage, state);

                    using (state.Metrics.StartEvent(Metric.HttpRequestTime))
                    {
                        responseMessage = await httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, cancellationToken)
                                          .ConfigureAwait(continueOnCapturedContext: false);

                        responseReceived = true;
                    }

                    responseData = new HttpClientResponseData(responseMessage);
                    if (!IsErrorResponse(responseMessage) ||
                        responseMessage.StatusCode == HttpStatusCode.NotFound && state.Request.Suppress404Exceptions)
                    {
                        using (state.Metrics.StartEvent(Metric.ResponseProcessingTime))
                        {
                            response = (T) await HandleHttpContentAsync(state, responseMessage, responseData)
                                       .ConfigureAwait(continueOnCapturedContext: false);
                        }
                    }
                    else
                    {
                        bool retry = await HandleHttpErrorResponseAsync(state, responseMessage, responseData, cancellationToken)
                                     .ConfigureAwait(continueOnCapturedContext: false);

                        if (retry)
                        {
                            shouldRetry = true;
                        }
                    }
                }
                catch (HttpRequestException e)
                {
                    var we = e.InnerException as WebException;

                    if (we != null)
                    {
                        if (WebExceptionStatusesToThrowOn.Contains(we.Status))
                        {
                            throw new AmazonServiceException("Encountered a non retryable WebException : " + we.Status, we);
                        }

                        // The WinRT framework doesn't break down errors, not all status values are available for WinRT.
                        if (
#if WIN_RT
                            (we.Status == WebExceptionStatus.UnknownError || WebExceptionStatusesToRetryOn.Contains(we.Status))
#else
                            WebExceptionStatusesToRetryOn.Contains(we.Status)
#endif
                            )
                        {
                            shouldRetry = RetryOrThrow(state, e);
                        }
                    }

                    var ioe = e.InnerException as IOException;
                    if (ioe != null)
                    {
#if !WIN_RT
                        if (IsInnerExceptionThreadAbort(ioe))
                        {
                            throw new AmazonServiceException(e);
                        }
#endif
                        shouldRetry = RetryOrThrow(state, e);
                    }

                    // Check if response is null at the end as
                    // it can be null for both WebException and IOException.
                    if (!shouldRetry && response == null)
                    {
                        shouldRetry = RetryOrThrow(state, e);
                    }

                    // If shouldRetry is not set by any of the above checks,
                    // re-throw the exception.
                    if (!shouldRetry)
                    {
                        throw new AmazonServiceException(e);
                    }
                }
                catch (TaskCanceledException taskCancelledException)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        throw;
                    }
                    else
                    {
                        // It's a timeout exception.
                        throw new AmazonServiceException(taskCancelledException);
                    }
                }
                catch (OperationCanceledException operationCancelledException)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        // Throw an exception with the original cancellation token.
                        throw new OperationCanceledException(operationCancelledException.Message, cancellationToken);
                    }
                    else
                    {
                        // It's a timeout exception.
                        throw new AmazonServiceException(operationCancelledException);
                    }
                }
                finally
                {
                    if (responseMessage != null && (response == null || !state.Unmarshaller.HasStreamingProperty))
                    {
                        responseMessage.Dispose();
                        responseMessage = null;
                    }
                }

                if (shouldRetry)
                {
                    pauseExponentially(state);
                    cancellationToken.ThrowIfCancellationRequested();
                    state.RetriesAttempt++;
                    var retryResponse = await InvokeHelper <T>(state, cancellationToken)
                                        .ConfigureAwait(continueOnCapturedContext: false);

                    return((T)retryResponse);
                }
                else
                {
                    LogFinalMetrics(state.Metrics);
                }
            }
            catch (Exception e)
            {
                // On errors that are passed to the client, invoke exception handlers
                if (!shouldRetry)
                {
                    ProcessExceptionHandlers(e, state.Request);
                }
                throw;
            }
            finally
            {
                // ProcessResponseHandlers is called only if a response was received from the server (success or error).
                // It's not called in case of client errors like timeout or proxy error.
                if (!shouldRetry && responseReceived)
                {
                    ProcessResponseHandlers(response, state.Request, responseData);
                }
            }

            return(response);
        }