Solved

Trying to run a c++ cgi post

Posted on 2010-09-14
9
944 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 33670408
>> 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
ID: 33670737
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
ID: 33671171
>> 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dmontgom
ID: 33673450

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
 

Author Comment

by:dmontgom
ID: 33674046
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
ID: 33674169
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
ID: 33674429
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
ID: 33674529
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
ID: 33674835
LOL....simple enough...

Thanks
0

Featured Post

Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

617 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