Solved

Simple CGI (written in VC++) does not work. Why?

Posted on 1998-11-22
3
216 Views
Last Modified: 2013-12-25
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?
 
0
Comment
Question by:misha051797
  • 2
3 Comments
 
LVL 8

Expert Comment

by:MaDdUCK
ID: 1829733
It is not wise to use cin like this for POST input. Use fread() instead. Here you can specify the maximum number of characters to be read. Your method will work, but this method is cleaner.

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!

0
 

Author Comment

by:misha051797
ID: 1829734
Thank you. Great advise...
0
 
LVL 8

Accepted Solution

by:
MaDdUCK earned 50 total points
ID: 1829735
does this mean it worked?
if not, then please reject my answer again. Otherwise I will also be willing to walk you through the process...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction:   Welcome to my first article ever. To begin with, the reason I write this article.  I participated in a question on Experts Exchange about the start command in Windows and there were some discussion about the usage. The discussio…
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…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

863 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

24 Experts available now in Live!

Get 1:1 Help Now