GCDWebServer - Static files from Documents

2.7k views Asked by At

I'm using GCDWebServer to create a HTTP server on my device. The actual content for the webpage is placed inside the Documents folder of the my App. The content contains a HTML file with a referred css and png file. The problem is that the css and png files are not accessible/usable from the server (I can only see the HTML text content).

The relevant code:

self.server = [[GCDWebServer alloc] init];

NSString *documents = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] absoluteString];
documents = [documents stringByAppendingPathComponent:@"MyWebsite"];

[self.server addGETHandlerForBasePath:@"/" directoryPath:documents indexFilename:nil cacheAge:0 allowRangeRequests:YES];

__weak typeof(self) weakSelf = self;

[_server addDefaultHandlerForMethod:@"GET" requestClass:[GCDWebServerRequest class] processBlock:^GCDWebServerResponse *(GCDWebServerRequest *request) {

        if ([[request path] isEqualToString:@"/status"]) {

            return [GCDWebServerDataResponse responseWithText:@"RUNNING"];
        }

        else if ([[request path] isEqualToString:@"/"]) {

            NSString *filePath = [documents stringByAppendingPathComponent:@"mypage.html"];
            NSString *html = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
            return [GCDWebServerDataResponse responseWithHTML:html];

        }    

    return nil;

}];

[self.server startWithOptions:@{GCDWebServerOption_Port: @(80)} error:&error];

Thanks!

1

There are 1 answers

0
Sebastian On BEST ANSWER

I found a solution:

Adding the handler with server addGETHandlerForBasePath and then server addDefaultHandlerForMethod: seems to be wrong.

I had to do the following changes:

addGETHandlerForBasePath:

// using `absoluteString` is adding "file://" in front of the path. This seems to be wrong. The following code will create a correct path string.
const char *path = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] fileSystemRepresentation];
NSString *documents = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:path length:strlen(path)];

// full path to the HTML file
NSString *htmlFile = [documents stringByAppendingPathComponent:...];

// "directoryPath" has to be the path to folder with the (main) HTML, not the subfolder of the files. I have a separate folder only for the static files. GCDWebServer will search for the files by it's self - recursive.
[self.server addGETHandlerForBasePath:@"/" directoryPath: documents indexFilename:htmlFile cacheAge:0 allowRangeRequests:YES];

add other GET handler:

I have to use the addHandler... method.

[self.server addHandlerForMethod:@"GET" path:@"/status" requestClass:[GCDWebServerRequest class] processBlock:^GCDWebServerResponse *(GCDWebServerRequest *request) {

    // ... same implementation as in the question
}];