UpdateCacheTime() private method

private UpdateCacheTime ( HttpJsonRequest httpJsonRequest ) : void
httpJsonRequest HttpJsonRequest
return void
Exemplo n.º 1
0
        private async Task <RavenJToken> CheckForErrorsAndReturnCachedResultIfAnyAsync(bool readErrorString)
        {
            if (Response.IsSuccessStatusCode)
            {
                return(null);
            }
            if (Response.StatusCode == HttpStatusCode.Unauthorized ||
                Response.StatusCode == HttpStatusCode.NotFound ||
                Response.StatusCode == HttpStatusCode.Conflict)
            {
                factory.InvokeLogRequest(owner, () => new RequestResultArgs
                {
                    DurationMilliseconds = CalculateDuration(),
                    Method     = Method,
                    HttpResult = (int)Response.StatusCode,
                    Status     = RequestStatus.ErrorOnServer,
                    Result     = Response.StatusCode.ToString(),
                    Url        = Url,
                    PostedData = postedData
                });

                throw ErrorResponseException.FromResponseMessage(Response, readErrorString);
            }

            if (Response.StatusCode == HttpStatusCode.NotModified &&
                CachedRequestDetails != null)
            {
                factory.UpdateCacheTime(this);
                var result = factory.GetCachedResponse(this, ResponseHeaders);

                // here we explicitly need to get Response.Headers, and NOT ResponseHeaders because we are
                // getting the value _right now_ from the secondary, and don't care about the 304, the force check
                // is still valid
                HandleReplicationStatusChanges(ResponseHeaders, primaryUrl, operationUrl);

                factory.InvokeLogRequest(owner, () => new RequestResultArgs
                {
                    DurationMilliseconds = CalculateDuration(),
                    Method     = Method,
                    HttpResult = (int)Response.StatusCode,
                    Status     = RequestStatus.Cached,
                    Result     = result.ToString(),
                    Url        = Url,
                    PostedData = postedData
                });

                return(result);
            }


            using (var sr = new StreamReader(await Response.GetResponseStreamWithHttpDecompression().ConfigureAwait(false)))
            {
                var readToEnd = sr.ReadToEnd();

                factory.InvokeLogRequest(owner, () => new RequestResultArgs
                {
                    DurationMilliseconds = CalculateDuration(),
                    Method     = Method,
                    HttpResult = (int)Response.StatusCode,
                    Status     = RequestStatus.Cached,
                    Result     = readToEnd,
                    Url        = Url,
                    PostedData = postedData
                });

                if (string.IsNullOrWhiteSpace(readToEnd))
                {
                    throw ErrorResponseException.FromResponseMessage(Response);
                }

                RavenJObject ravenJObject;
                try
                {
                    ravenJObject = RavenJObject.Parse(readToEnd);
                }
                catch (Exception e)
                {
                    throw new ErrorResponseException(Response, readToEnd, e);
                }
                if (ravenJObject.ContainsKey("IndexDefinitionProperty"))
                {
                    throw new IndexCompilationException(ravenJObject.Value <string>("Message"))
                          {
                              IndexDefinitionProperty = ravenJObject.Value <string>("IndexDefinitionProperty"),
                              ProblematicText         = ravenJObject.Value <string>("ProblematicText")
                          };
                }
                if (Response.StatusCode == HttpStatusCode.BadRequest && ravenJObject.ContainsKey("Message"))
                {
                    throw new BadRequestException(ravenJObject.Value <string>("Message"), ErrorResponseException.FromResponseMessage(Response));
                }
                if (ravenJObject.ContainsKey("Error"))
                {
                    var sb = new StringBuilder();
                    foreach (var prop in ravenJObject)
                    {
                        if (prop.Key == "Error")
                        {
                            continue;
                        }

                        sb.Append(prop.Key).Append(": ").AppendLine(prop.Value.ToString(Formatting.Indented));
                    }

                    if (sb.Length > 0)
                    {
                        sb.AppendLine();
                    }
                    sb.Append(ravenJObject.Value <string>("Error"));

                    throw new ErrorResponseException(Response, sb.ToString(), readToEnd);
                }
                throw new ErrorResponseException(Response, readToEnd);
            }
        }
Exemplo n.º 2
0
        private RavenJToken HandleErrors(WebException e)
        {
            var httpWebResponse = e.Response as HttpWebResponse;

            if (httpWebResponse == null ||
                httpWebResponse.StatusCode == HttpStatusCode.Unauthorized ||
                httpWebResponse.StatusCode == HttpStatusCode.NotFound ||
                httpWebResponse.StatusCode == HttpStatusCode.Conflict)
            {
                int httpResult = -1;
                if (httpWebResponse != null)
                {
                    httpResult = (int)httpWebResponse.StatusCode;
                }

                factory.InvokeLogRequest(owner, () => new RequestResultArgs
                {
                    DurationMilliseconds = CalculateDuration(),
                    Method     = webRequest.Method,
                    HttpResult = httpResult,
                    Status     = RequestStatus.ErrorOnServer,
                    Result     = e.Message,
                    Url        = webRequest.RequestUri.PathAndQuery,
                    PostedData = postedData
                });

                return(null);               //throws
            }

            if (httpWebResponse.StatusCode == HttpStatusCode.NotModified &&
                CachedRequestDetails != null)
            {
                factory.UpdateCacheTime(this);
                var result = factory.GetCachedResponse(this, httpWebResponse.Headers);

                HandleReplicationStatusChanges(httpWebResponse.Headers, primaryUrl, operationUrl);

                factory.InvokeLogRequest(owner, () => new RequestResultArgs
                {
                    DurationMilliseconds = CalculateDuration(),
                    Method     = webRequest.Method,
                    HttpResult = (int)httpWebResponse.StatusCode,
                    Status     = RequestStatus.Cached,
                    Result     = result.ToString(),
                    Url        = webRequest.RequestUri.PathAndQuery,
                    PostedData = postedData
                });

                return(result);
            }

            using (var sr = new StreamReader(e.Response.GetResponseStreamWithHttpDecompression()))
            {
                var readToEnd = sr.ReadToEnd();

                factory.InvokeLogRequest(owner, () => new RequestResultArgs
                {
                    DurationMilliseconds = CalculateDuration(),
                    Method     = webRequest.Method,
                    HttpResult = (int)httpWebResponse.StatusCode,
                    Status     = RequestStatus.Cached,
                    Result     = readToEnd,
                    Url        = webRequest.RequestUri.PathAndQuery,
                    PostedData = postedData
                });

                if (string.IsNullOrWhiteSpace(readToEnd))
                {
                    return(null);                   // throws
                }
                RavenJObject ravenJObject;
                try
                {
                    ravenJObject = RavenJObject.Parse(readToEnd);
                }
                catch (Exception)
                {
                    throw new InvalidOperationException(readToEnd, e);
                }

                if (ravenJObject.ContainsKey("Error"))
                {
                    var sb = new StringBuilder();
                    foreach (var prop in ravenJObject)
                    {
                        if (prop.Key == "Error")
                        {
                            continue;
                        }

                        sb.Append(prop.Key).Append(": ").AppendLine(prop.Value.ToString(Formatting.Indented));
                    }

                    sb.AppendLine()
                    .AppendLine(ravenJObject.Value <string>("Error"));

                    throw new InvalidOperationException(sb.ToString(), e);
                }
                throw new InvalidOperationException(readToEnd, e);
            }
        }
Exemplo n.º 3
0
        private string ReadStringInternal(Func <WebResponse> getResponse)
        {
            WebResponse response;

            try
            {
                response = getResponse();
                sp.Stop();
            }
            catch (WebException e)
            {
                sp.Stop();
                var httpWebResponse = e.Response as HttpWebResponse;
                if (httpWebResponse == null ||
                    httpWebResponse.StatusCode == HttpStatusCode.Unauthorized ||
                    httpWebResponse.StatusCode == HttpStatusCode.NotFound ||
                    httpWebResponse.StatusCode == HttpStatusCode.Conflict)
                {
                    int httpResult = -1;
                    if (httpWebResponse != null)
                    {
                        httpResult = (int)httpWebResponse.StatusCode;
                    }

                    factory.InvokeLogRequest(owner, new RequestResultArgs
                    {
                        DurationMilliseconds = CalculateDuration(),
                        Method     = webRequest.Method,
                        HttpResult = httpResult,
                        Status     = RequestStatus.ErrorOnServer,
                        Result     = e.Message,
                        Url        = webRequest.RequestUri.PathAndQuery,
                        PostedData = postedData
                    });
                    throw;
                }

                if (httpWebResponse.StatusCode == HttpStatusCode.NotModified &&
                    CachedRequestDetails != null)
                {
                    factory.UpdateCacheTime(this);
                    var result = factory.GetCachedResponse(this);

                    factory.InvokeLogRequest(owner, new RequestResultArgs
                    {
                        DurationMilliseconds = CalculateDuration(),
                        Method     = webRequest.Method,
                        HttpResult = (int)httpWebResponse.StatusCode,
                        Status     = RequestStatus.Cached,
                        Result     = result,
                        Url        = webRequest.RequestUri.PathAndQuery,
                        PostedData = postedData
                    });

                    return(result);
                }

                using (var sr = new StreamReader(e.Response.GetResponseStreamWithHttpDecompression()))
                {
                    var readToEnd = sr.ReadToEnd();

                    factory.InvokeLogRequest(owner, new RequestResultArgs
                    {
                        DurationMilliseconds = CalculateDuration(),
                        Method     = webRequest.Method,
                        HttpResult = (int)httpWebResponse.StatusCode,
                        Status     = RequestStatus.Cached,
                        Result     = readToEnd,
                        Url        = webRequest.RequestUri.PathAndQuery,
                        PostedData = postedData
                    });

                    RavenJObject ravenJObject;
                    try
                    {
                        ravenJObject = RavenJObject.Parse(readToEnd);
                    }
                    catch (Exception)
                    {
                        throw new InvalidOperationException(readToEnd, e);
                    }

                    if (ravenJObject.ContainsKey("Error"))
                    {
                        var sb = new StringBuilder();
                        foreach (var prop in ravenJObject)
                        {
                            if (prop.Key == "Error")
                            {
                                continue;
                            }

                            sb.Append(prop.Key).Append(": ").AppendLine(prop.Value.ToString(Formatting.Indented));
                        }

                        sb.AppendLine()
                        .AppendLine(ravenJObject.Value <string>("Error"));

                        throw new InvalidOperationException(sb.ToString());
                    }
                    throw new InvalidOperationException(readToEnd, e);
                }
            }

            ResponseHeaders    = response.Headers;
            ResponseStatusCode = ((HttpWebResponse)response).StatusCode;
            using (var responseStream = response.GetResponseStreamWithHttpDecompression())
            {
                var reader = new StreamReader(responseStream);
                var text   = reader.ReadToEnd();
                reader.Close();

                if (Method == "GET" && ShouldCacheRequest)
                {
                    factory.CacheResponse(Url, text, ResponseHeaders);
                }

                factory.InvokeLogRequest(owner, new RequestResultArgs
                {
                    DurationMilliseconds = CalculateDuration(),
                    Method     = webRequest.Method,
                    HttpResult = (int)ResponseStatusCode,
                    Status     = RequestStatus.SentToServer,
                    Result     = text,
                    Url        = webRequest.RequestUri.PathAndQuery,
                    PostedData = postedData
                });

                return(text);
            }
        }