首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GCDWebServer -来自文档的静态文件

GCDWebServer -来自文档的静态文件
EN

Stack Overflow用户
提问于 2015-06-22 15:34:19
回答 1查看 2.5K关注 0票数 2

我正在使用GCDWebServer在我的设备上创建一个HTTP服务器。网页的实际内容放置在my的Documents文件夹中。内容包含一个HTML文件,其中包含引用的css和png文件。问题是css和png文件无法从服务器访问/使用(我只能看到HTML文本内容)。

有关守则:

代码语言:javascript
复制
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];

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-22 19:19:34

我找到了一个解决办法:

使用server addGETHandlerForBasePath添加处理程序,然后添加server addDefaultHandlerForMethod:似乎是错误的。

我必须做以下更改:

addGETHandlerForBasePath:

代码语言:javascript
复制
// 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];

添加其他GET处理程序:

我必须使用addHandler...方法。

代码语言:javascript
复制
[self.server addHandlerForMethod:@"GET" path:@"/status" requestClass:[GCDWebServerRequest class] processBlock:^GCDWebServerResponse *(GCDWebServerRequest *request) {

    // ... same implementation as in the question
}];
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30984084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档