Merge "Fix 'Tags' padding to keep it farther from the edge and document the source...
[lhc/web/wiklou.git] / includes / libs / virtualrest / VirtualRESTServiceClient.php
1 <?php
2 /**
3 * Virtual HTTP service client
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Virtual HTTP service client loosely styled after a Virtual File System
25 *
26 * Services can be mounted on path prefixes so that virtual HTTP operations
27 * against sub-paths will map to those services. Operations can actually be
28 * done using HTTP messages over the wire or may simple be emulated locally.
29 *
30 * Virtual HTTP request maps are arrays that use the following format:
31 * - method : GET/HEAD/PUT/POST/DELETE
32 * - url : HTTP/HTTPS URL or virtual service path with a registered prefix
33 * - query : <query parameter field/value associative array> (uses RFC 3986)
34 * - headers : <header name/value associative array>
35 * - body : source to get the HTTP request body from;
36 * this can simply be a string (always), a resource for
37 * PUT requests, and a field/value array for POST request;
38 * array bodies are encoded as multipart/form-data and strings
39 * use application/x-www-form-urlencoded (headers sent automatically)
40 * - stream : resource to stream the HTTP response body to
41 * Request maps can use integer index 0 instead of 'method' and 1 instead of 'url'.
42 *
43 * @since 1.23
44 */
45 class VirtualRESTServiceClient {
46 /** @var MultiHttpClient */
47 private $http;
48 /** @var array Map of (prefix => VirtualRESTService|array) */
49 private $instances = [];
50
51 const VALID_MOUNT_REGEX = '#^/[0-9a-z]+/([0-9a-z]+/)*$#';
52
53 /**
54 * @param MultiHttpClient $http
55 */
56 public function __construct( MultiHttpClient $http ) {
57 $this->http = $http;
58 }
59
60 /**
61 * Map a prefix to service handler
62 *
63 * If $instance is in array, it must have these keys:
64 * - class : string; fully qualified VirtualRESTService class name
65 * - config : array; map of parameters that is the first __construct() argument
66 *
67 * @param string $prefix Virtual path
68 * @param VirtualRESTService|array $instance Service or info to yield the service
69 */
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
84 /**
85 * Unmap a prefix to service handler
86 *
87 * @param string $prefix Virtual path
88 */
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
98 /**
99 * Get the prefix and service that a virtual path is serviced by
100 *
101 * @param string $path
102 * @return array (prefix,VirtualRESTService) or (null,null) if none found
103 */
104 public function getMountAndService( $path ) {
105 $cmpFunc = 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
125 /**
126 * Execute a virtual HTTP(S) request
127 *
128 * This method returns a response map of:
129 * - code : HTTP response code or 0 if there was a serious cURL error
130 * - reason : HTTP response reason (empty if there was a serious cURL error)
131 * - headers : <header name/value associative array>
132 * - body : HTTP response body or resource (if "stream" was set)
133 * - error : Any cURL error string
134 * The map also stores integer-indexed copies of these values. This lets callers do:
135 * @code
136 * list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $client->run( $req );
137 * @endcode
138 * @param array $req Virtual HTTP request maps
139 * @return array Response array for request
140 */
141 public function run( array $req ) {
142 return $this->runMulti( [ $req ] )[0];
143 }
144
145 /**
146 * Execute a set of virtual HTTP(S) requests concurrently
147 *
148 * A map of requests keys to response maps is returned. Each response map has:
149 * - code : HTTP response code or 0 if there was a serious cURL error
150 * - reason : HTTP response reason (empty if there was a serious cURL error)
151 * - headers : <header name/value associative array>
152 * - body : HTTP response body or resource (if "stream" was set)
153 * - error : Any cURL error string
154 * The map also stores integer-indexed copies of these values. This lets callers do:
155 * @code
156 * list( $rcode, $rdesc, $rhdrs, $rbody, $rerr ) = $responses[0];
157 * @endcode
158 *
159 * @param array $reqs Map of Virtual HTTP request maps
160 * @return array $reqs Map of corresponding response values with the same keys/order
161 * @throws Exception
162 */
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 = function () use ( &$curUniqueId ) {
207 return $curUniqueId++;
208 };
209
210 $rounds = 0;
211 do {
212 if ( ++$rounds > 5 ) { // sanity
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 // Run the actual work HTTP requests
246 foreach ( $this->http->runMulti( $executeReqs ) as $index => $ranReq ) {
247 $doneReqs[$index] = $ranReq;
248 unset( $origPending[$index] );
249 }
250 $executeReqs = [];
251 // Services can also replace requests with new ones, either to
252 // defer the original or to set a proxy response to the original.
253 // Any replacement requests executed above will need to be replaced
254 // with new requests (eventually the original). The responses can be
255 // forced by setting 'response' rather than actually be sent over the wire.
256 $newReplaceReqsByService = [];
257 foreach ( $checkReqIndexesByPrefix as $prefix => $servReqIndexes ) {
258 $service = $this->getInstance( $prefix );
259 // $doneReqs actually has the requests (with 'response' set)
260 $servReqs = array_intersect_key( $doneReqs, $servReqIndexes );
261 foreach ( $service->onResponses( $servReqs, $idFunc ) as $index => $req ) {
262 // Services use unique IDs for replacement requests
263 if ( isset( $servReqs[$index] ) || isset( $origPending[$index] ) ) {
264 // A current or original request which was not modified
265 } else {
266 // Replacement requests with pre-set responses should not execute
267 $newReplaceReqsByService[$prefix][$index] = $req;
268 }
269 if ( isset( $req['response'] ) ) {
270 // Replacement requests with pre-set responses should not execute
271 unset( $origPending[$index] );
272 $doneReqs[$index] = $req;
273 } else {
274 // Update the request in case it was mangled
275 $executeReqs[$index] = $req;
276 }
277 }
278 }
279 // Update index of requests to inspect for replacement
280 $replaceReqsByService = $newReplaceReqsByService;
281 } while ( count( $origPending ) );
282
283 $responses = [];
284 // Update $reqs to include 'response' and normalized request 'headers'.
285 // This maintains the original order of $reqs.
286 foreach ( $reqs as $origIndex => $req ) {
287 $index = $armoredIndexMap[$origIndex];
288 if ( !isset( $doneReqs[$index] ) ) {
289 throw new UnexpectedValueException( "Response for request '$index' is NULL." );
290 }
291 $responses[$origIndex] = $doneReqs[$index]['response'];
292 }
293
294 return $responses;
295 }
296
297 /**
298 * @param string $prefix
299 * @return VirtualRESTService
300 */
301 private function getInstance( $prefix ) {
302 if ( !isset( $this->instances[$prefix] ) ) {
303 throw new RuntimeException( "No service registered at prefix '{$prefix}'." );
304 }
305
306 if ( !( $this->instances[$prefix] instanceof VirtualRESTService ) ) {
307 $config = $this->instances[$prefix]['config'];
308 $class = $this->instances[$prefix]['class'];
309 $service = new $class( $config );
310 if ( !( $service instanceof VirtualRESTService ) ) {
311 throw new UnexpectedValueException( "Registered service has the wrong class." );
312 }
313 $this->instances[$prefix] = $service;
314 }
315
316 return $this->instances[$prefix];
317 }
318 }