home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 35 Internet
/
35-Internet.zip
/
srev13g.zip
/
sreproxy.rxx
< prev
next >
Wrap
Text File
|
1998-12-17
|
9KB
|
340 lines
/* sre=proxy caching daemon */
/* ----------- User Configurable Parameters */
/*BEGIN --- */
/* default max-age (if none specified), in seconds */
max_age_default=86400
/* maximum number of selectors to cache */
cache_size=1250
/*END --- */
/* ----------- End Configurable Parameters */
parse upper arg tdir,usequeue , USESEM, max_semwait,port,dafilter
crlf='0d0a'x
signal on error name perr ; signal on syntax name perr
storage_dir=strip(storage_dir,'t','\')
verbose=value('SREF_'port'_VERBOSE',,'OS2ENVIRONMENT')
if verbose='' then verbose=1
if verbose<2 then verbose=0
numeric digits 11
/* no initializations, just wait for requests:
1) L - lookup something in cache
2) A - add to cache
3) R - remove expired entries from cache
4) S - construct and return list of current cache entries
*/
/* load srefiltr into macrospace */
oo=macroadd('SREF_SREFILTR',dafilter,'b')
if oo=0 then do
call pmprintf('SRE-http: Proxy error, could not load main filter into macrospace ')
/* do NOT set sref_proxy; hence, sreproxy.80 will ALWAYS call srefiltr.80 */
exit
end
else do
call pmprintf(' SRE-http Proxy: SREFILTR loaded into macrospace ')
end
cshl='' /* entries in cache (space delimited) */
aa=value('SREF_PROXY_BYTES',0,'os2environment') /* enable proxy */
aa=value('SREF_PROXY_HITS',0,'os2environment') /* enable proxy */
aa=value('SREF_PROXY',1,'os2environment') /* enable proxy */
aa=value('SREF_PROXY_BYTES2',0,'os2environment') /* enable proxy */
aa=value('SREF_PROXY_HITS2',0,'os2environment') /* enable proxy */
/* Start Infinite Loop */
bakme:
a=rxqueue('s',usequeue)
aq=queued()
if aq=0 then do
WOW=EVENTSEM_WAIT(USESEM,max_semwait)
aq=-1
if wow=640 then do
signal bakme
verbose=value('SREF_'port'_VERBOSE',,'OS2ENVIRONMENT')
if verbose<2 then verbose=0
end
IF WOW<>0 THEN do /* FATAL ERROR */
call pmprintf(' SRE-http Sreproxy ERROR: fatal semaphore error: 'wow)
FOO=VALUE('SREF_PROXY',-2,'OS2ENVIRONMENT') /* DISALBE SREPROXY */
EXIT
end
end
wow=EVENTSEM_RESET(usesem)
if aq=-1 then do
if queued()=0 then signal bakme
end
PARSE pull isit0
isit0=translate(isit0,' ','0009'x)
if isit0=" " then signal bakme
/* die command? */
if abbrev(isit0,'*DIE*')=1 then
exit
goobs:
nowtime=date('b')+ (time('s')/(24*60*60))
parse var isit0 idnum ',' newq ',' newsem ','type','lookfor
TYPE=UPPER(STRIP(TYPE))
if lookfor='' then lookfor='/'
if type='L' then do /* look for */
if verbose>1 then do
call pmprintf_sref(' SRE-http Proxy: looking for: 'lookfor)
end
lookfor=upper(space(upper(lookfor),0))
crc1=stringcrc(lookfor)
if cshl.crc1.!got<>1 then do
a=rxqueue('s',newq)
push idnum ',' 0
wow=eventsem_post(newsem)
signal bakme
end
/* got an entry, check expiration */
atail=crc1
aexp=cshl.atail.!EXP
asel=cshl.atail.!sel
if aexp< nowtime | asel<>lookfor then do /* expired entry, or bad crc */
if verbose>3 then call pmprintf(' SRE-http Proxy: Expired sreproxy entry: 'crc1)
cshl.atail.!GOT=0
a=rxqueue('s',newq)
push idnum ',' 0
wow=eventsem_post(newsem)
signal bakme
end
/* got a match, it's not expired or bad crc.
Return cache_Type,filename,type,lastmod,etag,maxage,headers (crlf delimited ) */
/* maxage is used in sreproxy, and shrinks to keep cache-control consistent to
original posting */
maxage=trunc( (aexp-nowtime)*60*60*24)
oo=cshl.atail.!ct','cshl.atail.!reqp||crlf||cshl.atail' , 'cshl.atail.!tf||crlf|| ,
cshl.atail.!type|| ,
crlf||cshl.atail.!lmod||crlf|| ,
cshl.atail.!etag||crlf||maxage','cshl.atail.!npf||crlf||cshl.atail.!hdr
a=rxqueue('s',newq)
push idnum ','oo
wow=eventsem_post(newsem)
signal bakme
end
if type='A' then do /* add an entry -- no return expected */
/* read filename, age, headers. Convert age to an expiration relative to now */
call add_cache
end
if type='SHOW' then do /* reset entries -- no return expected */
call showit lookfor
end
if WORDPOS(TYPE,'R RESET')>0 then do /* CALLED ON RESET */
call CLEANUP /* NOTHING TO RETURN */
end
signal bakme
perr:
call pmprintf(' sreproxy.rxx error at 'sigl': 'RESULT','RC)
FOO=VALUE('SREF_PROXY',-2,'OS2ENVIRONMENT') /* DISABLE SREPROXY */
exit
/*************************************************************/
/* add stuff to cache */
/* 1: Add host_nickname//selector to cshl
2: Create a host_nickname//selector cshl. entry with
atail=stringcrc(selector)
cshl.atail=absolute file name
cshl.atail.!sel=selector
cshl.atail.!EXP=expire date (julian)
cshl.atail.!type=type
cshl.atail.!etag=etag
cshl.atail.!lmod=last modification date
cshl.atail.!npf=nopostfilte flag
cshl.atail.!ct=cache type (static or semi dynamic ) & goserve cacheable flag
cshl.atail.!reqp= required priviliegs (if ct =2 or 4 )
cshl.atail.!tf = trigger file or timestamp
cshl.atail.!hdr=misc headers
*/
add_cache: /* stuff sent here by sref_gos */
/* first, parse stuffout (from queued input),
all crlf delimited */
/* note: cshtype: 0= public static , 1= public semi-dynamic
2= private static, 3=private semi-dynamic
0 1= public static & goserve cacheable
*/
parse var lookfor cshtype0 (crlf) asel (crlf) absfile0 (crlf) amaxage (crlf) lastmod (crlf) type (crlf) etag ,
(crlf) headers
parse var absfile0 absfile ',' trigfile
if asel='' then asel='/'
asel=upper(space(upper(asel),0))
parse var amaxage maxage ',' nopostf
parse var cshtype0 cshtype ',' reqprivs
if length(asel)>100 then return 0 /* selector too long to store */
crc1=stringcrc(asel)
if cshl.crc1.!got=1 then return 0 /* crc being used (should never happen */
if verbose>0 then call pmprintf_sref(' SRE-http Proxy: adding: 'asel)
if maxage<0 then max_age=0
if maxage='' | datatype(maxage)<>'NUM' then maxage=max_age_default
expires=nowtime+(maxage/(60*60*24))
if type='' then type='application/octet-stream'
/* trigfile is either a trigger file (i.e.; the original of an ssi cached file), or
the time stamp of a static document */
/* cache too big? */
if length(cshl)>(cache_size*4) then do /* max length of cache */
zz=trunc(cache_size/10)*4
do mmx=1 to zz by 4 /* get rid of first 10% */
att=substr(cshl,mmx,4)
cshl.att.!got=0
end
call cleanup
end
/* add stuff to cshl. */
atail=crc1
cshl.crc1.!got=1
cshl.crc1.!sel=asel
cshl.atail=absfile
cshl.atail.!EXP=expires
cshl.atail.!type=type
cshl.atail.!etag=etag
cshl.atail.!lmod=lastmod
cshl.atail.!npf=nopostf
cshl.atail.!ct=cshtype
cshl.atail.!reqp=reqprivs
cshl.atail.!hdr=headers
cshl.atail.!tf=trigfile
cshl=cshl||atail /* list of cache identifiers */
return 1
/************/
/* remove expired entries from cache */
cleanup:
verbose=value('SREF_'port'_VERBOSE',,'OS2ENVIRONMENT')
if verbose>0 then call pmprintf_sref(' SRE-http Proxy: removing old entries ')
new1=''
nold=length(cshl)/4
do mm=1 to length(cshl) by 4
atail=substr(cshl,mm,4)
atim=cshl.atail.!EXP
if atim<nowtime | cshl.atail.!got<>1 then do /* zap this one */
drop cshl.atail.!SEL
drop cshl.atail.!GOT
drop cshl.atail.!EXP
drop cshl.atail.!type
drop cshl.atail.!etag
drop cshl.atail.!lmod
drop cshl.atail.!ct
drop cshl.atail.!reqp
drop cshl.atail.!tf
drop cshl.atail.!hdr
drop cshl.atail
end
else do
new1=new1||atail
end
end
cshl=new1
nnew=length(new1)/4
if verbose>0 then
call pmprintf_sref('SRE-Proxy thread: retained '||nnew||' of 'nold' entries')
return 1
/***************/
/* write out current status */
showit:
parse arg alist
parse var alist i1 i2
ll1=length(cshl)/4
if datatype(i1)<>"NUM" then i1=1
if datatype(i2)<>"NUM" then i2=length(cshl)/4
if i2<i1 then do
i2=ll1 ; i1=1
end
if i1<0 then i1=1 ; if i2>ll1 then i2=ll1
crlf='0d0a'x
mess=' There are currently '||ll1|| ' entries in the SREPROXY cache.'crlf||crlf
do il=i1 to i2
istart=1+((il-1)*4)
atail=substr(cshl,istart,4)
if cshl.atail.!GOT<>1 then iterate
tt=il||' : 'cshl.atail.!sel
tt=tt' File:'cshl.atail
parse var cshl.atail.!ct actt .
if actt=1 | actt=3 then tt=tt' (semi-dynamics)'
if actt=2 | acct=4 then tt=tt' (private)'
tt=tt||crlf
etime=sref_gmt(0,cshl.atail.!exp,'I',0)
tt=tt' Expires: '||etime
if cshl.atail.!tf<>'' then do
tt=tt' :: TimeStamp/Triggerfile: 'cshl.atail.!tf
end
tt=tt||crlf
mess=mess||tt
end
a=rxqueue('s',newq)
push idnum ','mess
wow=eventsem_post(newsem)
return 0