HttpFoundationFactory.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\PsrHttpMessage\Factory;
  11. use Psr\Http\Message\ResponseInterface;
  12. use Psr\Http\Message\ServerRequestInterface;
  13. use Psr\Http\Message\StreamInterface;
  14. use Psr\Http\Message\UploadedFileInterface;
  15. use Psr\Http\Message\UriInterface;
  16. use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
  17. use Symfony\Component\HttpFoundation\Cookie;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\HttpFoundation\StreamedResponse;
  21. /**
  22. * {@inheritdoc}
  23. *
  24. * @author Kévin Dunglas <dunglas@gmail.com>
  25. */
  26. class HttpFoundationFactory implements HttpFoundationFactoryInterface
  27. {
  28. /**
  29. * @var int The maximum output buffering size for each iteration when sending the response
  30. */
  31. private $responseBufferMaxLength;
  32. public function __construct(int $responseBufferMaxLength = 16372)
  33. {
  34. $this->responseBufferMaxLength = $responseBufferMaxLength;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function createRequest(ServerRequestInterface $psrRequest, bool $streamed = false)
  40. {
  41. $server = [];
  42. $uri = $psrRequest->getUri();
  43. if ($uri instanceof UriInterface) {
  44. $server['SERVER_NAME'] = $uri->getHost();
  45. $server['SERVER_PORT'] = $uri->getPort() ?: ('https' === $uri->getScheme() ? 443 : 80);
  46. $server['REQUEST_URI'] = $uri->getPath();
  47. $server['QUERY_STRING'] = $uri->getQuery();
  48. if ('' !== $server['QUERY_STRING']) {
  49. $server['REQUEST_URI'] .= '?'.$server['QUERY_STRING'];
  50. }
  51. if ('https' === $uri->getScheme()) {
  52. $server['HTTPS'] = 'on';
  53. }
  54. }
  55. $server['REQUEST_METHOD'] = $psrRequest->getMethod();
  56. $server = array_replace($psrRequest->getServerParams(), $server);
  57. $parsedBody = $psrRequest->getParsedBody();
  58. $parsedBody = \is_array($parsedBody) ? $parsedBody : [];
  59. $request = new Request(
  60. $psrRequest->getQueryParams(),
  61. $parsedBody,
  62. $psrRequest->getAttributes(),
  63. $psrRequest->getCookieParams(),
  64. $this->getFiles($psrRequest->getUploadedFiles()),
  65. $server,
  66. $streamed ? $psrRequest->getBody()->detach() : $psrRequest->getBody()->__toString()
  67. );
  68. $request->headers->add($psrRequest->getHeaders());
  69. return $request;
  70. }
  71. /**
  72. * Converts to the input array to $_FILES structure.
  73. */
  74. private function getFiles(array $uploadedFiles): array
  75. {
  76. $files = [];
  77. foreach ($uploadedFiles as $key => $value) {
  78. if ($value instanceof UploadedFileInterface) {
  79. $files[$key] = $this->createUploadedFile($value);
  80. } else {
  81. $files[$key] = $this->getFiles($value);
  82. }
  83. }
  84. return $files;
  85. }
  86. /**
  87. * Creates Symfony UploadedFile instance from PSR-7 ones.
  88. */
  89. private function createUploadedFile(UploadedFileInterface $psrUploadedFile): UploadedFile
  90. {
  91. return new UploadedFile($psrUploadedFile, function () { return $this->getTemporaryPath(); });
  92. }
  93. /**
  94. * Gets a temporary file path.
  95. *
  96. * @return string
  97. */
  98. protected function getTemporaryPath()
  99. {
  100. return tempnam(sys_get_temp_dir(), uniqid('symfony', true));
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function createResponse(ResponseInterface $psrResponse, bool $streamed = false)
  106. {
  107. $cookies = $psrResponse->getHeader('Set-Cookie');
  108. $psrResponse = $psrResponse->withoutHeader('Set-Cookie');
  109. if ($streamed) {
  110. $response = new StreamedResponse(
  111. $this->createStreamedResponseCallback($psrResponse->getBody()),
  112. $psrResponse->getStatusCode(),
  113. $psrResponse->getHeaders()
  114. );
  115. } else {
  116. $response = new Response(
  117. $psrResponse->getBody()->__toString(),
  118. $psrResponse->getStatusCode(),
  119. $psrResponse->getHeaders()
  120. );
  121. }
  122. $response->setProtocolVersion($psrResponse->getProtocolVersion());
  123. foreach ($cookies as $cookie) {
  124. $response->headers->setCookie($this->createCookie($cookie));
  125. }
  126. return $response;
  127. }
  128. /**
  129. * Creates a Cookie instance from a cookie string.
  130. *
  131. * Some snippets have been taken from the Guzzle project: https://github.com/guzzle/guzzle/blob/5.3/src/Cookie/SetCookie.php#L34
  132. *
  133. * @throws \InvalidArgumentException
  134. */
  135. private function createCookie(string $cookie): Cookie
  136. {
  137. foreach (explode(';', $cookie) as $part) {
  138. $part = trim($part);
  139. $data = explode('=', $part, 2);
  140. $name = $data[0];
  141. $value = isset($data[1]) ? trim($data[1], " \n\r\t\0\x0B\"") : null;
  142. if (!isset($cookieName)) {
  143. $cookieName = $name;
  144. $cookieValue = $value;
  145. continue;
  146. }
  147. if ('expires' === strtolower($name) && null !== $value) {
  148. $cookieExpire = new \DateTime($value);
  149. continue;
  150. }
  151. if ('path' === strtolower($name) && null !== $value) {
  152. $cookiePath = $value;
  153. continue;
  154. }
  155. if ('domain' === strtolower($name) && null !== $value) {
  156. $cookieDomain = $value;
  157. continue;
  158. }
  159. if ('secure' === strtolower($name)) {
  160. $cookieSecure = true;
  161. continue;
  162. }
  163. if ('httponly' === strtolower($name)) {
  164. $cookieHttpOnly = true;
  165. continue;
  166. }
  167. if ('samesite' === strtolower($name) && null !== $value) {
  168. $samesite = $value;
  169. continue;
  170. }
  171. }
  172. if (!isset($cookieName)) {
  173. throw new \InvalidArgumentException('The value of the Set-Cookie header is malformed.');
  174. }
  175. return new Cookie(
  176. $cookieName,
  177. $cookieValue,
  178. isset($cookieExpire) ? $cookieExpire : 0,
  179. isset($cookiePath) ? $cookiePath : '/',
  180. isset($cookieDomain) ? $cookieDomain : null,
  181. isset($cookieSecure),
  182. isset($cookieHttpOnly),
  183. true,
  184. isset($samesite) ? $samesite : null
  185. );
  186. }
  187. private function createStreamedResponseCallback(StreamInterface $body): callable
  188. {
  189. return function () use ($body) {
  190. if ($body->isSeekable()) {
  191. $body->rewind();
  192. }
  193. if (!$body->isReadable()) {
  194. echo $body;
  195. return;
  196. }
  197. while (!$body->eof()) {
  198. echo $body->read($this->responseBufferMaxLength);
  199. }
  200. };
  201. }
  202. }