Solved

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

Posted on 1998-11-22
3
215 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Ever wondered how to display how many visitors you have online. In this tutorial I will show you an easy but effective way to display the number of online visitors in WhizBase. In this article I assume you have read my previous articles and know …
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 modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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 …

706 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

20 Experts available now in Live!

Get 1:1 Help Now