Solved

CGI and C/C++

Posted on 1997-04-20
1
215 Views
Last Modified: 2013-12-25
Hello everybody

I´m having trouble in "receiving" a form transmitted via POST method. My
program goes something like this:

#include "iostream.h"
#include "stdlib.h"
#include "stdio.h"
#include "conio.h"
#include "dos.h"
....
char *query_string;
if (getenv("REQUEST_METHOD")=="GET") query_string=getenv("QUERY_STRING");
else cin>>query_string;
....

I am using Visual C++ and Personal Web Server.
As POSTed forms are sent through stdin, I thought this should do the trick,
but it doesn´t. As I submit the form, my exe runs and hangs up my computer!
I think it is expecting data from stdin, but it should already be there!
I have no trouble with GET method, but sometimes I need to use POST.

Can anyone help me?

Thanks in advance
Ricardo Peres
Coimbra, Portugal
0
Comment
Question by:rjperes
1 Comment
 
LVL 5

Accepted Solution

by:
julio011597 earned 50 total points
ID: 1827928
Well, i don't know C++, but it seems that your program cannot work.

> if(getenv("REQUEST_METHOD") == "GET")
does not do what you need.
Include <string.h> for strcmp() and go like this:
if(!strcmp(getenv("REQUEST_METHOD"), "GET"))

> query_string = getenv("QUERY_STRING");
this is ok, but beware that if QUERY_STRING is empty, then query_string will be a NULL pointer; you should handle this case.

> else cin >> query_string;
Does cin allocate memory for query_string??
If not, as i suppose, you should do it yourself.
So the code should look something like this (a bit redundant, for clearness:):
--//--
char *temp;
size_t content_length; /* check size_t on your os: i.e. check type needed for malloc() parameter */
...
else {
  if(!(temp = getenv("CONTENT_LENGTH")) || !(content_lenght = (size_t)atol(temp)) {
    return NO_QUERY_STRING; /* or such */
  }
  else {
    if(!(query_string = malloc(content_length + 1))) {
      return MALLOC_FAILED; /* or such, again */
    }
    else cin >> query_string;
  }
}
...
--//--

Last, shouldn't be:
#include <iostream.h>
#include <stdlib.h>
... and so on??

HTH, julio
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to count occurrences of each item in an array.

786 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