-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only read up to Content-Length in stream wrapper
This commit updates the stream wrapper to only read up to the number of bytes returned in the Content-Length header when draining a stream synchronously.
- Loading branch information
Showing
3 changed files
with
114 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
use GuzzleHttp\Pool; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Psr7\Request; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\MessageFormatter; | ||
use GuzzleHttp\Middleware; | ||
use PHPHtmlParser\Dom; | ||
use PHPHtmlParser\Exceptions\EmptyCollectionException; | ||
|
||
$stack = new HandlerStack(); | ||
$formatter = new MessageFormatter(); | ||
$stack->setHandler(\GuzzleHttp\choose_handler()); | ||
$stack->push(Middleware::httpErrors()); | ||
$client = new Client(['handler' => $stack,]); | ||
|
||
function overviewRequestFactory($page = 1) | ||
{ | ||
return new Request('GET', sprintf('https://kat.cr/movies/%d/', $page)); | ||
} | ||
|
||
function subpageRequestFactory($url) | ||
{ | ||
return new Request('GET', 'https://kat.cr' . $url); | ||
} | ||
|
||
$iterator = new SplQueue(); | ||
|
||
// if the upper bound is set to a number > 5 it will work | ||
for ($i = 0; $i < 2; ++$i) { | ||
$iterator->push(overviewRequestFactory($i)); | ||
} | ||
|
||
$requestGenerator = function() use ($iterator) { | ||
while (!$iterator->isEmpty()) { | ||
yield $iterator->pop(); | ||
} | ||
}; | ||
|
||
$pool = new Pool($client, $requestGenerator(), [ | ||
'concurrency' => 5, | ||
'fulfilled' => function ($response) use ($iterator) { | ||
//parse HTML response | ||
$dom = new Dom(); | ||
$dom->load($response->getBody()); | ||
if ($dom->find('title')->innerHTML === 'Download Movie Torrents - Kickass Torrents') { | ||
//this is a overviewResponse | ||
echo "received overview response\n"; | ||
try { | ||
$rows = $dom->find('#mainSearchTable')->find('table')->find('tr'); | ||
for ($i = 1;$i < count($rows); ++$i) { //skip first row (header) | ||
$row = $rows[$i]; | ||
$url = $row->find('td')->find('.markeredBlock')->find('a')->getAttribute('href'); | ||
####### ADD NEW REQUEST | ||
echo $url . "\n"; | ||
$iterator->push(subpageRequestFactory($url)); | ||
} | ||
} catch (EmptyCollectionException $e) { | ||
echo $e; | ||
} | ||
} else { | ||
//this is a subpageResponse | ||
echo "Subpage response\n"; | ||
} | ||
}, | ||
'rejected' => function ($reason) { | ||
echo $reason; | ||
}, | ||
]); | ||
|
||
$pool->promise()->wait(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters