You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2013/02/12 04:14:33 UTC
[13/13] ios commit: Use correct MIME-type for asset-library responses.
Updated Branches:
refs/heads/master c3df2af2b -> 6e19115dd
Use correct MIME-type for asset-library responses.
Also refactored response sending to reduce copy/paste.
Project: http://git-wip-us.apache.org/repos/asf/cordova-ios/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-ios/commit/6e19115d
Tree: http://git-wip-us.apache.org/repos/asf/cordova-ios/tree/6e19115d
Diff: http://git-wip-us.apache.org/repos/asf/cordova-ios/diff/6e19115d
Branch: refs/heads/master
Commit: 6e19115dda861abdaf62a3d660c58c75b32bb03d
Parents: 4fd9731
Author: Andrew Grieve <ag...@chromium.org>
Authored: Mon Feb 11 22:12:33 2013 -0500
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Mon Feb 11 22:12:33 2013 -0500
----------------------------------------------------------------------
CordovaLib/Classes/CDVURLProtocol.m | 64 ++++++++++++-----------------
1 files changed, 27 insertions(+), 37 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/6e19115d/CordovaLib/Classes/CDVURLProtocol.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVURLProtocol.m b/CordovaLib/Classes/CDVURLProtocol.m
index 257c903..1959c77 100644
--- a/CordovaLib/Classes/CDVURLProtocol.m
+++ b/CordovaLib/Classes/CDVURLProtocol.m
@@ -20,6 +20,7 @@
#import <AssetsLibrary/ALAsset.h>
#import <AssetsLibrary/ALAssetRepresentation.h>
#import <AssetsLibrary/ALAssetsLibrary.h>
+#import <MobileCoreServices/MobileCoreServices.h>
#import "CDVURLProtocol.h"
#import "CDVCommandQueue.h"
#import "CDVWhitelist.h"
@@ -27,8 +28,6 @@
#import "CDVFile.h"
@interface CDVHTTPURLResponse : NSHTTPURLResponse
-- (id)initWithUnauthorizedURL:(NSURL*)url;
-- (id)initWithBlankResponse:(NSURL*)url;
@property (nonatomic) NSInteger statusCode;
@end
@@ -157,34 +156,26 @@ static CDVViewController *viewControllerForRequest(NSURLRequest* request)
NSURL* url = [[self request] URL];
if ([[url path] isEqualToString:@"/!gap_exec"]) {
- CDVHTTPURLResponse* response = [[CDVHTTPURLResponse alloc] initWithBlankResponse:url];
- [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
- [[self client] URLProtocolDidFinishLoading:self];
+ [self sendResponseWithResponseCode:200 data:nil mimeType:nil];
return;
} else if ([[url absoluteString] hasPrefix:kCDVAssetsLibraryPrefix]) {
ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset * asset) {
if (asset) {
// We have the asset! Get the data and send it along.
ALAssetRepresentation* assetRepresentation = [asset defaultRepresentation];
+ NSString* MIMEType = (__bridge_transfer NSString*)UTTypeCopyPreferredTagWithClass ((__bridge CFStringRef)[assetRepresentation UTI], kUTTagClassMIMEType);
Byte* buffer = (Byte*)malloc ([assetRepresentation size]);
NSUInteger bufferSize = [assetRepresentation getBytes:buffer fromOffset:0.0 length:[assetRepresentation size] error:nil];
NSData* data = [NSData dataWithBytesNoCopy:buffer length:bufferSize freeWhenDone:YES];
- CDVHTTPURLResponse* response = [[CDVHTTPURLResponse alloc] initWithBlankResponse:url];
- [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
- [[self client] URLProtocol:self didLoadData:data];
- [[self client] URLProtocolDidFinishLoading:self];
+ [self sendResponseWithResponseCode:200 data:data mimeType:MIMEType];
} else {
- // We couldn't find the asset. Send an error.
- CDVHTTPURLResponse* response = [[CDVHTTPURLResponse alloc] initWithUnauthorizedURL:url];
- [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
- [[self client] URLProtocolDidFinishLoading:self];
+ // Retrieving the asset failed for some reason. Send an error.
+ [self sendResponseWithResponseCode:404 data:nil mimeType:nil];
}
};
ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError * error) {
// Retrieving the asset failed for some reason. Send an error.
- CDVHTTPURLResponse* response = [[CDVHTTPURLResponse alloc] initWithUnauthorizedURL:url];
- [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
- [[self client] URLProtocolDidFinishLoading:self];
+ [self sendResponseWithResponseCode:401 data:nil mimeType:nil];
};
ALAssetsLibrary* assetsLibrary = [[ALAssetsLibrary alloc] init];
@@ -193,10 +184,7 @@ static CDVViewController *viewControllerForRequest(NSURLRequest* request)
}
NSString* body = [gWhitelist errorStringForURL:url];
- CDVHTTPURLResponse* response = [[CDVHTTPURLResponse alloc] initWithUnauthorizedURL:url];
- [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
- [[self client] URLProtocol:self didLoadData:[body dataUsingEncoding:NSASCIIStringEncoding]];
- [[self client] URLProtocolDidFinishLoading:self];
+ [self sendResponseWithResponseCode:401 data:[body dataUsingEncoding:NSASCIIStringEncoding] mimeType:nil];
}
- (void)stopLoading
@@ -209,29 +197,31 @@ static CDVViewController *viewControllerForRequest(NSURLRequest* request)
return NO;
}
-@end
-
-@implementation CDVHTTPURLResponse
-@synthesize statusCode;
-
-- (id)initWithUnauthorizedURL:(NSURL*)url
+- (void)sendResponseWithResponseCode:(NSInteger)statusCode data:(NSData*)data mimeType:(NSString*)mimeType
{
- self = [super initWithURL:url MIMEType:@"text/plain" expectedContentLength:-1 textEncodingName:@"UTF-8"];
- if (self) {
- self.statusCode = 401;
+ if (mimeType == nil) {
+ mimeType = @"text/plain";
}
- return self;
-}
+ NSString* encodingName = [@"text/plain" isEqualToString:mimeType] ? @"UTF-8" : nil;
+ CDVHTTPURLResponse* response =
+ [[CDVHTTPURLResponse alloc] initWithURL:[[self request] URL]
+ MIMEType:mimeType
+ expectedContentLength:[data length]
+ textEncodingName:encodingName];
+ response.statusCode = statusCode;
-- (id)initWithBlankResponse:(NSURL*)url
-{
- self = [super initWithURL:url MIMEType:@"text/plain" expectedContentLength:-1 textEncodingName:@"UTF-8"];
- if (self) {
- self.statusCode = 200;
+ [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
+ if (data != nil) {
+ [[self client] URLProtocol:self didLoadData:data];
}
- return self;
+ [[self client] URLProtocolDidFinishLoading:self];
}
+@end
+
+@implementation CDVHTTPURLResponse
+@synthesize statusCode;
+
- (NSDictionary*)allHeaderFields
{
return nil;