CacheResponse() private method

private CacheResponse ( string url, RavenJToken data, NameValueCollection headers ) : void
url string
data RavenJToken
headers System.Collections.Specialized.NameValueCollection
return void
コード例 #1
0
        public GetResponse[] HandleCachingResponse(GetResponse[] responses, HttpJsonRequestFactory jsonRequestFactory)
        {
            var hasCachedRequests = false;
            var requestStatuses   = new RequestStatus[responses.Length];

            for (int i = 0; i < responses.Length; i++)
            {
                if (responses[i] == null || responses[i].Status == 304)
                {
                    hasCachedRequests = true;

                    requestStatuses[i] = responses[i] == null ? RequestStatus.AggressivelyCached : RequestStatus.Cached;
                    responses[i]       = responses[i] ?? new GetResponse {
                        Status = 0
                    };

                    foreach (string header in cachedData[i].Headers)
                    {
                        responses[i].Headers[header] = cachedData[i].Headers[header];
                    }
                    responses[i].Result = cachedData[i].Data.CloneToken();
                    jsonRequestFactory.IncrementCachedRequests();
                }
                else
                {
                    requestStatuses[i] = responses[i].RequestHasErrors() ? RequestStatus.ErrorOnServer : RequestStatus.SentToServer;

                    var nameValueCollection = new NameValueCollection();
                    foreach (var header in responses[i].Headers)
                    {
                        nameValueCollection[header.Key] = header.Value;
                    }
                    jsonRequestFactory.CacheResponse(url + requests[i].UrlAndQuery, responses[i].Result, nameValueCollection);
                }
            }

            if (hasCachedRequests == false || convention.DisableProfiling ||
                holdProfilingInformation.ProfilingInformation.Requests.Count == 0)
            {
                return(responses);
            }

            var lastRequest = holdProfilingInformation.ProfilingInformation.Requests.Last();

            for (int i = 0; i < requestStatuses.Length; i++)
            {
                lastRequest.AdditionalInformation["NestedRequestStatus-" + i] = requestStatuses[i].ToString();
            }
            lastRequest.Result = JsonConvert.SerializeObject(responses);

            return(responses);
        }
コード例 #2
0
        private string ReadStringInternal(Func <WebResponse> getResponse)
        {
            WebResponse response;

            try
            {
                response = getResponse();
            }
            catch (WebException e)
            {
                var httpWebResponse = e.Response as HttpWebResponse;
                if (httpWebResponse == null ||
                    httpWebResponse.StatusCode == HttpStatusCode.NotFound ||
                    httpWebResponse.StatusCode == HttpStatusCode.Conflict)
                {
                    throw;
                }

                if (httpWebResponse.StatusCode == HttpStatusCode.NotModified &&
                    CachedRequestDetails != null)
                {
                    return(factory.GetCachedResponse(this));
                }

                using (var sr = new StreamReader(e.Response.GetResponseStreamWithHttpDecompression()))
                {
                    throw new InvalidOperationException(sr.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();
                factory.CacheResponse(response, text, this);
                return(text);
            }
        }
コード例 #3
0
ファイル: HttpJsonRequest.cs プロジェクト: webmonger/ravendb
        private RavenJToken ReadJsonInternal(Func <WebResponse> getResponse)
        {
            WebResponse response;

            try
            {
                response = getResponse();
                sp.Stop();
            }
            catch (WebException e)
            {
                sp.Stop();
                var result = HandleErrors(e);
                if (result == null)
                {
                    throw;
                }
                return(result);
            }
            catch (AggregateException e)
            {
                sp.Stop();
                var we = e.ExtractSingleInnerException() as WebException;
                if (we == null)
                {
                    throw;
                }
                var result = HandleErrors(we);
                if (result == null)
                {
                    throw;
                }
                return(result);
            }

            ResponseHeaders    = new NameValueCollection(response.Headers);
            ResponseStatusCode = ((HttpWebResponse)response).StatusCode;

            HandleReplicationStatusChanges(ResponseHeaders, primaryUrl, operationUrl);

            using (response)
                using (var responseStream = response.GetResponseStreamWithHttpDecompression())
                {
                    var data = RavenJToken.TryLoad(responseStream);

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

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

                    return(data);
                }
        }
コード例 #4
0
ファイル: MultiGetOperation.cs プロジェクト: GorelH/ravendb
		public GetResponse[] HandleCachingResponse(GetResponse[] responses, HttpJsonRequestFactory jsonRequestFactory)
		{
			var hasCachedRequests = false;
			var requestStatuses = new RequestStatus[responses.Length];
			for (int i = 0; i < responses.Length; i++)
			{
				if (responses[i] == null || responses[i].Status == 304)
				{
					hasCachedRequests = true;

					requestStatuses[i] = responses[i] == null ? RequestStatus.AggressivelyCached : RequestStatus.Cached;
					responses[i] = responses[i] ?? new GetResponse { Status = 0 };

					foreach (string header in cachedData[i].Headers)
					{
						responses[i].Headers[header] = cachedData[i].Headers[header];
					}
					responses[i].Result = cachedData[i].Data.CloneToken();
					jsonRequestFactory.IncrementCachedRequests();
				}
				else
				{
					requestStatuses[i] = responses[i].RequestHasErrors() ? RequestStatus.ErrorOnServer : RequestStatus.SentToServer;

					var nameValueCollection = new NameValueCollection();
					foreach (var header in responses[i].Headers)
					{
						nameValueCollection[header.Key] = header.Value;
					}
					jsonRequestFactory.CacheResponse(url + requests[i].UrlAndQuery, responses[i].Result, nameValueCollection);
				}
			}

			if (hasCachedRequests == false || convention.DisableProfiling ||
				holdProfilingInformation.ProfilingInformation.Requests.Count == 0)
				return responses;

			var lastRequest = holdProfilingInformation.ProfilingInformation.Requests.Last();
			for (int i = 0; i < requestStatuses.Length; i++)
			{
				lastRequest.AdditionalInformation["NestedRequestStatus-" + i] = requestStatuses[i].ToString();
			}
			lastRequest.Result = JsonConvert.SerializeObject(responses);

			return responses;
		}
コード例 #5
0
        private string ReadStringInternal(Func <WebResponse> getResponse)
        {
            WebResponse response;

            try
            {
                response = getResponse();
                sp.Stop();
            }
            catch (WebException e)
            {
                sp.Stop();
                var result = HanldeErrors(e);
                if (result == null)
                {
                    throw;
                }
                return(result);
            }
#if !NET_3_5
            catch (AggregateException e)
            {
                sp.Stop();
                var we = e.ExtractSingleInnerException() as WebException;
                if (we == null)
                {
                    throw;
                }
                var result = HanldeErrors(we);
                if (result == null)
                {
                    throw;
                }
                return(result);
            }
#endif

            ResponseHeaders    = new NameValueCollection(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);
            }
        }
コード例 #6
0
ファイル: HttpJsonRequest.cs プロジェクト: johannesg/ravendb
        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);
            }
        }