我无法弄清楚为什么CGI脚本的输出不稳定,尽管输入在所有运行中都是相同的。
我有这个CGI脚本[在C中],它生成一个html
页面,其中有一个表格,可以再现文件系统(Ubuntu)的树/层次结构视图。它在arm-platform上运行并且是迷你的(用lighthttpd开发。所以脚本的o / p是一个充满目录名和文件名的表。我的问题是它在运行时成功列出了该表中的文件系统(文件和文件夹列表),比如说现在并且在运行10分钟左右后不显示带有文件和文件夹的表,保持输入相同(文件系统)和文件仍然相同)[浏览器只显示错误消息Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data.
或者是空白页。]
可能是什么问题?我在服务器上运行脚本并验证输出是有效的HTML页面,文件和文件夹的路径是有效的,它们都存在。但是相同的脚本有时会成功地显示表格,其他明智的是我上面提到的那些错误。我怀疑这可能与许可问题有关,但如果是这样,它偶尔会如何显示呢?我已将我正在处理的所有文件和文件夹的权限更改为已满。但仍然没有帮助!! :(
脚本如下......
#include <stdio.h>
#include <dirent.h>
#include <string.h>
int main()
{
int dir_count = 0;
int file_count = 0;
int dir_index=0;
int file_index=0;
int inner_loop=0;
int len=0;
int i=0;
DIR * dirp;
DIR * dirp2;
DIR * dirp3;
char realbase[35]="/home";
char final_report_path[30];
char base[20];
char source[20];
char patient_id[15];
char patient_name[15];
int j=0;
struct dirent * entry;
char dir_name[500][20];
char* file_name[500];
dirp = opendir(realbase);
if(dirp)
while ((entry = readdir(dirp)) != NULL)
{
if (entry->d_type == DT_DIR)
{ /* If the entry is a regular file */
if(entry->d_name[0]!='.' && entry->d_name[1]!='.')
{
dir_count++;
strcpy(dir_name[dir_index++],entry->d_name);
}
}
}
closedir(dirp);
printf("Content-type: text/html\n\n");
printf("<html>\n");
printf("<body>\n");
printf("<h1>Blah Blah!!</h1>\n");
printf("\n");
printf(" \n");
printf("Click on the links to view Reports\n");
printf(" \n");
printf("<table border=\"3\" bordercolor=\"#c86260\" bgcolor=\"#ffffcc\" width=\"50%\" cellspacing=\"5\" cellpadding=\"3\">\n");
printf("<tr><td><b>Patient Name</b></td><td><b>Patient ID</b></td><td><b>Reports</b></td></tr>\n");
for(i=0;i<dir_count;i++)
{
strncpy(source, "", sizeof(source));
strncpy(base, "", sizeof(base));
strncpy(patient_name, "", sizeof(patient_name));
strncpy(patient_id, "", sizeof(patient_id));
strcpy(source,dir_name[i]);
if(source[0]!='.')
{
strcpy(base,"/home");
for(len=0;len<sizeof(source);len++)
{
if(source[len]!='_')
patient_name[len]=source[len];
else
{
len++;
break;
}
}
for(inner_loop=0;len<sizeof(source);len++,inner_loop++)
{
if(source[len]!='\0')
patient_id[inner_loop]=source[len];
else
{
break;
}
}
printf("<tr><td valign=\"center\">%s</td>",patient_name);
printf("<td valign=\"center\">%s</td>",patient_id);
strcat(base,source);
strcat(base,"/Reports/");
dirp2 = opendir(base);
if(dirp2)
while ((entry = readdir(dirp2)) != NULL)
{
if (entry->d_type == DT_REG)
{ /* If the entry is a regular file */
if(entry->d_name[0]!='.')
{
if(file_count==0)
{
printf("<td>");
}
if(file_count!=0)
{
printf("<br>");
}
file_count++;
file_name[file_index++]=entry->d_name;
printf("<a href=\"/home/%s/Reports/%s\">",dir_name[i],file_name[file_index-1]);
printf("%s",file_name[file_index-1]);
printf("</a>");
}
}
}
if(file_count>0)
printf("</td>");
printf("</tr>\n");
file_count=0;
file_index=0;
closedir(dirp2);
}
}
printf("</table>");
printf("</body>\n");
printf("</html>\n");
return 0;
}