Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache standard output stream

Tags:

c++

linux

apache

In my cpp file I am printing some debug messages to std::cout standard output stream. When i use this file and run the executable using Apache server. Where will the debug messages get printed. I don't see them printed in /var/lib/httpd/error_log.

Thanks in advance.

like image 963
PMat Avatar asked Nov 22 '25 01:11

PMat


1 Answers

The only reason you should be using the Apache web server to run a C++ program is if your making a CGI script

Check it out: http://en.wikipedia.org/wiki/Common_Gateway_Interface


The process here is that Apache, the web server, runs your program and uses the output(std::cout) as the page source.

The page source can either be html or plain text. The only problem is the server doesn't know, so you provide it with a little hint at the start of your output. It's called the header.

If your outputting html you must print:

Content-type: text/html

followed by two newlines.

or if you want the web server to interpret the data as plain text, you must initially print

Content-type: text/plain

also followed by two newlines.


For example, a C++ program which should work would look something like this:

#include <iostream>

int main()
{
  //output header, then one newline, then another, paired with a flush.
  std::cout << "Content-type: text/plain\n" << std::endl;
  //now your output
  //calculation...
  std::cout << "Hello World" << std::endl;
  return 0;
}

Any web server parameters can be queried with some pre-set environment variables. Read up on the wikipedia article I linked.


EDIT:

I apologize, The Content-type: text/html and Content-type: text/plain was correct, but I previously said they required a new line. I was mistaken, they require two new lines

If this is the first time you are seeing this post, than don't worry about it.