preliminary commit of data: support

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@36105 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2013-02-10 12:59:24 +00:00
parent c83facd84d
commit abd50f30b2

View file

@ -283,6 +283,8 @@ static NSLock *pairLock = nil;
@interface _NSHTTPSURLProtocol : _NSHTTPURLProtocol
@end
@interface _NSDataURLProtocol : NSURLProtocol
@end
// Internal data storage
@ -371,6 +373,7 @@ static NSURLProtocol *placeholder = nil;
[self registerClass: [_NSFTPURLProtocol class]];
[self registerClass: [_NSFileURLProtocol class]];
[self registerClass: [_NSAboutURLProtocol class]];
[self registerClass: [_NSDataURLProtocol class]];
}
}
@ -1827,6 +1830,93 @@ static NSURLProtocol *placeholder = nil;
@end
@implementation _NSDataURLProtocol
+ (BOOL) canInitWithRequest: (NSURLRequest*)request
{
return [[[request URL] scheme] isEqualToString: @"data"];
}
+ (NSURLRequest*) canonicalRequestForRequest: (NSURLRequest*)request
{
return request;
}
- (void) startLoading
{
NSURLResponse *r;
NSString *mime = @"text/plain";
NSString *encoding = @"US-ASCII";
NSData *data;
NSString *spec = [[this->request URL] resourceSpecifier];
NSRange comma = [spec rangeOfString:@","];
NSEnumerator *types;
NSString *type;
BOOL base64 = NO;
if (comma.location == NSNotFound)
{
NSDictionary *ui;
NSError *error;
ui = [NSDictionary dictionaryWithObjectsAndKeys:
[this->request URL], @"URL",
[[this->request URL] path], @"path",
nil];
error = [NSError errorWithDomain: @"can't load data"
code: 0
userInfo: ui];
[this->client URLProtocol: self didFailWithError: error];
return;
}
types = [[[spec substringToIndex: comma.location]
componentsSeparatedByString: @";"] objectEnumerator];
while (nil != (type = [types nextObject]))
{
if ([type isEqualToString: @"base64"])
{
base64 = YES;
}
else if ([type hasPrefix: @"charset="])
{
encoding = [type substringFromIndex: 8];
}
else if ([type length] > 0)
{
mime = type;
}
}
spec = [spec substringFromIndex: comma.location + 1];
if (YES == base64)
{
data = [GSMimeDocument decodeBase64:
[spec dataUsingEncoding: NSUTF8StringEncoding]];
}
else
{
data = [[spec stringByReplacingPercentEscapesUsingEncoding:
NSUTF8StringEncoding] dataUsingEncoding: NSUTF8StringEncoding];
}
r = [[NSURLResponse alloc] initWithURL: [this->request URL]
MIMEType: mime
expectedContentLength: [data length]
textEncodingName: encoding];
[this->client URLProtocol: self
didReceiveResponse: r
cacheStoragePolicy: NSURLRequestUseProtocolCachePolicy];
[this->client URLProtocol: self didLoadData: data];
[this->client URLProtocolDidFinishLoading: self];
RELEASE(r);
}
- (void) stopLoading
{
return;
}
@end
@implementation _NSAboutURLProtocol
+ (BOOL) canInitWithRequest: (NSURLRequest*)request