home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / www / src / WWW / Daemon / Implementation / VMServer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-14  |  4.3 KB  |  156 lines

  1. /*        Handle HTTP request for CERNVM WWW server    VMServer.c
  2. **
  3. **    This code takes a document name and converts it into
  4. **    a VM CMS command to produce the hypertext the caller requires.
  5. **
  6. ** History
  7. **    29 May 91    Separated from daemon program - TBL
  8. **    19 May 91    Retrieve subroutine format used. -TBL
  9. **    21 Dec 92    Made generic version -- all specifics in EXEC file TBL
  10. */
  11. /*    The server calls a program (typically W3SERV EXEC on VM)
  12. **    which must provide the data. It is called with the command line
  13. **    format
  14. **    EXEC W3SERV hostname docid [keywords]
  15. **
  16. **    where
  17. **        hostname     is the IP address of the requesting host
  18. **        docid        is the W3 address of the document
  19. **        keywords    are search terms if an index is being serached
  20. **
  21. **    The W3SERV program produces its results in hypertext. See the W3
  22. **    documentation for the format of the hypertext or look at some examples.
  23. **    This data must be output to the stack.
  24. **
  25. **     IT IS IMPORTANT that the W3SERV program returns the number of
  26. **    lines it has written to the stack, as that is the only way that we
  27. **    can know how many lines to pull back off.
  28. */
  29.  
  30. #define SERVER_COMMMAND        "EXEC W3SERVE"
  31.  
  32. #define BUFFER_SIZE 4096    /* Arbitrary size for efficiency */
  33.  
  34. #include "HTUtils.h"
  35. #include "tcp.h"        /* The whole mess of include files */
  36. #include "HTTCP.h"        /* Some utilities for TCP */
  37.  
  38. extern int WWW_TraceFlag;    /* Control diagnostic output */
  39. extern FILE * logfile;        /* Log file output */
  40. extern char HTClientHost[16];    /* Client name to be output */
  41. extern int HTWriteASCII(int soc, const char * s);    /* In HTDaemon.c */
  42.  
  43.  
  44.  
  45. /*    Strip white space off a string
  46. **
  47. ** On exit,
  48. **    Return value points to first non-white character, or to 0 if none.
  49. **    All trailing white space is overwritten with zero.
  50. */
  51. PRIVATE char * strip(char * s)
  52. {
  53. #define SPACE(c) ((c==' ')||(c=='\t')||(c=='\n')) 
  54.     char * p=s;
  55.     for(p=s;*p;p++);                /* Find end of string */
  56.     for(p--;p>=s;p--) {
  57.         if(SPACE(*p)) *p=0;    /* Zap trailing blanks */
  58.     else break;
  59.     }
  60.     while(SPACE(*s))s++;    /* Strip leading blanks */
  61.     return s;
  62. }
  63.  
  64.  
  65.  
  66. /*    Handle one message
  67. **    ------------------
  68. **
  69. ** On entry,
  70. **    soc        A file descriptor for input and output.
  71. ** On exit,
  72. **    returns        >0    Channel is still open.
  73. **            0    End of file was found, please close file
  74. **            <0    Error found, please close file.
  75. */
  76. /*    Retrieve information
  77. **    --------------------
  78. */
  79. #ifdef __STDC__
  80. int HTRetrieve(const char * arg, const char * keys, int soc)
  81. #else
  82. int HTRetrieve(arg, keys, soc)
  83.     char *arg;
  84.     char *keys;
  85.     int soc;
  86. #endif
  87. {
  88. #define COMMAND_SIZE    255
  89. #define MAX_LINES 10000            /* Maximum number of lines returned */
  90.     char command[COMMAND_SIZE+1];
  91.     char keywords[COMMAND_SIZE+1];
  92.     char argument[COMMAND_SIZE+1];
  93.     char buffer[BUFFER_SIZE];
  94.     char * filename;    /* Pointer to filename or group list*/
  95.     char system_command[COMMAND_SIZE+1];
  96.     int lines;        /* Number of lines returned by EXEC file */
  97.    
  98. /*     Remove host and any punctuation. (Could use HTParse @)
  99. */      
  100.     strcpy(argument, arg);
  101.     filename = argument;
  102.     if (argument[0]=='/') {
  103.     if (argument[1]=='/') {
  104.         filename = strchr(argument+2, '/');    /* Skip //host/ */
  105.         if (!filename) filename=argument+strlen(argument);
  106.     }
  107.     }
  108.  
  109.     if (!*filename) {
  110.         HTWriteASCII(soc,"599 Error: No document ID provided.<P>\r\n");
  111.     return 0;
  112.     }
  113.     
  114.  
  115. /*    Reconstitute keywords for search if any
  116. */
  117.     if (keys) strcpy(keywords, keys);
  118.     else keywords[0] = 0; /* As if just "?" given (historical) */
  119.  
  120.     {
  121.     char *p;
  122.     while((p=strchr(keywords,'+'))!=0) *p=' ';/* Remove +s */
  123.     }
  124.  
  125.     sprintf(system_command,"%s %s %s %s",
  126.             SERVER_COMMAND, HTClientHost, filename, keywords);
  127.  
  128. /*    Execute system command and get the results
  129. */
  130.     lines = system(system_command);    /* Number of stacked lines */
  131.     if (TRACE) printf("Command `%s' returned %i lines.\n",
  132.         system_command, lines);
  133.     if ((lines<=0)||(lines>=20000)) {
  134.     sprintf(buffer,
  135.     "\nSorry, the server executing command\n  %s\n  returned code %d\n", 
  136.             system_command, lines);
  137.     HTWriteASCII(soc, buffer);
  138.     return 0;
  139.     }
  140.     
  141.     /*    Copy the file across: */
  142.     
  143.     for(;lines; lines--){        /* Speed necessary here */
  144.     char *p;
  145.     if (!fgets(buffer, BUFFER_SIZE, stdin))
  146.         sprintf(buffer,
  147.  "*** End of file with %d lines left after command\n%s\n - mail BERND@CERNVM",
  148.         lines, system_command);
  149.     for(p=buffer; *p; p++) *p = TOASCII(*p);
  150.     write(soc, buffer, p-buffer);
  151.     }
  152.     return 0;
  153.     
  154. } /* HTRetrieve() */
  155.  
  156.