1515
1616import okhttp3 .Interceptor ;
1717import okhttp3 .Request ;
18+ import okhttp3 .RequestBody ;
1819import okhttp3 .Response ;
1920import okhttp3 .ResponseBody ;
2021
@@ -38,22 +39,6 @@ public class RetryHandler implements Interceptor{
3839 * Header name for the retry after information
3940 */
4041 private static final String RETRY_AFTER = "Retry-After" ;
41- /**
42- * Header name for the transfer information
43- */
44- private static final String TRANSFER_ENCODING = "Transfer-Encoding" ;
45- /**
46- * Chunked encoding header value
47- */
48- private static final String TRANSFER_ENCODING_CHUNKED = "chunked" ;
49- /**
50- * Binary content type header value
51- */
52- private static final String APPLICATION_OCTET_STREAM = "application/octet-stream" ;
53- /**
54- * Header name for the content type
55- */
56- private static final String CONTENT_TYPE = "Content-Type" ;
5742
5843 /**
5944 * Too many requests status code
@@ -117,7 +102,7 @@ boolean retryRequest(Response response, int executionCount, Request request, Ret
117102 shouldRetry =
118103 retryOptions != null
119104 && executionCount <= retryOptions .maxRetries ()
120- && checkStatus (statusCode ) && isBuffered (response , request )
105+ && checkStatus (statusCode ) && isBuffered (request )
121106 && shouldRetryCallback != null
122107 && shouldRetryCallback .shouldRetry (retryOptions .delay (), executionCount , request , response );
123108
@@ -157,28 +142,23 @@ boolean checkStatus(int statusCode) {
157142 || statusCode == MSClientErrorCodeGatewayTimeout );
158143 }
159144
160- boolean isBuffered (final Response response , final Request request ) {
161- String methodName = request .method ();
162- if (methodName .equalsIgnoreCase ("GET" ) || methodName .equalsIgnoreCase ("DELETE" ) || methodName .equalsIgnoreCase ("HEAD" ) || methodName .equalsIgnoreCase ("OPTIONS" ))
163- return true ;
145+ boolean isBuffered (final Request request ) {
146+ final String methodName = request .method ();
164147
165- boolean isHTTPMethodPutPatchOrPost = methodName .equalsIgnoreCase ("POST" ) ||
148+ final boolean isHTTPMethodPutPatchOrPost = methodName .equalsIgnoreCase ("POST" ) ||
166149 methodName .equalsIgnoreCase ("PUT" ) ||
167150 methodName .equalsIgnoreCase ("PATCH" );
168151
169- if (isHTTPMethodPutPatchOrPost && response != null ) {
170- final String contentTypeHeaderValue = response .header (CONTENT_TYPE );
171- final boolean isStream = contentTypeHeaderValue !=null && contentTypeHeaderValue .equalsIgnoreCase (APPLICATION_OCTET_STREAM );
172- if (!isStream ) {
173- final String transferEncoding = response .header (TRANSFER_ENCODING );
174- final boolean isTransferEncodingChunked = (transferEncoding != null ) &&
175- transferEncoding .equalsIgnoreCase (TRANSFER_ENCODING_CHUNKED );
176-
177- if (request .body () != null && isTransferEncodingChunked )
178- return true ;
152+ final RequestBody requestBody = request .body ();
153+ if (isHTTPMethodPutPatchOrPost && requestBody != null ) {
154+ try {
155+ return requestBody .contentLength () != -1L ;
156+ } catch (IOException ex ) {
157+ // expected
158+ return false ;
179159 }
180160 }
181- return false ;
161+ return true ;
182162 }
183163
184164 @ Override
0 commit comments