D CGI Hang with fgets

Thomas Kuehne thomas-dloop at kuehne.cn
Mon Nov 27 22:31:07 PST 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Morgan McDermott schrieb am 2006-11-27:
> Hi there!
>   I'm trying to make a D CGI script, and one of the essential things it 
> should do is to get POST data. My attempt below in D hangs at fgets()... 
> I've made it output to a file so that it may be tested on a webserver 
> (where I've been testing it). When the script has POST data to get, it 
> performs exactly the same as it does in the console (hangs at fgets()).
>
> I've been using a guide to C and CGI as my reference here:
> http://www.cs.tut.fi/~jkorpela/forms/cgic.html
>
><code>
> //Imports
> import std.stdio; //writefln()
> import std.string; //toString()
> import std.date : getUTCtime; // time + benchmarking
> import std.conv; //toInt()
> import std.stream; //testing only
> 	
> alias char[] string; //ease of use
>
> void writeNewLine(string line, string filename) {
> 	std.file.append(filename, line~"\n");
> }
>
> extern (C)
> 	{
> 		char* getPOSTData(uint len = 2048){
> 			char *prawData;
>
> 			printf("hang?");
> 			writeNewLine("Hanging point entered \n", "baz.txt");
>
> 			prawData = fgets(prawData, len+1, stdin);
>
> 			writeNewLine("Hanging point exited \n", "baz.txt");
> 			printf("Not hanging.. =-)");
>
> 			return prawData;
> 		}
> 	}
> 	
> extern (C) char* getenv(char*);
>
> void main() {
> 	string introduction = "New test at " ~ toString(getUTCtime());
> 	writeNewLine(introduction, "baz.txt");
> 	
> 	char *pdataLength= getenv("CONTENT_LENGTH");
> 	string scontentLength = toString(pdataLength);
> 	//Ensure no conversion error when no post data is passed
> 		if(scontentLength == ""){
> 			scontentLength = "0";
> 		}
> 	
> 	uint contentLength = toInt(scontentLength);
> 	
> 	char *ppostData = getPOSTData();
> 	writefln("Post Data: %s", toString(ppostData));
> 	writeNewLine("Post Data: " ~ toString(ppostData), "baz.txt");
> }
></code>
>
> Any ideas?

Change

# char *prawData;

to

# char *prawData = (new char[len]).ptr;

And send intput to stdin e.g.:
#
# programm < some_file
#

Thomas


-----BEGIN PGP SIGNATURE-----

iD8DBQFFa+H9LK5blCcjpWoRAo0sAKCk1D2u61K+G0r3N5iLc6LAtXzhvQCgjp5q
gh0GeTpX+kJmy+w8tJ6LDVU=
=LJup
-----END PGP SIGNATURE-----



More information about the Digitalmars-d-learn mailing list