home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / lora299s.zip / WEB.CPP < prev    next >
C/C++ Source or Header  |  1996-11-06  |  10KB  |  294 lines

  1.  
  2. // ----------------------------------------------------------------------
  3. // LoraBBS Professional Edition - Version 3.00.6
  4. // Copyright (c) 1996 by Marco Maccaferri. All rights reserved.
  5. //
  6. // History:
  7. //    13/06/96 - Initial coding.
  8. // ----------------------------------------------------------------------
  9.  
  10. #include "_ldefs.h"
  11. #include "lserver.h"
  12.  
  13. TWeb::TWeb (void)
  14. {
  15.    Cfg = NULL;
  16.    Log = NULL;
  17.    Tcp = NULL;
  18.    Location[0] = '\0';
  19. }
  20.  
  21. TWeb::~TWeb (void)
  22. {
  23. }
  24.  
  25. static CHAR *WeekDays[] = {
  26.    "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
  27.    "Friday", "Saturday"
  28. };
  29.  
  30. static CHAR *Months[] = {
  31.    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  32.    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  33. };
  34.  
  35. VOID TWeb::GetRequest (VOID)
  36. {
  37.    USHORT len = 0;
  38.    CHAR c, *pszResp = Request;
  39.  
  40.    do {
  41.       if (Tcp->BytesReady () == TRUE) {
  42.          if ((c = (CHAR)Tcp->ReadByte ()) != '\r') {
  43.             if (c != '\n') {
  44.                *pszResp++ = c;
  45.                if (++len >= sizeof (Request) - 1)
  46.                   c = '\r';
  47.             }
  48.          }
  49.       }
  50. #if defined(__OS2__)
  51.       else
  52.          DosSleep (1L);
  53. #elif defined(__NT__)
  54.       else
  55.          Sleep (1L);
  56. #endif
  57.    } while (c != '\r' && Tcp->Carrier () == TRUE);
  58.  
  59.    *pszResp = '\0';
  60. }
  61.  
  62. VOID TWeb::Run (VOID)
  63. {
  64.    CHAR *p, Found;
  65.    struct stat statbuf;
  66.    class TUser *User;
  67.  
  68.    GetRequest ();
  69.    while (Tcp->BytesReady () == TRUE)
  70.       Tcp->ReadByte ();
  71.  
  72.    if ((p = strchr (Request, '\r')) != NULL)
  73.       *p = '\0';
  74.    if ((p = strchr (Request, '\n')) != NULL)
  75.       *p = '\0';
  76.    if (Log != NULL)
  77.       Log->Write (":  %s", Request);
  78.  
  79.    if ((p = strchr (Request, ' ')) != NULL) {
  80.       while (*p == ' ')
  81.          *p++ = '\0';
  82.       if (!stricmp (Request, "GET")) {
  83.          strcpy (Request, p);
  84.          if ((p = strchr (Request, ' ')) != NULL) {
  85.             while (*p == ' ')
  86.                *p++ = '\0';
  87.          }
  88.          if (Request[0] == '\0' || !strcmp (Request, "/") || !strcmp (Request, "\\")) {
  89.             sprintf (Temp, "%slogo.htm", Cfg->TextFiles);
  90.             if (stat (Temp, &statbuf) != 0)
  91.                sprintf (Temp, "%slogo.html", Cfg->TextFiles);
  92.             if (stat (Temp, &statbuf) != 0)
  93.                URLNotFound (Request);
  94.             else
  95.                SendFile (Temp, "text/html");
  96.          }
  97.          else if (Request[0] == '/' || Request[0] == '\\') {
  98.             if (Request[1] == '~') {
  99.                Found = FALSE;
  100.                if ((p = strchr (&Request[2], '/')) != NULL)
  101.                   *p = '\0';
  102.                if ((User = new TUser (Cfg->UserFile)) != NULL) {
  103.                   if (User->First () == TRUE)
  104.                      do {
  105.                         if (!stricmp (User->MailBox, &Request[2])) {
  106.                            if (p != NULL)
  107.                               *p = '/';
  108.                            strcpy (Request, &Request[strlen (User->MailBox) + 2]);
  109.                            if (Request[0] == '\0' || !strcmp (Request, "/") || !strcmp (Request, "\\")) {
  110.                               sprintf (Temp, "%s%s\\index.htm", Cfg->UsersHomePath, User->MailBox);
  111.                               if (Request[0] == '\0')
  112.                                  sprintf (Location, "http://%s/~%s/", Tcp->HostIP, User->MailBox);
  113.                               if (stat (Temp, &statbuf) != 0)
  114.                                  sprintf (Temp, "%s%s\\index.html", Cfg->UsersHomePath, User->MailBox);
  115.                               SendFile (Temp, "text/html");
  116.                            }
  117.                            else if (Request[0] == '/' || Request[0] == '\\') {
  118.                               sprintf (Temp, "%s%s\\%s", Cfg->UsersHomePath, User->MailBox, &Request[1]);
  119.                               if (stat (Temp, &statbuf) != 0)
  120.                                  sprintf (Temp, "%s%s\\%s", Cfg->UsersHomePath, User->MailBox, &Request[1]);
  121.                               SendFile (Temp);
  122.                            }
  123.                            Found = TRUE;
  124.                            break;
  125.                         }
  126.                      } while (User->Next () == TRUE);
  127.                   delete User;
  128.                }
  129.                if (Found == FALSE)
  130.                   URLNotFound (Request);
  131.             }
  132.             else {
  133.                sprintf (Temp, "%s%s", Cfg->TextFiles, &Request[1]);
  134.                if (Temp[strlen (Temp) - 1] != '/' && Temp[strlen (Temp) - 1] != '\\')
  135.                   strcat (Temp, "\\");
  136.                strcat (Temp, "index.htm");
  137.                if (stat (Temp, &statbuf) == 0)
  138.                   SendFile (Temp);
  139.                else {
  140.                   strcat (Temp, "l");
  141.                   if (stat (Temp, &statbuf) == 0)
  142.                      SendFile (Temp);
  143.                   else {
  144.                      sprintf (Temp, "%s%s", Cfg->TextFiles, &Request[1]);
  145.                      if (stat (Temp, &statbuf) != 0)
  146.                         sprintf (Temp, "%s%s", Cfg->TextFiles, &Request[1]);
  147.                      SendFile (Temp);
  148.                   }
  149.                }
  150.             }
  151.          }
  152.          else
  153.             URLNotFound (Request);
  154.       }
  155.    }
  156. }
  157.  
  158. VOID TWeb::SendFile (PSZ File, PSZ MimeType)
  159. {
  160.    int fd;
  161.    CHAR *p;
  162.    USHORT i;
  163.    struct dosdate_t date_t;
  164.    struct dostime_t time_t;
  165.    struct stat statbuf;
  166.    struct tm *ftm;
  167.  
  168.    if (MimeType == NULL) {
  169.       MimeType = "application/octet-stream";
  170.       if ((p = strchr (File, '.')) != NULL) {
  171.          if (!stricmp (p, ".js") || !stricmp (p, ".ls") || !stricmp (p, ".mocha"))
  172.             MimeType = "application/x-javascript";
  173.          else if (!stricmp (p, ".exe") || !stricmp (p, ".bin"))
  174.             MimeType = "application/octet-stream";
  175.          else if (!stricmp (p, ".tar"))
  176.             MimeType = "application/x-tar";
  177.          else if (!stricmp (p, ".zip"))
  178.             MimeType = "application/x-zip-compressed";
  179.          else if (!stricmp (p, ".sit"))
  180.             MimeType = "application/x-stuffit";
  181.          else if (!stricmp (p, ".hqx"))
  182.             MimeType = "application/mac-binhex40";
  183.          else if (!stricmp (p, ".avi"))
  184.             MimeType = "video/x-msvideo";
  185.          else if (!stricmp (p, ".mov") || !stricmp (p, ".qt"))
  186.             MimeType = "video/quicktime";
  187.          else if (!stricmp (p, ".mpeg") || !stricmp (p, ".mpg") || !stricmp (p, ".mpe"))
  188.             MimeType = "video/mpeg";
  189.          else if (!stricmp (p, ".wav"))
  190.             MimeType = "audio/x-wav";
  191.          else if (!stricmp (p, ".aif") || !stricmp (p, ".aiff") || !stricmp (p, ".aifc"))
  192.             MimeType = "audio/x-aiff";
  193.          else if (!stricmp (p, ".au") || !stricmp (p, ".snd"))
  194.             MimeType = "audio/basic";
  195.          else if (!stricmp (p, ".gif"))
  196.             MimeType = "image/gif";
  197.          else if (!stricmp (p, ".jpg") || !stricmp (p, ".jpeg"))
  198.             MimeType = "image/jpeg";
  199.          else if (!stricmp (p, ".htm") || !stricmp (p, ".html"))
  200.             MimeType = "text/html";
  201.          else if (!stricmp (p, ".txt") || !stricmp (p, ".text"))
  202.             MimeType = "text/plain";
  203.       }
  204.    }
  205.  
  206.    while ((p = strchr (File, '/')) != NULL)
  207.       *p = '\\';
  208.  
  209.    if ((fd = open (File, O_RDONLY|O_BINARY)) != -1) {
  210.       if (Location[0] != '\0')
  211.          sprintf (Temp, "HTTP/1.0 302 Found\r\n");
  212.       else
  213.          sprintf (Temp, "HTTP/1.0 200 Ok\r\n");
  214.       Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  215.  
  216.       _dos_getdate (&date_t);
  217.       _dos_gettime (&time_t);
  218.       sprintf (Temp, "Date: %s, %d-%s-%d %d:%02d:%02d GMT\r\n", WeekDays[date_t.dayofweek], date_t.day, Months[date_t.month - 1], date_t.year % 100, time_t.hour, time_t.minute, time_t.second);
  219.       Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  220.  
  221.       sprintf (Temp, "Server: %s/%s\r\n", NAME, VERSION);
  222.       Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  223.  
  224.       sprintf (Temp, "MIME-version: 1.0\r\n");
  225.       Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  226.  
  227.       if (Location[0] != '\0') {
  228.          sprintf (Temp, "Location: %s\r\n", Location);
  229.          Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  230.       }
  231.  
  232.       sprintf (Temp, "Content-type: %s\r\n", MimeType);
  233.       Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  234.  
  235.       fstat (fd, &statbuf);
  236.       ftm = gmtime (&statbuf.st_mtime);
  237.       sprintf (Temp, "Last-modified: %s, %d-%s-%d %d:%02d:%02d GMT\r\n", WeekDays[ftm->tm_wday], ftm->tm_mday, Months[ftm->tm_mon], ftm->tm_year % 100, ftm->tm_hour, ftm->tm_min, ftm->tm_sec);
  238.       Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  239.  
  240.       sprintf (Temp, "Content-length: %lu\r\n", statbuf.st_size);
  241.       Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  242.  
  243.       Tcp->BufferBytes ((UCHAR *)"\r\n", 2);
  244.  
  245.       if (Location[0] == '\0') {
  246.          do {
  247.             i = (USHORT)read (fd, Temp, sizeof (Temp));
  248.             Tcp->BufferBytes ((UCHAR *)Temp, i);
  249.          } while (i == sizeof (Temp));
  250.       }
  251.  
  252.       Tcp->UnbufferBytes ();
  253.  
  254.       close (fd);
  255.    }
  256.    else
  257.       URLNotFound (File);
  258. }
  259.  
  260. VOID TWeb::URLNotFound (PSZ URL)
  261. {
  262.    struct dosdate_t date_t;
  263.    struct dostime_t time_t;
  264.  
  265.    sprintf (Temp, "HTTP/1.0 404 Not Found\r\n");
  266.    Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  267.  
  268.    _dos_getdate (&date_t);
  269.    _dos_gettime (&time_t);
  270.    sprintf (Temp, "Date: %s, %d-%s-%d %d:%02d:%02d GMT\r\n", WeekDays[date_t.dayofweek], date_t.day, Months[date_t.month - 1], date_t.year % 100, time_t.hour, time_t.minute, time_t.second);
  271.    Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  272.  
  273.    sprintf (Temp, "Server: %s/%s\r\n", NAME, VERSION);
  274.    Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  275.  
  276.    sprintf (Temp, "MIME-version: 1.0\r\n");
  277.    Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  278.  
  279.    sprintf (Temp, "Content-type: %s\r\n", "text/html");
  280.    Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  281.  
  282.    sprintf (Temp, "<HEAD><TITLE>404 Not Found</TITLE></HEAD>\r\n");
  283.    Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  284.    sprintf (Temp, "<BODY><H1>404 Not Found</H1>\r\n");
  285.    Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  286.    sprintf (Temp, "The requested URL %s was not found on this server.<P>\r\n", URL);
  287.    Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  288.    sprintf (Temp, "</BODY>\r\n");
  289.    Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  290.  
  291.    Tcp->UnbufferBytes ();
  292. }
  293.  
  294.