Пример #1
0
        public static CacheEntry Create(NetworkResponse response)
        {
            var headers = response.Headers;

            long server_date = 0;
            long server_expires = 0;
            long soft_expires = 0;
            long max_age = 0;
            var has_cache_control = false;

            var value = headers.Get("Date");
            if (value != null)
            {
                server_date = ParseDate(value);
            }

            value = headers.Get("Cache-Control");
            if (value != null)
            {
                has_cache_control = true;
                foreach (var token in value.Split(',').Select(x => x.Trim()))
                {
                    if (token == "no-cache" || token == "no-store")
                    {
                        return null;
                    }
                    else if (token.StartsWith("max-age="))
                    {
                        Int64.TryParse(token.Substring(8), out max_age);
                    }
                }
            }

            value = headers.Get("Expires");
            if (value != null)
            {
                server_expires = ParseDate(value);
            }

            if (has_cache_control)
            {
                var now = DateTime.Now.Ticks;
                soft_expires = now + max_age * 1000;
            }
            else if (server_date > 0 && server_expires > + server_date)
            {
                soft_expires = (server_expires - server_date);
            }

            return new CacheEntry
            {
                Data = response.Data,
                ETag = headers.Get("ETag"),
                Expires = soft_expires,
                SoftExpires = soft_expires,
                ServerDate = server_date,
                Headers = response.Headers,
            };
        }
Пример #2
0
 public override Response Parse(NetworkResponse response)
 {
     return(new Response <T>
     {
         CacheEntry = CacheEntry.Create(response),
         Result = ParseJson(response),
     });
 }
Пример #3
0
 public override Response Parse(NetworkResponse response)
 {
     return(new Response <string>
     {
         CacheEntry = CacheEntry.Create(response),
         Result = Encoding.UTF8.GetString(response.Data),
     });
 }
Пример #4
0
        public static CacheEntry Create(NetworkResponse response)
        {
            long server_date       = 0;
            long server_expires    = 0;
            long soft_expires      = 0;
            long max_age           = 0;
            var  has_cache_control = false;

            var value = response.Headers.Get("Date");

            if (value != null)
            {
                server_date = ParseDate(value);
            }

            value = response.Headers.Get("Cache-Control");
            if (value != null)
            {
                has_cache_control = true;
                foreach (var token in value.Split(',').Select(x => x.Trim()))
                {
                    if (token == "no-cache" || token == "no-store")
                    {
                        return(null);
                    }
                    else if (token.StartsWith("max-age="))
                    {
                        Int64.TryParse(token.Substring(8), out max_age);
                    }
                }
            }

            value = response.Headers.Get("Expires");
            if (value != null)
            {
                server_expires = ParseDate(value);
            }

            if (has_cache_control)
            {
                var now = DateTime.Now.Ticks;
                soft_expires = now + max_age * 1000;
            }
            else if (server_date > 0 && server_expires > +server_date)
            {
                soft_expires = (server_expires - server_date);
            }

            return(new CacheEntry
            {
                Data = response.Data,
                ETag = response.Headers.Get("ETag"),
                Expires = soft_expires,
                SoftExpires = soft_expires,
                ServerDate = server_date,
                Headers = response.Headers,
            });
        }
Пример #5
0
        private void NetworkConsumer()
        {
            foreach (var request in _network_queue.GetConsumingEnumerable())
            {
                request.AddMarker("network-dequeue");

                if (request.IsCanceled)
                {
                    request.Finish("network-discard-canceled");
                    continue;
                }

                NetworkResponse network_response = null;

                try
                {
                    network_response = _network.Execute(request);
                    request.AddMarker("network-http-complete");

                    if (network_response.StatusCode == HttpStatusCode.NotModified &&
                        request.ResponseDelievered)
                    {
                        request.Finish("not-modified");
                        continue;
                    }

                    var response = request.Parse(network_response);
                    request.AddMarker("network-parse-complete");

                    if (request.ShouldCache && response.CacheEntry != null)
                    {
                        _cache[request.CacheKey] = response.CacheEntry;
                        request.AddMarker("network-cache-written");
                    }

                    request.ResponseDelievered = true;
                    _delivery.PostResponse(request, response);
                }
                catch (RequestException ex)
                {
                    _delivery.PostException(request, ex);
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "Unhandled Exception");

                    var exception = new RequestException("Unhandled exception", ex, network_response);
                    _delivery.PostException(request, exception);
                }
            }
        }
Пример #6
0
        private T ParseJson(NetworkResponse response)
        {
            // XXX: can't assume utf8 encoding, need a parseCharset equiv
            var json_response = Encoding.UTF8.GetString(response.Data);

            try
            {
                return(_serializer.Deserialize <T>(json_response));
            }
            catch (Exception ex)
            {
                throw new ParseException(ex, response);
            }
        }
Пример #7
0
        public NetworkResponse Execute(Request request)
        {
            while (true)
            {
                NetworkResponse response = null;

                try
                {
                    var headers = GetCacheHeaders(request.CacheEntry);
                    response = _client.Execute(request, headers);

                    if (response.StatusCode == HttpStatusCode.NotModified)
                    {
                        return(new NetworkResponse
                        {
                            StatusCode = response.StatusCode,
                            Data = request.CacheEntry.Data,
                            Headers = response.Headers,
                        });
                    }

                    if (response.StatusCode != HttpStatusCode.OK &&
                        response.StatusCode != HttpStatusCode.NoContent)
                    {
                        throw new IOException();
                    }

                    return(response);
                }
                catch (TimeoutException ex)
                {
                    AttemptRetry(request, ex);
                }
                catch (IOException ex)
                {
                    if (response == null)
                    {
                        throw
                            new ConnectionException(ex, response);
                    }

                    _logger.Error("Unexpected response code {0} for {1}", response.StatusCode, request.Uri);
                    throw; // XXX logic
                }
            }
        }
Пример #8
0
 public abstract Response Parse(NetworkResponse response);
Пример #9
0
 public RequestException(string message, Exception exception, NetworkResponse response)
     : base(message, exception)
 {
     Response = response;
 }
Пример #10
0
 public abstract Response Parse(NetworkResponse response);
Пример #11
0
 public ParseException(Exception exception, NetworkResponse response)
     : base("Failed to parse response", exception, response)
 {
 }
Пример #12
0
 public ConnectionException(Exception exception, NetworkResponse response)
     : base("Failed to connect to server", exception, response)
 {
 }