This repository has been archived by the owner on Jun 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThriftHttpTransport.php
101 lines (80 loc) · 3.04 KB
/
ThriftHttpTransport.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
declare(strict_types=1);
namespace OpenTelemetry\Contrib\Jaeger;
use BadMethodCallException;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Thrift\Factory\TStringFuncFactory;
use Thrift\Transport\TTransport;
/**
* This was created in lieu of Thrift\Transport\THttpClient because of it's usage of fopen for making network requests
*/
class ThriftHttpTransport extends TTransport
{
private ClientInterface $psr18Client;
private RequestFactoryInterface $requestFactory;
private StreamFactoryInterface $streamFactory;
private ParsedEndpointUrl $parsedEndpoint;
private string $buffer = '';
public function __construct(
ClientInterface $client,
RequestFactoryInterface $requestFactory,
StreamFactoryInterface $streamFactory,
ParsedEndpointUrl $parsedEndpoint
) {
$this->psr18Client = $client;
$this->requestFactory = $requestFactory;
$this->streamFactory = $streamFactory;
$this->parsedEndpoint = $parsedEndpoint;
}
public function isOpen()
{
throw new BadMethodCallException(__FUNCTION__ . " is unused as of this writing. See Thrift\Transport\THttpClient for a reference implementation.");
}
public function open()
{
throw new BadMethodCallException(__FUNCTION__ . " is unused as of this writing. See Thrift\Transport\THttpClient for a reference implementation.");
}
public function close()
{
throw new BadMethodCallException(__FUNCTION__ . " is unused as of this writing. See Thrift\Transport\THttpClient for a reference implementation.");
}
public function read($len)
{
throw new BadMethodCallException(__FUNCTION__ . " is unused as of this writing. See Thrift\Transport\THttpClient for a reference implementation.");
}
/**
* Writes some data into the pending buffer
*
* @param string $buf The data to write
*/
public function write($buf)
{
$this->buffer .= $buf;
}
/**
* Opens and sends the actual request over the HTTP connection
*/
public function flush()
{
$endpointUrl = $this->parsedEndpoint->getEndpointUrl();
$host = $this->parsedEndpoint->getHost();
$request = $this->requestFactory->createRequest('POST', $endpointUrl);
$headers = [
'Host' => $host, //Port will be implied - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Host
'Accept' => 'application/x-thrift',
'User-Agent' => 'PHP/THttpClient',
'Content-Type' => 'application/x-thrift',
'Content-Length' => TStringFuncFactory::create()->strlen($this->buffer),
];
foreach ($headers as $key => $value) {
$request = $request->withAddedHeader($key, $value);
}
$request = $request->withBody(
$this->streamFactory->createStream($this->buffer)
);
$this->psr18Client->sendRequest($request);
$this->buffer = '';
}
}