1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the "php-ipfs" package. |
7
|
|
|
* |
8
|
|
|
* (c) Robert Schönthal <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace IPFS\Driver; |
15
|
|
|
|
16
|
|
|
use Http\Client\HttpAsyncClient; |
17
|
|
|
use Http\Message\MessageFactory; |
18
|
|
|
use Http\Message\MultipartStream\MultipartStreamBuilder; |
19
|
|
|
use Http\Message\UriFactory; |
20
|
|
|
use IPFS\Command\Command; |
21
|
|
|
use IPFS\Utils\AnnotationReader; |
22
|
|
|
use Psr\Http\Message\RequestInterface; |
23
|
|
|
use Psr\Http\Message\ResponseInterface; |
24
|
|
|
use Psr\Http\Message\UriInterface; |
25
|
|
|
|
26
|
|
|
class Http implements Driver |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var HttpAsyncClient |
30
|
|
|
*/ |
31
|
|
|
private $client; |
32
|
|
|
/** |
33
|
|
|
* @var MessageFactory |
34
|
|
|
*/ |
35
|
|
|
private $messageFactory; |
36
|
|
|
/** |
37
|
|
|
* @var UriFactory |
38
|
|
|
*/ |
39
|
|
|
private $uriFactory; |
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $baseUri; |
44
|
|
|
/** |
45
|
|
|
* @var AnnotationReader |
46
|
|
|
*/ |
47
|
|
|
private $reader; |
48
|
|
|
|
49
|
3 |
|
public function __construct(HttpAsyncClient $client, MessageFactory $messageFactory, UriFactory $uriFactory, AnnotationReader $reader, $baseUri = 'http://localhost:5001/api/v0') |
50
|
|
|
{ |
51
|
3 |
|
$this->client = $client; |
52
|
3 |
|
$this->messageFactory = $messageFactory; |
53
|
3 |
|
$this->uriFactory = $uriFactory; |
54
|
3 |
|
$this->baseUri = $baseUri; |
55
|
3 |
|
$this->reader = $reader; |
56
|
3 |
|
} |
57
|
|
|
|
58
|
2 |
|
public function execute(Command $command) |
59
|
|
|
{ |
60
|
2 |
|
$request = $this->buildRequest($command, $this->getConfig($command)); |
61
|
|
|
|
62
|
2 |
|
return $this->client->sendAsyncRequest($request)->then(function (ResponseInterface $response) { |
63
|
|
|
return (string) $response->getBody()->getContents(); |
64
|
2 |
|
})->wait(); |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
private function buildRequest(Command $command, $config): RequestInterface |
68
|
|
|
{ |
69
|
2 |
|
$body = $this->buildBody($command); |
70
|
|
|
|
71
|
2 |
|
return $this->messageFactory->createRequest( |
72
|
2 |
|
$config['method'], |
73
|
2 |
|
$this->buildUri($command, $config), |
74
|
2 |
|
$this->buildHeaders($body), |
75
|
2 |
|
$body->build() |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
private function buildUri(Command $command, array $config): UriInterface |
80
|
|
|
{ |
81
|
2 |
|
$uri = $this->uriFactory->createUri($this->baseUri . $config['path']); |
82
|
2 |
|
$vars = []; |
83
|
|
|
|
84
|
2 |
|
foreach ($command->getArguments() as $name => $value) { |
85
|
2 |
|
if (is_string($value) && is_readable($value)) { |
86
|
1 |
|
continue; |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
$vars[$name] = $value; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
//fix arg1= arg2= to arg= weird but seems to be correct (doubled query variable names) |
93
|
2 |
|
$query = preg_replace('/(\d+)=/', '=', http_build_query($vars)); |
94
|
|
|
|
95
|
2 |
|
return $uri->withQuery($query); |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
private function buildBody(Command $command): MultipartStreamBuilder |
99
|
|
|
{ |
100
|
2 |
|
$builder = new MultipartStreamBuilder(); |
101
|
|
|
|
102
|
2 |
|
foreach ($command->getArguments() as $name => $value) { |
103
|
2 |
|
if (is_string($value) && is_readable($value)) { |
104
|
1 |
|
$builder->addResource($name, fopen($command->getArguments()[$name], 'rb'), [ |
105
|
2 |
|
'filename' => $value, |
106
|
|
|
'headers' => ['Content-Type' => 'application/octet-stream'], |
107
|
|
|
]); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
2 |
|
return $builder; |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
|
private function buildHeaders(MultipartStreamBuilder $body): array |
115
|
|
|
{ |
116
|
2 |
|
$emptyBody = '--' . $body->getBoundary() . "--\r\n"; |
117
|
|
|
|
118
|
|
|
return [ |
119
|
2 |
|
'User-Agent' => 'php-ipfs', |
120
|
2 |
|
'Content-Type' => $body->build()->getContents() !== $emptyBody ? 'multipart/form-data; boundary=' . $body->getBoundary() : null, |
121
|
|
|
]; |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
private function getConfig(Command $command): array |
125
|
|
|
{ |
126
|
|
|
$config = [ |
127
|
2 |
|
'path' => '/' . str_replace(':', '/', $this->reader->getName($command->getMethod())), |
128
|
2 |
|
'method' => 'GET', |
129
|
|
|
]; |
130
|
|
|
|
131
|
2 |
|
foreach ($command->getArguments() as $arg) { |
132
|
2 |
|
if (is_string($arg) && is_readable($arg)) { |
133
|
1 |
|
$config['method'] = 'POST'; |
134
|
2 |
|
break; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
2 |
|
return $config; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|