Solved

Trying to run a c++ cgi post

Posted on 2010-09-14
9
902 Views
Last Modified: 2013-12-20
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
0
Comment
Question by:dmontgom
  • 5
  • 2
  • 2
9 Comments
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> 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
 

Author Comment

by:dmontgom
Comment Utility
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
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> 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
 

Author Comment

by:dmontgom
Comment Utility

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:dmontgom
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
 

Author Comment

by:dmontgom
Comment Utility
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
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
Comment Utility
Well, if your CGI app receives post data, it has to read it - otherwise, the server will assume an error.
0
 

Author Closing Comment

by:dmontgom
Comment Utility
LOL....simple enough...

Thanks
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now