2014-02-10 1 views
0

Mac에서 간단한 objective-CGI 프로그램을 실행하려고합니다. Apache2를 사용 중이며 OS X Mountain Lion을 실행 중입니다 (문제가있는 경우). 예를 들어 다음과 같은 코드를 가지고 : 나는 위의 코드를 컴파일, 그리고, CGI 디렉토리에 실행 파일을 복사 된 .cgi 확장명으로 이름을 변경하고, 권한을 실행했다Apache CGI로 실행 가능한 간단한 C 실행

#import <Foundation/Foundation.h> 

int main(int argc, const char * argv[]) 
{ 

    @autoreleasepool { 

     // insert code here... 
     NSLog(@"Content-type: text/plain\r\n"); 
     NSLog(@"From Cupertino, CA, we say, 'hello, World!'\r\n"); 

    } 
return 0; 
} 

.

나는 아파치에서 꽤 기본적인 설정을 사용했고, httpd.conf에서 지시어로 Option s ExecCGI을 사용하고, AddHandler cgi-script .cgi을 주석 처리하여 CGI 스크립트를 실행했다. 내가 파이썬 CGI 스크립트를 실행할 때 작동했지만 내 실행 파일 실행시 오류를 반환했습니다 :

90 [Sun Feb 09 20:01:33 2014] [error] [client ::1] 2014-02-09 20:01:33.247 testCGI.cgi[1498:707] Content-type: text/plain\r 
2991 [Sun Feb 09 20:01:33 2014] [error] [client ::1] 2014-02-09 20:01:33.248 testCGI.cgi[1498:707] From Cupertino, CA, we say, 'hello, World!'\r 
2992 [Sun Feb 09 20:01:33 2014] [error] [client ::1] Premature end of script headers: testCGI.cgi 

어떤 아이디어가 왜 Premature end of script headers: testCGI.cgi 무엇입니까를?

미리 감사드립니다.

답변

1

NSLogstderr으로, stdout으로는 쓰기 않기 때문입니다. 이렇게하려면 +fileHandleWithStandardOutputNSFileHandle 인 클래스 메서드를 사용하는 것이 좋습니다. 좋아요 :

#import <Foundation/Foundation.h> 

int main(int argc, const char * argv[]) 
{ 
    @autoreleasepool { 
     NSFileHandle *stdout = [NSFileHandle fileHandleWithStandardOutput]; 
     // don't use \r\n, but \n\n else you got an error "malformed header from script. Bad header=" 
     NSData *data = [@"Content-type: text/html\n\n" dataUsingEncoding:NSASCIIStringEncoding]; 
     [stdout writeData: data]; 

     data = [@"From Cupertino, CA, we say, 'hello, World!'\n\n" dataUsingEncoding:NSASCIIStringEncoding];  
     [stdout writeData: data]; 
    } 
    return 0; 
} 
+0

감사합니다. @Emmanuel. 아마 이것 (다른 것들 중에서)을 읽어야합니다 : https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html – topmulch

+0

@topmulch 환영하고, 네, 문서를 읽는 것은 일반적으로 좋은 생각입니다 :) – Emmanuel

관련 문제