home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / srev13g.zip / puburchk.rxx < prev    next >
Text File  |  1999-06-20  |  10KB  |  408 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:
  119. signal off error ; signal off syntax
  120. call pmprintf(' SRE-http Publicurl ERROR: error in public-url thread 'sigl)
  121. a=rxqueue('d',usequeue)
  122. a=rxqueue('c',usequeue)
  123. a=eventsem_close(usesem)
  124. a=eventsem_create(usesem)
  125. a=rxqueue('s',newq)
  126. push idnum ', 0 '
  127. wow=eventsem_post(newsem)
  128. call pmprintf('SRE-http Publicurl: done resetting public-url thread ')
  129. signal on error name iserror
  130. signal on syntax name iserror
  131. signal bakme
  132.  
  133.  
  134.  
  135. exit
  136.  
  137.  
  138.  
  139. /* ---------- */
  140. fig_purl:procedure expose purls. wildpurls. 
  141.  
  142. parse upper arg needle,hostn
  143.  
  144. needle=strip(needle,'l','/')
  145. if hostn<>'' then needle=upper(strip(hostn))'//'needle
  146. needle=upper(strip(needle))
  147.  
  148. if length(needle)<=purls.!maxlen then do
  149.   if upper(purls.needle)<>upper('PURLS.'needle) then do
  150.    return purls.needle
  151.   end
  152. end
  153.  
  154. /* check wildcards -- retain "best" match */
  155. oldresu='' ;isit=0
  156. do ii=1 to wildpurls.0
  157.  resu=sref_wild_match(needle,wildpurls.ii,oldresu)
  158.  
  159.  if resu=-1 then do
  160.        isit=ii
  161.        leave
  162.  end
  163.  if resu=0 then iterate
  164.  isit=ii
  165.  oldresu=resu
  166. end
  167. if isit>0 then
  168.   return wildpurls.isit.!value
  169. else
  170.   return 0
  171.  
  172.  
  173. /* ---------------- */
  174. /* read and set up public-url*/
  175. set_purls:procedure expose purls. wildpurls. cfglist_file cfgs_dir sport defrealm
  176. parse arg afile
  177.  
  178. npurls=0
  179.  
  180. a=sref_fileread(afile,'ulines',,'E')   /* read it to a stem variable */
  181.  
  182. oo=stream(afile,'c','close')
  183. if a=0 then do
  184.       call pmprintf(' SRE-http Publicurl: could not read (empty file?) '||afile)
  185.       ulines.0=0
  186. end
  187.  
  188. foo=add_cfglist('PUBURLS','PUBURLS.IN')          /*augment the ulines stem variable, using CFGLIST.CFG files */
  189.  
  190. foo=add_defrealm('PUBURLS')
  191.  
  192. drop purls. ; drop wildpurls.
  193. purls.!maxlen=0
  194. purls.0=0
  195.  
  196. wildpurls.0=0
  197. ii=0
  198. do mm=1 to ulines.0
  199.   t1=strip(ulines.mm)
  200.  
  201.   if t1="" then iterate
  202.   if abbrev(t1,';')=1 then iterate
  203.  
  204.  
  205.   tm1=strip(word(t1,1)) ; ahost=' '
  206.   if right(tm1,2)='//' then do
  207.         ahost=upper(left(tm1,length(tm1)-2))
  208.         parse var t1 foo t1a t1b
  209.         str=t1a' 't1b
  210.         t1a=ahost'//'t1a
  211.   end
  212.   else do
  213.       parse var t1 t1a t1b
  214.       str=t1a' 't1b
  215.   end
  216.   if pos('?',t1a)>0 then do
  217.      parse var t1a q1 '?' q2
  218.      t1a=strip(translate(q1,'/','\'),'l','/')||'?'||q2
  219.   end
  220.   else do
  221.       t1a=strip(translate(t1a,'/','\'),'l','/')
  222.   end
  223.  
  224. /* this is only used for *LIST* */
  225.   ipj=purls.0+1
  226.   tt1a=t1a
  227.   if pos('//',t1a)=0 then tt1a=' //'tt1a
  228.   purls.!l.ipj=tt1a' 't1b
  229.   purls.0=ipj
  230.  
  231.  
  232.  t1a=upper(t1a)
  233.  if pos('*',t1a)>0 then do
  234.      amm=wildpurls.0+1
  235.      wildpurls.amm.!value=str
  236.      wildpurls.amm=t1a
  237.      wildpurls.0=amm
  238.  end
  239.  else do
  240.     purls.t1a=str
  241.     purls.!maxlen=max(purls.!maxlen,length(str))
  242.  end
  243.  ii=ii+1
  244. end
  245.  
  246. return ii
  247.  
  248.  
  249.  
  250. /**********************************/
  251. /* Add entries to ulines. variable, from the defrealm_temp file.
  252.   Note that this file only has the appropriate port entries
  253.   in it */
  254. add_defrealm:procedure expose defrealm ulines. 
  255.  
  256. parse upper arg atype
  257. atype=strip(atype)
  258. crlf='0d0a'x
  259.  
  260. if defrealm='' then return 0
  261. aa=sref_open_read(defrealm,15,'READ')
  262. if aa<=0 then return 0
  263.  
  264. ii=stream(defrealm,'c','query size')
  265. if ii=0 | ii='' then return 0
  266.  
  267. stuff=charin(defrealm,1,ii)
  268. foo=stream(defrealm,'c','close')
  269.  
  270. do forever
  271.   if stuff="" then return 1
  272.   parse var stuff aline (crlf) stuff
  273.   if abbrev(aline,';')=1 then iterate
  274.   parse var aline btype ':' aline
  275.   if btype<>atype then iterate
  276.   ii=ulines.0+1
  277.   ulines.ii=aline
  278.   ulines.0=ii
  279. end
  280.  
  281.  
  282.  
  283.  
  284. /**********************************/
  285. /* Add entries to ulines. variable, from the  ATYPE files listed in  the CFGLIST.CFG file
  286.    (but only for port SPORT) 
  287. */
  288.  
  289. add_cfglist:procedure expose ulines. cfglist_file sport cfgs_dir
  290. parse upper arg atype,defname
  291. atype=strip(atype)
  292.  
  293. if cfglist_file='' then return /* nothing to do */
  294.  
  295. /* look for files */
  296. foo=translate(stream(cfglist_file,'c','open read'))
  297. if abbrev(foo,'READY')<>1 then do
  298.   call pmprintf('SRE-http: ' atype ' Warning: Unable to open 'cfglist_file)
  299.   return 0            /* unable to open */
  300. end
  301. inj=stream(cfglist_file,'c','query size')
  302.  
  303.  
  304. if inj=0 | inj='' then return   0        /* empty file */
  305. astuff=charin(cfglist_file,1,inj)
  306. foo=stream(cfglist_file,'c','close')
  307.  
  308. crlf='0d0a'x
  309. astuff=astuff||crlf||' '        /* place an elephant in cairo */
  310.  
  311. /* determine which files apply to this atype and port, by readling CFGLIST.CFG */
  312. mm=0
  313.  
  314. /* note: file is organized in blocks */
  315. curport=80              /* defaults port and host */
  316. curhost=''
  317. curfile=''
  318.  
  319. do forever              /* for all blocks in file */
  320.   if astuff='' then leave       /* all done  (note we always put an elephant in cairo */
  321.   parse var astuff aline (crlf) astuff
  322.   if abbrev(aline,';')=1 then iterate   /* ignore comments */
  323.   if aline='' then do           /* block end */
  324.      if curport<>sport | curfile='' then do     /* different port, or this atype file not specified */ 
  325.         nop 
  326.      end
  327.      else do        /* otherwise, add this entry to filelist */
  328.         mm=mm+1
  329.         ufiles.mm.!host=curhost
  330.         ufiles.mm.!file=curfile
  331.      end
  332.      curport=80 ; curhost='' ; curfile=''  /* clear block */
  333.      iterate
  334.   end
  335.  
  336. /* process an entry in this block */
  337.   parse upper var aline ltype ':' lstuff ;ltype=strip(ltype) ; lstuff=strip(lstuff)
  338.  
  339.   select
  340.     when ltype='PORT' then curport=lstuff
  341.     when ltype='HOST' then curhost=lstuff
  342.     when abbrev(ltype,atype)=1  then do
  343.  
  344.        curfile=strip(translate(lstuff,'\','/'),'l','\')
  345.        if pos(':',curfile)=0 then curfile=cfgs_dir||'\'||curfile
  346.  
  347.     end
  348.     when ltype='*'  then do
  349.        curfile=strip(translate(lstuff,'\','/'),,'\')
  350.        if pos(':',curfile)=0 then curfile=cfgs_dir||'\'||curfile
  351.        curfile=stream(curfile'\'defname,'c','query exists')
  352.     end
  353.  
  354.     otherwise nop
  355.   end
  356.  
  357. end
  358.  
  359. /* done reading cfglist; add entries from appropriate files */
  360. if mm=0 then return 1     /* no auxillary files of this type */
  361. ufiles.0=mm
  362.  
  363. do mm=1 to ulines.0              /* retain old uline */
  364.   ULINEStmp.mm=ulines.mm
  365. end
  366. ULINEStmp.0=ulines.0
  367.  
  368.  
  369. do mm=1 to ufiles.0
  370.   afile=ufiles.mm.!file
  371.   ahost=ufiles.mm.!host
  372.  
  373.   a=sref_fileread(afile,'ulines',,'E')   /* read it to a stem variable */
  374.  
  375.   if a=0 | ulines.0=0 then do
  376.       call pmprintf(' SRE-http 'atype': WARNING**: bad auxillary file: '||afile)
  377.       iterate
  378.   end
  379.    
  380.    call pmprintf('SRE-http 'atype': adding from 'AHOST ' specific file 'afile ',' ulines.0)
  381.  
  382.    do ii=1 to ulines.0
  383.         aline=strip(ulines.ii)
  384.         if ALINE=' ' then iterate
  385.         aline=translate(aline,' ','0009'x)
  386.         if  abbrev(strip(aline),';')=1 then iterate
  387.         parse var aline aw1 .
  388.         if pos('//',aw1)>0 then do
  389.             call pmprintf('SRE-http: 'atype 'warning: disallowed auxillary entry: 'aline)
  390.             iterate
  391.         end
  392.         if ahost<>'' then aline=ahost||'// '||aline
  393.         ii2=ULINEStmp.0+1
  394.         ULINEStmp.ii2=aline
  395.         ULINEStmp.0=ii2
  396.    end
  397. end
  398.  
  399. do mm=1 to ULINEStmp.0
  400.    ulines.mm=ULINEStmp.mm
  401. end
  402. ulines.0=ULINEStmp.0
  403. drop ULINEStmp.
  404. return 1
  405.  
  406.  
  407.   
  408.