Path: blob/master/src/aphront/requeststream/AphrontRequestStream.php
12241 views
<?php12final class AphrontRequestStream extends Phobject {34private $encoding;5private $stream;6private $closed;7private $iterator;89public function setEncoding($encoding) {10$this->encoding = $encoding;11return $this;12}1314public function getEncoding() {15return $this->encoding;16}1718public function getIterator() {19if (!$this->iterator) {20$this->iterator = new PhutilStreamIterator($this->getStream());21}22return $this->iterator;23}2425public function readData() {26if (!$this->iterator) {27$iterator = $this->getIterator();28$iterator->rewind();29} else {30$iterator = $this->getIterator();31}3233if (!$iterator->valid()) {34return null;35}3637$data = $iterator->current();38$iterator->next();3940return $data;41}4243private function getStream() {44if (!$this->stream) {45$this->stream = $this->newStream();46}4748return $this->stream;49}5051private function newStream() {52$stream = fopen('php://input', 'rb');53if (!$stream) {54throw new Exception(55pht(56'Failed to open stream "%s" for reading.',57'php://input'));58}5960$encoding = $this->getEncoding();61if ($encoding === 'gzip') {62// This parameter is magic. Values 0-15 express a time/memory tradeoff,63// but the largest value (15) corresponds to only 32KB of memory and64// data encoded with a smaller window size than the one we pass can not65// be decompressed. Always pass the maximum window size.6667// Additionally, you can add 16 (to enable gzip) or 32 (to enable both68// gzip and zlib). Add 32 to support both.69$zlib_window = 15 + 32;7071$ok = stream_filter_append(72$stream,73'zlib.inflate',74STREAM_FILTER_READ,75array(76'window' => $zlib_window,77));78if (!$ok) {79throw new Exception(80pht(81'Failed to append filter "%s" to input stream while processing '.82'a request with "%s" encoding.',83'zlib.inflate',84$encoding));85}86}8788return $stream;89}9091public static function supportsGzip() {92if (!function_exists('gzencode') || !function_exists('gzdecode')) {93return false;94}9596$has_zlib = false;9798// NOTE: At least locally, this returns "zlib.*", which is not terribly99// reassuring. We care about "zlib.inflate".100101$filters = stream_get_filters();102foreach ($filters as $filter) {103if (!strncasecmp($filter, 'zlib.', strlen('zlib.'))) {104$has_zlib = true;105break;106}107}108109return $has_zlib;110}111112}113114115