Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / resources / network-simulator.php
blob3de2bb92c538d4c757c6a219e553630295220fdb
1 <?php
2 require_once 'portabilityLayer.php';
4 // This script acts as a stateful proxy for retrieving files. When the state is set to
5 // offline, it simulates a network error with a nonsense response.
7 if (!sys_get_temp_dir()) {
8 echo "FAIL: No temp dir was returned.\n";
9 exit();
12 function setState($newState, $file)
14 file_put_contents($file, $newState);
17 function getState($file)
19 if (!file_exists($file)) {
20 return "Uninitialized";
22 return file_get_contents($file);
25 function contentType($path)
27 if (preg_match("/\.html$/", $path))
28 return "text/html";
29 if (preg_match("/\.manifest$/", $path))
30 return "text/cache-manifest";
31 if (preg_match("/\.js$/", $path))
32 return "text/javascript";
33 if (preg_match("/\.xml$/", $path))
34 return "application/xml";
35 if (preg_match("/\.xhtml$/", $path))
36 return "application/xhtml+xml";
37 if (preg_match("/\.svg$/", $path))
38 return "application/svg+xml";
39 if (preg_match("/\.xsl$/", $path))
40 return "application/xslt+xml";
41 if (preg_match("/\.gif$/", $path))
42 return "image/gif";
43 if (preg_match("/\.jpg$/", $path))
44 return "image/jpeg";
45 if (preg_match("/\.png$/", $path))
46 return "image/png";
47 return "text/plain";
50 function generateNoCacheHTTPHeader()
52 header("Expires: Thu, 01 Dec 2003 16:00:00 GMT");
53 header("Cache-Control: no-cache, no-store, must-revalidate");
54 header("Pragma: no-cache");
57 function generateResponse($path)
59 global $stateFile;
60 $state = getState($stateFile);
61 if ($state == "Offline") {
62 # Simulate a network error by replying with a nonsense response.
63 header('HTTP/1.1 307 Temporary Redirect');
64 header('Location: ' . $_SERVER['REQUEST_URI']); # Redirect to self.
65 header('Content-Length: 1');
66 header('Content-Length: 5', false); # Multiple content-length headers, some network stacks can detect this condition faster.
67 echo "Intentionally incorrect response.";
68 } else {
69 // A little securuty checking can't hurt.
70 if (strstr($path, ".."))
71 exit;
73 if ($path[0] == '/')
74 $path = '..' . $path;
76 if (!$_GET['allow-caching'])
77 generateNoCacheHTTPHeader();
79 if (file_exists($path)) {
80 header("Last-Modified: " . gmdate("D, d M Y H:i:s T", filemtime($path)));
81 header("Content-Type: " . contentType($path));
83 print file_get_contents($path);
84 } else {
85 header('HTTP/1.1 404 Not Found');
90 function handleIncreaseResourceCountCommand($path)
92 $resourceCountFile = sys_get_temp_dir() . "/resource-count";
93 $resourceCount = getState($resourceCountFile);
94 $pieces = explode(" ", $resourceCount);
95 $count = 0;
96 if (count($pieces) == 2 && $pieces[0] == $path) {
97 $count = 1 + $pieces[1];
98 } else {
99 $count = 1;
101 file_put_contents($resourceCountFile, $path . " " . $count);
102 generateResponse($path);
105 function handleResetResourceCountCommand()
107 $resourceCountFile = sys_get_temp_dir() . "/resource-count";
108 file_put_contents($resourceCountFile, 0);
109 generateNoCacheHTTPHeader();
110 header('HTTP/1.1 200 OK');
113 function handleGetResourceCountCommand($path)
115 $resourceCountFile = sys_get_temp_dir() . "/resource-count";
116 $resourceCount = getState($resourceCountFile);
117 $pieces = explode(" ", $resourceCount);
118 generateNoCacheHTTPHeader();
119 header('HTTP/1.1 200 OK');
120 if (count($pieces) == 2 && $pieces[0] == $path) {
121 echo $pieces[1];
122 } else {
123 echo 0;
127 function handleStartResourceRequestsLog()
129 $resourceLogFile = sys_get_temp_dir() . "/resource-log";
130 file_put_contents($resourceLogFile, "");
133 function handleClearResourceRequestsLog()
135 $resourceLogFile = sys_get_temp_dir() . "/resource-log";
136 file_put_contents($resourceLogFile, "");
139 function handleGetResourceRequestsLog()
141 $resourceLogFile = sys_get_temp_dir() . "/resource-log";
143 generateNoCacheHTTPHeader();
144 header("Content-Type: text/plain");
146 print file_get_contents($resourceLogFile);
149 function handleLogResourceRequest($path)
151 $resourceLogFile = sys_get_temp_dir() . "/resource-log";
153 $newData = "\n".$path;
154 file_put_contents($resourceLogFile, $newData, FILE_APPEND | LOCK_EX);
157 $stateFile = sys_get_temp_dir() . "/network-simulator-state";
158 $command = $_GET['command'];
159 if ($command) {
160 if ($command == "connect")
161 setState("Online", $stateFile);
162 else if ($command == "disconnect")
163 setState("Offline", $stateFile);
164 else if ($command == "increase-resource-count")
165 handleIncreaseResourceCountCommand($_GET['path']);
166 else if ($command == "reset-resource-count")
167 handleResetResourceCountCommand();
168 else if ($command == "get-resource-count")
169 handleGetResourceCountCommand($_GET['path']);
170 else if ($command == "start-resource-request-log")
171 handleStartResourceRequestsLog();
172 else if ($command == "clear-resource-request-log")
173 handleClearResourceRequestsLog();
174 else if ($command == "get-resource-request-log")
175 handleGetResourceRequestsLog();
176 else if ($command == "log-resource-request")
177 handleLogResourceRequest($_GET['path']);
178 else
179 echo "Unknown command: " . $command . "\n";
180 exit();
183 $requestedPath = $_GET['path'];
184 generateResponse($requestedPath);