HttpFoundationFactory.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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\File\UploadedFile;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\HttpFoundation\StreamedResponse;
  22. /**
  23. * {@inheritdoc}
  24. *
  25. * @author Kévin Dunglas <dunglas@gmail.com>
  26. */
  27. class HttpFoundationFactory implements HttpFoundationFactoryInterface
  28. {
  29. /**
  30. * @var int The maximum output buffering size for each iteration when sending the response
  31. */
  32. private $responseBufferMaxLength;
  33. public function __construct(int $responseBufferMaxLength = 16372)
  34. {
  35. $this->responseBufferMaxLength = $responseBufferMaxLength;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function createRequest(ServerRequestInterface $psrRequest)
  41. {
  42. $server = [];
  43. $uri = $psrRequest->getUri();
  44. if ($uri instanceof UriInterface) {
  45. $server['SERVER_NAME'] = $uri->getHost();
  46. $server['SERVER_PORT'] = $uri->getPort();
  47. $server['REQUEST_URI'] = $uri->getPath();
  48. $server['QUERY_STRING'] = $uri->getQuery();
  49. }
  50. $server['REQUEST_METHOD'] = $psrRequest->getMethod();
  51. $server = array_replace($server, $psrRequest->getServerParams());
  52. $parsedBody = $psrRequest->getParsedBody();
  53. $parsedBody = \is_array($parsedBody) ? $parsedBody : [];
  54. $request = new Request(
  55. $psrRequest->getQueryParams(),
  56. $parsedBody,
  57. $psrRequest->getAttributes(),
  58. $psrRequest->getCookieParams(),
  59. $this->getFiles($psrRequest->getUploadedFiles()),
  60. $server,
  61. $psrRequest->getBody()->__toString()
  62. );
  63. $request->headers->replace($psrRequest->getHeaders());
  64. return $request;
  65. }
  66. /**
  67. * Converts to the input array to $_FILES structure.
  68. *
  69. * @param array $uploadedFiles
  70. *
  71. * @return array
  72. */
  73. private function getFiles(array $uploadedFiles)
  74. {
  75. $files = [];
  76. foreach ($uploadedFiles as $key => $value) {
  77. if ($value instanceof UploadedFileInterface) {
  78. $files[$key] = $this->createUploadedFile($value);
  79. } else {
  80. $files[$key] = $this->getFiles($value);
  81. }
  82. }
  83. return $files;
  84. }
  85. /**
  86. * Creates Symfony UploadedFile instance from PSR-7 ones.
  87. *
  88. * @param UploadedFileInterface $psrUploadedFile
  89. *
  90. * @return UploadedFile
  91. */
  92. private function createUploadedFile(UploadedFileInterface $psrUploadedFile)
  93. {
  94. $temporaryPath = '';
  95. $clientFileName = '';
  96. if (UPLOAD_ERR_NO_FILE !== $psrUploadedFile->getError()) {
  97. $temporaryPath = $this->getTemporaryPath();
  98. $psrUploadedFile->moveTo($temporaryPath);
  99. $clientFileName = $psrUploadedFile->getClientFilename();
  100. }
  101. if (class_exists('Symfony\Component\HttpFoundation\HeaderUtils')) {
  102. // Symfony 4.1+
  103. return new UploadedFile(
  104. $temporaryPath,
  105. null === $clientFileName ? '' : $clientFileName,
  106. $psrUploadedFile->getClientMediaType(),
  107. $psrUploadedFile->getError(),
  108. true
  109. );
  110. }
  111. return new UploadedFile(
  112. $temporaryPath,
  113. null === $clientFileName ? '' : $clientFileName,
  114. $psrUploadedFile->getClientMediaType(),
  115. $psrUploadedFile->getSize(),
  116. $psrUploadedFile->getError(),
  117. true
  118. );
  119. }
  120. /**
  121. * Gets a temporary file path.
  122. *
  123. * @return string
  124. */
  125. protected function getTemporaryPath()
  126. {
  127. return tempnam(sys_get_temp_dir(), uniqid('symfony', true));
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function createResponse(ResponseInterface $psrResponse, bool $streamed = false)
  133. {
  134. $cookies = $psrResponse->getHeader('Set-Cookie');
  135. $psrResponse = $psrResponse->withoutHeader('Set-Cookie');
  136. if ($streamed) {
  137. $response = new StreamedResponse(
  138. $this->createStreamedResponseCallback($psrResponse->getBody()),
  139. $psrResponse->getStatusCode(),
  140. $psrResponse->getHeaders()
  141. );
  142. } else {
  143. $response = new Response(
  144. $psrResponse->getBody()->__toString(),
  145. $psrResponse->getStatusCode(),
  146. $psrResponse->getHeaders()
  147. );
  148. }
  149. $response->setProtocolVersion($psrResponse->getProtocolVersion());
  150. foreach ($cookies as $cookie) {
  151. $response->headers->setCookie($this->createCookie($cookie));
  152. }
  153. return $response;
  154. }
  155. /**
  156. * Creates a Cookie instance from a cookie string.
  157. *
  158. * Some snippets have been taken from the Guzzle project: https://github.com/guzzle/guzzle/blob/5.3/src/Cookie/SetCookie.php#L34
  159. *
  160. * @param string $cookie
  161. *
  162. * @return Cookie
  163. *
  164. * @throws \InvalidArgumentException
  165. */
  166. private function createCookie($cookie)
  167. {
  168. foreach (explode(';', $cookie) as $part) {
  169. $part = trim($part);
  170. $data = explode('=', $part, 2);
  171. $name = $data[0];
  172. $value = isset($data[1]) ? trim($data[1], " \n\r\t\0\x0B\"") : null;
  173. if (!isset($cookieName)) {
  174. $cookieName = $name;
  175. $cookieValue = $value;
  176. continue;
  177. }
  178. if ('expires' === strtolower($name) && null !== $value) {
  179. $cookieExpire = new \DateTime($value);
  180. continue;
  181. }
  182. if ('path' === strtolower($name) && null !== $value) {
  183. $cookiePath = $value;
  184. continue;
  185. }
  186. if ('domain' === strtolower($name) && null !== $value) {
  187. $cookieDomain = $value;
  188. continue;
  189. }
  190. if ('secure' === strtolower($name)) {
  191. $cookieSecure = true;
  192. continue;
  193. }
  194. if ('httponly' === strtolower($name)) {
  195. $cookieHttpOnly = true;
  196. continue;
  197. }
  198. if ('samesite' === strtolower($name) && null !== $value) {
  199. $samesite = $value;
  200. continue;
  201. }
  202. }
  203. if (!isset($cookieName)) {
  204. throw new \InvalidArgumentException('The value of the Set-Cookie header is malformed.');
  205. }
  206. return new Cookie(
  207. $cookieName,
  208. $cookieValue,
  209. isset($cookieExpire) ? $cookieExpire : 0,
  210. isset($cookiePath) ? $cookiePath : '/',
  211. isset($cookieDomain) ? $cookieDomain : null,
  212. isset($cookieSecure),
  213. isset($cookieHttpOnly),
  214. false,
  215. isset($samesite) ? $samesite : null
  216. );
  217. }
  218. private function createStreamedResponseCallback(StreamInterface $body): callable
  219. {
  220. return function () use ($body) {
  221. if ($body->isSeekable()) {
  222. $body->rewind();
  223. }
  224. if (!$body->isReadable()) {
  225. echo $body;
  226. return;
  227. }
  228. while (!$body->eof()) {
  229. echo $body->read($this->responseBufferMaxLength);
  230. }
  231. };
  232. }
  233. }