|
Проверьте правильность написания CGI на C++
#32291108
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
|
|
|
|
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122.
#include "stdlib.h"
#include "stdio.h"
#include <time.h>
#include <sys\timeb.h>
#include <string>
#include <fstream>
void main(void)
{
using namespace std;
string data;
string mode( "mode=" );
string url( "url=" );
int upos=- 1 ;
int imode=- 1 ;
int pos=- 1 ;
ifstream is( "c:\\mode.ini" );
if ( is.fail() )
{
}
else
{
while(!is.eof())
{
getline( is, data );
pos=data.find(mode); // тип операции( в файл либо url)
if(pos!=- 1 )
{
if(data[pos+ 5 ]=='0')
{
imode= 0 ;
}
if(data[pos+ 5 ]=='1')
{
imode= 1 ;
}
if(data[pos+ 5 ]=='2')
{
imode= 2 ;
}
}
upos=data.find(url);
if(upos!=- 1 )
{
data.replace( 0 ,upos+ 4 , "");
url=data;
}
}
}
FILE *hFile;
// получаем содержимое переменной окружения
char *cgi_data;
long content_length=atol(getenv(" CONTENT_LENGTH "));
cgi_data=new char[content_length];
if (cgi_data!=NULL)
fread(cgi_data,content_length,1 ,stdin);
//end
if(imode== 0 )
{
//определяем уникальное имя файла
struct _timeb timebuffer;
char *timeline;
long line;
_ftime( &timebuffer );
line=(timebuffer.time/ 1000 +timebuffer.millitm);
timeline=new char[ 20 ];
sprintf(timeline,"%d ",line);
//end
//пишем в файл
if((hFile=fopen(timeline," w "))!=NULL)
{
fwrite( cgi_data, sizeof( char ), content_length, hFile );
fclose(hFile);
delete timeline;
}
//end
}
if(imode==1 )
{
//пишем в один файл
FILE *hTemp;
while( 1 )
{
if((hTemp=fopen("c:\\test.txt "," r "))==NULL)
{
fclose(hTemp);
if((hTemp=fopen(" c:\\test.txt "," w "))!=NULL)
{
fclose(hTemp);
break;
}
}
}
if((hFile=fopen(" c:\\log.txt "," a "))!=NULL)
{
fwrite( cgi_data, sizeof( char ), content_length, hFile );
fclose(hFile);
}
remove(" c:\\test.txt");
//end
}
if(imode== 2 )
{
//пересылаем на другой Url
fwrite(cgi_data,content_length, 1 ,stdout);
//end
}
delete cgi_data;
}
|
|
|