Ben Copsey

Added workaround for problem with type conversion on 2.2.1 for apps with a base sdk >= 3.0

Fiddle with header organisation / comments
@@ -416,6 +416,11 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount; @@ -416,6 +416,11 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount;
416 - (void)attemptToApplyCredentialsAndResume; 416 - (void)attemptToApplyCredentialsAndResume;
417 - (void)attemptToApplyProxyCredentialsAndResume; 417 - (void)attemptToApplyProxyCredentialsAndResume;
418 418
  419 +// Attempt to show the built-in authentication dialog, returns YES if credentials were supplied, NO if user cancelled dialog / dialog is disabled / running on main thread
  420 +// Currently only used on iPhone OS
  421 +- (BOOL)showProxyAuthenticationDialog;
  422 +- (BOOL)showAuthenticationDialog;
  423 +
419 #pragma mark stream status handlers 424 #pragma mark stream status handlers
420 425
421 // CFnetwork event handlers 426 // CFnetwork event handlers
@@ -533,12 +538,12 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount; @@ -533,12 +538,12 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount;
533 + (void)reachabilityChanged:(NSNotification *)note; 538 + (void)reachabilityChanged:(NSNotification *)note;
534 #endif 539 #endif
535 540
536 - 541 +// Returns the maximum amount of data we can read as part of the current measurement period, and sleeps this thread if our allowance is used up
537 -- (BOOL)showProxyAuthenticationDialog;  
538 -- (BOOL)showAuthenticationDialog;  
539 -  
540 + (unsigned long)maxUploadReadLength; 542 + (unsigned long)maxUploadReadLength;
541 543
  544 +// Determines whether we're on iPhone OS 2.0 at runtime, currently used to determine whether we should apply a workaround for an issue with converting longs to doubles on iPhone OS 2
  545 ++ (BOOL)isiPhoneOS2;
  546 +
542 @property (retain) NSString *username; 547 @property (retain) NSString *username;
543 @property (retain) NSString *password; 548 @property (retain) NSString *password;
544 @property (retain) NSString *domain; 549 @property (retain) NSString *domain;
@@ -95,6 +95,8 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; @@ -95,6 +95,8 @@ static NSRecursiveLock *delegateAuthenticationLock = nil;
95 95
96 static NSOperationQueue *sharedRequestQueue = nil; 96 static NSOperationQueue *sharedRequestQueue = nil;
97 97
  98 +static BOOL isiPhoneOS2;
  99 +
98 // Private stuff 100 // Private stuff
99 @interface ASIHTTPRequest () 101 @interface ASIHTTPRequest ()
100 102
@@ -157,6 +159,12 @@ static NSOperationQueue *sharedRequestQueue = nil; @@ -157,6 +159,12 @@ static NSOperationQueue *sharedRequestQueue = nil;
157 ASIRequestCancelledError = [[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIRequestCancelledErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"The request was cancelled",NSLocalizedDescriptionKey,nil]] retain]; 159 ASIRequestCancelledError = [[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIRequestCancelledErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"The request was cancelled",NSLocalizedDescriptionKey,nil]] retain];
158 ASIUnableToCreateRequestError = [[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIUnableToCreateRequestErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to create request (bad url?)",NSLocalizedDescriptionKey,nil]] retain]; 160 ASIUnableToCreateRequestError = [[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIUnableToCreateRequestErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to create request (bad url?)",NSLocalizedDescriptionKey,nil]] retain];
159 ASITooMuchRedirectionError = [[NSError errorWithDomain:NetworkRequestErrorDomain code:ASITooMuchRedirectionErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"The request failed because it redirected too many times",NSLocalizedDescriptionKey,nil]] retain]; 161 ASITooMuchRedirectionError = [[NSError errorWithDomain:NetworkRequestErrorDomain code:ASITooMuchRedirectionErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"The request failed because it redirected too many times",NSLocalizedDescriptionKey,nil]] retain];
  162 +
  163 +#if TARGET_OS_IPHONE
  164 + isiPhoneOS2 = ((floorf([[[UIDevice currentDevice] systemVersion] floatValue]) == 2.0) ? YES : NO);
  165 +#else
  166 + isiPhoneOS2 = NO;
  167 +#endif
160 } 168 }
161 [super initialize]; 169 [super initialize];
162 } 170 }
@@ -698,7 +706,13 @@ static NSOperationQueue *sharedRequestQueue = nil; @@ -698,7 +706,13 @@ static NSOperationQueue *sharedRequestQueue = nil;
698 if (shouldResetProgressIndicators) { 706 if (shouldResetProgressIndicators) {
699 double amount = 1; 707 double amount = 1;
700 if (showAccurateProgress) { 708 if (showAccurateProgress) {
701 - amount = postLength; 709 +
  710 + //Workaround for an issue with converting a long to a double on iPhone OS 2.2.1 with a base SDK >= 3.0
  711 + if ([ASIHTTPRequest isiPhoneOS2]) {
  712 + amount = [[NSNumber numberWithUnsignedLongLong:postLength] doubleValue];
  713 + } else {
  714 + amount = (double)postLength;
  715 + }
702 } 716 }
703 [self resetUploadProgress:amount]; 717 [self resetUploadProgress:amount];
704 } 718 }
@@ -983,7 +997,16 @@ static NSOperationQueue *sharedRequestQueue = nil; @@ -983,7 +997,16 @@ static NSOperationQueue *sharedRequestQueue = nil;
983 997
984 // Update this request's own upload progress delegate 998 // Update this request's own upload progress delegate
985 if (uploadProgressDelegate) { 999 if (uploadProgressDelegate) {
986 - [ASIHTTPRequest setProgress:(double)(1.0*(totalBytesSent-uploadBufferSize)/(postLength-uploadBufferSize)) forProgressIndicator:uploadProgressDelegate]; 1000 +
  1001 + double progress;
  1002 + //Workaround for an issue with converting a long to a double on iPhone OS 2.2.1 with a base SDK >= 3.0
  1003 + if ([ASIHTTPRequest isiPhoneOS2]) {
  1004 + progress = [[NSNumber numberWithUnsignedLongLong:(totalBytesSent-uploadBufferSize)/(postLength-uploadBufferSize)] doubleValue];
  1005 + } else {
  1006 + progress = (double)(1.0*(totalBytesSent-uploadBufferSize)/(postLength-uploadBufferSize));
  1007 + }
  1008 +
  1009 + [ASIHTTPRequest setProgress:progress forProgressIndicator:uploadProgressDelegate];
987 1010
988 } 1011 }
989 1012
@@ -1047,7 +1070,15 @@ static NSOperationQueue *sharedRequestQueue = nil; @@ -1047,7 +1070,15 @@ static NSOperationQueue *sharedRequestQueue = nil;
1047 } 1070 }
1048 1071
1049 if (downloadProgressDelegate && contentLength > 0) { 1072 if (downloadProgressDelegate && contentLength > 0) {
1050 - [ASIHTTPRequest setProgress:(double)(1.0*bytesReadSoFar/(contentLength+partialDownloadSize)) forProgressIndicator:downloadProgressDelegate]; 1073 + double progress;
  1074 + //Workaround for an issue with converting a long to a double on iPhone OS 2.2.1 with a base SDK >= 3.0
  1075 + if ([ASIHTTPRequest isiPhoneOS2]) {
  1076 + progress = [[NSNumber numberWithUnsignedLongLong:bytesReadSoFar/(contentLength+partialDownloadSize)] doubleValue];
  1077 + } else {
  1078 + progress = (double)(1.0*bytesReadSoFar/(contentLength+partialDownloadSize));
  1079 + }
  1080 +
  1081 + [ASIHTTPRequest setProgress:progress forProgressIndicator:downloadProgressDelegate];
1051 } 1082 }
1052 1083
1053 [self setLastBytesRead:bytesReadSoFar]; 1084 [self setLastBytesRead:bytesReadSoFar];
@@ -2862,6 +2893,11 @@ static NSOperationQueue *sharedRequestQueue = nil; @@ -2862,6 +2893,11 @@ static NSOperationQueue *sharedRequestQueue = nil;
2862 return toRead; 2893 return toRead;
2863 } 2894 }
2864 2895
  2896 ++ (BOOL)isiPhoneOS2
  2897 +{
  2898 + return isiPhoneOS2;
  2899 +}
  2900 +
2865 2901
2866 @synthesize username; 2902 @synthesize username;
2867 @synthesize password; 2903 @synthesize password;
@@ -231,6 +231,7 @@ @@ -231,6 +231,7 @@
231 } 231 }
232 [self setUploadProgressBytes:[self uploadProgressBytes] - bytes]; 232 [self setUploadProgressBytes:[self uploadProgressBytes] - bytes];
233 233
  234 +
234 double progress = ([self uploadProgressBytes]*1.0)/([self uploadProgressTotalBytes]*1.0); 235 double progress = ([self uploadProgressBytes]*1.0)/([self uploadProgressTotalBytes]*1.0);
235 [ASIHTTPRequest setProgress:progress forProgressIndicator:[self uploadProgressDelegate]]; 236 [ASIHTTPRequest setProgress:progress forProgressIndicator:[self uploadProgressDelegate]];
236 } 237 }
@@ -243,7 +244,13 @@ @@ -243,7 +244,13 @@
243 } 244 }
244 [self setUploadProgressBytes:[self uploadProgressBytes] + bytes]; 245 [self setUploadProgressBytes:[self uploadProgressBytes] + bytes];
245 246
246 - double progress = ([self uploadProgressBytes]*1.0)/([self uploadProgressTotalBytes]*1.0); 247 + double progress;
  248 + //Workaround for an issue with converting a long to a double on iPhone OS 2.2.1 with a base SDK >= 3.0
  249 + if ([ASIHTTPRequest isiPhoneOS2]) {
  250 + progress = [[NSNumber numberWithUnsignedLongLong:[self uploadProgressBytes]] doubleValue]/[[NSNumber numberWithUnsignedLongLong:[self uploadProgressTotalBytes]] doubleValue];
  251 + } else {
  252 + progress = ([self uploadProgressBytes]*1.0)/([self uploadProgressTotalBytes]*1.0);
  253 + }
247 [ASIHTTPRequest setProgress:progress forProgressIndicator:[self uploadProgressDelegate]]; 254 [ASIHTTPRequest setProgress:progress forProgressIndicator:[self uploadProgressDelegate]];
248 255
249 } 256 }
@@ -263,7 +270,14 @@ @@ -263,7 +270,14 @@
263 return; 270 return;
264 } 271 }
265 [self setDownloadProgressBytes:[self downloadProgressBytes] + bytes]; 272 [self setDownloadProgressBytes:[self downloadProgressBytes] + bytes];
266 - double progress = ([self downloadProgressBytes]*1.0)/([self downloadProgressTotalBytes]*1.0); 273 +
  274 + double progress;
  275 + //Workaround for an issue with converting a long to a double on iPhone OS 2.2.1 with a base SDK >= 3.0
  276 + if ([ASIHTTPRequest isiPhoneOS2]) {
  277 + progress = [[NSNumber numberWithUnsignedLongLong:[self downloadProgressBytes]] doubleValue]/[[NSNumber numberWithUnsignedLongLong:[self downloadProgressTotalBytes]] doubleValue];
  278 + } else {
  279 + progress = ([self downloadProgressBytes]*1.0)/([self downloadProgressTotalBytes]*1.0);
  280 + }
267 [ASIHTTPRequest setProgress:progress forProgressIndicator:[self downloadProgressDelegate]]; 281 [ASIHTTPRequest setProgress:progress forProgressIndicator:[self downloadProgressDelegate]];
268 } 282 }
269 283