Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Allow EJBindingHttpRequest to access local URLs #52

Closed
wants to merge 1 commit into from

2 participants

@jamesnvc

There is probably a more performant way of doing this, but this seemed the most uniform interface. Simply detect if the URL requested was relative, and if so create a file URL instead of an HTTP URL.

@phoboslab phoboslab closed this in 45e55d4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 4, 2012
  1. @jamesnvc
This page is out of date. Refresh to see the latest.
Showing with 16 additions and 4 deletions.
  1. +16 −4 Classes/Ejecta/EJUtils/EJBindingHttpRequest.m
View
20 Classes/Ejecta/EJUtils/EJBindingHttpRequest.m
@@ -91,7 +91,14 @@ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLRespon
state = kEJHttpRequestStateHeadersReceived;
[response release];
- response = (NSHTTPURLResponse *)[responsep retain];
+ if ([responsep respondsToSelector:@selector(statusCode)]) {
+ response = (NSHTTPURLResponse*)[responsep retain];
+ } else {
+ response = [[NSHTTPURLResponse alloc] initWithURL:responsep.URL
+ statusCode:200
+ HTTPVersion:@"1.1"
+ headerFields:[NSDictionary dictionary]];
+ }
[self triggerEvent:@"progress" argc:0 argv:NULL];
[self triggerEvent:@"readystatechange" argc:0 argv:NULL];
}
@@ -177,8 +184,11 @@ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
if( !method || !url ) { return NULL; }
[self clearConnection];
-
- NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
+ NSURL *remoteUrl = [NSURL URLWithString:url];
+ if (![remoteUrl host]) { // No host => local file
+ remoteUrl = [NSURL fileURLWithPath:[[EJApp instance] pathForResource:url]];
+ }
+ NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:remoteUrl];
[request setHTTPMethod:method];
for( NSString * header in requestHeaders ) {
@@ -201,7 +211,9 @@ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
if( async ) {
state = kEJHttpRequestStateLoading;
- connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
+ connection = [[NSURLConnection alloc] initWithRequest:request
+ delegate:self
+ startImmediately:YES];
}
else {
NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
Something went wrong with that request. Please try again.