Skip to content

Commit

Permalink
Fix for HHVM stream_filters
Browse files Browse the repository at this point in the history
HHVM seemed to have trouble removing a read filter -- it appears to
succeed but reading input after removing continues to be filtered.

In addition using stream_copy_to_stream (likely only applies with
stream filters) would stop before eof was reached.
  • Loading branch information
zbateson committed Sep 25, 2016
1 parent 3fb0499 commit 4557a57
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions src/MimePart.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,19 @@ private function setCharsetStreamFilterOnStream($handle)
* Appends a stream filter the passed resource handle based on the type of
* encoding for the current mime part.
*
* @param resource $handle
* Unfortunately PHP seems to error out allocating memory for
* stream_filter_make_writable in Base64EncodeStreamFilter using
* STREAM_FILTER_WRITE, and HHVM doesn't seem to remove the filter properly
* for STREAM_FILTER_READ, so the function appends a read filter on
* $fromHandle if running through 'php', and a write filter on $toHandle if
* using HHVM.
*
* @param resource $fromHandle
* @param resource $toHandle
* @param \ZBateson\MailMimeParser\Stream\StreamLeftover $leftovers
* @return resource the stream filter
*/
private function setTransferEncodingFilterOnStream($handle, StreamLeftover $leftovers)
private function setTransferEncodingFilterOnStream($fromHandle, $toHandle, StreamLeftover $leftovers)
{
$encoding = strtolower($this->getHeaderValue('Content-Transfer-Encoding'));
$params = [
Expand All @@ -410,12 +418,21 @@ private function setTransferEncodingFilterOnStream($handle, StreamLeftover $left
'x-uuencode' => 'mailmimeparser-uuencode',
];
if (isset($typeToEncoding[$encoding])) {
return stream_filter_append(
$handle,
$typeToEncoding[$encoding],
STREAM_FILTER_READ,
$params
);
if (defined('HHVM_VERSION')) {
return stream_filter_append(
$toHandle,
$typeToEncoding[$encoding],
STREAM_FILTER_WRITE,
$params
);
} else {
return stream_filter_append(
$fromHandle,
$typeToEncoding[$encoding],
STREAM_FILTER_READ,
$params
);
}
}
return null;
}
Expand Down Expand Up @@ -445,7 +462,11 @@ private function copyContentStream($fromHandle, $toHandle, $isUUEncoded)
{
$pos = ftell($fromHandle);
rewind($fromHandle);
stream_copy_to_stream($fromHandle, $toHandle);
// changed from stream_copy_to_stream because hhvm seems to stop before
// end of file for some reason
while (($read = fread($fromHandle, 1024)) != false) {
fwrite($toHandle, $read);
}
fseek($fromHandle, $pos);
}

Expand Down Expand Up @@ -473,9 +494,10 @@ protected function writeContentTo($handle)
if (!empty($this->handle)) {
$filter = $this->setCharsetStreamFilterOnStream($handle);
$leftovers = new StreamLeftover();
$encodingFilter = $this->setTransferEncodingFilterOnStream($this->handle, $leftovers);
$encodingFilter = $this->setTransferEncodingFilterOnStream($this->handle, $handle, $leftovers);
$this->copyContentStream($this->handle, $handle, $this->isUUEncoded());
if ($encodingFilter !== null) {
fflush($handle);
stream_filter_remove($encodingFilter);
if (!empty($leftovers->encodedValue)) {
fwrite($handle, $leftovers->encodedValue);
Expand Down

0 comments on commit 4557a57

Please sign in to comment.