Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Trying to run a c++ cgi post

Posted on 2010-09-14
9
Medium Priority
?
957 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
Vim Reference Guide

Vim is a powerful text editor favored by many sysadmins and developers - here are some commands that you'll want to keep in your back pocket!

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
This article will show, step by step, how to integrate R code into a R Sweave document
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

670 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