MediaWiki REL1_39
VirtualRESTServiceClient.php
Go to the documentation of this file.
1<?php
47 private $http;
49 private $instances = [];
50
51 private const VALID_MOUNT_REGEX = '#^/[0-9a-z]+/([0-9a-z]+/)*$#';
52
56 public function __construct( MultiHttpClient $http ) {
57 $this->http = $http;
58 }
59
70 public function mount( $prefix, $instance ) {
71 if ( !preg_match( self::VALID_MOUNT_REGEX, $prefix ) ) {
72 throw new UnexpectedValueException( "Invalid service mount point '$prefix'." );
73 } elseif ( isset( $this->instances[$prefix] ) ) {
74 throw new UnexpectedValueException( "A service is already mounted on '$prefix'." );
75 }
76 if ( !( $instance instanceof VirtualRESTService ) ) {
77 if ( !isset( $instance['class'] ) || !isset( $instance['config'] ) ) {
78 throw new UnexpectedValueException( "Missing 'class' or 'config' ('$prefix')." );
79 }
80 }
81 $this->instances[$prefix] = $instance;
82 }
83
89 public function unmount( $prefix ) {
90 if ( !preg_match( self::VALID_MOUNT_REGEX, $prefix ) ) {
91 throw new UnexpectedValueException( "Invalid service mount point '$prefix'." );
92 } elseif ( !isset( $this->instances[$prefix] ) ) {
93 throw new UnexpectedValueException( "No service is mounted on '$prefix'." );
94 }
95 unset( $this->instances[$prefix] );
96 }
97
104 public function getMountAndService( $path ) {
105 $cmpFunc = static function ( $a, $b ) {
106 $al = substr_count( $a, '/' );
107 $bl = substr_count( $b, '/' );
108 return $bl <=> $al; // largest prefix first
109 };
110
111 $matches = []; // matching prefixes (mount points)
112 foreach ( $this->instances as $prefix => $unused ) {
113 if ( strpos( $path, $prefix ) === 0 ) {
114 $matches[] = $prefix;
115 }
116 }
117 usort( $matches, $cmpFunc );
118
119 // Return the most specific prefix and corresponding service
120 return $matches
121 ? [ $matches[0], $this->getInstance( $matches[0] ) ]
122 : [ null, null ];
123 }
124
141 public function run( array $req ) {
142 return $this->runMulti( [ $req ] )[0];
143 }
144
163 public function runMulti( array $reqs ) {
164 foreach ( $reqs as $index => &$req ) {
165 if ( isset( $req[0] ) ) {
166 $req['method'] = $req[0]; // short-form
167 unset( $req[0] );
168 }
169 if ( isset( $req[1] ) ) {
170 $req['url'] = $req[1]; // short-form
171 unset( $req[1] );
172 }
173 $req['chain'] = []; // chain or list of replaced requests
174 }
175 unset( $req ); // don't assign over this by accident
176
177 $curUniqueId = 0;
178 $armoredIndexMap = []; // (original index => new index)
179
180 $doneReqs = []; // (index => request)
181 $executeReqs = []; // (index => request)
182 $replaceReqsByService = []; // (prefix => index => request)
183 $origPending = []; // (index => 1) for original requests
184
185 foreach ( $reqs as $origIndex => $req ) {
186 // Re-index keys to consecutive integers (they will be swapped back later)
187 $index = $curUniqueId++;
188 $armoredIndexMap[$origIndex] = $index;
189 $origPending[$index] = 1;
190 if ( preg_match( '#^(http|ftp)s?://#', $req['url'] ) ) {
191 // Absolute FTP/HTTP(S) URL, run it as normal
192 $executeReqs[$index] = $req;
193 } else {
194 // Must be a virtual HTTP URL; resolve it
195 list( $prefix, $service ) = $this->getMountAndService( $req['url'] );
196 if ( !$service ) {
197 throw new UnexpectedValueException( "Path '{$req['url']}' has no service." );
198 }
199 // Set the URL to the mount-relative portion
200 $req['url'] = substr( $req['url'], strlen( $prefix ) );
201 $replaceReqsByService[$prefix][$index] = $req;
202 }
203 }
204
205 // Function to get IDs that won't collide with keys in $armoredIndexMap
206 $idFunc = static function () use ( &$curUniqueId ) {
207 return $curUniqueId++;
208 };
209
210 $rounds = 0;
211 do {
212 if ( ++$rounds > 5 ) {
213 throw new Exception( "Too many replacement rounds detected. Aborting." );
214 }
215 // Track requests executed this round that have a prefix/service.
216 // Note that this also includes requests where 'response' was forced.
217 $checkReqIndexesByPrefix = [];
218 // Resolve the virtual URLs valid and qualified HTTP(S) URLs
219 // and add any required authentication headers for the backend.
220 // Services can also replace requests with new ones, either to
221 // defer the original or to set a proxy response to the original.
222 $newReplaceReqsByService = [];
223 foreach ( $replaceReqsByService as $prefix => $servReqs ) {
224 $service = $this->getInstance( $prefix );
225 foreach ( $service->onRequests( $servReqs, $idFunc ) as $index => $req ) {
226 // Services use unique IDs for replacement requests
227 if ( isset( $servReqs[$index] ) || isset( $origPending[$index] ) ) {
228 // A current or original request which was not modified
229 } else {
230 // Replacement request that will convert to original requests
231 $newReplaceReqsByService[$prefix][$index] = $req;
232 }
233 if ( isset( $req['response'] ) ) {
234 // Replacement requests with pre-set responses should not execute
235 unset( $executeReqs[$index] );
236 unset( $origPending[$index] );
237 $doneReqs[$index] = $req;
238 } else {
239 // Original or mangled request included
240 $executeReqs[$index] = $req;
241 }
242 $checkReqIndexesByPrefix[$prefix][$index] = 1;
243 }
244 }
245
246 // MultiHttpClient::runMulti opts
247 $opts = [];
248
249 foreach ( $executeReqs as $index => &$req ) {
250 // Expand protocol-relative URLs
251 if ( preg_match( '#^//#', $req['url'] ) ) {
252 $req['url'] = wfExpandUrl( $req['url'], PROTO_CURRENT );
253 }
254
255 // Try to find a suitable timeout
256 //
257 // MultiHttpClient::runMulti opts is not per request,
258 // so pick the shortest one
259 if (
260 isset( $req['reqTimeout'] ) &&
261 ( !isset( $opts['reqTimeout'] ) ||
262 $req['reqTimeout'] < $opts['reqTimeout'] )
263 ) {
264 $opts['reqTimeout'] = $req['reqTimeout'];
265 }
266 }
267
268 // Run the actual work HTTP requests
269 foreach ( $this->http->runMulti( $executeReqs, $opts ) as $index => $ranReq ) {
270 $doneReqs[$index] = $ranReq;
271 unset( $origPending[$index] );
272 }
273 $executeReqs = [];
274 // Services can also replace requests with new ones, either to
275 // defer the original or to set a proxy response to the original.
276 // Any replacement requests executed above will need to be replaced
277 // with new requests (eventually the original). The responses can be
278 // forced by setting 'response' rather than actually be sent over the wire.
279 $newReplaceReqsByService = [];
280 foreach ( $checkReqIndexesByPrefix as $prefix => $servReqIndexes ) {
281 $service = $this->getInstance( $prefix );
282 // $doneReqs actually has the requests (with 'response' set)
283 $servReqs = array_intersect_key( $doneReqs, $servReqIndexes );
284 foreach ( $service->onResponses( $servReqs, $idFunc ) as $index => $req ) {
285 // Services use unique IDs for replacement requests
286 if ( isset( $servReqs[$index] ) || isset( $origPending[$index] ) ) {
287 // A current or original request which was not modified
288 } else {
289 // Replacement requests with pre-set responses should not execute
290 $newReplaceReqsByService[$prefix][$index] = $req;
291 }
292 if ( isset( $req['response'] ) ) {
293 // Replacement requests with pre-set responses should not execute
294 unset( $origPending[$index] );
295 $doneReqs[$index] = $req;
296 } else {
297 // Update the request in case it was mangled
298 $executeReqs[$index] = $req;
299 }
300 }
301 }
302 // Update index of requests to inspect for replacement
303 $replaceReqsByService = $newReplaceReqsByService;
304 } while ( count( $origPending ) );
305
306 $responses = [];
307 // Update $reqs to include 'response' and normalized request 'headers'.
308 // This maintains the original order of $reqs.
309 foreach ( $reqs as $origIndex => $req ) {
310 $index = $armoredIndexMap[$origIndex];
311 if ( !isset( $doneReqs[$index] ) ) {
312 throw new UnexpectedValueException( "Response for request '$index' is NULL." );
313 }
314 $responses[$origIndex] = $doneReqs[$index]['response'];
315 }
316
317 return $responses;
318 }
319
324 private function getInstance( $prefix ) {
325 if ( !isset( $this->instances[$prefix] ) ) {
326 throw new RuntimeException( "No service registered at prefix '{$prefix}'." );
327 }
328
329 if ( !( $this->instances[$prefix] instanceof VirtualRESTService ) ) {
330 $config = $this->instances[$prefix]['config'];
331 $class = $this->instances[$prefix]['class'];
332 $service = new $class( $config );
333 if ( !( $service instanceof VirtualRESTService ) ) {
334 throw new UnexpectedValueException( "Registered service has the wrong class." );
335 }
336 $this->instances[$prefix] = $service;
337 }
338
339 return $this->instances[$prefix];
340 }
341}
const PROTO_CURRENT
Definition Defines.php:198
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Class to handle multiple HTTP requests.
Virtual HTTP service client loosely styled after a Virtual File System.
mount( $prefix, $instance)
Map a prefix to service handler.
unmount( $prefix)
Unmap a prefix to service handler.
run(array $req)
Execute a virtual HTTP(S) request.
runMulti(array $reqs)
Execute a set of virtual HTTP(S) requests concurrently.
__construct(MultiHttpClient $http)
getMountAndService( $path)
Get the prefix and service that a virtual path is serviced by.
Virtual HTTP service instance that can be mounted on to a VirtualRESTService.