robbiehanson / CocoaHTTPServer

A small, lightweight, embeddable HTTP server for Mac OS X or iOS applications
Other
5.59k stars 1.31k forks source link

someone can help me ? #146

Open design365 opened 8 years ago

design365 commented 8 years ago

i use ccooahttpserver for my ios local http server , but when use UIWebview to load the local page: http://127.0.0.1:50396/3c59dc048e8850243be8079a5c74d079/index.html

it show me the html source code, but not display the page, why? please help

design365 commented 8 years ago

my pod file is : platform :ios, '7.0' pod 'ASIHTTPRequest' pod 'CocoaHTTPServer', '~> 2.3' pod 'RNCryptor', '~> 2.2' pod 'ZipArchive', '~> 1.4.0' pod 'MD5Digest', '~> 0.1.0' pod 'SDWebImage', '~> 3.7.2' pod 'MJRefresh', '~> 1.4.1' pod 'FMDB/SQLCipher' pod 'MGSwipeTableCell', '~> 1.4.0' pod 'VPImageCropper', '~> 0.0.4' pod 'JGProgressHUD', '~> 1.2.3'

design365 commented 8 years ago

it seems that when file encoding is utf-8(with BOM) , the cocoaHttpserver will be not work, someone can fix this bug?

gfox1984 commented 8 years ago

I'm having the same bug. I can confirm that removing the BOM solves the issue, but this is a problem when the content you are displaying comes from a 3rd party...

OPTJoker commented 7 years ago

I solved this problem by remove first 3 bytes of the file:

// filePath + fileName = the full path of the file
// like:
// filePath = /Users/zhanglei/Library/Developer/CoreSimulator/Devices/data/Containers/Data/Application/tmp/localhost/web
// fileName = index.html
// full path = /Users/zhanglei/Library/Developer/CoreSimulator/Devices/data/Containers/Data/Application/tmp/localhost/web/index.html

+ (BOOL)rewriteUTF8file:(NSString *)filePath fileName:(NSString *)fileName{

    NSData *fileData = [NSData dataWithContentsOfFile:[filePath stringByAppendingPathComponent:fileName]];

    char *buff = malloc(sizeof(char) * 3);
    [fileData getBytes:buff length:3];

    char bom16[3] = {0xef,0xbb,0xbf};//"\xef\xbb\xbf";
    char *p = bom16;
    char *q = buff;

    short cnt = 0;
    for (short i=0; i<3; i++) {
        if (*p++ == *q++) {
            cnt++;
        }
    }
    free(buff);

    // Match BOM fmt
    NSData *newData;

    if (cnt == 3) {
        NSLog(@"Matched \\Number!");
        newData = [fileData subdataWithRange:NSMakeRange(3, fileData.length-3)];
        NSError *rewriteErr;
        BOOL res = [newData writeToFile:[filePath stringByAppendingPathComponent:fileName] options:NSDataWritingAtomic error:&rewriteErr];
        if (res) {
            return YES;
        }else{
            NSLog(@"rewrite file Err:%@",rewriteErr);
            return NO;
        }
    }else{
        NSLog(@">>>Not Match BOM");
        return NO;
    }
}