public static EntityTag Parse(string original)
        {
            string s = original;
            EntityTag tag = new EntityTag();

            if (s.StartsWithInvariant("W/"))
            {
                tag.IsWeak = true;
                s = s.Substring(2);
            }
            if (s == "*")
            {
                tag.Tag = "*";
            }
            else if (!s.StartsWithInvariant("\"") && !s.EndsWithInvariant("\""))
            {
                tag.Tag = s;
            }
            else if (s.Length < 2 || !s.StartsWithInvariant("\"") || !s.EndsWithInvariant("\""))
            {
                throw new ArgumentOutOfRangeException(s);
            }
            else
            {
                tag.Tag = s.Substring(1, s.Length - 2);
            }
            return tag;
        }
Exemple #2
0
 public void StartPolling(Uri uri, EntityTag etag, DateTime? lastModifiedTime)
 {
     if (this.HttpClient == null)
     {
         throw new InvalidOperationException("The http client to use for polling is not specified.");
     }
     lock (thisLock)
     {
         if (this.isBusy)
         {
             throw new InvalidOperationException("Polling has already started");
         }
         this.isBusy = true;
     }
     this.uri = uri ?? this.HttpClient.BaseAddress;
     this.etag = etag;
     this.lastModifiedTime = lastModifiedTime;
     this.timer = new Timer(this.PollingInterval.TotalMilliseconds);
     this.timer.AutoReset = true;
     this.syncContext = SynchronizationContext.Current;
     this.timer.Elapsed += this.TimerElapsed;
     this.timer.Enabled = true;
     this.timer.Start();
 }
Exemple #3
0
 public DateOrEntityTag(EntityTag entityTag)
 {
     this.EntityTag = entityTag;
 }
Exemple #4
0
        void TimerElapsed(object sender, ElapsedEventArgs e)
        {
            if (!this.isBusy)
            {
                return;
            }
            if (!this.IgnoreExpiresHeader)
            {
                // Since Expires is a static guess by the server, use Expires only when the server did
                // not send back any Etag or LastModifiedTime, which makes conditional GET impossible
                if (this.etag == null && this.lastModifiedTime == null && this.expires != null && (DateTime.UtcNow < this.expires.Value.ToUniversalTime()))
                {
                    return;
                }
            }
            this.expires = null;
            HttpRequestMessage request = new HttpRequestMessage("GET", this.uri);
            if (this.etag != null)
            {
                var ifNoneMatch = new HeaderValues<EntityTag>();
                ifNoneMatch.Add(this.etag);
                request.Headers.IfNoneMatch = ifNoneMatch;
            }
            request.Headers.IfModifiedSince = this.lastModifiedTime;
            bool stopTimer = false;
            try
            {
                HttpResponseMessage response = null;
                
                try
                {
                    response = this.HttpClient.Send(request);
                }
                catch (Exception ex)
                {
                    if (!this.IgnoreSendErrors)
                    {
                        stopTimer = InvokeHandler(ex);
                    }
                    if (response != null)
                    {
                        response.Dispose();
                    }
                    return;
                }

                using (response)
                {
                    switch (response.StatusCode)
                    {
                        case HttpStatusCode.NotModified:
                            // the resource has not been modified
                            response.Dispose();
                            break;
                        case HttpStatusCode.OK:
                            // the resource has been modified. Fire the event, along with the response message
                            this.etag = response.Headers.ETag;
                            this.expires = response.Headers.Expires;
                            this.lastModifiedTime = response.Headers.LastModified;
                            try
                            {
                                stopTimer = InvokeHandler(response);
                            }
                            finally
                            {
                                response.Dispose();
                            }
                            break;
                        default:
                            // this is an unexpected error. Fire the event, if errors are not to be suppressed
                            try
                            {
                                if (!this.IgnoreNonOKStatusCodes)
                                {
                                    stopTimer = InvokeHandler(response);
                                }
                            }
                            finally
                            {
                                response.Dispose();
                            }
                            break;
                    }
                }
            }
            finally
            {
                if (stopTimer)
                {
                    StopPolling();
                }
            }
        }
 public DateOrEntityTag(EntityTag entityTag)
 {
     this.EntityTag = entityTag;
 }