views:

190

answers:

3

I'm running /usr/bin/perl or /usr/bin/php via an NSTask and want to retrieve the HTTP headers of the program. I've properly formatted the environment (Perl requires env vars to be prefixed with HTTP_), but neither of the task are returning anything other than raw output. I've been through the documentation on NSTask and the man pages for php and perl, and haven't seen any way to force the output of HTTP headers.

Any ideas?

EDIT: I realize this is an ancient question, but I just rediscovered it and realized I should report back. The problem was that I should've been using php-cli, and not php. I had to download and compile php-cli from source to get this to work as I was expecting. Once I did, however, it worked like a charm. :)

+1  A: 

You can't view or change the environment of another process. The exception is that you can change the environment of a child process, but only when you create that process. Thus, you can set up the initial environment of your perl or php subprocess, but you can't read what environment it has later, and you certainly cannot view what environment it had when it exited.

You need to make the Perl or PHP script output its environment in some format, and read that into your Cocoa process through a pipe.

Peter Hosey
Yes, I can set up the initial environment (`-[NSTask setEnvironment:]`), but php and perl (and other CGI programs) must have a way to output HTTP headers, otherwise how could they be used as CGI programs with Apache and other web servers?
Dave DeLong
They write the HTTP headers to their standard output, in HTTP header format.
Peter Hosey
+3  A: 

You might have to prefix your output with the HTTP header, built by you based on the raw output from your perl or php script.

For example, you know that the raw output from a php script would be of content-type: text/html, and you can get the length from the output itself. At least I think that's what I did when implementing a similar solution.

alesplin
A: 

Does the script print the headers when you run it from Terminal? If so, perhaps using -[NSTask setStandard*:] (maybe using NSPipe) can help capture the output. If not, it's possible that the web server injects the headers as it processes the CGI.

Quinn Taylor
The script does not print out headers when run from the Terminal. I think I may have to do what @alesplin suggested, unfortunately...
Dave DeLong