summaryrefslogtreecommitdiff
path: root/appl/svc/httpd/redirect.b
diff options
context:
space:
mode:
authorCharles.Forsyth <devnull@localhost>2006-12-22 17:07:39 +0000
committerCharles.Forsyth <devnull@localhost>2006-12-22 17:07:39 +0000
commit37da2899f40661e3e9631e497da8dc59b971cbd0 (patch)
treecbc6d4680e347d906f5fa7fca73214418741df72 /appl/svc/httpd/redirect.b
parent54bc8ff236ac10b3eaa928fd6bcfc0cdb2ba46ae (diff)
20060303a
Diffstat (limited to 'appl/svc/httpd/redirect.b')
-rw-r--r--appl/svc/httpd/redirect.b130
1 files changed, 130 insertions, 0 deletions
diff --git a/appl/svc/httpd/redirect.b b/appl/svc/httpd/redirect.b
new file mode 100644
index 00000000..cb3ed478
--- /dev/null
+++ b/appl/svc/httpd/redirect.b
@@ -0,0 +1,130 @@
+implement Redirect;
+
+include "sys.m";
+ sys : Sys;
+
+include "bufio.m";
+ bufio : Bufio;
+Iobuf : import bufio;
+
+include "string.m";
+ str : String;
+
+include "redirect.m";
+
+HASHSIZE : con 1019;
+
+
+Redir: adt{
+ pat, repl : string;
+};
+
+tab := array[HASHSIZE] of list of Redir;
+
+
+hashasu(key : string,n : int): int
+{
+ i,h : int;
+ i=0;
+ h=0;
+ while(i<len key){
+ h = 10*h + key[i];
+ h%= n;
+ i++;
+ }
+ return h;
+}
+
+insert(pat, repl : string)
+{
+ hash := hashasu(pat,HASHSIZE);
+ tab[hash]= Redir(pat, repl) :: tab[hash];
+}
+
+redirect_init(file : string)
+{
+ sys = load Sys Sys->PATH;
+ line : string;
+ flist : list of string;
+ n : int;
+ bb : ref Iobuf;
+ for(n=0;n<HASHSIZE;n++)
+ tab[n]= nil;
+ stderr := sys->fildes(2);
+ bufio = load Bufio Bufio->PATH;
+ if (bufio==nil){
+ sys->fprint(stderr,"redirect: cannot load %s: %r\n", Bufio->PATH);
+ raise "fail:bad module";
+ }
+ str = load String String->PATH;
+ if (str==nil){
+ sys->fprint(stderr,"redirect: cannot load %s: %r\n", String->PATH);
+ raise "fail:bad module";
+ }
+ bb = bufio->open(file,bufio->OREAD);
+ if (bb==nil)
+ return;
+ while((line = bb.gets('\n'))!=nil){
+ line = line[0:len line -1]; #chop newline
+ if (str->in('#',line)){
+ (line,nil) = str->splitl(line, "#");
+ if (line!=nil){
+ n = len line;
+ while(line[n]==' '||line[n]=='\t') n--;
+ # and preceeding blanks
+ line = line[0:n];
+ }
+ }
+ if (line!=nil){
+ (n,flist)=sys->tokenize(line,"\t ");
+ if (n==2)
+ insert(hd flist,hd tl flist);
+ }
+ }
+
+}
+
+lookup(pat : string): ref Redir
+{
+ srch : list of Redir;
+ tmp : Redir;
+ hash : int;
+ hash = hashasu(pat,HASHSIZE);
+ for(srch = tab[hash]; srch!=nil; srch = tl srch){
+ tmp = hd srch;
+ if(tmp.pat==nil)
+ return nil;
+ if(pat==tmp.pat)
+ return ref tmp;
+ }
+ return nil;
+}
+
+
+redirect(path : string): string {
+ redir : ref Redir;
+ newpath, oldp : string;
+ s : int;
+ if((redir = lookup(path))!=nil)
+ if(redir.repl==nil)
+ return nil;
+ else
+ return redir.repl;
+ for(s = len path - 1; s>0; s--){
+ if(path[s]=='/'){
+ oldp = path[s+1:];
+ path = path[0:s];
+ if((redir = lookup(path))!=nil){
+ if(redir.repl!=nil)
+ newpath=sys->sprint("%s/%s",
+ redir.repl,oldp);
+ else
+ newpath = nil;
+ path = path+"/"+oldp;
+ return newpath;
+ }
+ path = path+"/"+oldp;
+ }
+ }
+ return nil;
+}