misha051797
asked on
Simple CGI (written in VC++) does not work. Why?
I am writing a simple CGI script to print the variables passed through the form. My form has only on submit button and i use POST method while executing the script. The script is simple:
#include <iostream.h>
#include <string.h>
void main(){
cout << "Content-type:TEXT/HTML\n\ n";
char* streamSize = getenv("CONTENT_LENGTH");
long dataSize = atol(streamSize); // convert size from char*
char* buffer = new char[dataSize]; // get some room
cin >> buffer;
cout << buffer; // just print variables
delete [] buffer; //let's delete buffer
cout << "this is it";
}
Simple. is not it? Even more it works as a CGI script on the web (IIS 3.0) only when i commented the
delete [] buffer; line. Otherwise it does not.
Is there a specific reason why?
#include <iostream.h>
#include <string.h>
void main(){
cout << "Content-type:TEXT/HTML\n\
char* streamSize = getenv("CONTENT_LENGTH");
long dataSize = atol(streamSize); // convert size from char*
char* buffer = new char[dataSize]; // get some room
cin >> buffer;
cout << buffer; // just print variables
delete [] buffer; //let's delete buffer
cout << "this is it";
}
Simple. is not it? Even more it works as a CGI script on the web (IIS 3.0) only when i commented the
delete [] buffer; line. Otherwise it does not.
Is there a specific reason why?
ASKER
Thank you. Great advise...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
here is the code that I use:
==
char *data = new char[len + 1];
int new_len = fread(data, 1, len, stdin);
data[new_len]='\0';
std::string result = data;
delete[] data;
return result;
==
it may not be as pretty, but it works fine and cannot ever overflow or seg fault.
also, change your new char line to new char[dataSize + 1] ! (you need the \0 character.
Can you run the prog from the command line? simply set the dataSize to say 10, and then enter a 10 byte string using the keyboard. See what happens.
keep me posted!