Solved

CGI and C/C++

Posted on 1997-04-20
1
208 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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

763 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

9 Experts available now in Live!

Get 1:1 Help Now