|
| 1 | +--TEST-- |
| 2 | +Chunked encoding (multiple buckets) |
| 3 | +--SKIPIF-- |
| 4 | +<?php |
| 5 | +$filters = stream_get_filters(); |
| 6 | +if(! in_array( "dechunk", $filters )) die( "skip Chunked filter not available." ); |
| 7 | +?> |
| 8 | +--INI-- |
| 9 | +allow_url_fopen=1 |
| 10 | +--FILE-- |
| 11 | +<?php |
| 12 | + |
| 13 | +// Filter that splits each bucket in the input into two, with $chunkSize bytes in the first bucket. |
| 14 | +final class Splitter extends php_user_filter { |
| 15 | + public static int $chunkSize; |
| 16 | + function filter($in, $out, &$consumed, $closing): int { |
| 17 | + while ($bucket = stream_bucket_make_writeable($in)) { |
| 18 | + $head = substr($bucket->data, 0, self::$chunkSize); |
| 19 | + $head_bucket = stream_bucket_new($this->stream, $head); |
| 20 | + stream_bucket_append($out, $head_bucket); |
| 21 | + |
| 22 | + $tail = substr($bucket->data, self::$chunkSize); |
| 23 | + $tail_bucket = stream_bucket_new($this->stream, $tail); |
| 24 | + stream_bucket_append($out, $tail_bucket); |
| 25 | + |
| 26 | + $consumed += strlen($bucket->data); |
| 27 | + return PSFS_PASS_ON; |
| 28 | + } |
| 29 | + return PSFS_FEED_ME; |
| 30 | + } |
| 31 | +} |
| 32 | +stream_filter_register("Splitter", "Splitter"); |
| 33 | + |
| 34 | +$testdata = "2;key=value\r\nte\r\n2\r\nst\r\n0\r\nTrailer: section\r\n\r\n"; |
| 35 | + |
| 36 | +// Split test data on every possible position |
| 37 | +for ($i = 1; $i < strlen($testdata); $i++) { |
| 38 | + Splitter::$chunkSize = $i; |
| 39 | + |
| 40 | + $fp = fopen("data:text/plain,$testdata", "r"); |
| 41 | + stream_filter_append($fp, "Splitter", STREAM_FILTER_READ); |
| 42 | + stream_filter_append($fp, "dechunk", STREAM_FILTER_READ); |
| 43 | + |
| 44 | + var_dump(stream_get_contents($fp)); |
| 45 | + fclose($fp); |
| 46 | +} |
| 47 | +?> |
| 48 | +--EXPECT-- |
| 49 | +string(4) "test" |
| 50 | +string(4) "test" |
| 51 | +string(4) "test" |
| 52 | +string(4) "test" |
| 53 | +string(4) "test" |
| 54 | +string(4) "test" |
| 55 | +string(4) "test" |
| 56 | +string(4) "test" |
| 57 | +string(4) "test" |
| 58 | +string(4) "test" |
| 59 | +string(4) "test" |
| 60 | +string(4) "test" |
| 61 | +string(4) "test" |
| 62 | +string(4) "test" |
| 63 | +string(4) "test" |
| 64 | +string(4) "test" |
| 65 | +string(4) "test" |
| 66 | +string(4) "test" |
| 67 | +string(4) "test" |
| 68 | +string(4) "test" |
| 69 | +string(4) "test" |
| 70 | +string(4) "test" |
| 71 | +string(4) "test" |
| 72 | +string(4) "test" |
| 73 | +string(4) "test" |
| 74 | +string(4) "test" |
| 75 | +string(4) "test" |
| 76 | +string(4) "test" |
| 77 | +string(4) "test" |
| 78 | +string(4) "test" |
| 79 | +string(4) "test" |
| 80 | +string(4) "test" |
| 81 | +string(4) "test" |
| 82 | +string(4) "test" |
| 83 | +string(4) "test" |
| 84 | +string(4) "test" |
| 85 | +string(4) "test" |
| 86 | +string(4) "test" |
| 87 | +string(4) "test" |
| 88 | +string(4) "test" |
| 89 | +string(4) "test" |
| 90 | +string(4) "test" |
| 91 | +string(4) "test" |
| 92 | +string(4) "test" |
| 93 | +string(4) "test" |
| 94 | +string(4) "test" |
0 commit comments