3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
22 * This class contains the shared methods between the Proxy and makeRequest handlers
26 protected $disallowedHeaders = array('User-Agent', 'Keep-Alive', 'Host', 'Accept-Encoding', 'Set-Cookie', 'Content-Length', 'Content-Encoding', 'ETag', 'Last-Modified', 'Accept-Ranges', 'Vary', 'Expires', 'Date', 'Pragma', 'Cache-Control', 'Transfer-Encoding', 'If-Modified-Since');
28 public function __construct($context) {
29 $this->context
= $context;
33 * Retrieves the actual content
35 * @param string $url the url to fetch
36 * @return the filled in request (RemoteContentRequest)
38 protected function fetchContent($url, $method = 'GET') {
39 // Check the protocol requested - curl doesn't really support file://
40 // requests but the 'error' should be handled properly
41 $protocolSplit = explode('://', $url, 2);
42 if (count($protocolSplit) < 2) {
43 throw new Exception("Invalid protocol specified");
45 $protocol = strtoupper($protocolSplit[0]);
46 if ($protocol != "HTTP" && $protocol != "HTTPS") {
47 throw new Exception("Invalid protocol specified in url: " . htmlentities($protocol));
50 if ($method == 'POST') {
51 $data = isset($_GET['postData']) ?
$_GET['postData'] : false;
53 $data = isset($_POST['postData']) ?
$_POST['postData'] : false;
57 $data = urldecode($data);
58 $entries = explode('&', $data);
59 foreach ($entries as $entry) {
60 $parts = explode('=', $entry);
61 // Process only if its a valid value=something pair
62 if (count($parts) == 2) {
63 $postData .= urlencode($parts[0]) . '=' . urlencode($parts[1]) . '&';
66 // chop of the trailing &
67 if (strlen($postData)) {
68 $postData = substr($postData, 0, strlen($postData) - 1);
71 // even if postData is an empty string, it will still post (since RemoteContentRquest checks if its false)
72 // so the request to POST is still honored
73 $request = new RemoteContentRequest($url, null, $postData);
74 $request = $this->context
->getHttpFetcher()->fetch($request, $this->context
);
76 $request = new RemoteContentRequest($url);
77 $request = $this->context
->getHttpFetcher()->fetch($request, $this->context
);
83 * Sets the caching (Cache-Control & Expires) with a cache age of $lastModified
84 * or if $lastModified === false, sets Pragma: no-cache & Cache-Control: no-cache
86 protected function setCachingHeaders($lastModified = false) {
87 $maxAge = $this->context
->getIgnoreCache() ?
false : $this->context
->getRefreshInterval();
90 header("Last-Modified: $lastModified");
92 // time() is a kernel call, so lets avoid it and use the request time instead
93 $time = $_SERVER['REQUEST_TIME'];
94 $expires = $maxAge !== false ?
$time +
$maxAge : $time - 3000;
95 $public = $maxAge ?
'public' : 'private';
96 $maxAge = $maxAge === false ?
'0' : $maxAge;
97 header("Cache-Control: {$public}; max-age={$maxAge}", true);
98 header("Expires: " . gmdate("D, d M Y H:i:s", $expires) . " GMT", true);
100 header("Cache-Control: no-cache", true);
101 header("Pragma: no-cache", true);
106 * Does a quick-and-dirty url validation
109 * @return string the 'validated' url
111 protected function validateUrl($url) {
112 if (! @parse_url
($url)) {
113 throw new Exception("Invalid Url");
120 * Returns the request headers, using the apache_request_headers function if it's
121 * available, and otherwise tries to guess them from the $_SERVER superglobal
125 protected function request_headers() {
126 // Try to use apache's request headers if available
127 if (function_exists("apache_request_headers")) {
128 if (($headers = apache_request_headers())) {
132 // if that failed, try to create them from the _SERVER superglobal
134 foreach (array_keys($_SERVER) as $skey) {
135 if (substr($skey, 0, 5) == "HTTP_") {
136 $headername = str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($skey, 0, 5)))));
137 $headers[$headername] = $_SERVER[$skey];