3 final class AphrontRequestStream
extends Phobject
{
10 public function setEncoding($encoding) {
11 $this->encoding
= $encoding;
15 public function getEncoding() {
16 return $this->encoding
;
19 public function getIterator() {
20 if (!$this->iterator
) {
21 $this->iterator
= new PhutilStreamIterator($this->getStream());
23 return $this->iterator
;
26 public function readData() {
27 if (!$this->iterator
) {
28 $iterator = $this->getIterator();
31 $iterator = $this->getIterator();
34 if (!$iterator->valid()) {
38 $data = $iterator->current();
44 private function getStream() {
46 $this->stream
= $this->newStream();
52 private function newStream() {
53 $stream = fopen('php://input', 'rb');
57 'Failed to open stream "%s" for reading.',
61 $encoding = $this->getEncoding();
62 if ($encoding === 'gzip') {
63 // This parameter is magic. Values 0-15 express a time/memory tradeoff,
64 // but the largest value (15) corresponds to only 32KB of memory and
65 // data encoded with a smaller window size than the one we pass can not
66 // be decompressed. Always pass the maximum window size.
68 // Additionally, you can add 16 (to enable gzip) or 32 (to enable both
69 // gzip and zlib). Add 32 to support both.
70 $zlib_window = 15 +
32;
72 $ok = stream_filter_append(
77 'window' => $zlib_window,
82 'Failed to append filter "%s" to input stream while processing '.
83 'a request with "%s" encoding.',
92 public static function supportsGzip() {
93 if (!function_exists('gzencode') ||
!function_exists('gzdecode')) {
99 // NOTE: At least locally, this returns "zlib.*", which is not terribly
100 // reassuring. We care about "zlib.inflate".
102 $filters = stream_get_filters();
103 foreach ($filters as $filter) {
104 if (!strncasecmp($filter, 'zlib.', strlen('zlib.'))) {