response.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. namespace FPM;
  3. class Response
  4. {
  5. const HEADER_SEPARATOR = "\r\n\r\n";
  6. /**
  7. * @var array
  8. */
  9. private $data;
  10. /**
  11. * @var string
  12. */
  13. private $rawData;
  14. /**
  15. * @var string
  16. */
  17. private $rawHeaders;
  18. /**
  19. * @var string
  20. */
  21. private $rawBody;
  22. /**
  23. * @var array
  24. */
  25. private $headers;
  26. /**
  27. * @var bool
  28. */
  29. private $valid;
  30. /**
  31. * @var bool
  32. */
  33. private $expectInvalid;
  34. /**
  35. * @param string|array|null $data
  36. * @param bool $expectInvalid
  37. */
  38. public function __construct($data = null, $expectInvalid = false)
  39. {
  40. if (!is_array($data)) {
  41. $data = [
  42. 'response' => $data,
  43. 'err_response' => null,
  44. 'out_response' => $data,
  45. ];
  46. }
  47. $this->data = $data;
  48. $this->expectInvalid = $expectInvalid;
  49. }
  50. /**
  51. * @param mixed $body
  52. * @param string $contentType
  53. * @return Response
  54. */
  55. public function expectBody($body, $contentType = 'text/html')
  56. {
  57. if ($multiLine = is_array($body)) {
  58. $body = implode("\n", $body);
  59. }
  60. if (
  61. $this->checkIfValid() &&
  62. $this->checkDefaultHeaders($contentType) &&
  63. $body !== $this->rawBody
  64. ) {
  65. if ($multiLine) {
  66. $this->error(
  67. "==> The expected body:\n$body\n" .
  68. "==> does not match the actual body:\n$this->rawBody"
  69. );
  70. } else {
  71. $this->error(
  72. "The expected body '$body' does not match actual body '$this->rawBody'"
  73. );
  74. }
  75. }
  76. return $this;
  77. }
  78. /**
  79. * @return Response
  80. */
  81. public function expectEmptyBody()
  82. {
  83. return $this->expectBody('');
  84. }
  85. /**
  86. * @param string $contentType
  87. * @return string|null
  88. */
  89. public function getBody($contentType = 'text/html')
  90. {
  91. if ($this->checkIfValid() && $this->checkDefaultHeaders($contentType)) {
  92. return $this->rawBody;
  93. }
  94. return null;
  95. }
  96. /**
  97. * Print raw body
  98. */
  99. public function dumpBody()
  100. {
  101. var_dump($this->getBody());
  102. }
  103. /**
  104. * Print raw body
  105. */
  106. public function printBody()
  107. {
  108. echo $this->getBody() . "\n";
  109. }
  110. /**
  111. * Debug response output
  112. */
  113. public function debugOutput()
  114. {
  115. echo "-------------- RESPONSE: --------------\n";
  116. echo "OUT:\n";
  117. echo $this->data['out_response'];
  118. echo "ERR:\n";
  119. echo $this->data['err_response'];
  120. echo "---------------------------------------\n\n";
  121. }
  122. /**
  123. * @return string|null
  124. */
  125. public function getErrorData()
  126. {
  127. return $this->data['err_response'];
  128. }
  129. /**
  130. * Check if the response is valid and if not emit error message
  131. *
  132. * @return bool
  133. */
  134. private function checkIfValid()
  135. {
  136. if ($this->isValid()) {
  137. return true;
  138. }
  139. if (!$this->expectInvalid) {
  140. $this->error("The response is invalid: $this->rawData");
  141. }
  142. return false;
  143. }
  144. /**
  145. * @param string $contentType
  146. * @return bool
  147. */
  148. private function checkDefaultHeaders($contentType)
  149. {
  150. // check default headers
  151. return (
  152. $this->checkHeader('X-Powered-By', '|^PHP/7|', true) &&
  153. $this->checkHeader('Content-type', '|^' . $contentType . '(;\s?charset=\w+)?|', true)
  154. );
  155. }
  156. /**
  157. * @param string $name
  158. * @param string $value
  159. * @param bool $useRegex
  160. * @return bool
  161. */
  162. private function checkHeader(string $name, string $value, $useRegex = false)
  163. {
  164. $lcName = strtolower($name);
  165. $headers = $this->getHeaders();
  166. if (!isset($headers[$lcName])) {
  167. return $this->error("The header $name is not present");
  168. }
  169. $header = $headers[$lcName];
  170. if (!$useRegex) {
  171. if ($header === $value) {
  172. return true;
  173. }
  174. return $this->error("The header $name value '$header' is not the same as '$value'");
  175. }
  176. if (!preg_match($value, $header)) {
  177. return $this->error("The header $name value '$header' does not match RegExp '$value'");
  178. }
  179. return true;
  180. }
  181. /**
  182. * @return array|null
  183. */
  184. private function getHeaders()
  185. {
  186. if (!$this->isValid()) {
  187. return null;
  188. }
  189. if (is_array($this->headers)) {
  190. return $this->headers;
  191. }
  192. $headerRows = explode("\r\n", $this->rawHeaders);
  193. $headers = [];
  194. foreach ($headerRows as $headerRow) {
  195. $colonPosition = strpos($headerRow, ':');
  196. if ($colonPosition === false) {
  197. $this->error("Invalid header row (no colon): $headerRow");
  198. }
  199. $headers[strtolower(substr($headerRow, 0, $colonPosition))] = trim(
  200. substr($headerRow, $colonPosition + 1)
  201. );
  202. }
  203. return ($this->headers = $headers);
  204. }
  205. /**
  206. * @return bool
  207. */
  208. private function isValid()
  209. {
  210. if ($this->valid === null) {
  211. $this->processData();
  212. }
  213. return $this->valid;
  214. }
  215. /**
  216. * Process data and set validity and raw data
  217. */
  218. private function processData()
  219. {
  220. $this->rawData = $this->data['out_response'];
  221. $this->valid = (
  222. !is_null($this->rawData) &&
  223. strpos($this->rawData, self::HEADER_SEPARATOR)
  224. );
  225. if ($this->valid) {
  226. list ($this->rawHeaders, $this->rawBody) = array_map(
  227. 'trim',
  228. explode(self::HEADER_SEPARATOR, $this->rawData)
  229. );
  230. }
  231. }
  232. /**
  233. * Emit error message
  234. *
  235. * @param string $message
  236. * @return bool
  237. */
  238. private function error($message)
  239. {
  240. echo "ERROR: $message\n";
  241. return false;
  242. }
  243. }