Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Trying to run a c++ cgi post

Posted on 2010-09-14
9
Medium Priority
?
961 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
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
Technology Partners: 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 1000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
This article will show, step by step, how to integrate R code into a R Sweave document
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

885 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