c3ebf82b78582b31033106a7c03961eb9cbd137d
[atutor.git] / mods / wiki / plugins / lib / speed.php
1 <?php
2
3 /*
4    *** STILL BROKEN ! ***
5
6    This plugin implements some speed-ups for the HTTP protocol (used by
7    most proxies and all browsers maintaining their own cache). The
8    rendering of the current page will then be aborted, if this plugin
9    detects it is unnecessary, because a client told so.
10
11    It can compare against "ETags" and "Last-Modified" values, and aborts
12    further ewiki processing early, if one of these conditions matches, and
13    also terminates ewiki for HEAD method requests (because no output is
14    required then).
15
16    This will only take effect, if you disable EWIKI_NOCACHE (set it to 0).
17    (Should we output the "*: no-cache" headers conditionally? - how?)
18 */
19
20
21 #-- plugin glue
22 $ewiki_plugins["handler"][] = "ewiki_speed_abort";
23
24
25 #-- implementation
26 function ewiki_speed_abort($id, &$data, $action) {
27
28    $o = "";  // unused here
29    $inverse = 0;
30    $yes = 0;
31    $precond = 0;
32
33    #-- ETag comparisions
34    if (($if=$_SERVER["HTTP_IF_MATCH"]) || ($if=$_SERVER["HTTP_IF_NONE_MATCH"]) && ($inverse=1) ) {
35
36       ($data["version"])
37       and ($etag = ewiki_etag($data))
38       or ($etag = "never:matching:".time());
39
40       #-- walk through comparison values
41       foreach (explode(",", $if) as $match) {
42          $match = trim(trim($match), '"');
43          if (($match == "*") || ($match == $etag)) {
44             $yes = 1;
45          }
46          $precond = 1;
47       }
48
49    }
50
51    #-- check against modification time
52    if (($if=$_SERVER["HTTP_IF_MODIFIED_SINCE"]) || ($if=$_SERVER["HTTP_IF_UNMODIFIED_SINCE"]) && ($inverse=1)) {
53
54       ($modif = $data["lastmodified"])
55       or ($modif = UNIX_MILLENNIUM);
56
57       $if = strtotime(trim($if));
58
59       if ($modif > $if) {
60          $yes = 1;
61       }
62    }
63
64    #-- invert result
65    if ($inverse) {
66      $yes = $yes ? 0 : 1;
67    }
68
69    #-- abort ewiki rendering, if matched or senseful to do so
70    if ($yes || ($_SERVER["REQUEST_METHOD"] == "HEAD")) {
71       /*      
72          #ewiki_http_headers($o, $id, $data, $action);
73          (It was probably bad to send the ETag and Content-Version fields
74          for this http answer?)
75       */
76       header("Status: 304 Not Modified");
77       die(304);
78    }
79    elseif ($precond) {
80       if (!$inverse || ($_SERVER["REQUEST_METHOD"]!="GET")) {
81          header("Status: 412 Precondition Failed");
82          die(412);
83       }
84    }
85
86 }
87
88
89 ?>