monitor.c
上传用户:seven77cht
上传日期:2007-01-04
资源大小:486k
文件大小:12k
源码类别:

浏览器

开发平台:

Unix_Linux

  1. /***************************************
  2.   $Header: /home/amb/wwwoffle/RCS/monitor.c 1.28 1999/09/11 14:00:03 amb Exp $
  3.   WWWOFFLE - World Wide Web Offline Explorer - Version 2.5.
  4.   A header file for all of the programs wwwoffle, wwwoffled.
  5.   ******************/ /******************
  6.   Written by Andrew M. Bishop
  7.   This file Copyright 1998,99 Andrew M. Bishop
  8.   It may be distributed under the GNU Public License, version 2, or
  9.   any higher version.  See section COPYING of the GNU Public license
  10.   for conditions under which this file may be redistributed.
  11.   ***************************************/
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include <unistd.h>
  17. #include <time.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <dirent.h>
  21. #include <fcntl.h>
  22. #include "wwwoffle.h"
  23. #include "misc.h"
  24. #include "config.h"
  25. #include "errors.h"
  26. /*+ Need this for Win32 to use binary mode +*/
  27. #ifndef O_BINARY
  28. #define O_BINARY 0
  29. #endif
  30. static void MonitorFormParse(int fd,Body *request_body);
  31. /*++++++++++++++++++++++++++++++++++++++
  32.   Send to the client a page to allow monitoring using HTML.
  33.   int fd The file descriptor of the client.
  34.   URL *Url The URL that was used to request this page.
  35.   Body *request_body The body of the HTTP request.
  36.   ++++++++++++++++++++++++++++++++++++++*/
  37. void MonitorPage(int fd,URL *Url,Body *request_body)
  38. {
  39.  if(!strcmp("/monitor-options/",Url->path))
  40.    {
  41.     char MofY[13],DofM[32],DofW[8],HofD[25];
  42.     char dofmstr[128],hofdstr[64],*p;
  43.     int i;
  44.     URL *monUrl=NULL;
  45.     if(Url->args && (monUrl=SplitURL(Url->args)))
  46.        ReadMonitorTimesSpoolFile(monUrl,MofY,DofM,DofW,HofD);
  47.     else
  48.       {
  49.        strcpy(MofY,"111111111111");
  50.        strcpy(DofM,"1111111111111111111111111111111");
  51.        strcpy(DofW,"1111111");
  52.        strcpy(HofD,"100000000000000000000000");
  53.       }
  54.     if(!strcmp(DofM,"1111111111111111111111111111111"))
  55.        strcpy(dofmstr,"");
  56.     else
  57.       {
  58.        *dofmstr=0;
  59.        p=dofmstr;
  60.        for(i=0;i<31;i++)
  61.           if(DofM[i]=='1')
  62.             {
  63.              sprintf(p,"%d ",i+1);
  64.              p+=strlen(p);
  65.             }
  66.       }
  67.     if(!strcmp(HofD,"100000000000000000000000"))
  68.        strcpy(hofdstr,"");
  69.     else
  70.       {
  71.        *hofdstr=0;
  72.        p=hofdstr;
  73.        for(i=0;i<24;i++)
  74.           if(HofD[i]=='1')
  75.             {
  76.              sprintf(p,"%d ",i);
  77.              p+=strlen(p);
  78.             }
  79.       }
  80.     HTMLMessage(fd,200,"WWWOFFLE Monitor Page",NULL,"MonitorPage",
  81.                 "url",Url->args,
  82.                 "mofy1",MofY[0]=='1'?"Yes":NULL,
  83.                 "mofy2",MofY[1]=='1'?"Yes":NULL,
  84.                 "mofy3",MofY[2]=='1'?"Yes":NULL,
  85.                 "mofy4",MofY[3]=='1'?"Yes":NULL,
  86.                 "mofy5",MofY[4]=='1'?"Yes":NULL,
  87.                 "mofy6",MofY[5]=='1'?"Yes":NULL,
  88.                 "mofy7",MofY[6]=='1'?"Yes":NULL,
  89.                 "mofy8",MofY[7]=='1'?"Yes":NULL,
  90.                 "mofy9",MofY[8]=='1'?"Yes":NULL,
  91.                 "mofy10",MofY[9]=='1'?"Yes":NULL,
  92.                 "mofy11",MofY[10]=='1'?"Yes":NULL,
  93.                 "mofy12",MofY[11]=='1'?"Yes":NULL,
  94.                 "dofm",dofmstr,
  95.                 "dofw0",DofW[0]=='1'?"Yes":NULL,
  96.                 "dofw1",DofW[1]=='1'?"Yes":NULL,
  97.                 "dofw2",DofW[2]=='1'?"Yes":NULL,
  98.                 "dofw3",DofW[3]=='1'?"Yes":NULL,
  99.                 "dofw4",DofW[4]=='1'?"Yes":NULL,
  100.                 "dofw5",DofW[5]=='1'?"Yes":NULL,
  101.                 "dofw6",DofW[6]=='1'?"Yes":NULL,
  102.                 "hofd",hofdstr,
  103.                 NULL);
  104.    }
  105.  else if(!strcmp("/monitor-request/",Url->path))
  106.     MonitorFormParse(fd,request_body);
  107.  else
  108.    {
  109.     char *page=(char*)malloc(strlen(Url->path)+(Url->args?strlen(Url->args):0)+4);
  110.     if(Url->args)
  111.        sprintf(page,"%s?%s",Url->path,Url->args);
  112.     else
  113.        sprintf(page,"%s",Url->path);
  114.     HTMLMessage(fd,404,"WWWOFFLE Illegal Monitor Page",NULL,"MonitorIllegal",
  115.                 "url",page,
  116.                 NULL);
  117.     free(page);
  118.    }
  119. }
  120. /*++++++++++++++++++++++++++++++++++++++
  121.   Parse the reply from the form.
  122.   int fd The file descriptor of the client.
  123.   Body *request_body The body of the HTTP request sent by the browser.
  124.   ++++++++++++++++++++++++++++++++++++++*/
  125. static void MonitorFormParse(int fd,Body *request_body)
  126. {
  127.  int i,mfd=-1;
  128.  char *copy,*url=NULL;
  129.  URL *Url;
  130.  char mofy[12]="NNNNNNNNNNNN",*dofm=NULL,dofw[7]="NNNNNNN",*hofd=NULL;
  131.  char MofY[13],DofM[32],DofW[8],HofD[25];
  132.  if(!request_body)
  133.    {
  134.     HTMLMessage(fd,404,"WWWOFFLE Monitor Form Error",NULL,"MonitorFormError",
  135.                 "body",NULL,
  136.                 NULL);
  137.     return;
  138.    }
  139.  copy=(char*)malloc(request_body->length+1);
  140.  strcpy(copy,request_body->content);
  141.  for(i=0;copy[i];i++)
  142.    {
  143.     if(i!=0 && copy[i-1]=='&')
  144.        copy[i-1]=0;
  145.     if(i==0 || copy[i-1]==0)
  146.       {
  147.        if(!strncmp("url=",&copy[i],4))
  148.           url=&copy[i+4];
  149.        if(!strncmp("mofy1=",&copy[i],6))
  150.           mofy[0]=copy[i+6];
  151.        if(!strncmp("mofy2=",&copy[i],6))
  152.           mofy[1]=copy[i+6];
  153.        if(!strncmp("mofy3=",&copy[i],6))
  154.           mofy[2]=copy[i+6];
  155.        if(!strncmp("mofy4=",&copy[i],6))
  156.           mofy[3]=copy[i+6];
  157.        if(!strncmp("mofy5=",&copy[i],6))
  158.           mofy[4]=copy[i+6];
  159.        if(!strncmp("mofy6=",&copy[i],6))
  160.           mofy[5]=copy[i+6];
  161.        if(!strncmp("mofy7=",&copy[i],6))
  162.           mofy[6]=copy[i+6];
  163.        if(!strncmp("mofy8=",&copy[i],6))
  164.           mofy[7]=copy[i+6];
  165.        if(!strncmp("mofy9=",&copy[i],6))
  166.           mofy[8]=copy[i+6];
  167.        if(!strncmp("mofy10=",&copy[i],7))
  168.           mofy[9]=copy[i+7];
  169.        if(!strncmp("mofy11=",&copy[i],7))
  170.           mofy[10]=copy[i+7];
  171.        if(!strncmp("mofy12=",&copy[i],7))
  172.           mofy[11]=copy[i+7];
  173.        if(!strncmp("dofm=",&copy[i],5))
  174.           dofm=&copy[i+5];
  175.        if(!strncmp("dofw0=",&copy[i],6))
  176.           dofw[0]=copy[i+6];
  177.        if(!strncmp("dofw1=",&copy[i],6))
  178.           dofw[1]=copy[i+6];
  179.        if(!strncmp("dofw2=",&copy[i],6))
  180.           dofw[2]=copy[i+6];
  181.        if(!strncmp("dofw3=",&copy[i],6))
  182.           dofw[3]=copy[i+6];
  183.        if(!strncmp("dofw4=",&copy[i],6))
  184.           dofw[4]=copy[i+6];
  185.        if(!strncmp("dofw5=",&copy[i],6))
  186.           dofw[5]=copy[i+6];
  187.        if(!strncmp("dofw6=",&copy[i],6))
  188.           dofw[6]=copy[i+6];
  189.        if(!strncmp("hofd=",&copy[i],5))
  190.           hofd=&copy[i+5];
  191.       }
  192.    }
  193.  if(url==NULL || *url==0)
  194.    {
  195.     HTMLMessage(fd,404,"WWWOFFLE Monitor Form Error",NULL,"MonitorFormError",
  196.                 "body",request_body->content,
  197.                 NULL);
  198.     free(copy);
  199.     return;
  200.    }
  201.  url=URLDecode(url,1);
  202.  Url=SplitURL(url);
  203.  /* Parse the requested time */
  204.  strcpy(MofY,"000000000000");
  205.  for(i=0;i<12;i++)
  206.     if(mofy[i]=='Y')
  207.        MofY[i]='1';
  208.  if(!*dofm)
  209.     strcpy(DofM,"1111111111111111111111111111111");
  210.  else
  211.    {
  212.     int d,any=0;
  213.     char *p=dofm;
  214.     strcpy(DofM,"0000000000000000000000000000000");
  215.     while(*p)
  216.       {
  217.        while(*p && !isdigit(*p))
  218.           p++;
  219.        if(!*p)
  220.           break;
  221.        d=atoi(p)-1;
  222.        if(d>=0 && d<31)
  223.          {
  224.           DofM[d]='1';
  225.           any++;
  226.          }
  227.        while(isdigit(*p))
  228.           p++;
  229.       }
  230.     if(!any)
  231.        strcpy(DofM,"1111111111111111111111111111111");
  232.    }
  233.  strcpy(DofW,"0000000");
  234.  for(i=0;i<7;i++)
  235.     if(dofw[i]=='Y')
  236.        DofW[i]='1';
  237.  if(!*hofd)
  238.     strcpy(HofD,"100000000000000000000000");
  239.  else
  240.    {
  241.     int h,any=0;
  242.     char *p=hofd;
  243.     strcpy(HofD,"000000000000000000000000");
  244.     while(*p)
  245.       {
  246.        while(*p && !isdigit(*p))
  247.           p++;
  248.        if(!*p)
  249.           break;
  250.        h=atoi(p);
  251.        if(h>=0 && h<24)
  252.          {
  253.           HofD[h]='1';
  254.           any++;
  255.          }
  256.        while(isdigit(*p))
  257.           p++;
  258.       }
  259.     if(!any)
  260.        strcpy(HofD,"100000000000000000000000");
  261.    }
  262.  mfd=CreateMonitorSpoolFile(Url,MofY,DofM,DofW,HofD);
  263.  if(mfd==-1)
  264.     HTMLMessage(fd,500,"WWWOFFLE Server Error",NULL,"ServerError",
  265.                 "error","Cannot open file to store monitor request",
  266.                 NULL);
  267.  else
  268.    {
  269.     int ofd;
  270.     Header *new_request_head=RequestURL(Url,NULL);
  271.     char *head=HeaderString(new_request_head);
  272.     write_string(mfd,head);
  273.     close(mfd);
  274.     ofd=OpenOutgoingSpoolFile(0);
  275.     if(ofd==-1)
  276.        PrintMessage(Warning,"Cannot open outgoing spool file for monitored URL '%s' [%!s].",url);
  277.     else
  278.       {
  279.        write_string(ofd,head);
  280.        CloseOutgoingSpoolFile(ofd,Url);
  281.       }
  282.     HTMLMessage(fd,200,"WWWOFFLE Monitor Will Get",NULL,"MonitorWillGet",
  283.                 "url",Url->name,
  284.                 NULL);
  285.     free(head);
  286.     FreeHeader(new_request_head);
  287.    }
  288.  free(copy);
  289.  FreeURL(Url);
  290. }
  291. /*++++++++++++++++++++++++++++++++++++++
  292.   Convert monitor requests into outgoing requests.
  293.   ++++++++++++++++++++++++++++++++++++++*/
  294. void RequestMonitoredPages(void)
  295. {
  296.  DIR *dir;
  297.  struct dirent* ent;
  298.  /* Open the monitor subdirectory. */
  299.  if(chdir("monitor"))
  300.    {PrintMessage(Warning,"Cannot change to directory 'monitor' [%!s]; no files monitored.");return;}
  301.  dir=opendir(".");
  302.  if(!dir)
  303.    {PrintMessage(Warning,"Cannot open directory 'monitor' [%!s]; no files monitored.");chdir("..");return;}
  304.  ent=readdir(dir);  /* skip .  */
  305.  if(!ent)
  306.    {PrintMessage(Warning,"Cannot read directory 'monitor' [%!s]; no files monitored.");closedir(dir);chdir("..");return;}
  307.  ent=readdir(dir);  /* skip .. */
  308.  /* Scan through all of the files. */
  309.  while((ent=readdir(dir)))
  310.    {
  311.     struct stat buf;
  312.     if(lstat(ent->d_name,&buf))
  313.       {PrintMessage(Inform,"Cannot stat file 'monitor/%s' [%!s]; race condition?",ent->d_name);return;}
  314.     else if(S_ISREG(buf.st_mode) && *ent->d_name=='O')
  315.       {
  316.        char *url=FileNameToURL(ent->d_name);
  317.        URL *Url=SplitURL(url);
  318.        int last,next;
  319.        chdir("..");
  320.        MonitorTimes(Url,&last,&next);
  321.        PrintMessage(Debug,"Monitoring %s; last=%dh next=%dh =>%s",Url->name,last,next,next?"No":"Yes");
  322.        chdir("monitor");
  323.        if(next==0)
  324.          {
  325.           int ifd=open(ent->d_name,O_RDONLY|O_BINARY);
  326.           init_buffer(ifd);
  327.           if(ifd==-1)
  328.              PrintMessage(Warning,"Cannot open monitored file 'monitor/%s' to read [%!s].",ent->d_name);
  329.           else
  330.             {
  331.              int ofd;
  332.              chdir("..");
  333.              ofd=OpenOutgoingSpoolFile(0);
  334.              if(ofd==-1)
  335.                 PrintMessage(Warning,"Cannot open outgoing spool file for monitored URL '%s' [%!s].",url);
  336.              else
  337.                {
  338.                 char *contents=(char*)malloc(buf.st_size);
  339.                 URL *Url=SplitURL(url);
  340.                 read_data(ifd,contents,buf.st_size);
  341.                 write_data(ofd,contents,buf.st_size);
  342.                 CloseOutgoingSpoolFile(ofd,Url);
  343.                 free(contents);
  344.                 FreeURL(Url);
  345.                }
  346.              chdir("monitor");
  347.              close(ifd);
  348.              free(url);
  349.             }
  350.          }
  351.        FreeURL(Url);
  352.       }
  353.    }
  354.  chdir("..");
  355.  closedir(dir);
  356. }
  357. /*++++++++++++++++++++++++++++++++++++++
  358.   Calculate the monitoring interval and times of previous and next monitoring.
  359.   URL *Url The URL of the file to check for.
  360.   int *last The time in hours ago that the file was last monitored.
  361.   int *next The time in hours from now that it should next be monitored.
  362.   ++++++++++++++++++++++++++++++++++++++*/
  363. void MonitorTimes(URL *Url,int *last,int *next)
  364. {
  365.  time_t now=time(NULL),then,when;
  366.  char MofY[13],DofM[32],DofW[8],HofD[25];
  367.  int mofy,dofm,dofw,hofd;
  368.  struct tm *tim;
  369.  then=ReadMonitorTimesSpoolFile(Url,MofY,DofM,DofW,HofD);
  370.  now =3600*(now/3600);
  371.  then=3600*(then/3600);
  372.  if(!then || then>now)
  373.     then=now;
  374.  mofy=dofm=dofw=hofd=0;
  375.  for(when=then+3600;when<=now;when+=3600)
  376.    {
  377.     if(hofd==0)
  378.       {
  379.        tim=localtime(&when);
  380.        mofy=tim->tm_mon;
  381.        dofm=tim->tm_mday-1;
  382.        dofw=tim->tm_wday;
  383.        hofd=tim->tm_hour;
  384.       }
  385.     if(MofY[mofy]=='1' && DofM[dofm]=='1' && DofW[dofw]=='1' && HofD[hofd]=='1')
  386.       {*next=0;break;}
  387.     hofd=(hofd+1)%24;
  388.    }
  389.  if(when>now)
  390.    {
  391.     hofd=0;
  392.     for(when=now+3600;when<(now+31*24*3600);when+=3600)
  393.       {
  394.        if(hofd==0)
  395.          {
  396.           tim=localtime(&when);
  397.           mofy=tim->tm_mon;
  398.           dofm=tim->tm_mday-1;
  399.           dofw=tim->tm_wday;
  400.           hofd=tim->tm_hour;
  401.          }
  402.        if(MofY[mofy]=='1' && DofM[dofm]=='1' && DofW[dofw]=='1' && HofD[hofd]=='1')
  403.           break;
  404.        hofd=(hofd+1)%24;
  405.       }
  406.     *next=(when-now)/3600;
  407.    }
  408.  *last=(now-then)/3600;
  409. }