Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CGI Timeout

Status
Not open for further replies.

davewelsh

Programmer
Jun 26, 2001
71
CA
I didn't see a CGI forum so I'm sorry if this is the wrong place.

I have some C code that sends a file from the web server to the user.

Code:
/* change stdout to binary */
if (_setmode( _fileno( stdout ), _O_BINARY ) == -1)
{
  printf("Content-type: text/plain\n\nUnable to set file mode.\n");
  return -1;
}
	
/* open file for reading */
if ((in = fopen(filename, "rb")) == NULL)
{
  printf("Content-type: text/plain\n\nUnable to open file.\n");
  return -1;
}

/* get length */
fseek(in, 0, SEEK_END);
filesize = ftell(in);
fseek(in, 0, SEEK_SET);

/* write header */
printf("HTTP/1.0 200 OK\n");
printf("Content-type: application/x-pdf\n");
printf("Content-disposition: attachment; filename=%s\n", outputname);
printf("Content-length: %d\n",  (int) filesize);
printf("\n");

/* While there are lines in input file, read them in and output the results */
while (len = fread(buffer, sizeof(char), 100, in))
{
  fwrite(buffer, sizeof(char), len, stdout);
}

fclose(in);	// close the file
	
return 0;

There is more to the program, but this is the important fragment. My problem is that the web host called me today and said that my program times out (or crashes) several times per day. I used the program several times on the website and it completed successfully.

What I think is going on is that this program will not end until after the while loop. And, if a user is on a slow connection, this could take several minutes...

I could be completely wrong and the content gets written to some buffer and the prgram exits right away. (And the user downloads from the buffer.) But then what is the source of my timeout problem?

Any insights would be appreciated.
 
I guess what I'm wondering is if the printf lines are asynchronous or not. If they are synchronous, then how would I avoid the timeout? If they are asynchronous, what is causing the timeout?
 
Hi,

davewelsh,

I belive fread & fwrite are synchronous; write asynchronous code involves more difficult approach ( are you using Windows OS? ).

I belive that is the web that server "times out":

your program hangs (or crashes), and webserver reports a timeout.

I don't know which may be the problem, but I should approach the
debug with this strategy:

1) think what happens when more than 1 user, use your page or file.

2) implement a sort of log file: using stderr ( data will go in the log file of webserver, maby );
or write at web site (if possible) text file reporting each line the time and the step executed.

By another program ( which only you know ), or whit special and hidden parameter in input at the program (POST-ed or GET-ed), you download or simply printout to follow what appens when crash .

Don't forget to use flush function each write on log file:
if your program crashes, if not flushed, you can lose all or part of log file.


Good luck.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top