Changeset View
Changeset View
Standalone View
Standalone View
src/aphront/requeststream/AphrontRequestStream.php
| <?php | <?php | ||||
| final class AphrontRequestStream extends Phobject { | final class AphrontRequestStream extends Phobject { | ||||
| private $encoding; | private $encoding; | ||||
| private $stream; | private $stream; | ||||
| private $closed; | private $closed; | ||||
| private $iterator; | |||||
| public function setEncoding($encoding) { | public function setEncoding($encoding) { | ||||
| $this->encoding = $encoding; | $this->encoding = $encoding; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function getEncoding() { | public function getEncoding() { | ||||
| return $this->encoding; | return $this->encoding; | ||||
| } | } | ||||
| public function getIterator() { | |||||
| if (!$this->iterator) { | |||||
| $this->iterator = new PhutilStreamIterator($this->getStream()); | |||||
| } | |||||
| return $this->iterator; | |||||
| } | |||||
| public function readData() { | public function readData() { | ||||
| if ($this->closed) { | if (!$this->iterator) { | ||||
| return null; | $iterator = $this->getIterator(); | ||||
| $iterator->rewind(); | |||||
| } else { | |||||
| $iterator = $this->getIterator(); | |||||
| } | } | ||||
| $stream = $this->getStream(); | if (!$iterator->valid()) { | ||||
| if (feof($stream)) { | |||||
| $this->closeStream(); | |||||
| return null; | return null; | ||||
| } | } | ||||
| $bytes = fread($stream, 64 * 1024); | $data = $iterator->current(); | ||||
| if ($bytes === false) { | $iterator->next(); | ||||
| throw new Exception( | |||||
| pht('Failed to fread() from request input stream.')); | |||||
| } | |||||
| return $bytes; | return $data; | ||||
| } | } | ||||
| private function getStream() { | private function getStream() { | ||||
| if (!$this->stream) { | if (!$this->stream) { | ||||
| $this->stream = $this->newStream(); | $this->stream = $this->newStream(); | ||||
| } | } | ||||
| return $this->stream; | return $this->stream; | ||||
| } | } | ||||
| private function closeStream() { | |||||
| $stream = $this->getStream(); | |||||
| fclose($stream); | |||||
| $this->closed = true; | |||||
| } | |||||
| private function newStream() { | private function newStream() { | ||||
| $stream = fopen('php://input', 'rb'); | $stream = fopen('php://input', 'rb'); | ||||
| if (!$stream) { | if (!$stream) { | ||||
| throw new Exception( | throw new Exception( | ||||
| pht( | pht( | ||||
| 'Failed to open stream "%s" for reading.', | 'Failed to open stream "%s" for reading.', | ||||
| 'php://input')); | 'php://input')); | ||||
| } | } | ||||
| Show All 33 Lines | |||||