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