Пример #1
0
 /**
  * Purge URI
  *
  * @param string $uri
  * @return boolean
  */
 function purge($uri)
 {
     require_once W3TC_INC_DIR . '/functions/http.php';
     @set_time_limit($this->_timeout);
     if (strpos($uri, '/') !== 0) {
         $uri = '/' . $uri;
     }
     foreach ((array) $this->_servers as $server) {
         $url = sprintf('http://%s%s', $server, $uri);
         $response = w3_http_request($url, array('method' => 'PURGE'));
         if (is_nxt_error($response)) {
             $this->_log($url, sprintf('Unable to send request: %s.', implode('; ', $response->get_error_messages())));
             return false;
         }
         if ($response['response']['code'] !== 200) {
             $this->_log($url, 'Bad response code.');
             return false;
         }
         $this->_log($url, 'OK');
     }
     return true;
 }
Пример #2
0
/**
 * Send PURGE request to Varnish server
 *
 * @param string $url
 * @param string $auth
 * $param boolean $check_status
 * @param bool $check_status
 * @return string
 */
function w3_http_purge($url, $auth = '', $check_status = true)
{
    return w3_http_request('PURGE', $url, null, $auth, $check_status);
}
Пример #3
0
/**
 * Sends HTTP GET request
 *
 * @param string $url
 * @param array $args
 * @return array|WP_Error
 */
function w3_http_get($url, $args = array())
{
    $args = array_merge($args, array('method' => 'GET'));
    return w3_http_request($url, $args);
}
Пример #4
0
 function _request($varnish_server, $url)
 {
     $parse_url = @parse_url($url);
     if (!$parse_url || !isset($parse_url['host'])) {
         return new WP_Error('http_request_failed', 'Unrecognized URL format ' . $url);
     }
     $host = $parse_url['host'];
     $port = isset($parse_url['port']) ? (int) $parse_url['port'] : 80;
     $path = !empty($parse_url['path']) ? $parse_url['path'] : '/';
     $query = isset($parse_url['query']) ? $parse_url['query'] : '';
     $request_uri = $path . ($query != '' ? '?' . $query : '');
     if (strpos($varnish_server, ':')) {
         list($varnish_host, $varnish_port) = explode(':', $varnish_server);
     } else {
         $varnish_host = $varnish_server;
         $varnish_port = 80;
     }
     // if url host is the same as varnish server - we can use regular
     // wordpress http infrastructure, otherwise custom request should be
     // sent using fsockopen, since we send request to other server than
     // specified by $url
     if ($host == $varnish_host && $port == $varnish_port) {
         return w3_http_request($url, array('method' => 'PURGE'));
     }
     $request_headers_array = array(sprintf('PURGE %s HTTP/1.1', $request_uri), sprintf('Host: %s', $host), sprintf('User-Agent: %s', W3TC_POWERED_BY), 'Connection: close');
     $request_headers = implode("\r\n", $request_headers_array);
     $request = $request_headers . "\r\n\r\n";
     // log what we are about to do
     $this->_log($url, sprintf('Connecting to %s ...', $varnish_host));
     $this->_log($url, sprintf('PURGE %s HTTP/1.1', $request_uri));
     $this->_log($url, sprintf('Host: %s', $host));
     $errno = null;
     $errstr = null;
     $fp = @fsockopen($varnish_host, $varnish_port, $errno, $errstr, 10);
     if (!$fp) {
         return new WP_Error('http_request_failed', $errno . ': ' . $errstr);
     }
     @stream_set_timeout($fp, 60);
     @fputs($fp, $request);
     $response = '';
     while (!@feof($fp)) {
         $response .= @fgets($fp, 4096);
     }
     @fclose($fp);
     list($response_headers, $contents) = explode("\r\n\r\n", $response, 2);
     $matches = null;
     if (preg_match('~^HTTP/1.[01] (\\d+)~', $response_headers, $matches)) {
         $status = (int) $matches[1];
         $return = array('response' => array('code' => $status));
         return $return;
     }
     return new WP_Error('http_request_failed', 'Unrecognized response header' . $response_headers);
 }