Skip to content

Commit

Permalink
Separate public and private parts of the API, support redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Jul 14, 2024
1 parent d8c6a40 commit 2cda531
Show file tree
Hide file tree
Showing 20 changed files with 540 additions and 677 deletions.
6 changes: 6 additions & 0 deletions chunked_encoding_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ const server = http.createServer((req, res) => {
const acceptEncoding = req.headers['accept-encoding'];
const useGzip = acceptEncoding && acceptEncoding.includes('gzip');

if (req.headers['please-redirect']) {
res.writeHead(301, { Location: req.url });
res.end();
return;
}

// Set headers for chunked transfer encoding if HTTP/1.1
if (isHttp11) {
res.setHeader('Transfer-Encoding', 'chunked');
Expand Down
42 changes: 27 additions & 15 deletions http_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,50 @@
use WordPress\AsyncHttp\Request;

require __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/src/WordPress/Streams/StreamWrapperInterface.php';
require_once __DIR__ . '/src/WordPress/Streams/VanillaStreamWrapper.php';
require_once __DIR__ . '/src/WordPress/Streams/StreamPeekerWrapper.php';
require_once __DIR__ . '/src/WordPress/AsyncHttp/InflateStreamWrapper.php';
require_once __DIR__ . '/src/WordPress/AsyncHttp/InflateStreamWrapperData.php';

$requests = [
new Request("https://playground.internal"),
(new Request("https://anglesharp.azurewebsites.net/Chunked"))->set_http_version('1.1'),
(new Request("https://anglesharp.azurewebsites.net/Chunked"))->set_http_version('1.0'),
(new Request("http://127.0.0.1:3000/"))->set_http_version('1.0'),
new Request("https://anglesharp.azurewebsites.net/Chunked", [
'http_version' => '1.1'
]),
new Request("https://anglesharp.azurewebsites.net/Chunked", [
'http_version' => '1.0'
]),
new Request("http://127.0.0.1:3000/", [
'http_version' => '1.0',
'headers' => [
'please-redirect' => 'yes'
]
]),
];

// var_dump(streams_http_response_read_bytes($streams, 1024));
// Enqueuing another request here is instant and won't start the download yet.
$client = new Client();
$client->set_progress_callback( function ( Request $request, $downloaded, $total ) {
// echo "$request->url – Downloaded: $downloaded / $total\n";
} );
$client = new Client([
'concurrency' => 2,
'on_progress' => function ( Request $request, $downloaded, $total ) {
echo "$request->url – Downloaded: $downloaded / $total\n";
},
'max_redirects' => 0
]);

$client->enqueue( $requests );

echo $client->read_bytes($requests[3], 100, Client::READ_POLL_ANY)."\n\n";

while(true) {
$request = $client->next_response_chunk();
$request = $client->await_response_bytes();
if(false === $request) {
break;
}
echo "GOT DATA CHUNK ON REQUEST $request->id:\n";
echo $request->get_response()->consume_buffer(1024);
echo $client->read_bytes($request, 1024);
echo "----------------\n\n";
}

foreach($client->get_failed_requests() as $failed_request) {
echo "Failed request to " . $failed_request->url . "" . $failed_request->error . "\n";
}


// $client->wait_for_headers($requests[3]);
// var_dump($requests[3]->get_response()->get_headers());
Loading

0 comments on commit 2cda531

Please sign in to comment.