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.
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 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.