OLD | NEW |
1 /* | 1 /* |
2 Copyright 2013 Google Inc | 2 Copyright 2013 Google Inc |
3 | 3 |
4 Licensed under the Apache License, Version 2.0 (the "License"); | 4 Licensed under the Apache License, Version 2.0 (the "License"); |
5 you may not use this file except in compliance with the License. | 5 you may not use this file except in compliance with the License. |
6 You may obtain a copy of the License at | 6 You may obtain a copy of the License at |
7 | 7 |
8 http://www.apache.org/licenses/LICENSE-2.0 | 8 http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 |
10 Unless required by applicable law or agreed to in writing, software | 10 Unless required by applicable law or agreed to in writing, software |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 #endregion | 85 #endregion |
86 | 86 |
87 /// <summary> Number of tries. Default is <c>3</c>. </summary> | 87 /// <summary> Number of tries. Default is <c>3</c>. </summary> |
88 private int numTries = 3; | 88 private int numTries = 3; |
89 | 89 |
90 /// <summary>· | 90 /// <summary>· |
91 /// Gets or sets the number of tries that will be allowed to execute. Re
tries occur as a result of either | 91 /// Gets or sets the number of tries that will be allowed to execute. Re
tries occur as a result of either |
92 /// <see cref="IHttpUnsuccessfulResponseHandler"/> or <see cref="IHttpEx
ceptionHandler"/> which handles the | 92 /// <see cref="IHttpUnsuccessfulResponseHandler"/> or <see cref="IHttpEx
ceptionHandler"/> which handles the |
93 /// abnormal Http response or exception, before being terminated.· | 93 /// abnormal Http response or exception, before being terminated.· |
94 /// Set <c>1</c> for not retrying requests. The default value is <c>3</c
>". | 94 /// Set <c>1</c> for not retrying requests. The default value is <c>3</c
>". |
| 95 /// <remarks> |
| 96 /// The number of allowed redirects (3xx) is defined by <seealso cref="N
umRedirects"/>. This property defines |
| 97 /// only the allowed tries for >=400 responses, or in a case an exceptio
n was thrown. For example you set· |
| 98 /// <see cref="NumTries"/> to 1 and <see cref="NumRedirect"/> to 5, a re
try will happen only for 5 redirects. |
| 99 /// </remarks> |
95 /// </summary> | 100 /// </summary> |
96 public int NumTries | 101 public int NumTries |
97 { | 102 { |
98 get { return numTries; } | 103 get { return numTries; } |
99 set | 104 set |
100 { | 105 { |
101 if (value > MaxAllowedNumTries || value < 1) | 106 if (value > MaxAllowedNumTries || value < 1) |
102 { | 107 { |
103 throw new ArgumentOutOfRangeException("NumTries"); | 108 throw new ArgumentOutOfRangeException("NumTries"); |
104 } | 109 } |
105 numTries = value; | 110 numTries = value; |
106 } | 111 } |
107 } | 112 } |
108 | 113 |
| 114 /// <summary> Number of redirects allowed. Default is <c>10</c>. </summa
ry> |
| 115 private int numRedirect = 10; |
| 116 |
| 117 /// <summary>· |
| 118 /// Gets or sets the number of redirects that will be allowed to execute
. The default value is <c>10</c>. |
| 119 /// See <see cref="NumTries"/> for more information. |
| 120 /// </summary> |
| 121 public int NumRedirects |
| 122 { |
| 123 get { return numRedirect; } |
| 124 set |
| 125 { |
| 126 if (value > MaxAllowedNumTries || value < 1) |
| 127 { |
| 128 throw new ArgumentOutOfRangeException("NumRedirects"); |
| 129 } |
| 130 numRedirect = value; |
| 131 } |
| 132 } |
| 133 |
109 /// <summary>· | 134 /// <summary>· |
110 /// Gets or sets whether the handler should follow a redirect when a red
irect response is received. Default· | 135 /// Gets or sets whether the handler should follow a redirect when a red
irect response is received. Default· |
111 /// value is <c>true</c>. | 136 /// value is <c>true</c>. |
112 /// </summary> | 137 /// </summary> |
113 public bool FollowRedirect { get; set; } | 138 public bool FollowRedirect { get; set; } |
114 | 139 |
115 /// <summary> Gets or sets whether logging is enabled. Default value is
<c>true</c>. </summary> | 140 /// <summary> Gets or sets whether logging is enabled. Default value is
<c>true</c>. </summary> |
116 public bool IsLoggingEnabled { get; set; } | 141 public bool IsLoggingEnabled { get; set; } |
117 | 142 |
118 /// <summary> Gets or sets the application name which will be used on th
e User-Agent header. </summary> | 143 /// <summary> Gets or sets the application name which will be used on th
e User-Agent header. </summary> |
(...skipping 12 matching lines...) Expand all Loading... |
131 /// The main logic of sending a request to the server. This send method
adds the User-Agent header to a request | 156 /// The main logic of sending a request to the server. This send method
adds the User-Agent header to a request |
132 /// with <see cref="ApplicationName"/> and the library version. It also
calls interceptors before each attempt, | 157 /// with <see cref="ApplicationName"/> and the library version. It also
calls interceptors before each attempt, |
133 /// and unsuccessful response handler or exception handlers when abnorma
l response or exception occurred. | 158 /// and unsuccessful response handler or exception handlers when abnorma
l response or exception occurred. |
134 /// </summary> | 159 /// </summary> |
135 protected override async Task<HttpResponseMessage> SendAsync(HttpRequest
Message request, | 160 protected override async Task<HttpResponseMessage> SendAsync(HttpRequest
Message request, |
136 CancellationToken cancellationToken) | 161 CancellationToken cancellationToken) |
137 { | 162 { |
138 var loggable = IsLoggingEnabled && Logger.IsDebugEnabled; | 163 var loggable = IsLoggingEnabled && Logger.IsDebugEnabled; |
139 | 164 |
140 int triesRemaining = NumTries; | 165 int triesRemaining = NumTries; |
| 166 int redirectRemaining = NumRedirects; |
| 167 |
141 Exception lastException = null; | 168 Exception lastException = null; |
142 | 169 |
143 // set User-Agent header | 170 // set User-Agent header |
144 var userAgent = (ApplicationName == null ? "" : ApplicationName "
") UserAgentSuffix; | 171 var userAgent = (ApplicationName == null ? "" : ApplicationName "
") UserAgentSuffix; |
145 // TODO: setting the User-Agent won't work on Silverlight. We may ne
ed to create a special callback here to· | 172 // TODO: setting the User-Agent won't work on Silverlight. We may ne
ed to create a special callback here to· |
146 // set it correctly. Also check what happen in WP? | 173 // set it correctly. Also check what happen in WP? |
147 request.Headers.Add("User-Agent", userAgent); | 174 request.Headers.Add("User-Agent", userAgent); |
148 | 175 |
149 HttpResponseMessage response = null; | 176 HttpResponseMessage response = null; |
150 do // while (triesRemaining > 0) | 177 do // while (triesRemaining > 0) |
(...skipping 19 matching lines...) Expand all Loading... |
170 { | 197 { |
171 // send the request! | 198 // send the request! |
172 response = await base.SendAsync(request, cancellationToken).
ConfigureAwait(false); | 199 response = await base.SendAsync(request, cancellationToken).
ConfigureAwait(false); |
173 } | 200 } |
174 catch (Exception ex) | 201 catch (Exception ex) |
175 { | 202 { |
176 lastException = ex; | 203 lastException = ex; |
177 } | 204 } |
178 | 205 |
179 // decrease the number of retries | 206 // decrease the number of retries |
180 triesRemaining--; | 207 if (response == null || (int)response.StatusCode >= 400) |
| 208 { |
| 209 triesRemaining--; |
| 210 } |
181 | 211 |
182 // exception was thrown , try to handle it | 212 // exception was thrown , try to handle it |
183 if (response == null) | 213 if (response == null) |
184 { | 214 { |
185 var exceptionHandled = false; | 215 var exceptionHandled = false; |
186 | 216 |
187 // try to handle the exception with each handler | 217 // try to handle the exception with each handler |
188 foreach (var handler in exceptionHandlers) | 218 foreach (var handler in exceptionHandlers) |
189 { | 219 { |
190 exceptionHandled |= handler.HandleException(new HandleEx
ceptionArgs | 220 exceptionHandled |= handler.HandleException(new HandleEx
ceptionArgs |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 TotalTries = NumTries, | 260 TotalTries = NumTries, |
231 CurrentFailedTry = NumTries - triesRemaining
, | 261 CurrentFailedTry = NumTries - triesRemaining
, |
232 CancellationToken = cancellationToken | 262 CancellationToken = cancellationToken |
233 }); | 263 }); |
234 } | 264 } |
235 | 265 |
236 if (!errorHandled) | 266 if (!errorHandled) |
237 { | 267 { |
238 if (HandleRedirect(response)) | 268 if (HandleRedirect(response)) |
239 { | 269 { |
| 270 if (redirectRemaining-- == 0) |
| 271 { |
| 272 triesRemaining = 0; |
| 273 } |
| 274 |
240 errorHandled = true; | 275 errorHandled = true; |
241 if (loggable) | 276 if (loggable) |
242 { | 277 { |
243 Logger.Debug("Redirect response was handled
successfully. Redirect to {0}", | 278 Logger.Debug("Redirect response was handled
successfully. Redirect to {0}", |
244 response.Headers.Location); | 279 response.Headers.Location); |
245 } | 280 } |
246 } | 281 } |
247 else | 282 else |
248 { | 283 { |
249 if (loggable) | 284 if (loggable) |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 request.Headers.Remove("Authorization"); | 341 request.Headers.Remove("Authorization"); |
307 request.Headers.IfMatch.Clear(); | 342 request.Headers.IfMatch.Clear(); |
308 request.Headers.IfNoneMatch.Clear(); | 343 request.Headers.IfNoneMatch.Clear(); |
309 request.Headers.IfModifiedSince = null; | 344 request.Headers.IfModifiedSince = null; |
310 request.Headers.IfUnmodifiedSince = null; | 345 request.Headers.IfUnmodifiedSince = null; |
311 request.Headers.Remove("If-Range"); | 346 request.Headers.Remove("If-Range"); |
312 return true; | 347 return true; |
313 } | 348 } |
314 } | 349 } |
315 } | 350 } |
OLD | NEW |