PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/external/webkit/LayoutTests/http/tests/resources/network-simulator.php

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk
PHP | 186 lines | 158 code | 23 blank | 5 comment | 57 complexity | daa042e9da67568122aa4fac3c4489e3 MD5 | raw file
  1. <?php
  2. require_once 'portabilityLayer.php';
  3. // This script acts as a stateful proxy for retrieving files. When the state is set to
  4. // offline, it simulates a network error with a nonsense response.
  5. if (!sys_get_temp_dir()) {
  6. echo "FAIL: No temp dir was returned.\n";
  7. exit();
  8. }
  9. function setState($newState, $file)
  10. {
  11. file_put_contents($file, $newState);
  12. }
  13. function getState($file)
  14. {
  15. if (!file_exists($file)) {
  16. return "Uninitialized";
  17. }
  18. return file_get_contents($file);
  19. }
  20. function contentType($path)
  21. {
  22. if (preg_match("/\.html$/", $path))
  23. return "text/html";
  24. if (preg_match("/\.manifest$/", $path))
  25. return "text/cache-manifest";
  26. if (preg_match("/\.js$/", $path))
  27. return "text/javascript";
  28. if (preg_match("/\.xml$/", $path))
  29. return "application/xml";
  30. if (preg_match("/\.xhtml$/", $path))
  31. return "application/xhtml+xml";
  32. if (preg_match("/\.svg$/", $path))
  33. return "application/svg+xml";
  34. if (preg_match("/\.xsl$/", $path))
  35. return "application/xslt+xml";
  36. if (preg_match("/\.gif$/", $path))
  37. return "image/gif";
  38. if (preg_match("/\.jpg$/", $path))
  39. return "image/jpeg";
  40. if (preg_match("/\.png$/", $path))
  41. return "image/png";
  42. return "text/plain";
  43. }
  44. function generateNoCacheHTTPHeader()
  45. {
  46. header("Expires: Thu, 01 Dec 2003 16:00:00 GMT");
  47. header("Cache-Control: no-cache, no-store, must-revalidate");
  48. header("Pragma: no-cache");
  49. }
  50. function generateResponse($path)
  51. {
  52. global $stateFile;
  53. $state = getState($stateFile);
  54. if ($state == "Offline") {
  55. # Simulate a network error by replying with a nonsense response.
  56. header('HTTP/1.1 307 Temporary Redirect');
  57. header('Location: ' . $_SERVER['REQUEST_URI']); # Redirect to self.
  58. header('Content-Length: 1');
  59. header('Content-Length: 5', false); # Multiple content-length headers, some network stacks can detect this condition faster.
  60. echo "Intentionally incorrect response.";
  61. } else {
  62. // A little securuty checking can't hurt.
  63. if (strstr($path, ".."))
  64. exit;
  65. if ($path[0] == '/')
  66. $path = '..' . $path;
  67. generateNoCacheHTTPHeader();
  68. if (file_exists($path)) {
  69. header("Last-Modified: " . gmdate("D, d M Y H:i:s T", filemtime($path)));
  70. header("Content-Type: " . contentType($path));
  71. print file_get_contents($path);
  72. } else {
  73. header('HTTP/1.1 404 Not Found');
  74. }
  75. }
  76. }
  77. function handleIncreaseResourceCountCommand($path)
  78. {
  79. $resourceCountFile = sys_get_temp_dir() . "/resource-count";
  80. $resourceCount = getState($resourceCountFile);
  81. $pieces = explode(" ", $resourceCount);
  82. $count = 0;
  83. if (count($pieces) == 2 && $pieces[0] == $path) {
  84. $count = 1 + $pieces[1];
  85. } else {
  86. $count = 1;
  87. }
  88. file_put_contents($resourceCountFile, $path . " " . $count);
  89. generateResponse($path);
  90. }
  91. function handleResetResourceCountCommand()
  92. {
  93. $resourceCountFile = sys_get_temp_dir() . "/resource-count";
  94. file_put_contents($resourceCountFile, 0);
  95. generateNoCacheHTTPHeader();
  96. header('HTTP/1.1 200 OK');
  97. }
  98. function handleGetResourceCountCommand($path)
  99. {
  100. $resourceCountFile = sys_get_temp_dir() . "/resource-count";
  101. $resourceCount = getState($resourceCountFile);
  102. $pieces = explode(" ", $resourceCount);
  103. generateNoCacheHTTPHeader();
  104. header('HTTP/1.1 200 OK');
  105. if (count($pieces) == 2 && $pieces[0] == $path) {
  106. echo $pieces[1];
  107. } else {
  108. echo 0;
  109. }
  110. }
  111. function handleStartResourceRequestsLog()
  112. {
  113. $resourceLogFile = sys_get_temp_dir() . "/resource-log";
  114. file_put_contents($resourceLogFile, "");
  115. }
  116. function handleClearResourceRequestsLog()
  117. {
  118. $resourceLogFile = sys_get_temp_dir() . "/resource-log";
  119. file_put_contents($resourceLogFile, "");
  120. }
  121. function handleGetResourceRequestsLog()
  122. {
  123. $resourceLogFile = sys_get_temp_dir() . "/resource-log";
  124. generateNoCacheHTTPHeader();
  125. header("Content-Type: text/plain");
  126. print file_get_contents($resourceLogFile);
  127. }
  128. function handleLogResourceRequest($path)
  129. {
  130. $resourceLogFile = sys_get_temp_dir() . "/resource-log";
  131. $newData = "\n".$path;
  132. // Documentation says that appends are atomic.
  133. file_put_contents($resourceLogFile, $newData, FILE_APPEND);
  134. generateResponse($path);
  135. }
  136. $stateFile = sys_get_temp_dir() . "/network-simulator-state";
  137. $command = $_GET['command'];
  138. if ($command) {
  139. if ($command == "connect")
  140. setState("Online", $stateFile);
  141. else if ($command == "disconnect")
  142. setState("Offline", $stateFile);
  143. else if ($command == "increase-resource-count")
  144. handleIncreaseResourceCountCommand($_GET['path']);
  145. else if ($command == "reset-resource-count")
  146. handleResetResourceCountCommand();
  147. else if ($command == "get-resource-count")
  148. handleGetResourceCountCommand($_GET['path']);
  149. else if ($command == "start-resource-request-log")
  150. handleStartResourceRequestsLog();
  151. else if ($command == "clear-resource-request-log")
  152. handleClearResourceRequestsLog();
  153. else if ($command == "get-resource-request-log")
  154. handleGetResourceRequestsLog();
  155. else if ($command == "log-resource-request")
  156. handleLogResourceRequest($_GET['path']);
  157. else
  158. echo "Unknown command: " . $command . "\n";
  159. exit();
  160. }
  161. $requestedPath = $_GET['path'];
  162. generateResponse($requestedPath);
  163. ?>