337486678d17cb7bba3a7ed4907e2018b27663af
[WebKit-https.git] / LayoutTests / http / tests / xmlhttprequest / resources / access-control-basic-preflight-cache-timeout.php
1 <?php
2 require_once 'portabilityLayer.php';
3
4 $tmpFile = sys_get_temp_dir() . '/' . $_GET['filename'];
5
6 function fail()
7 {
8     header("Access-Control-Origin: http://127.0.0.1:8000");
9     header("Access-Control-Credentials: true");
10     header("Access-Control-Allow-Methods: PUT");
11     header("Access-Control-Allow-Headers: x-webkit-test");
12     echo "FAIL: " . $_SERVER['REQUEST_METHOD'] . "\n";
13     exit();
14 }
15
16 function setState($newState, $file)
17 {
18     file_put_contents($file, $newState);
19 }
20
21 function getState($file)
22 {
23     if (!file_exists($file)) {
24         return "Uninitialized";
25     }
26     return file_get_contents($file);
27 }
28
29 $state = getState($tmpFile);
30
31 if ($state == "Uninitialized") {
32     if ($_SERVER['REQUEST_METHOD'] == "OPTIONS") {
33         header("Access-Control-Origin: http://127.0.0.1:8000");
34         header("Access-Control-Credentials: true");
35         header("Access-Control-Allow-Methods: PUT");
36         header("Access-Control-Allow-Headers: x-webkit-test");
37         header("Access-Control-Max-Age: 1"); // 1 second
38         setState("OptionsSent", $tmpFile);
39     } else {
40         fail();
41     }
42 } else if ($state == "OptionsSent") {
43     if ($_SERVER['REQUEST_METHOD'] == "PUT") {
44         header("Access-Control-Origin: http://127.0.0.1:8000");
45         header("Access-Control-Credentials: true");
46         echo "PASS: First PUT request.";
47         setState("FirstPUTSent", $tmpFile);
48     } else {
49         fail();
50     }
51 } else if ($state == "FirstPUTSent") {
52     if ($_SERVER['REQUEST_METHOD'] == "OPTIONS") {
53         header("Access-Control-Origin: http://127.0.0.1:8000");
54         header("Access-Control-Credentials: true");
55         header("Access-Control-Allow-Methods: PUT");
56         header("Access-Control-Allow-Headers: x-webkit-test");
57         setState("SecondOPTIONSSent", $tmpFile);
58     } else if ($_SERVER['REQUEST_METHOD'] == "PUT") {
59         header("Access-Control-Origin: http://127.0.0.1:8000");
60         header("Access-Control-Credentials: true");
61         echo "FAIL: Second PUT request sent without preflight";
62     }
63 } else if ($state == "SecondOPTIONSSent") {
64     if ($_SERVER['REQUEST_METHOD'] == "PUT") {
65         header("Access-Control-Origin: http://127.0.0.1:8000");
66         header("Access-Control-Credentials: true");
67         echo "PASS: Second OPTIONS request was sent.";
68     } else {
69         fail();
70     }
71 } else {
72     fail();
73 }
74 ?>