The following is a simple implementation that I added to my ASIHTTPRequest subclass to support file-based URLs: //. This is by no means a complete and perhaps the absence of some properties that should be set, and it will not call all delegates, but for my purposes it was enough.
- (void)startRequest { if ([url isFileURL]) { // ASIHTTPRequest does not support handling file:// URLs, this is my own simple implementation here if ([self isCancelled]) { return; } [self performSelectorOnMainThread:@selector(requestStarted) withObject:nil waitUntilDone:[NSThread isMainThread]]; NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease]; NSString *filePath = [url path]; BOOL isDirectory = NO; if ([fileManager fileExistsAtPath:filePath isDirectory:&isDirectory] && !isDirectory) { responseStatusCode = 200; [self setRawResponseData:[NSData dataWithContentsOfFile:filePath]]; [self setContentLength:rawResponseData.length]; [self setTotalBytesRead:[self contentLength]]; } else { responseStatusCode = 404; [self setContentLength:0]; [self setError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Cannot open file at path '%@'",filePath],NSLocalizedDescriptionKey,error,NSUnderlyingErrorKey,nil]]]; } complete = YES; downloadComplete = YES; [self requestFinished]; [self markAsFinished]; } else { // let the original implementation deal with all the other URLs [super startRequest]; } }
source share