Trying to run a c++ cgi post

Hi,

Below is my code. I can use c++ get for cgi with no problem.  When I try to use a post...below is the error I get on ubuntu 9.04 running apache2.  I just comment the get and uncomment out the post getenv("CONTENT_LENGTH").  Also below is a littple script I use to test the get and post request and response.  So....what could the problem be?  Also below is the apache error log and the brower message.

params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
print "http://localhost/cgi-bin/post.o?%s" % params
f = urllib.urlopen("http://localhost/cgi-bin/post.o?%s" % params)


int main() {


      string query_string;

      //query_string = getenv("CONTENT_LENGTH");
      query_string = getenv("QUERY_STRING");
      cout << "content-type: text/html" << endl << endl;
      cout << "<html><body>test</body></html>" << endl;
      
      return 0;
}



APACHE ERROR ERROR LOG
[Tue Sep 14 17:18:03 2010] [client 127.0.0.1] Premature end of script headers: post.o

WEB BROWSER ERROR
Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.
Apache/2.2.14 (Ubuntu) Server at localhost Port 80
dmontgomAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
jkrConnect With a Mentor Commented:
Well, if your CGI app receives post data, it has to read it - otherwise, the server will assume an error.
0
 
Infinity08Commented:
>> Premature end of script headers: post.o

post.o indicates that you're trying to run an object file.

Make sure to compile the C code to an executable, rename it with a .cgi extension (for clarity), put it in your cgi directory with appropriate permissions (0755 - just like the cgi directory), and then try again.
0
 
dmontgomAuthor Commented:
Here my bash script for compiling and running

Again, if I use get...it works.  For a post ...it does not

#!/usr/bin/bash
g++ cpp_post_test.cpp -o post.o
cp post.o post.cgi
chmod a+x post.cgi
chmod 0755 post.cgi
sudo cp post.cgi /usr/lib/cgi-bin/
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
Infinity08Commented:
>> Again, if I use get...it works.  For a post ...it does not

Could you post the complete/exact code that works, and the complete/exact code that doesn't work ?
0
 
dmontgomAuthor Commented:

GET AND POST QUERY
http://localhost/cgi-bin/post.cgi?eggs=2&bacon=0&spam=1

WORKS - GET

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <string>
#include <time.h>

using namespace std;


int main() {


      string query_string;

      query_string = getenv("QUERY_STRING");
      cout << "content-type: text/html" << endl << endl;
      cout << "<html><body>test</body></html>" << endl;
      return 0;
}




DOES NOT WORK POST

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <string>
#include <time.h>

using namespace std;

int main() {

      string query_string;
      query_string = getenv("CONTENT_LENGTH");
      cout << "content-type: text/html" << endl << endl;
      cout << "<html><body>test</body></html>" << endl;

      return 0;
}

[Tue Sep 14 23:21:40 2010] [error] [client 127.0.0.1] Premature end of script headers: post.cgi
0
 
dmontgomAuthor Commented:
PS

I tried using this code to create a post using python.  I get this error in the Apache Log

service_url = 'http://localhost/cgi-bin/post.cgi'
#xml_request="name=adfadfadfadfadf"
xml_request = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})

headers = {'Content-Type': 'Content-Type: text/html; charset=utf-8','Content-Length': len(xml_request)}
req = urllib2.Request(service_url, xml_request, headers)
xml = urllib2.urlopen(req).read()


[Wed Sep 15 00:08:39 2010] [error] [client 127.0.0.1] (104)Connection reset by peer: ap_content_length_filter: apr_bucket_read() failed


Traceback (most recent call last):
  File "/home//Aptana Studio 3 Workspace/pycpp_post_test/post.py", line 13, in <module>
    xml = urllib2.urlopen(req).read()
  File "/usr/lib/python2.6/socket.py", line 329, in read
    data = self._sock.recv(rbufsize)
  File "/usr/lib/python2.6/httplib.py", line 518, in read
    return self._read_chunked(amt)
  File "/usr/lib/python2.6/httplib.py", line 561, in _read_chunked

0
 
jkrCommented:
Does it work if you use the following?
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <string>
#include <time.h>

using namespace std;

int main() {

      string query_string;
      query_string = getenv("CONTENT_LENGTH");
      cout << "content-type: text/html" << endl << endl;
      cout << "<html><body>test</body></html>" << endl;

      int nLen = atoi(query_string.c_str());

      char* pBuf = new char[nLen + 1];

      cin.read(pBuf,nLen);

      delete[] pBuf;

      return 0;
}

Open in new window

0
 
dmontgomAuthor Commented:
Yes...Great....


So...please explain what when on?  Why did it not work with just with the query_string = getenv("CONTENT_LENGTH"); statement?  Why is the other lines of code required?
0
 
dmontgomAuthor Commented:
LOL....simple enough...

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.