1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
341
  
342
  
343
  
344
  
345
  
346
  
347
  
348
  
349
  
350
  
351
  
352
  
353
  
354
  
355
  
356
  
357
  
358
  
359
  
360
  
361
  
362
  
363
  
364
  
365
  
366
  
367
  
368
  
369
  
370
  
371
  
372
  
373
  
374
  
375
  
376
  
377
  
378
  
379
  
380
  
381
  
382
  
383
  
384
  
385
  
386
  
387
  
388
  
389
  
390
  
391
  
392
  
393
  
394
  
395
  
396
  
397
  
398
  
399
  
400
  
401
  
402
  
403
  
404
  
405
  
406
  
407
  
408
  
409
  
410
  
411
  
412
  
413
  
414
  
415
  
416
  
417
  
418
  
419
  
420
  
421
  
422
  
423
  
424
  
425
  
426
  
427
  
428
  
429
  
430
  
431
  
432
  
433
  
434
  
435
  
436
  
437
  
438
  
439
  
440
  
441
  
442
  
443
  
444
  
445
  
// This is a roxen module. Copyright © 1996 - 2009, Roxen IS. 
 
// The redirect module. Redirects requests from one filename to 
// another. This can be done using "internal" redirects (much like a 
// symbolic link in unix), or with normal HTTP redirects. 
 
constant cvs_version = "$Id$"; 
constant thread_safe = 1; 
 
inherit "module"; 
#include <module.h> 
 
private int redirs = 0; 
 
void create() 
{ 
  defvar("fileredirect", "", "Redirect patterns", TYPE_TEXT_FIELD|VAR_INITIAL, 
         #"\ 
A list of patterns to redirect one URL to another. Each line is a 
pattern rule according to one of the following formats: 
 
<dl> 
<dt>[<tt>permanent</tt>] <i>regexp target</i> 
  <dd><p>Any URL path that matches <i>regexp</i> is redirected to 
  <i>target</i>. The <i>regexp</i> is assumed to always contain a '*' 
  character, otherwise it is interpreted as a <i>prefix</i> instead - 
  see next rule.</p> 
 
  <p>You can use '(' and ')' in <i>regexp</i> to extract parts of it. 
  The parts can then be insterted into the <i>target</i> pattern with 
  $1, $2 etc.</p> 
 
<dt>[<tt>permanent</tt>] <i>prefix target</i> 
  <dd><p>Any URL path that begins with <i>prefix</i> is redirected to 
  <i>target</i>.</p> 
 
<dt>[<tt>permanent</tt>] <tt>exact</tt> <i>path target</i> 
  <dd><p>If the URL path is exactly <i>path</i>, then redirect to 
  <i>target</i>.</p> 
 
  <p>These rules are handled more efficiently than the preceding ones. 
  While every rule of the preceding types adds a little bit of extra 
  processing time to every request, there can be almost any amount of 
  'exact' rules without additional slowdown.</p> 
</dl> 
 
<p>Rules with the 'exact' keyword are tested first, then the other 
rules are tested in the order they are written. Therefore more 
specific rules should come before generic ones, and time can be saved 
by putting rules which are hit frequently first.</p> 
 
<p>If <i>target</i> isn't an absolute URL then the redirect is handled 
internally, otherwise a redirect response is sent. The response uses 
302 Moved Temporarily by default, but if the rule is preceded by 
'permanent' then a 301 Moved Permanently is sent instead.</p> 
 
<p>\"%f\" in the <i>target</i> field is replaced with the filename of 
the matched file, \"%p\" is replaced with the full path, and \"%u\" is 
replaced with the base URL of the server.</p> 
 
<p>\"%u\" is useful in front of <i>target</i> to construct an absolute 
URL. Note that it does not include an ending '/', so you should 
provide one yourself.</p> 
 
<p>In addition, patterns can also be included from another file using 
an include directive: 
 
<blockquote><tt>#include &lt;</tt><i>path</i><tt>&gt;</tt></blockquote> 
 
The path is relative to the Roxen server directory in the real 
filesystem.</p> 
 
<p>If the include file is accessible from Roxen's virtual file system use the 
virtual-include directive instead. 
 
<blockquote><tt>#virtual-include &lt;</tt><i>path</i><tt>&gt;</tt></blockquote> 
</p> 
 
<p>Other lines beginning with '#' are treated as comments. Empty lines 
are ignored.</p> 
 
<p>Some examples:</p> 
 
<pre>/from/.*                        http://to.roxen.com/to/%f 
.*\\.cgi                         http://cgi.foo.bar/cgi-bin/%p 
/thb/.*                         %u/thb_gone.html 
permanent /from/(.*)            %u/to/$1 
/roxen/                         http://www.roxen.com/ 
exact /                         /main/index.html 
.*/SE/liu/lysator/(.*)\\.class   /java/classes/SE/liu/lysator/$1.class 
/(.*)\\.en\\.html                 /(en)/$1.html 
(.*)/index\\.html                %u/$1/ 
permanent exact /               %u/main/index.html 
</pre> 
 
<p><b>Note:</b> The keyword 'permanent' only works if you use an 
absolute URL, either literally or by starting <i>target</i> with %u."); 
 
  defvar("poll_interval", 60, "Poll interval", TYPE_INT, 
         "Time in seconds between polls of the files <tt>#include</tt>d " 
         "in the redirect pattern."); 
} 
 
array(string(0..255)) redirect_from = ({}); 
array(string(0..255)) redirect_to = ({}); 
array(int) redirect_code = ({}); 
mapping(string(0..255):array(string(0..255)|int)) exact_patterns = ([]); 
 
 
//! Returns false if AC module reload detected. 
bool try_ac_backdoor(RequestID id) 
{ 
  //  Unlimited access privileges using AC backdoor? This is enabled 
  //  by setting the Force Access popup menu to a specific value in 
  //  the preference wizard. 
  mapping acvar = roxen->query_var("AC"); 
  if (object acmodule = acvar?->loaders[my_configuration()]) { 
    //  There is a slight chance that the AC module is reloading at 
    //  this moment. We need to detect that and reschedule the 
    //  crawling in a couple of seconds. 
    if (!acmodule->online_db || !acmodule->online_db->acdb) { 
      return false; 
    } 
    acmodule->online_db->acdb->backdoor_request(id); 
  } 
  return true; 
} 
 
Stdio.Stat virtual_file_stat(string file, RequestID id) 
{ 
  array(int)|Stdio.Stat file_stat = 
    my_configuration()->try_stat_file(file, id); 
  if (arrayp(file_stat)) { 
    file_stat = Stdio.Stat(file_stat); 
  } 
  return file_stat; 
} 
 
class RedirectFile { 
  string file; 
  // Used for storing the time from last time we checked this file. 
  int check_time; 
  // Used for storing the stat from last time we checked this file. 
  Stdio.Stat stat; 
 
  protected void create(string file, Stdio.Stat|void stat) 
  { 
    this::file = file; 
    this::check_time = time(1); 
    if (stat) { 
      this::stat = stat; 
    } else { 
      this::stat = stat_file(); 
    } 
  } 
 
  Stdio.Stat stat_file() 
  { 
    return file_stat(file); 
  } 
} 
 
class VirtualRedirectFile { 
  inherit RedirectFile; 
 
  //! NB: May return 0. 
  Stdio.Stat stat_file() 
  { 
    Stdio.Stat stat = UNDEFINED; 
    RequestID fake_id = roxen.InternalRequestID(); 
    mixed e = catch { 
      fake_id->set_path(file); 
      if (!try_ac_backdoor(fake_id)) { 
        destruct(fake_id); 
        return 0; 
      } 
      stat = virtual_file_stat(file, fake_id); 
    }; 
    destruct(fake_id); 
    if (e) { 
      report_warning("Redirect: Error while trying to stat file %s.\n", file); 
    } 
    return stat; 
  } 
 
  string file_read(RequestID id) { 
    return my_configuration()->try_get_file(file, id); 
  } 
} 
 
void parse_virtual_include_file(string file, int|void no_tries) 
{ 
  if (no_tries >= 9) { 
    report_warning("Redirect: Failed to read file %s. (Tried %d times.)\n", 
      file, no_tries + 1); 
  } 
  RequestID fake_id = roxen.InternalRequestID(); 
  mixed e = catch { 
    fake_id->set_path(file); 
    if (!try_ac_backdoor(fake_id)) { 
      destruct(fake_id); 
      report_error("Redirect: Failed to parse virtual file [%s] due to " 
                   "AC module reload detected. Will try again shortly.\n", 
                   file); 
      roxen.background_run(10, parse_virtual_include_file, file, ++no_tries); 
      return 0; 
    } 
    Stdio.Stat file_stat = virtual_file_stat(file, fake_id); 
    RedirectFile redirect_file = VirtualRedirectFile(file, file_stat); 
    dependencies[file] = redirect_file; 
    if (string contents = redirect_file->file_read(fake_id)) { 
      parse_redirect_string(contents); 
    } else { 
      report_warning ("Cannot read redirect patterns from "+file+".\n"); 
    } 
  }; 
  destruct(fake_id); 
  if (e) { 
    report_warning("Redirect: Error while reading file %s.\n", file); 
  } 
} 
 
//! Mapping from filename to 
//! @array 
//!   @elem int poll_interval 
//!     Poll interval in seconds. 
//!   @elem int last_poll 
//!     Time the file was last polled. 
//!   @elem Stdio.Stat stat 
//!     Stat at the time of @[last_poll]. 
//! @endarray 
mapping(string(0..255):RedirectFile) dependencies = ([]); 
 
void parse_redirect_string(string what) 
{ 
  foreach(replace(what, "\t", " ")/"\n", 
          string(0..255) s) 
  { 
    if (sscanf (s, "#virtual-include%*[ ]<%s>", string file) == 2) { 
      parse_virtual_include_file(file); 
    } 
    else if (sscanf (s, "#include%*[ ]<%s>", string file) == 2) { 
      dependencies[file] = RedirectFile(file); 
      if (string(0..255) contents = Stdio.read_bytes(file)) { 
        parse_redirect_string(contents); 
      } else { 
        report_warning ("Cannot read redirect patterns from "+file+".\n"); 
      } 
    } 
    else if (sizeof(s) && (s[0] != '#')) { 
      int ret_code; 
      array(string(0..255)) a = s/" " - ({""}); 
      if (sizeof (a) && a[0] == "permanent") { 
        a = a[1..]; 
        ret_code = 301; 
      } else 
        ret_code = 302; 
      // FIXME: http_encode_invalids() generates upper-case hex-escapes, but 
      //        there may be verbatim lower-case escapes in the patterns. 
      if(sizeof(a)>=3 && a[0]=="exact") { 
        string(0..255) match_url = Roxen.http_encode_invalids(a[1]); 
        string(0..255) dest_url = Roxen.http_encode_invalids(a[2]); 
        if (exact_patterns[match_url]) 
          report_warning ("Duplicate redirect pattern %O.\n", s); 
        exact_patterns[match_url] = ({ dest_url, ret_code }); 
      } 
      else if (sizeof(a)==2) { 
        string(0..255) from_url = Roxen.http_encode_invalids(a[0]); 
        string(0..255) to_url = Roxen.http_encode_invalids(a[1]); 
        if (search (redirect_from, from_url) >= 0) 
          report_warning ("Duplicate redirect pattern %O.\n", s); 
        redirect_from += ({ from_url }); 
        redirect_to += ({ to_url }); 
        redirect_code += ({ ret_code }); 
      } 
      else if (sizeof (a)) 
        report_warning ("Invalid redirect pattern %O.\n", s); 
    } 
  } 
} 
 
roxen.BackgroundProcess file_poller_proc; 
 
void start_poller() 
{ 
  if (sizeof(dependencies)) { 
    int poll_interval = query("poll_interval"); 
    int next = 0x7fffffff; 
    foreach(dependencies;; RedirectFile dependency) { 
      int deptime = poll_interval + dependency->check_time; 
      if (deptime < next) next = deptime; 
    } 
    next -= time(1); 
    if (next < 0) next = 0; 
    if (file_poller_proc) 
      file_poller_proc->set_period (next); 
    else 
      file_poller_proc = roxen.BackgroundProcess (next, file_poller); 
  } 
} 
 
void file_poller() 
{ 
  int changed; 
  foreach(dependencies; string fname; RedirectFile dependency) { 
    Stdio.Stat stat = dependency->stat_file(); 
    if (!((!stat && !dependency->stat) || 
          (stat && dependency->stat && stat->mtime == dependency->stat->mtime))) { 
      // mtime for the file has changed, or it has been created or deleted 
      // since last poll. 
      changed = 1; 
      break; 
    } 
    dependency->check_time = time(1); 
    dependency->stat = stat; 
  } 
  if (changed) start(); 
  else start_poller(); 
} 
 
void start() 
{ 
  redirect_from = ({}); 
  redirect_to = ({}); 
  redirect_code = ({}); 
  exact_patterns = ([]); 
  dependencies = ([]); 
  parse_redirect_string(query("fileredirect")); 
  start_poller(); 
} 
 
constant module_type = MODULE_FIRST; 
constant module_name = "Redirect Module"; 
constant module_doc  = 
  "The redirect module. Redirects requests from one filename to " 
  "another. This can be done using \"internal\" redirects (much" 
  " like a symbolic link in unix), or with normal HTTP redirects."; 
constant module_unique = 0; 
 
string status() 
{ 
  return sprintf("Number of patterns: " 
                 "%d prefix/regexp + %d exact = %d total<br />\n" 
                 "Redirects so far: %d", 
                 sizeof(redirect_from),sizeof(exact_patterns), 
                 sizeof(redirect_from)+sizeof(exact_patterns), 
                 redirs); 
} 
 
 
mixed first_try(object id) 
{ 
  if(id->misc->is_redirected) 
    return 0; 
 
  string orig_url; 
  string from; 
  from = orig_url = Roxen.http_encode_invalids(id->not_query); 
  if(id->query) 
    if(sscanf(id->raw_url, "%*s?%s", string tmp)) 
      from += "?"+tmp; 
 
  string to; 
  int ret_code = 302; 
 
  if (array exact_ent = exact_patterns[from]) 
    [to, ret_code] = exact_ent; 
 
  else 
    for (int i = 0; i < sizeof (redirect_from); i++) { 
      string f = redirect_from[i]; 
 
      if(has_prefix(from, f)) 
      { 
        to = redirect_to[i] + from[strlen(f)..]; 
        ret_code = redirect_code[i]; 
        //  Do not explicitly remove the query part of the URL. 
        // sscanf(to, "%s?", to); 
        break; 
      } 
 
      else if( has_value(f, "*") || has_value( f, "(") ) { 
        function split; 
        if(f[0] != '^') f = "^" + f; 
        if(catch (split = Regexp(f)->split)) 
        { 
          report_error("REDIRECT: Compile error in regular expression. ("+f+")\n"); 
          continue; 
        } 
 
        if(array foo = split(from)) { 
          array bar = Array.map(foo, lambda(string s, mapping f) { 
                                       return "$"+(f->num++); 
                                     }, ([ "num":1 ])); 
          foo +=({(({""}) + (id->not_query/"/" - ({""})))[-1], 
                  id->not_query[1..] }); 
          bar +=({ "%f", "%p" }); 
 
          string redir_to = redirect_to[i]; 
          to = replace(redir_to, (array(string)) bar, (array(string)) foo); 
          ret_code = redirect_code[i]; 
          break; 
        } 
      } 
    } 
 
  if(!to) 
    return 0; 
 
  string url = id->url_base()[..<1]; 
  to = replace(to, "%u", url); 
  if(to == url + orig_url 
#if 0 
     // The following is disabled since it can hardly ever be true. /mast 
     || url == orig_url 
#endif 
    ) 
    return 0; 
 
  id->misc->is_redirected = 1; // Prevent recursive internal redirects 
 
  redirs++; 
  if (sscanf (to, "%*[-+.a-zA-Z0-9]://%*c") == 2) 
  { 
    return Roxen.http_low_answer( ret_code, "") 
      + ([ "extra_heads":([ "Location":Roxen.http_encode_invalids(to) ]) ]); 
  } else { 
    if (!id->misc->redirected_raw_url) { 
      // Keep track of the original raw_url. 
      id->misc->redirected_raw_url = id->raw_url; 
      id->misc->redirected_not_query = id->not_query; 
      // And our destination (in case of chained redirects). 
      id->misc->redirected_to = to; 
    } 
 
    id->real_variables = id->misc->post_variables ? 
      id->misc->post_variables + ([]) : ([]); 
    id->variables = FakedVariables(id->real_variables); 
    id->raw_url = to; 
    id->not_query = id->scan_for_query( to ); 
    id->not_query = utf8_to_string(Roxen.http_decode_string(id->not_query)); 
  } 
}