You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by sh...@apache.org on 2015/03/14 00:59:01 UTC

cordova-plugins git commit: CordovaLocalWebserver: add /error/ route.

Repository: cordova-plugins
Updated Branches:
  refs/heads/master 5d5e36da2 -> 0ccc653ae


CordovaLocalWebserver: add /error/ route.


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugins/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugins/commit/0ccc653a
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugins/tree/0ccc653a
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugins/diff/0ccc653a

Branch: refs/heads/master
Commit: 0ccc653aeb7cc4554ff4a9a8fd756955cd90341d
Parents: 5d5e36d
Author: Shazron Abdullah <sh...@apache.org>
Authored: Fri Mar 13 16:59:03 2015 -0700
Committer: Shazron Abdullah <sh...@apache.org>
Committed: Fri Mar 13 16:59:03 2015 -0700

----------------------------------------------------------------------
 local-webserver/src/ios/CDVLocalWebServer.m | 44 +++++++++++++++++++-----
 1 file changed, 36 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugins/blob/0ccc653a/local-webserver/src/ios/CDVLocalWebServer.m
----------------------------------------------------------------------
diff --git a/local-webserver/src/ios/CDVLocalWebServer.m b/local-webserver/src/ios/CDVLocalWebServer.m
index 3413dda..280c279 100644
--- a/local-webserver/src/ios/CDVLocalWebServer.m
+++ b/local-webserver/src/ios/CDVLocalWebServer.m
@@ -27,11 +27,13 @@
 
 #define LOCAL_FILESYSTEM_PATH   @"local-filesystem"
 #define ASSETS_LIBRARY_PATH     @"assets-library"
+#define ERROR_PATH              @"error"
 
 @interface GCDWebServer()
 - (GCDWebServerResponse*)_responseWithContentsOfDirectory:(NSString*)path;
 @end
 
+
 @implementation CDVLocalWebServer
 
 - (void) pluginInitialize {
@@ -60,27 +62,37 @@
         }
     }
 #endif
+
+    NSString* authToken = [NSString stringWithFormat:@"cdvToken=%@", [[NSProcessInfo processInfo] globallyUniqueString]];
+    self.server = [[GCDWebServer alloc] init];
+    [self.server startWithPort:0 bonjourName:nil];
+    [GCDWebServer setLogLevel:kGCDWebServerLoggingLevel_Error];
+
     if (useLocalWebServer) {
-		// Create server
-        self.server = [[GCDWebServer alloc] init];
-		NSString* authToken = [NSString stringWithFormat:@"cdvToken=%@", [[NSProcessInfo processInfo] globallyUniqueString]];
-        
         [self addAppFileSystemHandler:authToken basePath:[NSString stringWithFormat:@"/%@/", appBasePath] indexPage:indexPage];
         
-        [self.server startWithPort:port bonjourName:nil];
-        [GCDWebServer setLogLevel:kGCDWebServerLoggingLevel_Error];
-        
         // add after server is started to get the true port
         [self addFileSystemHandlers:authToken];
+        [self addErrorSystemHandler:authToken];
         
         // Update the startPage (supported in cordova-ios 3.7.0, see https://issues.apache.org/jira/browse/CB-7857)
 		vc.startPage = [NSString stringWithFormat:@"http://localhost:%lu/%@/%@?%@", (unsigned long)self.server.port, appBasePath, indexPage, authToken];
         
     } else {
-        NSLog(@"WARNING: CordovaLocalWebServer: <content> tag src is not http://localhost[:port] (is %@), local web server not started.", vc.startPage);
+        NSString* error = [NSString stringWithFormat:@"WARNING: CordovaLocalWebServer: <content> tag src is not http://localhost[:port] (is %@).", vc.startPage];
+        NSLog(@"%@", error);
+        
+        [self addErrorSystemHandler:authToken];
+
+        vc.startPage = [self createErrorUrl:error authToken:authToken];
     }
 }
 
+- (NSString*) createErrorUrl:(NSString*)error authToken:(NSString*)authToken
+{
+    return [NSString stringWithFormat:@"http://localhost:%lu/%@/%@?%@", (unsigned long)self.server.port, ERROR_PATH, [error stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], authToken];
+}
+
 -  (void) addFileSystemHandlers:(NSString*)authToken
 {
     [self addLocalFileSystemHandler:authToken];
@@ -268,4 +280,20 @@
     [self addFileSystemHandler:processRequestBlock basePath:basePath authToken:authToken cacheAge:0];
 }
 
+- (void) addErrorSystemHandler:(NSString*)authToken
+{
+    NSString* basePath = [NSString stringWithFormat:@"/%@/", ERROR_PATH];
+
+    GCDWebServerAsyncProcessBlock processRequestBlock = ^void (GCDWebServerRequest* request, GCDWebServerCompletionBlock complete) {
+        
+        NSString* errorString = [request.path substringFromIndex:basePath.length]; // error string is from the url path
+        NSString* html = [NSString stringWithFormat:@"<h1 style='margin-top:40px; font-size:6vw'>ERROR</h1><h2 style='font-size:3vw'>%@</h2>", errorString];
+        GCDWebServerResponse* response = [GCDWebServerDataResponse responseWithHTML:html];
+        complete(response);
+    };
+    
+    [self addFileSystemHandler:processRequestBlock basePath:basePath authToken:authToken cacheAge:0];
+}
+
+
 @end
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org