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 2014/11/22 11:11:18 UTC

cordova-plugins git commit: Rolled in GCDWebServer+LocalhostOnlyBaseHandler Obj-C category into main class, so it will use the common code for all the handlers.

Repository: cordova-plugins
Updated Branches:
  refs/heads/master 91fe9ae85 -> 4e006c4e0


Rolled in GCDWebServer+LocalhostOnlyBaseHandler Obj-C category into main class, so it will use the common code for all the handlers.


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

Branch: refs/heads/master
Commit: 4e006c4e08d9244b19373fd4efd57dd7b111f15f
Parents: 91fe9ae
Author: Shazron Abdullah <sh...@apache.org>
Authored: Sat Nov 22 02:11:17 2014 -0800
Committer: Shazron Abdullah <sh...@apache.org>
Committed: Sat Nov 22 02:11:17 2014 -0800

----------------------------------------------------------------------
 local-webserver/plugin.xml                      |   3 -
 local-webserver/src/ios/CDVLocalWebServer.m     |  56 ++++++++--
 .../ios/GCDWebServer+LocalhostOnlyBaseHandler.h |  26 -----
 .../ios/GCDWebServer+LocalhostOnlyBaseHandler.m | 102 -------------------
 4 files changed, 48 insertions(+), 139 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugins/blob/4e006c4e/local-webserver/plugin.xml
----------------------------------------------------------------------
diff --git a/local-webserver/plugin.xml b/local-webserver/plugin.xml
index c4b0862..a7eac33 100644
--- a/local-webserver/plugin.xml
+++ b/local-webserver/plugin.xml
@@ -51,9 +51,6 @@
 	    <header-file src="src/ios/CDVLocalFileSystem+NativeURL.h" />
 	    <source-file src="src/ios/CDVLocalFileSystem+NativeURL.m" />
 	    
-	    <header-file src="src/ios/GCDWebServer+LocalhostOnlyBaseHandler.h" />
-	    <source-file src="src/ios/GCDWebServer+LocalhostOnlyBaseHandler.m" />
-        
 	    <header-file src="src/ios/GCDWebServer/GCDWebServer/Core/GCDWebServer.h" />
 	    <header-file src="src/ios/GCDWebServer/GCDWebServer/Core/GCDWebServerConnection.h" />
 	    <header-file src="src/ios/GCDWebServer/GCDWebServer/Core/GCDWebServerFunctions.h" />

http://git-wip-us.apache.org/repos/asf/cordova-plugins/blob/4e006c4e/local-webserver/src/ios/CDVLocalWebServer.m
----------------------------------------------------------------------
diff --git a/local-webserver/src/ios/CDVLocalWebServer.m b/local-webserver/src/ios/CDVLocalWebServer.m
index 0024e6b..024a6f0 100644
--- a/local-webserver/src/ios/CDVLocalWebServer.m
+++ b/local-webserver/src/ios/CDVLocalWebServer.m
@@ -19,20 +19,23 @@
 
 #import "CDVLocalWebServer.h"
 #import "GCDWebServerPrivate.h"
-#import "GCDWebServer+LocalhostOnlyBaseHandler.h"
 #import <Cordova/CDVViewController.h>
 #import "CDVLocalFileSystem+NativeURL.h"
 #import "CDVAssetLibraryFileSystem+NativeURL.h"
 #import <AssetsLibrary/AssetsLibrary.h>
 #import <MobileCoreServices/MobileCoreServices.h>
 
+@interface GCDWebServer()
+- (GCDWebServerResponse*)_responseWithContentsOfDirectory:(NSString*)path;
+@end
+
 @implementation CDVLocalWebServer
 
 - (void) pluginInitialize {
 
     BOOL useLocalWebServer = NO;
     NSString* indexPage = @"index.html";
-    NSString* subPath = @"www";
+    NSString* appBasePath = @"www";
     NSUInteger port = 80;
 
     // check the content tag src
@@ -57,18 +60,16 @@
     if (useLocalWebServer) {
 		// Create server
         self.server = [[GCDWebServer alloc] init];
-        NSString* path = [self.commandDelegate pathForResource:indexPage];
 		NSString* authToken = [NSString stringWithFormat:@"cdvToken=%@", [[NSProcessInfo processInfo] globallyUniqueString]];
-        NSString* appPath = [NSString stringWithFormat:@"/%@/", subPath];
-		[self.server addLocalhostOnlyGETHandlerForBasePath:appPath directoryPath:[path stringByDeletingLastPathComponent] indexFilename:indexPage cacheAge:0 allowRangeRequests:YES authToken:authToken];
+        
+        [self addAppFileSystemHandler:authToken basePath:[NSString stringWithFormat:@"/%@/", appBasePath] indexPage:indexPage];
+        [self addFileSystemHandlers:authToken];
         
         [self.server startWithPort:port bonjourName:nil];
         [GCDWebServer setLogLevel:kGCDWebServerLoggingLevel_Error];
         
-        [self addFileSystemHandlers: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, subPath, indexPage, authToken];
+		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);
@@ -142,6 +143,45 @@
     [self.server addHandlerWithMatchBlock:matchBlock asyncProcessBlock:asyncProcessBlock];
 }
 
+- (void) addAppFileSystemHandler:(NSString*)authToken basePath:(NSString*)basePath indexPage:(NSString*)indexPage
+{
+    BOOL allowRangeRequests = YES;
+    
+    NSString* directoryPath = [[self.commandDelegate pathForResource:indexPage] stringByDeletingLastPathComponent];
+;
+    
+    GCDWebServerAsyncProcessBlock processRequestBlock = ^void (GCDWebServerRequest* request, GCDWebServerCompletionBlock complete) {
+        
+        NSString* filePath = [directoryPath stringByAppendingPathComponent:[request.path substringFromIndex:basePath.length]];
+        NSString* fileType = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:NULL] fileType];
+        GCDWebServerResponse* response = nil;
+
+        if (fileType) {
+            if ([fileType isEqualToString:NSFileTypeDirectory]) {
+                if (indexPage) {
+                    NSString* indexPath = [filePath stringByAppendingPathComponent:indexPage];
+                    NSString* indexType = [[[NSFileManager defaultManager] attributesOfItemAtPath:indexPath error:NULL] fileType];
+                    if ([indexType isEqualToString:NSFileTypeRegular]) {
+                        complete([GCDWebServerFileResponse responseWithFile:indexPath]);
+                    }
+                }
+                response = [self.server _responseWithContentsOfDirectory:filePath];
+            } else if ([fileType isEqualToString:NSFileTypeRegular]) {
+                if (allowRangeRequests) {
+                    response = [GCDWebServerFileResponse responseWithFile:filePath byteRange:request.byteRange];
+                    [response setValue:@"bytes" forAdditionalHeader:@"Accept-Ranges"];
+                } else {
+                    response = [GCDWebServerFileResponse responseWithFile:filePath];
+                }
+            }
+        }
+        
+        complete(response);
+    };
+    
+    [self addFileSystemHandler:processRequestBlock basePath:basePath authToken:authToken cacheAge:0];
+}
+
 - (void) addLocalFileSystemHandler:(NSString*)authToken
 {
     NSString* basePath = @"/local-filesystem/";

http://git-wip-us.apache.org/repos/asf/cordova-plugins/blob/4e006c4e/local-webserver/src/ios/GCDWebServer+LocalhostOnlyBaseHandler.h
----------------------------------------------------------------------
diff --git a/local-webserver/src/ios/GCDWebServer+LocalhostOnlyBaseHandler.h b/local-webserver/src/ios/GCDWebServer+LocalhostOnlyBaseHandler.h
deleted file mode 100644
index b1b14ef..0000000
--- a/local-webserver/src/ios/GCDWebServer+LocalhostOnlyBaseHandler.h
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements.  See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership.  The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License.  You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied.  See the License for the
- specific language governing permissions and limitations
- under the License.
- */
-
-#import "GCDWebServer.h"
-
-@interface GCDWebServer (LocalhostOnlyBaseHandler)
-
-- (void)addLocalhostOnlyGETHandlerForBasePath:(NSString*)basePath directoryPath:(NSString*)directoryPath indexFilename:(NSString*)indexFilename cacheAge:(NSUInteger)cacheAge allowRangeRequests:(BOOL)allowRangeRequests authToken:(NSString*)authToken;
-
-@end

http://git-wip-us.apache.org/repos/asf/cordova-plugins/blob/4e006c4e/local-webserver/src/ios/GCDWebServer+LocalhostOnlyBaseHandler.m
----------------------------------------------------------------------
diff --git a/local-webserver/src/ios/GCDWebServer+LocalhostOnlyBaseHandler.m b/local-webserver/src/ios/GCDWebServer+LocalhostOnlyBaseHandler.m
deleted file mode 100644
index 1137a54..0000000
--- a/local-webserver/src/ios/GCDWebServer+LocalhostOnlyBaseHandler.m
+++ /dev/null
@@ -1,102 +0,0 @@
-/*
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements.  See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership.  The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License.  You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied.  See the License for the
- specific language governing permissions and limitations
- under the License.
- */
-
-#import "GCDWebServer+LocalhostOnlyBaseHandler.h"
-#import "GCDWebServerPrivate.h"
-
-@interface GCDWebServer()
-- (GCDWebServerResponse*)_responseWithContentsOfDirectory:(NSString*)path;
-@end
-
-@implementation GCDWebServer (LocalhostOnlyBaseHandler)
-
-NSString* _authTokenKV = nil;
-
-- (void)addLocalhostOnlyGETHandlerForBasePath:(NSString*)basePath directoryPath:(NSString*)directoryPath indexFilename:(NSString*)indexFilename cacheAge:(NSUInteger)cacheAge allowRangeRequests:(BOOL)allowRangeRequests authToken:(NSString *)authToken {
-	if ([basePath hasPrefix:@"/"] && [basePath hasSuffix:@"/"]) {
-		_authTokenKV = authToken;
-		GCDWebServer* __unsafe_unretained server = self;
-		[self addHandlerWithMatchBlock:^GCDWebServerRequest *(NSString* requestMethod, NSURL* requestURL, NSDictionary* requestHeaders, NSString* urlPath, NSDictionary* urlQuery) {
-			
-			if (![requestMethod isEqualToString:@"GET"]) {
-				return nil;
-			}
-			if (![urlPath hasPrefix:basePath]) {
-				return nil;
-			}
-			return [[GCDWebServerRequest alloc] initWithMethod:requestMethod url:requestURL headers:requestHeaders path:urlPath query:urlQuery];
-			
-		} processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
-			
-			//check if it is a request from localhost
-			NSString *host = [request.headers objectForKey:@"Host"];
-			if (host==nil || [host hasPrefix:@"localhost"] == NO ) {
-				return [GCDWebServerErrorResponse responseWithClientError:kGCDWebServerHTTPStatusCode_Forbidden message:@"FORBIDDEN"];
-			}
-			
-			//check if the querystring or the cookie has the token
-			BOOL hasToken = (request.URL.query && [request.URL.query containsString:_authTokenKV]);
-			NSString *cookie = [request.headers objectForKey:@"Cookie"];
-			BOOL hasCookie = (cookie && [cookie containsString:_authTokenKV]);
-			if (!hasToken && !hasCookie) {
-				return [GCDWebServerErrorResponse responseWithClientError:kGCDWebServerHTTPStatusCode_Forbidden message:@"FORBIDDEN"];
-			}
-			
-			GCDWebServerResponse* response = nil;
-			NSString* filePath = [directoryPath stringByAppendingPathComponent:[request.path substringFromIndex:basePath.length]];
-			NSString* fileType = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:NULL] fileType];
-			if (fileType) {
-				if ([fileType isEqualToString:NSFileTypeDirectory]) {
-					if (indexFilename) {
-						NSString* indexPath = [filePath stringByAppendingPathComponent:indexFilename];
-						NSString* indexType = [[[NSFileManager defaultManager] attributesOfItemAtPath:indexPath error:NULL] fileType];
-						if ([indexType isEqualToString:NSFileTypeRegular]) {
-							return [GCDWebServerFileResponse responseWithFile:indexPath];
-						}
-					}
-					response = [server _responseWithContentsOfDirectory:filePath];
-				} else if ([fileType isEqualToString:NSFileTypeRegular]) {
-					if (allowRangeRequests) {
-						response = [GCDWebServerFileResponse responseWithFile:filePath byteRange:request.byteRange];
-						[response setValue:@"bytes" forAdditionalHeader:@"Accept-Ranges"];
-					} else {
-						response = [GCDWebServerFileResponse responseWithFile:filePath];
-					}
-				}
-			}
-			if (response) {
-				response.cacheControlMaxAge = cacheAge;
-			} else {
-				response = [GCDWebServerResponse responseWithStatusCode:kGCDWebServerHTTPStatusCode_NotFound];
-			}
-			
-			if (hasToken && !hasCookie) {
-				//set cookie
-				[response setValue:_authTokenKV forAdditionalHeader:@"Set-Cookie"];
-			}
-			
-			return response;
-			
-		}];
-	} else {
-		GWS_DNOT_REACHED();
-	}
-}
-
-@end


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