xref: /plugin/davcal/vendor/sabre/http/examples/reverseproxy.php (revision a1a3b6794e0e143a4a8b51d3185ce2d339be61ab)
1*a1a3b679SAndreas Boehler<?php
2*a1a3b679SAndreas Boehler
3*a1a3b679SAndreas Boehler// The url we're proxying to.
4*a1a3b679SAndreas Boehler$remoteUrl = 'http://example.org/';
5*a1a3b679SAndreas Boehler
6*a1a3b679SAndreas Boehler// The url we're proxying from. Please note that this must be a relative url,
7*a1a3b679SAndreas Boehler// and basically acts as the base url.
8*a1a3b679SAndreas Boehler//
9*a1a3b679SAndreas Boehler// If your $remoteUrl doesn't end with a slash, this one probably shouldn't
10*a1a3b679SAndreas Boehler// either.
11*a1a3b679SAndreas Boehler$myBaseUrl = '/reverseproxy.php';
12*a1a3b679SAndreas Boehler// $myBaseUrl = '/~evert/sabre/http/examples/reverseproxy.php/';
13*a1a3b679SAndreas Boehler
14*a1a3b679SAndreas Boehleruse Sabre\HTTP\Sapi;
15*a1a3b679SAndreas Boehleruse Sabre\HTTP\Client;
16*a1a3b679SAndreas Boehler
17*a1a3b679SAndreas Boehler// Find the autoloader
18*a1a3b679SAndreas Boehler$paths = [
19*a1a3b679SAndreas Boehler    __DIR__ . '/../vendor/autoload.php',
20*a1a3b679SAndreas Boehler    __DIR__ . '/../../../autoload.php',
21*a1a3b679SAndreas Boehler    __DIR__ . '/vendor/autoload.php',
22*a1a3b679SAndreas Boehler
23*a1a3b679SAndreas Boehler];
24*a1a3b679SAndreas Boehler
25*a1a3b679SAndreas Boehlerforeach ($paths as $path) {
26*a1a3b679SAndreas Boehler    if (file_exists($path)) {
27*a1a3b679SAndreas Boehler        include $path;
28*a1a3b679SAndreas Boehler        break;
29*a1a3b679SAndreas Boehler    }
30*a1a3b679SAndreas Boehler}
31*a1a3b679SAndreas Boehler
32*a1a3b679SAndreas Boehler
33*a1a3b679SAndreas Boehler$request = Sapi::getRequest();
34*a1a3b679SAndreas Boehler$request->setBaseUrl($myBaseUrl);
35*a1a3b679SAndreas Boehler
36*a1a3b679SAndreas Boehler$subRequest = clone $request;
37*a1a3b679SAndreas Boehler
38*a1a3b679SAndreas Boehler// Removing the Host header.
39*a1a3b679SAndreas Boehler$subRequest->removeHeader('Host');
40*a1a3b679SAndreas Boehler
41*a1a3b679SAndreas Boehler// Rewriting the url.
42*a1a3b679SAndreas Boehler$subRequest->setUrl($remoteUrl . $request->getPath());
43*a1a3b679SAndreas Boehler
44*a1a3b679SAndreas Boehler$client = new Client();
45*a1a3b679SAndreas Boehler
46*a1a3b679SAndreas Boehler// Sends the HTTP request to the server
47*a1a3b679SAndreas Boehler$response = $client->send($subRequest);
48*a1a3b679SAndreas Boehler
49*a1a3b679SAndreas Boehler// Sends the response back to the client that connected to the proxy.
50*a1a3b679SAndreas BoehlerSapi::sendResponse($response);
51