home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / ONLINE / SREFV12J / SREFQUIK.80 < prev    next >
Text File  |  1997-05-14  |  12KB  |  363 lines

  1. /* sre-filter ver 1.2j
  2. Note that in many cases, this will call the SREF_MAINROUTINE(...)
  3.  
  4. But, by having a small filter grab many of the requests, we
  5. can save substantial amounts of time.
  6.  
  7. ----------------------------------------
  8. SREFQUIK specific user-changable parameters:
  9.  
  10. NO_RECORDS.1, ..., NO_RECORDS.n and NO_RECORDS.0
  11.    A stem variable containing selectors that should NOT be subject
  12.    to post-filtering.
  13.  
  14.    Use of NO_RECORDS complements  NORECORD public_urls (it's
  15.    quicker to check then the PUBLIC_URLS themself).
  16.  
  17.    Furthermore, cached hits (or literal public_Urls) will NOT
  18.    check the ACCESS_FILE for the NO_POSTFILTER permission. You can
  19.    use the NO_RECORDS. variables as a substitute.
  20.  
  21.    NOTE:
  22.      *   Too speed up processing, abbreviation matching is done.  That is,
  23.          wildcards are NOT recognized.
  24.          For example: A NO_RECORDS value of DIR1/FOO will match both the
  25.                       DIR1/FOO.HTM and DIR1/FOO2/BAR.HTM selectors.
  26.      *   In other words: do NOT include * in the NO_RECORDS. stem variables
  27.      *   You MUST include a NO_RECORDS.0=n variable, where n is the
  28.          number of NO_RECORDS variables you've specified.
  29.  
  30. NO_RECORDS_ONLY
  31.    To further speed up processing, you can suppress looking for
  32.    NORECORD PUBLIC_URLS.  If NO_RECORDS_ONLY=1, then just the
  33.    NO_RECORDS. variables are checked. Of course, this does mean
  34.    being sure you have the appropriate NO_RECORDS. entries.
  35.  
  36. */
  37.  
  38. no_records.0=0          /* no_records.0 MUST be specified */
  39. no_records.1='/img'
  40. no_records_only=0       /* 1=only look in no_records, 0=also look in public_urls */
  41.  
  42.  
  43. /******************** End of user changable parameters *******/
  44. parse arg source, request , seloriginal
  45.  
  46. if request=" " then return 'NODATA'   
  47.  
  48. source0=source ; request0=request ;seloriginal0=seloriginal ;
  49. parse var source myaddr port transaction who whoport .
  50. os2e='os2environment'
  51. response_status=' '
  52. no_rec=-1
  53.  
  54. /*-check on libraries first */
  55. foo=rxfuncquery('sysloadfuncs')
  56. if foo=0 then
  57.    foo=rxfuncquery('rexxlibregister')
  58. if foo<>0 then
  59.   signal dogoaway
  60.  
  61. /* ---------Get stuff sent from GOSERVE program -----------------*/
  62. parse var request foo sel .
  63. sel=strip(strip(sel,'l','/') )             /* get rid of leading / */
  64. parse var request verb uri protocol .        /* split up the request line */
  65.  
  66. enmadd="SREF_"||port||"_"
  67. basesem='\SEM32\'||enmadd
  68. ddir=0
  69.  
  70. /* ---- Do we need to initialize sre-filter ?  --- */
  71. sem_status=eventsem_query(basesem)
  72. if sem_status<0 then
  73.   signal dogoaway
  74.  
  75.  
  76. issent=completed()
  77. if issent=0 then do
  78.   loadthreshold=value(enmadd||'loadthreshold',,'os2environment')
  79.   backupserverlist=value(enmadd||'backupserverlist',,'os2environment')
  80.   save_state=value(enmadd||'save_state',,'os2environment')
  81.   if loadthreshold>0 & BackupServerList \= '' then do
  82.     'EXTRACT CLIENTS'
  83.      if Clients > LoadThreshold then do
  84.           i1=transaction//(words(backupserverlist)+1)
  85.           if i1=0 then i1=1
  86.           auri=word(backupserverlist,i1)
  87.           auri2='http://'||auri||'/'||sel
  88.           'HEADER ADD Location:' auri2
  89.           'RESPONSE HTTP/1.0 302 Moved Temporarily'  /* Set HTTP response line */
  90.           'STRING  Redirect to backup server at ' auri
  91.           exit             /* no postfiltering */
  92.      end
  93.   end
  94. end                     /* end of load balancing check */
  95.  
  96. /* save some request specific variables? */
  97. if save_state=1 then do
  98.    foo=sref_save_state(source,request,seloriginal,enmadd,port)
  99. end
  100.  
  101. /* if call a prefilter, go to main routine */
  102.  
  103. pre_filter=value(enmadd||'PRE_FILTER',,'os2environment')
  104. if pre_filter="FIRST" then
  105.    signal dogoaway
  106.  
  107.  
  108. nhosts=value(enmadd||'HOSTS.0',,'os2environment')
  109. if nhosts>0 then do
  110.    signal dogoaway
  111. end
  112.  
  113. /* 2) MAKE SURE THE REQUEST IS STILL ACTIVE!!
  114.      (IF CACHING OCCURED, IT MIGHT NOT BE !! */
  115.  
  116. if issent<>0 then do
  117.  
  118.    if check_norec(seloriginal)=1 then return ' '
  119.  
  120.    post_filter_message="SREFQUIK: Cached file sent:"||seloriginal
  121.  
  122.    if no_records_only=1 then signal do_post_filter
  123.  
  124. /* is it a public url with NORECORD option */
  125.     oof=upper(seloriginal)
  126.     call diinky oof
  127.     if pos('NORECORD',Useliteral)>0 then return ' '
  128.     signal do_post_filter
  129. END
  130.  
  131. /* 3) Check for basic grooviness */
  132. if left(protocol,4)\='HTTP' & protocol\='' then
  133.    signal dogoaway
  134.  
  135. /* 4) Is it one of the literal public_urlS ??. */
  136. tsel=translate(sel)
  137. call diinky  tsel           /* use globals */
  138.  
  139. if gotit>0 then do        /* if gotit, then reset the sel */
  140. /* if it's a literal public file, return it now! */
  141.   if abbrev(useliteral,"LITERAL")=1 then do
  142.       if exact_name<>" " then do
  143.           afile=exact_name
  144.       end
  145.       else do
  146.           parse var usesel asel '?' .
  147.           ddir=datadir()
  148.           afile=translate(ddir||asel,'\','/')
  149.           if pos('~',afile)>0 then do
  150.              home_dir=value(enmadd||'HOME_DIR',,os2e)
  151.              afile=sref_replacestrg(afile,'~',home_dir) ;
  152.           end
  153.       end
  154.       ii=stream(afile,'c','query exists')
  155.       if ii="" then do
  156.           transaction="NOFILE_"||transaction
  157. /* reset to original on error */
  158.           source=source0 ; seloriginal=seloriginal0 ; request=request0 ;
  159.           signal dogoaway
  160.       end
  161.       else do
  162.          atype=sref_mediatype(afile)
  163.          'FILE TYPE ' atype ' NAME ' afile
  164.          post_filter_message="Literal public_URL returned:" ||afile
  165.          response_status='200 '||dosdir(afile,'s')
  166.          signal do_post_filter
  167.       end
  168.   end           /* useliteral */
  169. /* non literal public url */
  170.  
  171.    transaction="PUBFILE_"||transaction
  172.    source=myaddr' 'port' 'transaction' 'who' 'whoport
  173.    seloriginal='_'||USELITERAL||','||usesel' 'seloriginal
  174. end   /* gotit */
  175.  
  176. /* if here, not a literal... */
  177. dogoaway:  /*  call the main filter (with perhaps flags to indicate issues */
  178. /* check on advanced REXX functions */
  179. foo=rxfuncquery('sysloadfuncs')
  180. if foo=1 then do
  181.   call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  182.   call SysLoadFuncs
  183. end
  184. foo=rxfuncquery('rexxlibregister')
  185. if foo=1 then do
  186.  call rxfuncadd 'rexxlibregister','rexxlib', 'rexxlibregister'
  187.  call rexxlibregister
  188. end
  189.  
  190. aroutine='SREF_MAIN_'||port
  191. foo=load_bigone(aroutine,port)
  192.  
  193. signal on error name  yike2 ; signal on syntax name yike2 ;
  194. wow2=' '
  195. goaway='wow='||aroutine||'(source,request,seloriginal)'
  196. interpret goaway
  197. wow2=wow
  198. yike2: ;
  199. return wow2
  200.  
  201.  
  202. /* jump here after completion code -- do record option and/or post-filter */
  203. do_post_filter:
  204. /* check the no_records?*/
  205. if no_rec=-1 then  do
  206.    if check_norec(seloriginal)=1 then return ' '
  207. end
  208.  
  209. IF RESPONSE_STATUS=' '  THEN  RESPONSE_STATUS='200 '||EXTRACT('BYTESSENT')
  210.  
  211. /*check for 400 status */
  212. are1=strip(word(response_status,1))
  213. if are1>399 then
  214.    record_option='NO'
  215. else
  216.    record_option=strip(value(enmadd||'RECORD_OPTION',,os2e))
  217.  
  218.  
  219. post_filter=strip(value(enmadd||'POST_FILTER',,os2e))
  220. write_logs=strip(value(enmadd||'write_logs',,os2e))
  221.  
  222. if useliteral="LITERAL_NORECORD" then do
  223.    if save_state=1 then foo=sysfiledelete(tcachfile)
  224.    return ' '
  225. end
  226.  
  227. if record_option="NO" & post_filter=0 & write_logs=0 then  do /* nothing to do in post filter */
  228.    if save_state=1 then foo=sysfiledelete(tcachfile)
  229.    return ' '
  230. end
  231.  
  232. a=rxqueue('s',enmadd||'POSTF')
  233. record_all_file=value(enmadd||'RECORD_ALL_FILE',,os2e)
  234. postfilter_name=value(enmadd||'POSTFILTER_NAME',,os2e)
  235. if ddir=0 then ddir=datadir()
  236. tempfile=ddir'$'transaction'.'port                      /* Often used */
  237. servername=servername()
  238.  
  239. browser=' ';authuser=' '; clientname0=who
  240.   thereferer=reqfield('referer')
  241. if write_logs=1 then do
  242.   clientname0=clientname()
  243.   browser=reqfield('user-agent')
  244.   goo=reqfield('AUTHORIZATION:')
  245.   authuser='-'
  246.   if goo<>" "  then do
  247.       parse var goo . m64 .              /* get the encoded cookie */
  248.       dec=pack64(m64)                       /* and decode it */
  249.       parse upper var dec authuser ':' .      /* split to userid */
  250.   end
  251. end
  252.  
  253. fuse=' '
  254. if save_state=1 then do
  255.    tdir=strip(TEMPDATA_DIR,'t','\')||'\'
  256.    fuse=dostempname(tdir||'_T??????.TMP')
  257.    foo2=dosrename(tcachfile,fuse)
  258.    if foo2=0 then fuse=' '
  259. end
  260.  
  261. selrec=sref_replacestrg(seloriginal,',','%2c','ALL')
  262. request0=sref_replacestrg(request,',','%2c','ALL')
  263. post_filter_message=sref_replacestrg(post_filter_message,',','%2c','ALL')
  264. push  record_option ',' record_all_file ',' post_filter ',' postfilter_name ',' port ',' ,
  265.                  post_filter_message ',' source ',' request0 ',' selrec ',' ,
  266.                 tempfile ','  servername ','  ',' afile ',' who ',' ,
  267.                 enmadd ',' thereferer ',' fuse ',' RESPONSE_STATUS ',' write_logs ',' clientname0 ',' authuser ',' browser
  268.  
  269. aa=eventsem_post(basesem||'POSTF')
  270.  
  271. return ' '  /* note that postfilter and recorder don't need to tell the filter anything*/
  272.  
  273.  
  274.  
  275.  
  276.  
  277. /* ------------------------------------------------- */
  278. /* Routine to load srefbig, if neessary */
  279. /* ------------------------------------------------- */
  280.  
  281. load_bigone:procedure expose transaction
  282. parse arg aroutine,aport
  283. if transaction=1 then
  284.   foo=macrodrop(aroutine)   /* makesure you refresh on first transaction */
  285.  
  286. foo=macroquery(aroutine)
  287. if foo="" | foo=0 then do
  288.   say " SREFQUIK: Loading main SRE-Filter code into macrospace "foo
  289.   aff='SREFILTR.'||aport
  290.   foo2=macroadd(aroutine,aff)
  291.   if foo2=0 then do
  292.         say " error loading sre-filter main program into macrospace "foo2
  293.         'string Server is busy, try again later '
  294.          exit ' '
  295.   end
  296. end
  297. return 0
  298.  
  299.  
  300.  
  301. /*********/
  302. /* check the no_records. list. Return 1 if "norecord" pertains */
  303. check_norec:procedure expose no_records. no_rec enmadd os2e
  304. parse upper arg s1
  305.  
  306. no_rec=strip(value(enmadd||'NO_NO_RECORD',,os2e))
  307. select              /* deal with !directives */
  308.   when abbrev(s1,'!NORECORD') & no_rec<>1 then return 1  /* enuf said */
  309.   when abbrev(upper(s1),'!SENDAS_')=1 then do /* strip !sendas_ */
  310.          ooy=pos('/',s1)
  311.          ooy2=pos('?',s1)
  312.          if ooy2>0 & ooy2< ooy then ooy=ooy2
  313.          s1=substr(s1,ooy+1)
  314.   end
  315.   when abbrev(upper(s1),'!FORCE')=1 then s1=substr(s1,8)
  316.   otherwise nop
  317. end
  318. do mm=1 to no_records.0
  319.   al=strip(translate(no_records.mm,'/','\'))
  320.   al=upper(strip(al,'l','/'))
  321.   if abbrev(s1,al)=1 then return 1
  322. end
  323. return 0
  324.  
  325.  
  326.  
  327. /********************************/
  328. diinky:
  329. parse arg tsel
  330. gotit=0 ; doexact=0 ; asterat=0 ; afteraster=0
  331. public_urls.0=value( enmadd||'public_urlS.0',,'os2environment') ;
  332. do m=1 to public_urls.0
  333.   public_urls.m=value( enmadd||'public_urlS.'||m,,'os2environment' );
  334.   parse var public_urls.m  purl  isliteral  aname0
  335.   purl=strip(purl) 
  336.   ares=sref_wildcard(tsel,purl||' '||aname0,doexact)
  337.   parse var ares astat "," afname ;  astat=strip(astat)
  338.  
  339.   if astat=0 then iterate   /* no match */
  340.   if astat=1 then do
  341.       usesel=sel ;
  342.       useliteral=strip(upper(isliteral)) ;
  343.       exact_name=' '
  344.       if aname0<>' ' then exact_name=strip(afname)
  345.       gotit=m
  346.       leave           /*first exact match rules */
  347.   end
  348. /* astat=2;; non exact: see if it's a "better match" (more chars before *) */
  349.     foo1=pos('*',purl)
  350.     if foo1 >= asterat then do
  351.        tmp1=length(purl)-foo1
  352.        if foo1>asterat | tmp1>afteraster then  do
  353.           asterat=foo1 ; afteraster=tmp1
  354.           usesel=sel ;
  355.           useliteral=strip(upper(isliteral)) ;
  356.           exact_name=' '
  357.           if aname0<>' ' then exact_name=strip(afname)
  358.           gotit=m
  359.        end              /* either condition */
  360.     end                 /* ge asterat */
  361. end
  362. return 0
  363.