MultipartRequestParser.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-only
  5. */
  6. namespace OCA\DAV\BulkUpload;
  7. use OCP\AppFramework\Http;
  8. use Psr\Log\LoggerInterface;
  9. use Sabre\DAV\Exception;
  10. use Sabre\DAV\Exception\BadRequest;
  11. use Sabre\DAV\Exception\LengthRequired;
  12. use Sabre\HTTP\RequestInterface;
  13. class MultipartRequestParser {
  14. /** @var resource */
  15. private $stream;
  16. /** @var string */
  17. private $boundary = '';
  18. /** @var string */
  19. private $lastBoundary = '';
  20. /**
  21. * @throws BadRequest
  22. */
  23. public function __construct(
  24. RequestInterface $request,
  25. protected LoggerInterface $logger,
  26. ) {
  27. $stream = $request->getBody();
  28. $contentType = $request->getHeader('Content-Type');
  29. if (!is_resource($stream)) {
  30. throw new BadRequest('Body should be of type resource');
  31. }
  32. if ($contentType === null) {
  33. throw new BadRequest('Content-Type can not be null');
  34. }
  35. $this->stream = $stream;
  36. $boundary = $this->parseBoundaryFromHeaders($contentType);
  37. $this->boundary = '--' . $boundary . "\r\n";
  38. $this->lastBoundary = '--' . $boundary . "--\r\n";
  39. }
  40. /**
  41. * Parse the boundary from the Content-Type header.
  42. * Example: Content-Type: "multipart/related; boundary=boundary_bf38b9b4b10a303a28ed075624db3978"
  43. *
  44. * @throws BadRequest
  45. */
  46. private function parseBoundaryFromHeaders(string $contentType): string {
  47. try {
  48. [$mimeType, $boundary] = explode(';', $contentType);
  49. [$boundaryKey, $boundaryValue] = explode('=', $boundary);
  50. } catch (\Exception $e) {
  51. throw new BadRequest('Error while parsing boundary in Content-Type header.', Http::STATUS_BAD_REQUEST, $e);
  52. }
  53. $boundaryValue = trim($boundaryValue);
  54. // Remove potential quotes around boundary value.
  55. if (str_starts_with($boundaryValue, '"') && str_ends_with($boundaryValue, '"')) {
  56. $boundaryValue = substr($boundaryValue, 1, -1);
  57. }
  58. if (trim($mimeType) !== 'multipart/related') {
  59. throw new BadRequest('Content-Type must be multipart/related');
  60. }
  61. if (trim($boundaryKey) !== 'boundary') {
  62. throw new BadRequest('Boundary is invalid');
  63. }
  64. return $boundaryValue;
  65. }
  66. /**
  67. * Check whether the stream's cursor is sitting right before the provided string.
  68. *
  69. * @throws Exception
  70. */
  71. private function isAt(string $expectedContent): bool {
  72. $expectedContentLength = strlen($expectedContent);
  73. $content = fread($this->stream, $expectedContentLength);
  74. if ($content === false) {
  75. throw new Exception('An error occurred while checking content');
  76. }
  77. $seekBackResult = fseek($this->stream, -$expectedContentLength, SEEK_CUR);
  78. if ($seekBackResult === -1) {
  79. throw new Exception('Unknown error while seeking content', Http::STATUS_INTERNAL_SERVER_ERROR);
  80. }
  81. return $expectedContent === $content;
  82. }
  83. /**
  84. * Check whether the stream's cursor is sitting right before the boundary.
  85. */
  86. private function isAtBoundary(): bool {
  87. return $this->isAt($this->boundary);
  88. }
  89. /**
  90. * Check whether the stream's cursor is sitting right before the last boundary.
  91. */
  92. public function isAtLastBoundary(): bool {
  93. return $this->isAt($this->lastBoundary);
  94. }
  95. /**
  96. * Parse and return the next part of the multipart headers.
  97. *
  98. * Example:
  99. * --boundary_azertyuiop
  100. * Header1: value
  101. * Header2: value
  102. *
  103. * Content of
  104. * the part
  105. *
  106. */
  107. public function parseNextPart(): array {
  108. $this->readBoundary();
  109. $headers = $this->readPartHeaders();
  110. $content = $this->readPartContent((int)$headers['content-length'], $headers['x-file-md5']);
  111. return [$headers, $content];
  112. }
  113. /**
  114. * Read the boundary and check its content.
  115. *
  116. * @throws BadRequest
  117. */
  118. private function readBoundary(): string {
  119. if (!$this->isAtBoundary()) {
  120. throw new BadRequest('Boundary not found where it should be.');
  121. }
  122. return fread($this->stream, strlen($this->boundary));
  123. }
  124. /**
  125. * Return the headers of a part of the multipart body.
  126. *
  127. * @throws Exception
  128. * @throws BadRequest
  129. * @throws LengthRequired
  130. */
  131. private function readPartHeaders(): array {
  132. $headers = [];
  133. while (($line = fgets($this->stream)) !== "\r\n") {
  134. if ($line === false) {
  135. throw new Exception('An error occurred while reading headers of a part');
  136. }
  137. if (!str_contains($line, ':')) {
  138. $this->logger->error('Header missing ":" on bulk request: ' . json_encode($line));
  139. throw new Exception('An error occurred while reading headers of a part', Http::STATUS_BAD_REQUEST);
  140. }
  141. try {
  142. [$key, $value] = explode(':', $line, 2);
  143. $headers[strtolower(trim($key))] = trim($value);
  144. } catch (\Exception $e) {
  145. throw new BadRequest('An error occurred while parsing headers of a part', Http::STATUS_BAD_REQUEST, $e);
  146. }
  147. }
  148. if (!isset($headers['content-length'])) {
  149. throw new LengthRequired('The Content-Length header must not be null.');
  150. }
  151. if (!isset($headers['x-file-md5'])) {
  152. throw new BadRequest('The X-File-MD5 header must not be null.');
  153. }
  154. return $headers;
  155. }
  156. /**
  157. * Return the content of a part of the multipart body.
  158. *
  159. * @throws Exception
  160. * @throws BadRequest
  161. */
  162. private function readPartContent(int $length, string $md5): string {
  163. $computedMd5 = $this->computeMd5Hash($length);
  164. if ($md5 !== $computedMd5) {
  165. throw new BadRequest('Computed md5 hash is incorrect.');
  166. }
  167. if ($length === 0) {
  168. $content = '';
  169. } else {
  170. $content = stream_get_line($this->stream, $length);
  171. }
  172. if ($content === false) {
  173. throw new Exception("Fail to read part's content.");
  174. }
  175. if ($length !== 0 && feof($this->stream)) {
  176. throw new Exception('Unexpected EOF while reading stream.');
  177. }
  178. // Read '\r\n'.
  179. stream_get_contents($this->stream, 2);
  180. return $content;
  181. }
  182. /**
  183. * Compute the MD5 hash of the next x bytes.
  184. */
  185. private function computeMd5Hash(int $length): string {
  186. $context = hash_init('md5');
  187. hash_update_stream($context, $this->stream, $length);
  188. fseek($this->stream, -$length, SEEK_CUR);
  189. return hash_final($context);
  190. }
  191. }