home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / foxsrch.wiz / server.exe / MYCGI.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-11  |  3.8 KB  |  161 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <windows.h>
  5. #include <time.h>
  6. #include <direct.h>
  7.  
  8. #define BUFFERLEN 1000
  9. #define MaxFileLen 255
  10. #define MaxInsLen 1000
  11.  
  12. void errorform(char *errorstring, char *errortitle);
  13. void cleanup(char *ackFileName, char *datFileName, char *atnFileName);
  14.  
  15. void main(int argc,char *argv[]) {
  16.  
  17. char tmpname[MaxFileLen];
  18. char *datFileName,*ackFileName,*atnFileName;
  19. FILE *datFile,*atnFile,*tempFile,*tFH;
  20. char buffer[BUFFERLEN],*t1,*tempbuffer,*cgiPath;
  21. int cont_len,bytesread;
  22. time_t begintout, chktout;
  23. char **envptr;
  24.  
  25. tmpnam(tmpname);
  26.  
  27. cgiPath="\\temp";
  28. _mkdir(cgiPath);
  29.  
  30. //Turn stdin buffering off
  31. setvbuf(stdin,NULL,_IONBF,0);
  32.  
  33. datFileName=(char *)malloc(sizeof(cgiPath)+sizeof(tmpname)+5);
  34. ackFileName=(char *)malloc(sizeof(cgiPath)+sizeof(tmpname)+5);
  35. atnFileName=(char *)malloc(sizeof(cgiPath)+sizeof(tmpname)+5);
  36.  
  37. // Create the names for the three files.
  38. sprintf(datFileName,"%s\\%sdat\0",cgiPath,tmpname);
  39. sprintf(ackFileName,"%s\\%sack\0",cgiPath,tmpname);
  40. sprintf(atnFileName,"%s\\%satn\0",cgiPath,tmpname);
  41.  
  42. //Create Data File
  43. datFile = fopen(datFileName,"w");
  44. if (datFile == NULL) {
  45.     errorform("Communication with the server is not possible at this time.","Server Inaccessible");
  46.     cleanup(ackFileName,datFileName,atnFileName);
  47.     return;
  48.     }
  49.  
  50.  
  51. //We could check REQUEST_METHOD here, but what would be the point?
  52. //If CONTENT_LENGTH is NULL, we won't read the stdin, otherwise we will.
  53. //This is entirely for POST method requests.
  54. t1 = getenv("CONTENT_LENGTH");
  55. if (t1 != NULL) {
  56.     cont_len = atoi(t1);
  57.  
  58.     tempbuffer=(char *)malloc(sizeof(char)*cont_len);
  59.     if (tempbuffer == NULL)
  60.         {
  61.         cleanup(ackFileName,datFileName,atnFileName);
  62.         return;
  63.         }
  64.  
  65.     bytesread=fread(tempbuffer,sizeof(char),cont_len,stdin);
  66.     if (bytesread!=0)
  67.         fwrite(tempbuffer,sizeof(char),bytesread,datFile);
  68.  
  69.     free(tempbuffer);
  70. }
  71.  
  72. //This part reads all of the environment variables and sends
  73. //them along in the DAT file as well.  All ampersand delimited.
  74. envptr=environ;
  75. while (*envptr != NULL)
  76. {
  77.     fputs("&",datFile);
  78.     fputs(*envptr,datFile);
  79.     envptr++;
  80. }
  81.  
  82. fclose(datFile);
  83.  
  84. //Create Atn File
  85. atnFile = fopen(atnFileName,"w");
  86. if (atnFile == NULL) {
  87.     errorform("Communication with the server is not possible at this time.","Server Inaccessible");
  88.     cleanup(ackFileName,datFileName,atnFileName);
  89.     return;
  90.     }
  91. fclose(atnFile);
  92.  
  93. _flushall();
  94.  
  95.  
  96. // Wait for Server to ACK
  97. time(&begintout);
  98. do {
  99.     tempFile = fopen(ackFileName,"r");
  100.     time(&chktout);
  101.     Sleep(250);
  102. } while ((tempFile == NULL) && (difftime(chktout,begintout) < 60));
  103.  
  104. if (tempFile != NULL)
  105.     fclose(tempFile);
  106. else
  107.     {
  108.     errorform("The server timed out.  It is probably too busy to fulfill your request.","Server Timeout");
  109.     cleanup(ackFileName,datFileName,atnFileName);
  110.     return;
  111.     }
  112.  
  113. // Send the Data file to stdout.
  114. datFile = fopen(datFileName,"r");
  115. if (datFile == NULL) {
  116.     errorform("A server error occurred.  No return data was sent.", "Server Error");
  117.     cleanup(ackFileName,datFileName,atnFileName);
  118.     return;
  119.     }
  120. else {
  121.     while (!feof(datFile))
  122.         {
  123.         if (fgets(buffer,BUFFERLEN,datFile) != NULL)
  124.             printf("%s",buffer);
  125.         } 
  126.     fclose(datFile);
  127.     }
  128.  
  129. // Cleanup all files and free allocated memory.
  130. cleanup(ackFileName,datFileName,atnFileName);
  131.  
  132. }
  133.  
  134. void errorform(char *errorstring, char *errortitle)
  135. {
  136.     printf("Content-Type: text/html\n\n");
  137.     printf("<head><title>%s</title></head>\n",errortitle);
  138.     printf("<body>\n<h1>%s</h1>\n",errortitle);
  139.     printf("%s<hr>\n",errorstring);
  140. }
  141.  
  142. void cleanup(char *ackFileName, char *datFileName, char *atnFileName)
  143. {
  144.     if (ackFileName != NULL)
  145.         {
  146.         remove(ackFileName);
  147.         free(ackFileName);
  148.         }
  149.     if (datFileName != NULL)
  150.         {
  151.         remove(datFileName);
  152.         free(datFileName);
  153.         }
  154.     if (atnFileName != NULL)
  155.         {
  156.         remove(atnFileName);
  157.         free(atnFileName);
  158.         }
  159.  
  160. }
  161.