home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / srev13h.zip / puburchk.rxx < prev    next >
Text File  |  1999-11-11  |  10KB  |  401 lines

  1. /* Check public urls module for SRE-http. Uses contents of puburlfile
  2. and queues & semaphores to communicate with filter.
  3.  
  4. Returns 0, or matching public url
  5.  
  6. */
  7.  
  8. parse upper arg purlfile,  usequeue , USESEM, max_semwait,sport
  9.  
  10. /*
  11. call pmprintf_sref(' SRE-http public-url thread: using='||purlfile)
  12. call pmprintf_sref(' SRE-http public-url thread: queue='||usequeue)
  13. call pmprintf_sref(' SRE-http public-url thread: semaphore='||usesem)
  14. */
  15.  
  16. mytid=dostid()
  17. foo=pmprintf_sref(' SRE-http Publicurl: thread, file, queue='||mytid', 'purlfile', 'usequeue,,sport)
  18.  
  19. cfgs_dir=value('SRE_CFGS_DIR',,'os2environment')
  20. cfglist_file=cfgs_dir||'\CFGLIST.CFG'
  21. crlf='0d0a'x
  22.  
  23. defrealm=value('SREF_DEFREALM_FILE_TEMP',,'os2environment')
  24.  
  25. resetit:
  26. if usequeue="" | USESEM="" then do
  27.    call pmprintf('SRE-http Publicurl ERROR: initialization ERROR: '||usequeue)
  28.    exit
  29. end
  30.  
  31. npurls=set_purls(purlfile)
  32.  
  33. foo=pmprintf_sref(' SRE-http Publicurl:  #public-urls = ' npurls)
  34.  
  35. call value 'SREF_NUM_PURLS',npurls,'os2environment'
  36.  
  37. /* Initialization now done == start waiting for requests for public url info */
  38. signal on error name iserror
  39. signal on syntax name iserror
  40. bakme:
  41.  a=rxqueue('s',usequeue)
  42.  aq=queued()
  43.  if aq=0 then do
  44.     WOW=EVENTSEM_WAIT(USESEM,max_semwait)
  45.     aq=-1
  46.     if wow=640 then do
  47.          signal bakme
  48.     end
  49.  
  50.     IF WOW<>0 THEN do         /* FATAL ERROR */
  51.        call pmprintf(' SRE-http Publicurl ERROR:  fatal semaphore error ')
  52.        exit
  53.     end
  54.  end
  55.  wow=EVENTSEM_RESET(usesem)
  56.  if aq=-1 then
  57.     if queued()=0 then    signal bakme
  58.  
  59.   PARSE pull isit0
  60.  
  61.   isit0=translate(isit0,' ','000d0a09'x)
  62.   if isit0="" then signal bakme
  63.  
  64. /* die command? */
  65.   if abbrev(isit0,'*DIE*')=1 then  exit
  66.  
  67.   parse var isit0  idnum ',' newq ',' newsem ',' ISIT
  68.   parse var idnum idnum host_nickname
  69.  
  70.  isitorig=isit
  71.  if newq="" | newsem=""  then do
  72.         call pmprintf(' SRE-http Publicurl ERROR: missing queue or semaphore ')
  73.         signal bakme
  74.  end
  75.  newq=upper(strip(newq)); newsem=upper(strip(newsem))
  76.  ISIT=STRIP(ISIT)
  77.  
  78.  if abbrev(isit,'*LIST*') then do
  79.       dog1=''
  80.       do kk=1 to purls.0
  81.         t1=purls.!l.kk
  82.         dog1=dog1||t1||crlf
  83.       end
  84.       a=rxqueue('s',newq)
  85.       push idnum ',' dog1
  86.       wow=eventsem_post(newsem)
  87.       signal bakme
  88.  end
  89.  
  90.  
  91.  if abbrev(isit,'*RESET*') then do
  92.        parse var isit foo newfile .
  93.           if newfile<>' ' then purlfile=newfile
  94.           call set_purls(purlfile)
  95.          foo=call pmprintf_sref(' SRE-http Publicurl thread reset:  #public-urls = ' npurls)
  96.   end
  97.   else do
  98.        if  npurls=0 then do
  99.            dog1=0
  100.        end
  101.        else do
  102.           if pos('?',isit)>0 then do
  103.              parse var isit a1 '?' a2
  104.              isit=translate(a1,'/','\')
  105.              isit=strip(isit,'l','/')||'?'||a2
  106.           end
  107.           dog1=fig_purl(isit,host_nickname)
  108.       end
  109.  
  110.       a=rxqueue('s',newq)
  111.       push idnum ',' dog1
  112.       wow=eventsem_post(newsem)
  113.  end
  114.  
  115.  
  116. signal bakme
  117.  
  118. iserror:                /* jump here on an error */
  119. signal off error ; signal off syntax
  120. call pmprintf_sref(' Error in daemon ('usequeue'), exiting: 'sigl','rc)
  121. a=rxqueue('d',usequeue)
  122. call pmprintf("   Status= "a " shutting down "usequeue)
  123. a=eventsem_close(usesem)
  124. call pmprintf("   Status= "a " shutting down "usesem)
  125. foo=value('SREF_REDO',1,'os2environment')
  126. badt=value('SREF_PUBURL_BAD',1,'os2environment')
  127.  
  128. exit
  129.  
  130.  
  131.  
  132. /* ---------- */
  133. fig_purl:procedure expose purls. wildpurls.  usesem usequeue
  134.  
  135. parse upper arg needle,hostn
  136.  
  137. needle=strip(needle,'l','/')
  138. if hostn<>'' then needle=upper(strip(hostn))'//'needle
  139. needle=upper(strip(needle))
  140.  
  141. if length(needle)<=purls.!maxlen then do
  142.   if upper(purls.needle)<>upper('PURLS.'needle) then do
  143.    return purls.needle
  144.   end
  145. end
  146.  
  147. /* check wildcards -- retain "best" match */
  148. oldresu='' ;isit=0
  149. do ii=1 to wildpurls.0
  150.  resu=sref_wild_match(needle,wildpurls.ii,oldresu)
  151.  
  152.  if resu=-1 then do
  153.        isit=ii
  154.        leave
  155.  end
  156.  if resu=0 then iterate
  157.  isit=ii
  158.  oldresu=resu
  159. end
  160. if isit>0 then
  161.   return wildpurls.isit.!value
  162. else
  163.   return 0
  164.  
  165.  
  166. /* ---------------- */
  167. /* read and set up public-url*/
  168. set_purls:procedure expose purls. wildpurls. cfglist_file cfgs_dir sport defrealm
  169. parse arg afile
  170.  
  171. npurls=0
  172.  
  173. a=sref_fileread(afile,'ulines',,'E')   /* read it to a stem variable */
  174.  
  175. oo=stream(afile,'c','close')
  176. if a=0 then do
  177.       call pmprintf(' SRE-http Publicurl: could not read (empty file?) '||afile)
  178.       ulines.0=0
  179. end
  180.  
  181. foo=add_cfglist('PUBURLS','PUBURLS.IN')          /*augment the ulines stem variable, using CFGLIST.CFG files */
  182.  
  183. foo=add_defrealm('PUBURLS')
  184.  
  185. drop purls. ; drop wildpurls.
  186. purls.!maxlen=0
  187. purls.0=0
  188.  
  189. wildpurls.0=0
  190. ii=0
  191. do mm=1 to ulines.0
  192.   t1=strip(ulines.mm)
  193.  
  194.   if t1="" then iterate
  195.   if abbrev(t1,';')=1 then iterate
  196.  
  197.  
  198.   tm1=strip(word(t1,1)) ; ahost=' '
  199.   if right(tm1,2)='//' then do
  200.         ahost=upper(left(tm1,length(tm1)-2))
  201.         parse var t1 foo t1a t1b
  202.         str=t1a' 't1b
  203.         t1a=ahost'//'t1a
  204.   end
  205.   else do
  206.       parse var t1 t1a t1b
  207.       str=t1a' 't1b
  208.   end
  209.   if pos('?',t1a)>0 then do
  210.      parse var t1a q1 '?' q2
  211.      t1a=strip(translate(q1,'/','\'),'l','/')||'?'||q2
  212.   end
  213.   else do
  214.       t1a=strip(translate(t1a,'/','\'),'l','/')
  215.   end
  216.  
  217. /* this is only used for *LIST* */
  218.   ipj=purls.0+1
  219.   tt1a=t1a
  220.   if pos('//',t1a)=0 then tt1a=' //'tt1a
  221.   purls.!l.ipj=tt1a' 't1b
  222.   purls.0=ipj
  223.  
  224.  
  225.  t1a=upper(t1a)
  226.  if pos('*',t1a)>0 then do
  227.      amm=wildpurls.0+1
  228.      wildpurls.amm.!value=str
  229.      wildpurls.amm=t1a
  230.      wildpurls.0=amm
  231.  end
  232.  else do
  233.     purls.t1a=str
  234.     purls.!maxlen=max(purls.!maxlen,length(str))
  235.  end
  236.  ii=ii+1
  237. end
  238.  
  239. return ii
  240.  
  241.  
  242.  
  243. /**********************************/
  244. /* Add entries to ulines. variable, from the defrealm_temp file.
  245.   Note that this file only has the appropriate port entries
  246.   in it */
  247. add_defrealm:procedure expose defrealm ulines. 
  248.  
  249. parse upper arg atype
  250. atype=strip(atype)
  251. crlf='0d0a'x
  252.  
  253. if defrealm='' then return 0
  254. aa=sref_open_read(defrealm,15,'READ')
  255. if aa<=0 then return 0
  256.  
  257. ii=stream(defrealm,'c','query size')
  258. if ii=0 | ii='' then return 0
  259.  
  260. stuff=charin(defrealm,1,ii)
  261. foo=stream(defrealm,'c','close')
  262.  
  263. do forever
  264.   if stuff="" then return 1
  265.   parse var stuff aline (crlf) stuff
  266.   if abbrev(aline,';')=1 then iterate
  267.   parse var aline btype ':' aline
  268.   if btype<>atype then iterate
  269.   ii=ulines.0+1
  270.   ulines.ii=aline
  271.   ulines.0=ii
  272. end
  273.  
  274.  
  275.  
  276.  
  277. /**********************************/
  278. /* Add entries to ulines. variable, from the  ATYPE files listed in  the CFGLIST.CFG file
  279.    (but only for port SPORT) 
  280. */
  281.  
  282. add_cfglist:procedure expose ulines. cfglist_file sport cfgs_dir
  283. parse upper arg atype,defname
  284. atype=strip(atype)
  285.  
  286. if cfglist_file='' then return /* nothing to do */
  287.  
  288. /* look for files */
  289. foo=translate(stream(cfglist_file,'c','open read'))
  290. if abbrev(foo,'READY')<>1 then do
  291.   call pmprintf('SRE-http: ' atype ' Warning: Unable to open 'cfglist_file)
  292.   return 0            /* unable to open */
  293. end
  294. inj=stream(cfglist_file,'c','query size')
  295.  
  296.  
  297. if inj=0 | inj='' then return   0        /* empty file */
  298. astuff=charin(cfglist_file,1,inj)
  299. foo=stream(cfglist_file,'c','close')
  300.  
  301. crlf='0d0a'x
  302. astuff=astuff||crlf||' '        /* place an elephant in cairo */
  303.  
  304. /* determine which files apply to this atype and port, by readling CFGLIST.CFG */
  305. mm=0
  306.  
  307. /* note: file is organized in blocks */
  308. curport=80              /* defaults port and host */
  309. curhost=''
  310. curfile=''
  311.  
  312. do forever              /* for all blocks in file */
  313.   if astuff='' then leave       /* all done  (note we always put an elephant in cairo */
  314.   parse var astuff aline (crlf) astuff
  315.   if abbrev(aline,';')=1 then iterate   /* ignore comments */
  316.   if aline='' then do           /* block end */
  317.      if curport<>sport | curfile='' then do     /* different port, or this atype file not specified */ 
  318.         nop 
  319.      end
  320.      else do        /* otherwise, add this entry to filelist */
  321.         mm=mm+1
  322.         ufiles.mm.!host=curhost
  323.         ufiles.mm.!file=curfile
  324.      end
  325.      curport=80 ; curhost='' ; curfile=''  /* clear block */
  326.      iterate
  327.   end
  328.  
  329. /* process an entry in this block */
  330.   parse upper var aline ltype ':' lstuff ;ltype=strip(ltype) ; lstuff=strip(lstuff)
  331.  
  332.   select
  333.     when ltype='PORT' then curport=lstuff
  334.     when ltype='HOST' then curhost=lstuff
  335.     when abbrev(ltype,atype)=1  then do
  336.  
  337.        curfile=strip(translate(lstuff,'\','/'),'l','\')
  338.        if pos(':',curfile)=0 then curfile=cfgs_dir||'\'||curfile
  339.  
  340.     end
  341.     when ltype='*'  then do
  342.        curfile=strip(translate(lstuff,'\','/'),,'\')
  343.        if pos(':',curfile)=0 then curfile=cfgs_dir||'\'||curfile
  344.        curfile=stream(curfile'\'defname,'c','query exists')
  345.     end
  346.  
  347.     otherwise nop
  348.   end
  349.  
  350. end
  351.  
  352. /* done reading cfglist; add entries from appropriate files */
  353. if mm=0 then return 1     /* no auxillary files of this type */
  354. ufiles.0=mm
  355.  
  356. do mm=1 to ulines.0              /* retain old uline */
  357.   ULINEStmp.mm=ulines.mm
  358. end
  359. ULINEStmp.0=ulines.0
  360.  
  361.  
  362. do mm=1 to ufiles.0
  363.   afile=ufiles.mm.!file
  364.   ahost=ufiles.mm.!host
  365.  
  366.   a=sref_fileread(afile,'ulines',,'E')   /* read it to a stem variable */
  367.  
  368.   if a=0 | ulines.0=0 then do
  369.       call pmprintf(' SRE-http 'atype': WARNING**: bad auxillary file: '||afile)
  370.       iterate
  371.   end
  372.    
  373.    call pmprintf('  SRE-http '||lower(atype)||': adding from 'AHOST ' specific file 'afile ',' ulines.0)
  374.  
  375.    do ii=1 to ulines.0
  376.         aline=strip(ulines.ii)
  377.         if ALINE=' ' then iterate
  378.         aline=translate(aline,' ','0009'x)
  379.         if  abbrev(strip(aline),';')=1 then iterate
  380.         parse var aline aw1 .
  381.         if pos('//',aw1)>0 then do
  382.             call pmprintf('SRE-http: 'atype 'warning: disallowed auxillary entry: 'aline)
  383.             iterate
  384.         end
  385.         if ahost<>'' then aline=ahost||'// '||aline
  386.         ii2=ULINEStmp.0+1
  387.         ULINEStmp.ii2=aline
  388.         ULINEStmp.0=ii2
  389.    end
  390. end
  391.  
  392. do mm=1 to ULINEStmp.0
  393.    ulines.mm=ULINEStmp.mm
  394. end
  395. ulines.0=ULINEStmp.0
  396. drop ULINEStmp.
  397. return 1
  398.  
  399.  
  400.   
  401.