Skip to content

Commit

Permalink
Replace CocoaHTTPServer with GCDWebServer - https://github.com/swissp…
Browse files Browse the repository at this point in the history
  • Loading branch information
dsward2 committed Mar 24, 2020
1 parent 10de7a8 commit d052404
Show file tree
Hide file tree
Showing 9 changed files with 2,145 additions and 377 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
@@ -1,3 +0,0 @@
[submodule "CocoaLumberjack"]
path = CocoaLumberjack
url = https://git@github.com/CocoaLumberjack/CocoaLumberjack.git
2,280 changes: 2,068 additions & 212 deletions macSVG.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion macSVG/MacSVG-Info.plist
Expand Up @@ -105,7 +105,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>2819</string>
<string>2843</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.graphics-design</string>
<key>LSMinimumSystemVersion</key>
Expand Down
14 changes: 0 additions & 14 deletions macSVG/WebServer Classes/SVGHTTPConnection.h

This file was deleted.

75 changes: 0 additions & 75 deletions macSVG/WebServer Classes/SVGHTTPConnection.m

This file was deleted.

13 changes: 0 additions & 13 deletions macSVG/WebServer Classes/SVGHTTPResponse.h

This file was deleted.

21 changes: 0 additions & 21 deletions macSVG/WebServer Classes/SVGHTTPResponse.m

This file was deleted.

4 changes: 2 additions & 2 deletions macSVG/WebServer Classes/WebServerController.h
Expand Up @@ -10,13 +10,13 @@

#import <Cocoa/Cocoa.h>

@class HTTPServer;
@class GCDWebServer;

@interface WebServerController : NSObject
{
}

@property(strong) HTTPServer * httpServer;
@property(strong) GCDWebServer * httpServer;
@property(assign) NSUInteger webServerPort;

- (void)startProcessing;
Expand Down
110 changes: 74 additions & 36 deletions macSVG/WebServer Classes/WebServerController.m
Expand Up @@ -13,17 +13,9 @@
#import "MacSVGDocumentWindowController.h"
#import "MacSVGDocument.h"

// CocoaHTTPServer
#import "HTTPServer.h"
#import <CocoaLumberjack/DDLog.h>
#import <CocoaLumberjack/DDTTYLogger.h>
#import "HTTPDynamicFileResponse.h"
#import "HTTPLogging.h"
#import "SVGHTTPConnection.h"


// Log levels: off, error, warn, info, verbose
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
#import "GCDWebServer.h"
#import "GCDWebServerDataRequest.h"
#import "GCDWebServerDataResponse.h"


@interface WebServerController (PrivateMethods)
Expand Down Expand Up @@ -71,45 +63,44 @@ - (void)startProcessing
httpServerPort = 8080;
}
self.webServerPort = httpServerPort;

// For CocoaHTTPServer
// Configure our logging framework.
// To keep things simple and fast, we're just going to log to the Xcode console.
[DDLog addLogger:[DDTTYLogger sharedInstance]];

// Initalize our http server
self.httpServer = [[HTTPServer alloc] init];

// Tell server to use our custom SVGHTTPConnection class.
[self.httpServer setConnectionClass:[SVGHTTPConnection class]];

// Tell the server to broadcast its presence via Bonjour.
// This allows browsers such as Safari to automatically discover our service.
//[httpServer setType:@"_http._tcp."];

// Normally there's no need to run our server on any specific port.
// Technologies like Bonjour allow clients to dynamically discover the server's port at runtime.
// However, for easy testing you may want force a certain port so you can just hit the refresh button.
[self.httpServer setPort:self.webServerPort];


// Serve files from our embedded Web folder
NSString * webPath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:@"Web"];
//DDLogVerbose(@"Setting document root: %@", webPath);
//NSString * webPath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:@"Web"];

[self.httpServer setDocumentRoot:webPath];
//[self.httpServer setDocumentRoot:webPath];

// Start the server (and check for problems)
self.httpServer = [[GCDWebServer alloc] init];

NSError *error;
BOOL success = [self.httpServer start:&error];

if(!success)
__weak WebServerController * weakSelf = self;

// Add a handler to respond to GET requests on any URL
[self.httpServer addDefaultHandlerForMethod:@"GET"
requestClass:[GCDWebServerDataRequest class]
processBlock:^GCDWebServerDataResponse *(GCDWebServerRequest* request) {

return [GCDWebServerDataResponse responseWithData:[weakSelf svgData] contentType:@"image/svg+xml"];

}];

BOOL success = [self.httpServer startWithPort:self.webServerPort bonjourName:NULL];

if(success == YES)
{
NSLog(@"Web server running at %@", self.httpServer.serverURL);
}
else
{
DDLogError(@"Error starting HTTP Server: %@", error);
NSLog(@"Error starting HTTP Server");
}
}
}


//==================================================================================
// stopProcessing
//==================================================================================
Expand All @@ -121,4 +112,51 @@ - (void)stopProcessing
self.httpServer = NULL;
}

//==================================================================================
// svgData
//==================================================================================

- (NSData *)svgData
{
// FIXME TODO response header needs Content-Type image/svg+xml

MacSVGDocument * macSVGDocument = [self findFrontmostMacSVGDocument];

NSXMLDocument * svgXmlDocument = macSVGDocument.svgXmlDocument;

NSData * svgData = svgXmlDocument.XMLData;

return svgData;
}


//==================================================================================
// findFrontmostMacSVGDocument
//==================================================================================

- (MacSVGDocument *)findFrontmostMacSVGDocument
{
__block MacSVGDocument * result = NULL;

dispatch_sync(dispatch_get_main_queue(), ^{
NSArray *orderedDocuments = NSApp.orderedDocuments;
NSUInteger documentCount = orderedDocuments.count;
int i;
for (i = 0; i < documentCount; i++)
{
if (result == NULL)
{
NSDocument *aDocument = (NSDocument *)orderedDocuments[i];
if ([aDocument isMemberOfClass:[MacSVGDocument class]] == YES)
{
result = (MacSVGDocument *)aDocument;
}
}
}
});

return result;
}


@end

0 comments on commit d052404

Please sign in to comment.