Revision cfed7823 trunk/Pithos.Network/RestClient.cs

b/trunk/Pithos.Network/RestClient.cs
40 40
        private readonly Dictionary<string, string> _parameters=new Dictionary<string, string>();
41 41
        public Dictionary<string, string> Parameters
42 42
        {
43
            get { return _parameters; }            
43
            get
44
            {
45
                Contract.Ensures(_parameters!=null);
46
                return _parameters;
47
            }            
48
        }
49

  
50
        [ContractInvariantMethod]
51
        private void Invariants()
52
        {
53
            Contract.Invariant(Headers!=null);    
44 54
        }
45 55

  
46 56
        public RestClient():base()
......
52 62
        public RestClient(RestClient other)
53 63
            : base()
54 64
        {
65
            if (other==null)
66
                throw new ArgumentNullException("other");
67
            Contract.EndContractBlock();
68

  
55 69
            CopyHeaders(other);
56 70
            Timeout = other.Timeout;
57 71
            Retries = other.Retries;
......
69 83
        {
70 84
            TimedOut = false;
71 85
            var webRequest = base.GetWebRequest(address);
72
            var request = webRequest as HttpWebRequest;
86
            var request = (HttpWebRequest)webRequest;
73 87
            if (IfModifiedSince.HasValue)
74 88
                request.IfModifiedSince = IfModifiedSince.Value;
75 89
            request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
......
96 110
            return response;
97 111
        }
98 112

  
99

  
113
        private readonly List<HttpStatusCode> _allowedStatusCodes=new List<HttpStatusCode>{HttpStatusCode.NotModified};
114
        public List<HttpStatusCode> AllowedStatusCodes
115
        {
116
            get
117
            {
118
                return _allowedStatusCodes;
119
            }            
120
        }
100 121

  
101 122
        protected override WebResponse GetWebResponse(WebRequest request)
102 123
        {
......
113 134
                if (exc.Response!=null)
114 135
                {
115 136
                    var response = (exc.Response as HttpWebResponse);
116
                    if (response.StatusCode == HttpStatusCode.NotModified)
137
                    if (AllowedStatusCodes.Contains(response.StatusCode))
138
                    {
139
                        StatusCode = response.StatusCode;
140
                        LastModified = response.LastModified;
141
                        StatusDescription = response.StatusDescription;
142

  
117 143
                        return response;
144
                    }
118 145
                    if (exc.Response.ContentLength > 0)
119 146
                    {
120 147
                        string content = GetContent(exc.Response);
......
129 156

  
130 157
        private static string GetContent(WebResponse webResponse)
131 158
        {
159
            if (webResponse == null)
160
                throw new ArgumentNullException("webResponse");
161
            Contract.EndContractBlock();
162

  
132 163
            string content;
133 164
            using (var stream = webResponse.GetResponseStream())
134 165
            using (var reader = new StreamReader(stream))
......
168 199

  
169 200
        public void Head(string address,int retries=0)
170 201
        {
202
            AllowedStatusCodes.Add(HttpStatusCode.NotFound);
171 203
            RetryWithoutContent(address, retries, "HEAD");
172 204
        }
173 205

  
......
183 215

  
184 216
        public string GetHeaderValue(string headerName)
185 217
        {
218
            if (this.ResponseHeaders==null)
219
                throw new InvalidOperationException("ResponseHeaders are null");
220
            Contract.EndContractBlock();
221

  
186 222
            var values=this.ResponseHeaders.GetValues(headerName);
187 223
            if (values == null)
188 224
                throw new WebException(String.Format("The {0}  header is missing", headerName));
......
293 329
        /// <param name="source">The RestClient from which the headers are copied</param>
294 330
        public void CopyHeaders(RestClient source)
295 331
        {
296
            Contract.Requires(source != null, "source can't be null");
297 332
            if (source == null)
298 333
                throw new ArgumentNullException("source", "source can't be null");
334
            Contract.EndContractBlock();
335
            //The Headers getter initializes the property, it is never null
336
            Contract.Assume(Headers!=null);
337
                
299 338
            CopyHeaders(source.Headers,Headers);
300 339
        }
301 340
        
......
306 345
        /// <param name="target">The target collection to which the headers are copied</param>
307 346
        public static void CopyHeaders(WebHeaderCollection source,WebHeaderCollection target)
308 347
        {
309
            Contract.Requires(source != null, "source can't be null");
310
            Contract.Requires(target != null, "target can't be null");
311 348
            if (source == null)
312 349
                throw new ArgumentNullException("source", "source can't be null");
313 350
            if (target == null)
314 351
                throw new ArgumentNullException("target", "target can't be null");
352
            Contract.EndContractBlock();
353

  
315 354
            for (int i = 0; i < source.Count; i++)
316 355
            {
317 356
                target.Add(source.GetKey(i), source[i]);
......
327 366

  
328 367
        private Task<T> Retry<T>(Func<T> original, int retryCount, TaskCompletionSource<T> tcs = null)
329 368
        {
369
            if (original==null)
370
                throw new ArgumentNullException("original");
371
            Contract.EndContractBlock();
372

  
330 373
            if (tcs == null)
331 374
                tcs = new TaskCompletionSource<T>();
332 375
            Task.Factory.StartNew(original).ContinueWith(_original =>
......
374 417

  
375 418
        private HttpStatusCode GetStatusCode(WebException we)
376 419
        {
420
            if (we==null)
421
                throw new ArgumentNullException("we");
377 422
            var statusCode = HttpStatusCode.RequestTimeout;
378 423
            if (we.Response != null)
379 424
            {
......
382 427
            }
383 428
            return statusCode;
384 429
        }
430

  
431
        public UriBuilder GetAddressBuilder(string container, string objectName)
432
        {
433
            var builder = new UriBuilder(String.Join("/", BaseAddress, container, objectName));
434
            return builder;
435
        }
385 436
    }
386 437

  
387 438
    public class RetryException:Exception

Also available in: Unified diff