Stashing refactoring and reorganizing
This commit is contained in:
parent
37524696e3
commit
5e8fbc0da6
1 changed files with 113 additions and 138 deletions
|
|
@ -687,23 +687,6 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) {}
|
||||||
|
|
||||||
#pragma mark -
|
#pragma mark -
|
||||||
|
|
||||||
static NSString * const kAFMultipartTemporaryFileDirectoryName = @"com.alamofire.uploads";
|
|
||||||
|
|
||||||
static NSString * AFMultipartTemporaryFileDirectoryPath() {
|
|
||||||
static NSString *multipartTemporaryFilePath = nil;
|
|
||||||
static dispatch_once_t onceToken;
|
|
||||||
dispatch_once(&onceToken, ^{
|
|
||||||
multipartTemporaryFilePath = [[NSTemporaryDirectory() stringByAppendingPathComponent:kAFMultipartTemporaryFileDirectoryName] copy];
|
|
||||||
|
|
||||||
NSError *error = nil;
|
|
||||||
if(![[NSFileManager defaultManager] createDirectoryAtPath:multipartTemporaryFilePath withIntermediateDirectories:YES attributes:nil error:&error]) {
|
|
||||||
NSLog(@"Failed to create multipary temporary file directory at %@", multipartTemporaryFilePath);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return multipartTemporaryFilePath;
|
|
||||||
}
|
|
||||||
|
|
||||||
static NSString * const kAFMultipartFormBoundary = @"Boundary+0xAbCdEfGbOuNdArY";
|
static NSString * const kAFMultipartFormBoundary = @"Boundary+0xAbCdEfGbOuNdArY";
|
||||||
|
|
||||||
static NSString * const kAFMultipartFormCRLF = @"\r\n";
|
static NSString * const kAFMultipartFormCRLF = @"\r\n";
|
||||||
|
|
@ -882,7 +865,6 @@ static inline NSString * AFContentTypeForPathExtension(NSString *extension) {
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSMutableURLRequest *)requestByFinalizingMultipartFormData {
|
- (NSMutableURLRequest *)requestByFinalizingMultipartFormData {
|
||||||
// Return the original request if no data has been added
|
|
||||||
if ([self.bodyStream isEmpty]) {
|
if ([self.bodyStream isEmpty]) {
|
||||||
return self.request;
|
return self.request;
|
||||||
}
|
}
|
||||||
|
|
@ -922,77 +904,22 @@ static inline NSString * AFContentTypeForPathExtension(NSString *extension) {
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setInitialAndFinalBoundaries {
|
- (void)setInitialAndFinalBoundaries {
|
||||||
if ([self.HTTPBodyParts count] > 0) { // If there's any HTTP body parts...
|
if ([self.HTTPBodyParts count] > 0) {
|
||||||
// Reset all HTTP body parts boundary settings first.
|
for (AFHTTPBodyPart *bodyPart in self.HTTPBodyParts) {
|
||||||
for (AFHTTPBodyPart *theHTTPBodyPart in self.HTTPBodyParts) {
|
bodyPart.hasInitialBoundary = NO;
|
||||||
theHTTPBodyPart.hasInitialBoundary = NO;
|
bodyPart.hasFinalBoundary = NO;
|
||||||
theHTTPBodyPart.hasFinalBoundary = NO;
|
|
||||||
}
|
}
|
||||||
// Set the first and last object boundary settings.
|
|
||||||
[[self.HTTPBodyParts objectAtIndex:0] setHasInitialBoundary:YES];
|
[[self.HTTPBodyParts objectAtIndex:0] setHasInitialBoundary:YES];
|
||||||
[[self.HTTPBodyParts lastObject] setHasFinalBoundary:YES];
|
[[self.HTTPBodyParts lastObject] setHasFinalBoundary:YES];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - NSStream subclass overrides
|
|
||||||
|
|
||||||
- (void)open {
|
|
||||||
if ([self.HTTPBodyParts count] > 0) {
|
|
||||||
// This call might be redundant but can be a good safety measure
|
|
||||||
// to make sure that the boundary settings are correctly set.
|
|
||||||
// But even so, the length might be screw up if the user try to
|
|
||||||
// mess with the HTTP body part between finalizing the multipart
|
|
||||||
// form data request and here.
|
|
||||||
[self setInitialAndFinalBoundaries];
|
|
||||||
|
|
||||||
self.HTTPBodyPartEnumerator = [self.HTTPBodyParts objectEnumerator];
|
|
||||||
}
|
|
||||||
|
|
||||||
streamStatus = NSStreamStatusOpen;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)close {
|
|
||||||
streamStatus = NSStreamStatusClosed;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop
|
|
||||||
forMode:(NSString *)mode
|
|
||||||
{}
|
|
||||||
|
|
||||||
- (void)removeFromRunLoop:(NSRunLoop *)aRunLoop
|
|
||||||
forMode:(NSString *)mode
|
|
||||||
{}
|
|
||||||
|
|
||||||
- (id)propertyForKey:(NSString *)key {
|
|
||||||
return nil;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (BOOL)setProperty:(id)property forKey:(NSString *)key {
|
|
||||||
return NO;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (NSStreamStatus)streamStatus {
|
|
||||||
return streamStatus;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (NSError *)streamError {
|
|
||||||
return nil;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (BOOL)isEmpty {
|
- (BOOL)isEmpty {
|
||||||
return [self.HTTPBodyParts count] == 0;
|
return [self.HTTPBodyParts count] == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (unsigned long long)contentLength {
|
#pragma mark - NSInputStream
|
||||||
unsigned long long length = 0;
|
|
||||||
for (AFHTTPBodyPart *bodyPart in self.HTTPBodyParts) {
|
|
||||||
length += [bodyPart contentLength];
|
|
||||||
}
|
|
||||||
|
|
||||||
return length;
|
|
||||||
}
|
|
||||||
|
|
||||||
#pragma mark - NSInputStream subclass overrides
|
|
||||||
|
|
||||||
- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)length {
|
- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)length {
|
||||||
if ([self streamStatus] == NSStreamStatusClosed) {
|
if ([self streamStatus] == NSStreamStatusClosed) {
|
||||||
|
|
@ -1014,15 +941,65 @@ static inline NSString * AFContentTypeForPathExtension(NSString *extension) {
|
||||||
return bytesRead;
|
return bytesRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (BOOL)hasBytesAvailable {
|
|
||||||
return [self streamStatus] == NSStreamStatusOpen;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (BOOL)getBuffer:(uint8_t **)buffer length:(NSUInteger *)len {
|
- (BOOL)getBuffer:(uint8_t **)buffer length:(NSUInteger *)len {
|
||||||
return NO;
|
return NO;
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - Undocumented CFReadStream bridged methods
|
- (BOOL)hasBytesAvailable {
|
||||||
|
return [self streamStatus] == NSStreamStatusOpen;
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma mark - NSStream
|
||||||
|
|
||||||
|
- (void)open {
|
||||||
|
if (self.streamStatus == NSStreamStatusOpen) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
streamStatus = NSStreamStatusOpen;
|
||||||
|
|
||||||
|
[self setInitialAndFinalBoundaries];
|
||||||
|
self.HTTPBodyPartEnumerator = [self.HTTPBodyParts objectEnumerator];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)close {
|
||||||
|
streamStatus = NSStreamStatusClosed;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (id)propertyForKey:(NSString *)key {
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (BOOL)setProperty:(id)property forKey:(NSString *)key {
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop
|
||||||
|
forMode:(NSString *)mode
|
||||||
|
{}
|
||||||
|
|
||||||
|
- (void)removeFromRunLoop:(NSRunLoop *)aRunLoop
|
||||||
|
forMode:(NSString *)mode
|
||||||
|
{}
|
||||||
|
|
||||||
|
- (NSStreamStatus)streamStatus {
|
||||||
|
return streamStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSError *)streamError {
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (unsigned long long)contentLength {
|
||||||
|
unsigned long long length = 0;
|
||||||
|
for (AFHTTPBodyPart *bodyPart in self.HTTPBodyParts) {
|
||||||
|
length += [bodyPart contentLength];
|
||||||
|
}
|
||||||
|
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma mark - Undocumented CFReadStream Bridged Methods
|
||||||
|
|
||||||
- (void)_scheduleInCFRunLoop:(CFRunLoopRef)aRunLoop
|
- (void)_scheduleInCFRunLoop:(CFRunLoopRef)aRunLoop
|
||||||
forMode:(CFStringRef)aMode
|
forMode:(CFStringRef)aMode
|
||||||
|
|
@ -1082,62 +1059,16 @@ typedef enum {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (BOOL)transitionToNextPhase {
|
|
||||||
if (![[NSThread currentThread] isMainThread]) {
|
|
||||||
[self performSelectorOnMainThread:@selector(transitionToNextPhase) withObject:nil waitUntilDone:YES];
|
|
||||||
return YES;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (_phase) {
|
|
||||||
case AFEncapsulationBoundaryPhase:
|
|
||||||
_phase = AFHeaderPhase;
|
|
||||||
break;
|
|
||||||
case AFHeaderPhase:
|
|
||||||
[self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
|
|
||||||
[self.inputStream open];
|
|
||||||
_phase = AFBodyPhase;
|
|
||||||
break;
|
|
||||||
case AFBodyPhase:
|
|
||||||
[self.inputStream close];
|
|
||||||
_phase = AFFinalBoundaryPhase;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
_phase = AFEncapsulationBoundaryPhase;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
_phaseReadOffset = 0;
|
|
||||||
|
|
||||||
return YES;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (NSString *)stringForHeaders {
|
- (NSString *)stringForHeaders {
|
||||||
NSMutableString *headerString = [NSMutableString string];
|
NSMutableString *headerString = [NSMutableString string];
|
||||||
for (NSString *field in [self.headers allKeys]) {
|
for (NSString *field in [self.headers allKeys]) {
|
||||||
[headerString appendString:[NSString stringWithFormat:@"%@: %@%@", field, [self.headers valueForKey:field], kAFMultipartFormCRLF]];
|
[headerString appendString:[NSString stringWithFormat:@"%@: %@%@", field, [self.headers valueForKey:field], kAFMultipartFormCRLF]];
|
||||||
}
|
}
|
||||||
// There's a CRLF after the header string.
|
|
||||||
[headerString appendString:kAFMultipartFormCRLF];
|
[headerString appendString:kAFMultipartFormCRLF];
|
||||||
|
|
||||||
return [NSString stringWithString:headerString];
|
return [NSString stringWithString:headerString];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSInteger)readData:(NSData *)data
|
|
||||||
intoBuffer:(uint8_t *)buffer
|
|
||||||
maxLength:(NSUInteger)length
|
|
||||||
{
|
|
||||||
NSRange range = NSMakeRange(_phaseReadOffset, MIN([data length], length));
|
|
||||||
[data getBytes:buffer range:range];
|
|
||||||
|
|
||||||
_phaseReadOffset += range.length;
|
|
||||||
|
|
||||||
if (range.length >= [data length]) {
|
|
||||||
[self transitionToNextPhase];
|
|
||||||
}
|
|
||||||
|
|
||||||
return range.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (unsigned long long)contentLength {
|
- (unsigned long long)contentLength {
|
||||||
unsigned long long length = 0;
|
unsigned long long length = 0;
|
||||||
|
|
||||||
|
|
@ -1155,6 +1086,16 @@ typedef enum {
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (BOOL)hasBytesAvailable {
|
||||||
|
switch (self.inputStream.streamStatus) {
|
||||||
|
case NSStreamStatusAtEnd:
|
||||||
|
case NSStreamStatusClosed:
|
||||||
|
case NSStreamStatusError:
|
||||||
|
return NO;
|
||||||
|
default:
|
||||||
|
return YES;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)length {
|
- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)length {
|
||||||
NSInteger bytesRead = 0;
|
NSInteger bytesRead = 0;
|
||||||
|
|
@ -1187,15 +1128,49 @@ typedef enum {
|
||||||
return bytesRead;
|
return bytesRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (BOOL)hasBytesAvailable {
|
- (NSInteger)readData:(NSData *)data
|
||||||
switch (self.inputStream.streamStatus) {
|
intoBuffer:(uint8_t *)buffer
|
||||||
case NSStreamStatusAtEnd:
|
maxLength:(NSUInteger)length
|
||||||
case NSStreamStatusClosed:
|
{
|
||||||
case NSStreamStatusError:
|
NSRange range = NSMakeRange(_phaseReadOffset, MIN([data length], length));
|
||||||
return NO;
|
[data getBytes:buffer range:range];
|
||||||
default:
|
|
||||||
|
_phaseReadOffset += range.length;
|
||||||
|
|
||||||
|
if (range.length >= [data length]) {
|
||||||
|
[self transitionToNextPhase];
|
||||||
|
}
|
||||||
|
|
||||||
|
return range.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (BOOL)transitionToNextPhase {
|
||||||
|
if (![[NSThread currentThread] isMainThread]) {
|
||||||
|
[self performSelectorOnMainThread:@selector(transitionToNextPhase) withObject:nil waitUntilDone:YES];
|
||||||
return YES;
|
return YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch (_phase) {
|
||||||
|
case AFEncapsulationBoundaryPhase:
|
||||||
|
_phase = AFHeaderPhase;
|
||||||
|
break;
|
||||||
|
case AFHeaderPhase:
|
||||||
|
[self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
|
||||||
|
[self.inputStream open];
|
||||||
|
_phase = AFBodyPhase;
|
||||||
|
break;
|
||||||
|
case AFBodyPhase:
|
||||||
|
[self.inputStream close];
|
||||||
|
_phase = AFFinalBoundaryPhase;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
_phase = AFEncapsulationBoundaryPhase;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
_phaseReadOffset = 0;
|
||||||
|
|
||||||
|
return YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue