]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/libs/virtualrest/VirtualRESTService.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / includes / libs / virtualrest / VirtualRESTService.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 instance that can be mounted on to a VirtualRESTService
25  *
26  * Sub-classes manage the logic of either:
27  *   - a) Munging virtual HTTP request arrays to have qualified URLs and auth headers
28  *   - b) Emulating the execution of virtual HTTP requests (e.g. brokering)
29  *
30  * Authentication information can be cached in instances of the class for performance.
31  * Such information should also be cached locally on the server and auth requests should
32  * have reasonable timeouts.
33  *
34  * @since 1.23
35  */
36 abstract class VirtualRESTService {
37         /** @var array Key/value map */
38         protected $params = [];
39
40         /**
41          * @param array $params Key/value map
42          */
43         public function __construct( array $params ) {
44                 $this->params = $params;
45         }
46
47         /**
48          * Return the name of this service, in a form suitable for error
49          * reporting or debugging.
50          *
51          * @return string The name of the service behind this VRS object.
52          */
53         public function getName() {
54                 return isset( $this->params['name'] ) ? $this->params['name'] : static::class;
55         }
56
57         /**
58          * Prepare virtual HTTP(S) requests (for this service) for execution
59          *
60          * This method should mangle any of the $reqs entry fields as needed:
61          *   - url      : munge the URL to have an absolute URL with a protocol
62          *                and encode path components as needed by the backend [required]
63          *   - query    : include any authentication signatures/parameters [as needed]
64          *   - headers  : include any authentication tokens/headers [as needed]
65          *
66          * The incoming URL parameter will be relative to the service mount point.
67          *
68          * This method can also remove some of the requests as well as add new ones
69          * (using $idGenerator to set each of the entries' array keys). For any existing
70          * or added request, the 'response' array can be filled in, which will prevent the
71          * client from executing it. If an original request is removed, at some point it
72          * must be added back (with the same key) in onRequests() or onResponses();
73          * it's reponse may be filled in as with other requests.
74          *
75          * @param array $reqs Map of Virtual HTTP request arrays
76          * @param Closure $idGeneratorFunc Method to generate unique keys for new requests
77          * @return array Modified HTTP request array map
78          */
79         public function onRequests( array $reqs, Closure $idGeneratorFunc ) {
80                 $result = [];
81                 foreach ( $reqs as $key => $req ) {
82                         // The default encoding treats the URL as a REST style path that uses
83                         // forward slash as a hierarchical delimiter (and never otherwise).
84                         // Subclasses can override this, and should be documented in any case.
85                         $parts = array_map( 'rawurlencode', explode( '/', $req['url'] ) );
86                         $req['url'] = $this->params['baseUrl'] . '/' . implode( '/', $parts );
87                         $result[$key] = $req;
88                 }
89                 return $result;
90         }
91
92         /**
93          * Mangle or replace virtual HTTP(S) requests which have been responded to
94          *
95          * This method may mangle any of the $reqs entry 'response' fields as needed:
96          *   - code    : perform any code normalization [as needed]
97          *   - reason  : perform any reason normalization [as needed]
98          *   - headers : perform any header normalization [as needed]
99          *
100          * This method can also remove some of the requests as well as add new ones
101          * (using $idGenerator to set each of the entries' array keys). For any existing
102          * or added request, the 'response' array can be filled in, which will prevent the
103          * client from executing it. If an original request is removed, at some point it
104          * must be added back (with the same key) in onRequests() or onResponses();
105          * it's reponse may be filled in as with other requests. All requests added to $reqs
106          * will be passed through onRequests() to handle any munging required as normal.
107          *
108          * The incoming URL parameter will be relative to the service mount point.
109          *
110          * @param array $reqs Map of Virtual HTTP request arrays with 'response' set
111          * @param Closure $idGeneratorFunc Method to generate unique keys for new requests
112          * @return array Modified HTTP request array map
113          */
114         public function onResponses( array $reqs, Closure $idGeneratorFunc ) {
115                 return $reqs;
116         }
117 }