Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix #52 - allow EJBindingHttpRequest to access local files

  • Loading branch information...
commit 45e55d44a3ff01e204d4dee51f8e61de99f29eb7 1 parent a23d026
@phoboslab authored
View
2  Classes/Ejecta/EJUtils/EJBindingHttpRequest.h
@@ -38,7 +38,7 @@ typedef enum {
EJHttpRequestState state;
NSURLConnection * connection;
- NSHTTPURLResponse * response;
+ NSURLResponse * response;
NSMutableData * responseBody;
}
View
51 Classes/Ejecta/EJUtils/EJBindingHttpRequest.m
@@ -31,6 +31,18 @@ - (void)clearRequest {
[password release]; password = NULL;
}
+- (int)getStatusCode {
+ if( !response ) {
+ return 0;
+ }
+ else if( [response isKindOfClass:[NSHTTPURLResponse class]] ) {
+ return ((NSHTTPURLResponse *)response).statusCode;;
+ }
+ else {
+ return 200; // assume everything went well for non-HTTP resources
+ }
+}
+
- (NSString *)getResponseText {
if( !response || !responseBody ) { return NULL; }
@@ -148,11 +160,14 @@ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
}
EJ_BIND_FUNCTION(getAllResponseHeaders, ctx, argc, argv) {
- if( !response ) { return NULL; }
+ if( !response || ![response isKindOfClass:[NSHTTPURLResponse class]] ) {
+ return NULL;
+ }
+ NSHTTPURLResponse * urlResponse = (NSHTTPURLResponse *)response;
NSMutableString * headers = [NSMutableString string];
- for( NSString * key in [response allHeaderFields] ) {
- id value = [[response allHeaderFields] objectForKey:key];
+ for( NSString * key in urlResponse.allHeaderFields ) {
+ id value = [urlResponse.allHeaderFields objectForKey:key];
[headers appendFormat:@"%@: %@\n", key, value];
}
@@ -160,10 +175,13 @@ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
}
EJ_BIND_FUNCTION(getResponseHeader, ctx, argc, argv) {
- if( argc < 1 || !response ) { return NULL; }
+ if( argc < 1 || !response || ![response isKindOfClass:[NSHTTPURLResponse class]] ) {
+ return NULL;
+ }
+ NSHTTPURLResponse * urlResponse = (NSHTTPURLResponse *)response;
NSString * header = JSValueToNSString( ctx, argv[0] );
- NSString * value = [[response allHeaderFields] objectForKey:header];
+ NSString * value = [urlResponse.allHeaderFields objectForKey:header];
return value ? NSStringToJSValue(ctx, value) : NULL;
}
@@ -177,8 +195,13 @@ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
if( !method || !url ) { return NULL; }
[self clearConnection];
-
- NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
+
+ NSURL * requestUrl = [NSURL URLWithString:url];
+ if( !requestUrl.host ) {
+ // No host? Assume we have a local file
+ requestUrl = [NSURL fileURLWithPath:[[EJApp instance] pathForResource:url]];
+ }
+ NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:requestUrl];
[request setHTTPMethod:method];
for( NSString * header in requestHeaders ) {
@@ -209,7 +232,13 @@ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[response retain];
state = kEJHttpRequestStateDone;
- if( response.statusCode == 200 ) {
+ if( [response isKindOfClass:[NSHTTPURLResponse class]] ) {
+ NSHTTPURLResponse * urlResponse = (NSHTTPURLResponse *)response;
+ if( urlResponse.statusCode == 200 ) {
+ [self triggerEvent:@"load" argc:0 argv:NULL];
+ }
+ }
+ else {
[self triggerEvent:@"load" argc:0 argv:NULL];
}
[self triggerEvent:@"loadend" argc:0 argv:NULL];
@@ -245,14 +274,12 @@ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
}
EJ_BIND_GET(status, ctx) {
- return JSValueMakeNumber( ctx, response ? response.statusCode : 0 );
+ return JSValueMakeNumber( ctx, [self getStatusCode] );
}
EJ_BIND_GET(statusText, ctx) {
- if( !response ) { return NULL; }
-
// FIXME: should be "200 OK" instead of just "200"
- NSString * code = [NSString stringWithFormat:@"%d", response.statusCode];
+ NSString * code = [NSString stringWithFormat:@"%d", [self getStatusCode]];
return NSStringToJSValue(ctx, code);
}
Please sign in to comment.
Something went wrong with that request. Please try again.