changelog shortlog tags branches changeset files revisions annotate raw help

Mercurial > hg > werc / bin/werc.rc

changeset 634: 44d651b498d4
parent: 899a5097436e
child: 167d7cba8e73
author: Uriel <u@berlinblue.org>
date: Mon, 24 May 2010 23:35:08 +0000
permissions: -rwxr-xr-x
description: Handle https urls consistently in redirects.
1 #!/usr/local/plan9/bin/rc
2 . ./cgilib.rc
3 . ./werclib.rc
4 . ./wercconf.rc
5 . ./corehandlers.rc
6 . ./fltr_cache.rc
7 cd ..
8 
9 forbidden_uri_chars='[^a-zA-Z0-9_+\-\/\.,:]'
10 difs=$ifs # Used to restore default ifs when needed
11 
12 # Expected input: ls -F style, $sitedir/path/to/files/
13 # <ls -F+x><symlink hack><Useless?><hiden files >
14 dirfilter='s/\*$//; s,/+\./+,/,g; s,^\./,,; /\/[._][^\/]/d; /'$forbidden_uri_chars'/d; /\/sitemap\.xml$/d; /\/index\.(md|html|txt|tpl)$/d; /\/(robots|sitemap)\.txt$/d; /_werc\/?$/d; '
15 dirclean=' s/\.(md|html|txt)$//; '
16 
17 # Careful, the proper p9p path might not be set until initrc.local is sourced
18 path=(. $PLAN9/bin ./bin /bin /usr/bin)
19 
20 headers=lib/headers.tpl
21 res_tail='</body></html>'
22 http_content_type='text/html'
23 ll_add handlers_bar_left nav_tree
24 werc_apps=( apps/* )
25 werc_root=`{pwd}
26 sitesdir=sites
27 
28  . ./etc/initrc
29 
30 if(test -f etc/initrc.local)
31  . ./etc/initrc.local
32 
33 for(a in $werc_apps)
34  . ./$a/app.rc
35 
36 fn werc_exec_request {
37  site=$SERVER_NAME
38  base_url=http://$site
39  sitedir=$sitesdir/$site
40  master_template=`{get_lib_file default_master.tpl}
41  current_date_time=`{date}
42 
43  # Note: $REQUEST_URI is not officially in CGI 1.1, but seems to be de-facto
44  # Note: We only urldecode %5F->'_' because some sites (stackoverflow.com?) urlencode it in their links,
45  # perhaps we should completel urldecode the whole url.
46  req_path=`{echo -n $REQUEST_URI | sed 's/\?.*//; s!//+!/!g; s/%5[Ff]/_/g; s/'^$forbidden_uri_chars^'//g; s/\.\.*/./g; 1q'}
47  req_url=$base_url^$req_path
48  local_path=$sitedir$req_path
49  local_file=''
50  ifs='/' { args=`{echo -n $req_path} }
51 
52  # Preload post args for templates where cgi's stdin is not accessible
53  if(~ $REQUEST_METHOD POST) {
54  load_post_args
55  login_user
56  }
57 
58  if(~ $req_path */index)
59  perm_redirect `{echo $req_path | sed 's,/index$,/,'}
60 
61  if(~ $local_path */) {
62  if(test -d $local_path)
63  local_path=$local_path^'index'
64  # XXX: This redir might step on apps with synthetic dirs.
65  if not if(ls `{basename -d $local_path}^* >/dev/null >[2]/dev/null)
66  perm_redirect `{echo $req_path|sed 's,/+$,,'}
67  }
68  if not if(~ $req_path *'.' *',' *';' *':')
69  perm_redirect `{echo $req_path | sed 's/[.,;:)]$//'}
70  if not if(test -d $local_path)
71  perm_redirect $req_path^'/'
72 
73  if(! ~ $#args 0)
74  ifs=$NEW_LINE { pageTitle=`{ echo $args|sed -e 's/ / - /g' -e 's/([a-z])-([a-z])/\1 \2/g' -e 's/_/ /g' } }
75 
76  cd $sitedir
77  req_paths_list='/' # Note: req_paths_list doesn't include 'stnythetic' dirs.
78  conf_wd='/' # Used in config files to know where we are in the document tree.
79  if(test -f _werc/config)
80  . _werc/config
81  for(i in $args) {
82  conf_wd=$conf_wd^$i
83  req_paths_list=($req_paths_list $conf_wd)
84  if(test -d $i) {
85  conf_wd=$conf_wd'/'
86  cd $i
87  if(test -f _werc/config)
88  . _werc/config
89  }
90  }
91  cd $werc_root
92 
93  if(~ $#perm_redir_to 1)
94  perm_redirect $perm_redir_to
95  for(l in $perm_redir_patterns) {
96  p=$$l
97  r=$p(1)
98  # If target is absolute, then patern must match whole string
99  if(~ $p(2) http://* https://*)
100  r='^'$r
101  t=`{ echo $req_path | sed 's!'^$r^'!'^$p(2)^'!' } # Malicious danger!
102 
103  if(! ~ $"t '' $req_path)
104  perm_redirect $t
105  }
106 
107  setup_handlers
108 
109 
110  # Set Page title
111  if(! ~ $local_file '') {
112  t=`{get_file_title $local_file}
113  if(! ~ $"t '')
114  pageTitle=$t
115  }
116 
117  # XXX Is this never true? because we set pageTitle earlier based on url.
118  if(~ $"pageTitle '')
119  pageTitle=$"siteTitle' '$"siteSubTitle
120 # if not
121 # pageTitle=$"pageTitle' | '$"siteTitle' '$"siteSubTitle
122 
123  for(h in $extraHttpHeaders)
124  echo $h
125  echo 'Content-Type: '^$http_content_type
126  echo # End of HTTP headers
127 
128  if(! ~ $#debug 0)
129  dprint $"SERVER_NAME^$"REQUEST_URI - $"HTTP_USER_AGENT - $"REQUEST_METHOD - $"handler_body_main - $"master_template
130 
131  if(~ $REQUEST_METHOD HEAD)
132  exit
133 
134  template $headers $master_template | awk_buffer
135  echo $res_tail
136 }
137 
138 werc_exec_request