Ben Copsey

Lots of fixes for last commit

... ... @@ -53,10 +53,7 @@
if (!fileData) {
fileData = [[NSMutableDictionary alloc] init];
}
NSMutableDictionary *file = [[[NSMutableDictionary alloc] init] autorelease];
[file setObject:data forKey:@"data"];
[file setObject:@"file" forKey:@"filename"];
[fileData setValue:file forKey:key];
[fileData setObject:data forKey:key];
[self setRequestMethod:@"POST"];
}
... ... @@ -97,10 +94,18 @@
e = [fileData keyEnumerator];
i=0;
while (key = [e nextObject]) {
NSString *filePath = [fileData objectForKey:key];
[self appendPostData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n",key,[filePath lastPathComponent]] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *fileName = @"file";
id file = [fileData objectForKey:key];
if ([file isKindOfClass:[NSString class]]) {
fileName = (NSString *)file;
}
[self appendPostData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n",key,fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[self appendPostData:contentTypeHeader];
[self appendPostDataFromFile:filePath];
if ([file isKindOfClass:[NSString class]]) {
[self appendPostDataFromFile:fileName];
} else {
[self appendPostData:file];
}
i++;
// Only add the boundary if this is not the last item in the post body
if (i != [fileData count]) {
... ...
... ... @@ -391,4 +391,5 @@ typedef enum _ASINetworkErrorType {
@property (retain) NSInputStream *postBodyReadStream;
@property (assign) BOOL shouldStreamPostDataFromDisk;
@property (assign) BOOL didCreateTemporaryPostDataFile;
@end
... ...
... ... @@ -213,18 +213,19 @@ static NSError *ASIUnableToCreateRequestError;
[self setupPostBody];
NSInputStream *stream = [[[NSInputStream alloc] initWithFileAtPath:file] autorelease];
[stream open];
NSMutableData *d;
while ([stream hasBytesAvailable]) {
d = [[NSMutableData alloc] initWithLength:256*1024];
d = [NSMutableData dataWithLength:256*1024];
int bytesRead = [stream read:[d mutableBytes] maxLength:256*1024];
if (bytesRead == 0) {
break;
}
[d setLength:bytesRead];
if ([self shouldStreamPostDataFromDisk]) {
[[self postBodyWriteStream] write:[d mutableBytes] maxLength:bytesRead];
} else {
NSLog(@"foo");
[[self postBody] appendData:[NSData dataWithBytes:[d mutableBytes] length:bytesRead]];
}
[d release];
}
[stream close];
}
... ... @@ -273,6 +274,7 @@ static NSError *ASIUnableToCreateRequestError;
}
}
#pragma mark request logic
// Create the request
... ... @@ -289,6 +291,10 @@ static NSError *ASIUnableToCreateRequestError;
return;
}
if (!haveBuiltPostBody) {
[self buildPostBody];
}
// Create a new HTTP request.
request = CFHTTPMessageCreateRequest(kCFAllocatorDefault, (CFStringRef)requestMethod, (CFURLRef)url, kCFHTTPVersion1_1);
if (!request) {
... ... @@ -335,11 +341,6 @@ static NSError *ASIUnableToCreateRequestError;
}
if (!haveBuiltPostBody) {
[self buildPostBody];
}
// Accept a compressed response
if ([self allowCompressedResponse]) {
[self addRequestHeader:@"Accept-Encoding" value:@"gzip"];
... ... @@ -545,21 +546,26 @@ static NSError *ASIUnableToCreateRequestError;
- (void)removeTemporaryDownloadFile
{
//Remove the temporary file
if (temporaryFileDownloadPath) {
NSError *removeError = nil;
[[NSFileManager defaultManager] removeItemAtPath:temporaryFileDownloadPath error:&removeError];
if (removeError) {
[self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to delete file at %@ with error: %@",temporaryFileDownloadPath,removeError],NSLocalizedDescriptionKey,removeError,NSUnderlyingErrorKey,nil]]];
}
[self setTemporaryFileDownloadPath:nil];
}
}
- (void)removePostDataFile
{
if (postBodyFilePath) {
NSError *removeError = nil;
[[NSFileManager defaultManager] removeItemAtPath:postBodyFilePath error:&removeError];
if (removeError) {
[self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to delete file at %@ with error: %@",postBodyFilePath,removeError],NSLocalizedDescriptionKey,removeError,NSUnderlyingErrorKey,nil]]];
}
[self setPostBodyFilePath:nil];
}
}
... ... @@ -819,7 +825,7 @@ static NSError *ASIUnableToCreateRequestError;
[invocation setSelector:selector];
[invocation setArgument:&progress atIndex:2];
// If we're running in the main thread, update the progress straight away. Otherwise, it's not that urgent
[invocation performSelectorOnMainThread:@selector(invokeWithTarget:) withObject:indicator waitUntilDone:[NSThread isMainThread]];
}
... ... @@ -838,8 +844,6 @@ static NSError *ASIUnableToCreateRequestError;
}
}
// Subclasses can override this method to perform error handling in the same thread
// If not overidden, it will call the didFailSelector on the delegate (by default requestFailed:)`
- (void)failWithError:(NSError *)theError
... ... @@ -1534,7 +1538,6 @@ static NSError *ASIUnableToCreateRequestError;
return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
}
@synthesize username;
@synthesize password;
@synthesize domain;
... ...
... ... @@ -42,6 +42,10 @@
- (void)dealloc
{
//We need to clear the delegate on any requests that haven't got around to cleaning up yet, as otherwise they'll try to let us know if something goes wrong, and we'll be long gone by then
for (ASIHTTPRequest *request in [self operations]) {
[request setDelegate:nil];
}
[super dealloc];
}
... ...
... ... @@ -13,7 +13,7 @@
- (void)testPostWithFileUpload
{
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/post"];
NSURL *url = [NSURL URLWithString:@"http://asi/ASIHTTPRequest/tests/post"];
//Create a 32kb file
unsigned int size = 1024*32;
... ... @@ -33,8 +33,11 @@
[request setPostValue:d forKey:@"post_var2"];
[request setPostValue:v forKey:@"post_var3"];
[request setFile:path forKey:@"file"];
[request setPostBodyFilePath:@"/Users/ben/Desktop/111.txt"];
[request start];
NSLog([request responseString]);
NSLog([NSString stringWithFormat:@"post_var: %@\r\npost_var2: %@\r\npost_var3: %@\r\nfile_name: %@\r\nfile_size: %hu",@"foo",d,v,@"bigfile",size]);
BOOL success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"post_var: %@\r\npost_var2: %@\r\npost_var3: %@\r\nfile_name: %@\r\nfile_size: %hu",@"foo",d,v,@"bigfile",size]]);
GHAssertTrue(success,@"Failed to upload the correct data (using local file)");
... ... @@ -46,6 +49,8 @@
[request setData:data forKey:@"file"];
[request start];
NSLog([request responseString]);
NSLog([NSString stringWithFormat:@"post_var: %@\r\npost_var2: %@\r\npost_var3: %@\r\nfile_name: %@\r\nfile_size: %hu",@"foo",d,v,@"bigfile",size]);
success = ([[request responseString] isEqualToString:[NSString stringWithFormat:@"post_var: %@\r\npost_var2: %@\r\npost_var3: %@\r\nfile_name: %@\r\nfile_size: %hu",@"foo",d,v,@"file",size]]);
GHAssertTrue(success,@"Failed to upload the correct data (using NSData)");
}
... ...
... ... @@ -13,7 +13,7 @@
@implementation ASIHTTPRequestTests
/*
- (void)testBasicDownload
{
... ... @@ -165,7 +165,7 @@
[request start];
NSString *tempPath = [request temporaryFileDownloadPath];
GHAssertNotNil(tempPath,@"Failed to download file to temporary location");
GHAssertNil(tempPath,@"Failed to clean up temporary download file");
//BOOL success = (![[NSFileManager defaultManager] fileExistsAtPath:tempPath]);
//GHAssertTrue(success,@"Failed to remove file from temporary location");
... ... @@ -188,7 +188,7 @@
GHAssertTrue(success,@"Failed to properly increment download progress %f != 1.0",progress);
}
*/
- (void)setProgress:(float)newProgress;
... ... @@ -201,7 +201,7 @@
{
progress = 0;
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ignore"]] autorelease];
[request setPostBody:[NSMutableData dataWithLength:1024*32]];
[request setPostBody:(NSMutableData *)[@"This is the request body" dataUsingEncoding:NSUTF8StringEncoding]];
[request setUploadProgressDelegate:self];
[request start];
... ... @@ -211,9 +211,11 @@
- (void)testPostBodyStreamedFromDisk
{
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ignore"];
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/print_request_body"];
NSString *requestBody = @"This is the request body";
NSString *requestContentPath = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"testfile.txt"];
[[NSMutableData dataWithLength:1024*32] writeToFile:requestContentPath atomically:NO];
[[requestBody dataUsingEncoding:NSUTF8StringEncoding] writeToFile:requestContentPath atomically:NO];
// Test using a user-specified file as the request body (useful for PUT)
progress = 0;
... ... @@ -222,36 +224,32 @@
[request setShouldStreamPostDataFromDisk:YES];
[request setUploadProgressDelegate:self];
[request setPostBodyFilePath:requestContentPath];
//[request start];
//Wait for 1 second
//[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
[request start];
BOOL success = (progress > 0.95);
//GHAssertTrue(success,@"Failed to properly increment upload progress %f != 1.0",progress);
GHAssertTrue(success,@"Failed to properly increment upload progress %f != 1.0",progress);
success = [[request responseString] isEqualToString:requestBody];
GHAssertTrue(success,@"Failed upload the correct request body");
// Test building a request body by appending data
progress = 0;
request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setShouldStreamPostDataFromDisk:YES];
[request setRequestMethod:@"PUT"];
[request setUploadProgressDelegate:self];
NSData *d = [@"Below this will be the file I am posting:\r\n" dataUsingEncoding:NSUTF8StringEncoding];
[request appendPostData:[NSData dataWithBytes:[d bytes] length:[d length]]];
[request appendPostDataFromFile:requestContentPath];
[request start];
//Wait for 1 second
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
success = (progress > 0.95);
GHAssertTrue(success,@"Failed to properly increment upload progress %f != 1.0",progress);
success = [[request responseString] isEqualToString:requestBody];
GHAssertTrue(success,@"Failed upload the correct request body");
}
/*
- (void)testCookies
... ... @@ -571,9 +569,8 @@
success = ([newPartialContent isEqualToString:@"This is the content we ought to be getting if we start from byte 95."]);
GHAssertTrue(success,@"Failed to append the correct data to the end of the file?");
success = success = (progress > 0.95);
success = (progress > 0.95);
GHAssertTrue(success,@"Failed to correctly display increment progress for a partial download");
}
*/
@end
... ...
... ... @@ -91,9 +91,9 @@
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ignore"];
int fileSizes[4] = {16,64,128,512};
int fileSizes[3] = {16,64,257};
int i;
for (i=0; i<4; i++) {
for (i=0; i<3; i++) {
NSData *data = [[[NSMutableData alloc] initWithLength:fileSizes[i]*1024] autorelease];
NSString *path = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"file%hi",i]];
[data writeToFile:path atomically:NO];
... ... @@ -117,7 +117,7 @@
[networkQueue cancelAllOperations];
[networkQueue setShowAccurateProgress:YES];
for (i=0; i<4; i++) {
for (i=0; i<3; i++) {
NSData *data = [[[NSMutableData alloc] initWithLength:fileSizes[i]*1024] autorelease];
NSString *path = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"file%hi",i]];
[data writeToFile:path atomically:NO];
... ... @@ -135,12 +135,8 @@
success = (progress > 0.95);
GHAssertTrue(success,@"Failed to increment progress properly");
[networkQueue release];
}
... ...