      SUBROUTINE PLW.CACHE.PAGE(FNAME,MOD.TM,R.RES,R.ERR)
*PIC-LAN
*
*ALL
*PC
*N
*
*
*
*
      INCLUDE SYS.TYPE.INCLUDE
*
      INCLUDE PL.COMMON
      INCLUDE PL.COMMON.DEFS
      INCLUDE PL.DEFS
      INCLUDE PLIP.COMMON
      INCLUDE PLW.COMMON
*
      J.LST = '0FILE':VM:'1FILE':VM:'0DSG':VM:'1DSG':VM:'0DSG-RAW':VM:'1DSG-RAW':VM:'0HOST':VM:'1HOST':VM:'0HOST-RAW':VM:'1HOST-RAW'
*
      R.ERR = ''
      R.RES = 0
      RESX = ''
      PL$PBHDR = ''
*
      PLW$LEVEL = PLW$LEVEL + 1
*
      IF FNAME[1,1] <> '/' THEN
         IF PL$PGNAME = '' THEN
            FNAME = '/' : FNAME
         END ELSE
            T = OCONV(PL$PGNAME,'G1/999')
            C = COUNT(T,'/')
            IF NOT(C) THEN
               FNAME = '/' : FNAME
            END ELSE
               C = INDEX(T,'/',C)
               FNAME = '/' : T[1,C-1] : '/' : FNAME
            END
         END
      END
*
      IF FNAME[LEN(FNAME),1] = '/' THEN FNAME = FNAME : 'INDEX.HTM'
*
      FNAME = OCONV(FNAME,'MCU')
*
      IF FNAME[1,1] = '&' OR FNAME[1,2] = '/&' THEN
         MATREAD DATA$ARRAY FROM PL$DATA.FD , PL$V.PORT.NO : "*LOCAL" THEN
            FLG = YES
            IF FNAME[1,1] = '/' THEN FNAME = FNAME[2,9999]
            IF FNAME[1,1] = '&' THEN FNAME = FNAME[2,9999]
            IF FNAME[1,4] = 'AMP;' OR FNAME[1,4] = 'amp;' THEN FNAME = FNAME[5,9999]
            IF FNAME[LEN(FNAME),1] = '/' THEN FNAME = FNAME[1,LEN(FNAME)-1]
            DNAME = '/local pages/'
         END ELSE
            R.ERR = '500 INTERNAL ERROR [PLW.PAGE:1];Local content file not found.  [PLW.PAGE:1:' : FNAME : ']'
            RETURN
         END
      END ELSE
         FLG = NO
         IF FNAME[1,LEN(FNAME)] <> '/' THEN FNAME = FNAME : '/'
         I1 = COUNT(FNAME,'/') + 1
         FOR I = I1 TO 1 STEP -1 UNTIL FLG
            DNAME = OCONV(FNAME,'G0/':I) : '/'
            MATREAD DATA$ARRAY FROM PL$DATA.FD , PL$V.PORT.NO : '*' : ST$IP : DNAME THEN
               FLG = YES
               FNAME = OCONV(FNAME,'G':I:'/99')
               BEGIN CASE
                  CASE FNAME = '/'
                     FNAME = ''
                  CASE FNAME[LEN(FNAME),1] = '/'
                     FNAME = FNAME[1,LEN(FNAME)-1]
               END CASE
            END
         NEXT I
      END
*
      IF FLG THEN
         IF INDEX(FNAME,'/',1) AND NOT(INDEX(DATA$OPT,'S',1)) THEN
            FLG = NO
         END
      END
*
      IF NOT(FLG) THEN
         R.ERR = '404 NOT FOUND [PLW.PAGE:2];Directory not found.  [PLW.PAGE:2:' : FNAME : ']'
         PLW$LEVEL = PLW$LEVEL - 1
         RETURN
      END
*
      IF LEN(FNAME) > (MAX.ID.LEN-12) THEN
         IF INDEX(FNAME,'/',1) THEN
            R.ERR = '404 NOT FOUND [PLW.PAGE:3];Directory format is invalid (too long).  [PLW.PAGE:3:' : FNAME : ']'
            PLW$LEVEL = PLW$LEVEL - 1 
            RETURN
         END ELSE
            R.ERR = '404 NOT FOUND [PLW.PAGE:4];File name format is invalid (too long).  [PLW.PAGE:4:' : FNAME : ']'
            PLW$LEVEL = PLW$LEVEL - 1
            RETURN
         END
      END
*
      PL$PGNAME = DATA$PREFIX : DNAME : FNAME
*
*$*      IF PLW$LEVEL = 1 THEN
*$*         IF INDEX(DATA$OPT,'D',1) THEN
*$*            IF INDEX(DATA$OPT,'A',1) THEN
*$*               FLG = YES
*$*            END ELSE
*$*               T = COUNT(FNAME,'.')
*$*               FILE.TP = FIELD(FNAME,'.',T+1)
*$*               FILE.TP = OCONV(FILE.TP,'MCU')
*$*               READV M.TP FROM PL$CTRL.FD , 'MIME*' : FILE.TP , 1 ELSE
*$*                  READV M.TP FROM PL$CTRL.FD , 'MIME*' , 1 ELSE
*$*                     M.TP = ''
*$*                  END
*$*               END
*$*               IF M.TP = 'text/html' THEN
*$*                  FLG = YES
*$*               END ELSE
*$*                  FLG = NO
*$*               END
*$*            END
*$*            IF FLG THEN
*$*               PL_SETVAR FNAME TO '_FNAME' ELSE NULL
*$*               CALL PLW.PAGE(DNAME:'__DEFAULT.HTM',MOD.TM,R.ERR)
*$*               RETURN
*$*            END
*$*         END
*$*      END
*
      C.OPT = ( INDEX(DATA$OPT,'C',1) <> 0 )
      LOCATE(C.OPT:DATA$TP,J.LST,1;L) ELSE
         R.ERR = '500 INTERNAL ERROR [PLW.PAGE:5];Internal Error, invalid directory type.  [PLW.PAGE:5]'
         PLW$LEVEL = PLW$LEVEL - 1
         RETURN
      END
*
      ON L GOSUB 100,200,100,200,300,300,100,200,400,400
      IF R.ERR <> '' THEN RETURN
*
      IF CTRL$TP THEN
         ON CTRL$TP GOSUB 11000,12000
      END
*
      PLW$LEVEL = PLW$LEVEL - 1
*
      RETURN
*
100   REM FILE
*
      MATREAD CTRL$ARRAY FROM PL$PGCTRL.FD , PL$PGNAME THEN
         T = PR$NOW - CTRL$CACHETM
         IF T < 0 OR T >= PLIP$WWW.CACHE.TIMEOUT THEN
            READ SRC0 FROM PL$PGCACHE.FD , PL$PGNAME THEN
               BEGIN CASE
                  CASE DATA$TP = 'FILE'
                     READ SRC1 FROM F$ARRAY(DATA$FNUM) , FNAME THEN
                        IF SRC0 = SRC1 THEN
                           CTRL$CACHETM = PR$NOW
                           WRITEV CTRL$CACHETM ON PL$PGCTRL.FD , PL$PGNAME , CTRL$CACHETM$N
                        END ELSE
                           CALL PLW.BLD.PAGE(FNAME,R.ERR)
                           R.RES = 1
                           IF R.ERR = '' THEN
                              WRITE SRC1 ON PL$PGCACHE.FD , PL$PGNAME
                           END
                        END
                     END ELSE
                        R.ERR = '404 NOT FOUND [PLW.PAGE:6];File not found.  [PLW.PAGE:6:' : FNAME : ']'
                     END
                  CASE DATA$TP = 'HOST'
                     CALL PLU.HOSTDIR(DATA$HOSTPATH:FNAME,FILE.PATH,FILE.NAME,FILE.EXT,SRC1)
                     IF SRC1 <> '' THEN
                        IF SRC0<1> = SRC1 THEN
                           CTRL$CACHETM = PR$NOW
                           WRITEV CTRL$CACHETM ON PL$PGCTRL.FD , PL$PGNAME , CTRL$CACHETM$N
                        END ELSE
                           CALL PLW.BLD.PAGE(FNAME,R.ERR)
                           R.RES = 1
                           IF R.ERR = '' THEN
                              WRITE SRC1 : AM : PR$NOW ON PL$PGCACHE.FD , PL$PGNAME
                           END
                        END
                     END ELSE
                        R.ERR = '404 NOT FOUND [PLW.PAGE:7];File not found on host.  [PLW.PAGE:7:' : DATA$HOSTPATH : FNAME : ']'
                     END
               END CASE
            END ELSE
               CALL PLW.BLD.PAGE(FNAME,R.ERR)
               R.RES = 1
            END
         END ELSE
*$*         PL_PR 'CACHE '
         END
      END ELSE
         CALL PLW.BLD.PAGE(FNAME,R.ERR)
         R.RES = 1
      END
*
      RETURN
*
200   REM FILE - no cache check
*
      MATREAD CTRL$ARRAY FROM PL$PGCTRL.FD , PL$PGNAME ELSE GOTO 100
      RETURN
*
300   REM DSGRAW
*
      MAT CTRL$ARRAY = ''
      CALL PLW.DSGRAW(FNAME,MOD.TM,R.ERR)
      IF R.ERR = '' THEN RESX = AM
      RETURN
*
400   REM HOSTRAW
*
      MAT CTRL$ARRAY = ''
      FNAME = OCONV(FNAME,'MCL')
      CALL PLW.HOSTRAW(FNAME,MOD.TM,R.ERR)
      IF R.ERR = '' THEN RESX = AM
      RETURN
*
*
11000 REM Process type '1' GET requests
*
      IF MOD.TM <> '' THEN
         IF MOD.TM = CTRL$TM THEN
            R.ERR = '304 NOT MODIFIED'
            PL$PBHDR = CTRL$NMHDR<1,1> : PL_TODATE(PL_NOW()-PLIP$TZ) : CTRL$NMHDR<1,2>
            RETURN
         END
      END
*
      READ RESX FROM PL$PGDATA.FD , PL$PGNAME ELSE
         R.ERR = '500 INTERNAL ERROR [PLW.PAGE:9];Internal Error, unable to read image item.  [PLW.PAGE:9]'
         RETURN
      END
*
      PL$PBHDR = CTRL$HDR<1,1> : PL_TODATE(PL_NOW()-PLIP$TZ) : CTRL$HDR<1,2>
*
      RETURN
*
12000 REM Process type '2' GET requests
*
      READV ATTR1 FROM PL$MD.FD , CTRL$SUB , 1 THEN
         IF OCONV(ATTR1,'MCU') = 'V' THEN
            PL_PRSTATE 'Calling embedded subroutine ' : CTRL$SUB
            DELETE PL$STATE.FD , 'RUN*' : PL$V.PORT.NO
            CALL @CTRL$SUB(R.ERR)
            DELETE PL$STATE.FD , 'RUN*' : PL$V.PORT.NO
            PL_PRSTATE ''
            CTRL$TMS = ''
         END ELSE
            R.ERR = '500 INTERNAL ERROR [PLW.PAGE:10];Internal Error, embedded subroutine ' : CTRL$SUB : ' has an invalid catalog pointer.  [PLW.PAGE:10]'
         END
      END ELSE
         R.ERR = '500 INTERNAL ERROR [PLW.PAGE:11];Internal Error, embedded subroutine ' : CTRL$SUB : ' either had error(s) during compile or is not cataloged.  [PLW.PAGE:11]'
      END
*
      RETURN
   END
