コード例 #1
0
 private function getCacheKey(Request $request)
 {
     // If Vary headers have been passed in, fetch each header and add it to the cache key.
     $url = $request->getUri();
     $key = 'cache:' . base64_encode($url);
     return $key;
 }
コード例 #2
0
ファイル: Proxy.php プロジェクト: athlon1600/php-proxy
 public function forward(Request $request, $url)
 {
     // change request URL
     $request->setUrl($url);
     // prepare request and response objects
     $this->request = $request;
     $this->response = new Response();
     $options = array(CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_TIMEOUT => 0, CURLOPT_RETURNTRANSFER => false, CURLOPT_HEADER => false, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_FOLLOWLOCATION => false, CURLOPT_AUTOREFERER => false);
     // this is probably a good place to add custom curl options that way other critical options below would overwrite that
     $config_options = Config::get('curl', array());
     $options = array_merge_custom($options, $config_options);
     $options[CURLOPT_HEADERFUNCTION] = array($this, 'header_callback');
     $options[CURLOPT_WRITEFUNCTION] = array($this, 'write_callback');
     // Notify any listeners that the request is ready to be sent, and this is your last chance to make any modifications.
     $this->dispatcher->dispatch('request.before_send', new ProxyEvent(array('request' => $this->request, 'response' => $this->response)));
     // We may not even need to send this request if response is already available somewhere (CachePlugin)
     if ($this->request->params->has('request.complete')) {
         // do nothing?
     } else {
         // any plugin might have changed our URL by this point
         $options[CURLOPT_URL] = $this->request->getUri();
         // fill in the rest of cURL options
         $options[CURLOPT_HTTPHEADER] = explode("\r\n", $this->request->getRawHeaders());
         $options[CURLOPT_CUSTOMREQUEST] = $this->request->getMethod();
         $options[CURLOPT_POSTFIELDS] = $this->request->getRawBody();
         $ch = curl_init();
         curl_setopt_array($ch, $options);
         // fetch the status - if exception if throw any at callbacks, then the error will be supressed
         $result = @curl_exec($ch);
         // there must have been an error if at this point
         if (!$result) {
             $error = sprintf('(%d) %s', curl_errno($ch), curl_error($ch));
             throw new \Exception($error);
         }
         // we have output waiting in the buffer?
         $this->response->setContent($this->output_buffer);
         // saves memory I would assume?
         $this->output_buffer = null;
     }
     $this->dispatcher->dispatch('request.complete', new ProxyEvent(array('request' => $this->request, 'response' => $this->response)));
     return $this->response;
 }
コード例 #3
0
ファイル: index.php プロジェクト: TylerWolfe/php-proxy-app
    $plugin_class = $plugin . 'Plugin';
    if (file_exists('./plugins/' . $plugin_class . '.php')) {
        // use user plugin from /plugins/
        require_once './plugins/' . $plugin_class . '.php';
    } else {
        if (class_exists('\\Proxy\\Plugin\\' . $plugin_class)) {
            // does the native plugin from php-proxy package with such name exist?
            $plugin_class = '\\Proxy\\Plugin\\' . $plugin_class;
        }
    }
    // otherwise plugin_class better be loaded already through composer.json and match namespace exactly \\Vendor\\Plugin\\SuperPlugin
    $proxy->getEventDispatcher()->addSubscriber(new $plugin_class());
}
try {
    // request sent to index.php
    $request = Request::createFromGlobals();
    // remove all GET parameters such as ?q=
    $request->get->clear();
    // forward it to some other URL
    $response = $proxy->forward($request, $url);
    // if that was a streaming response, then everything was already sent and script will be killed before it even reaches this line
    $response->send();
} catch (Exception $ex) {
    // if the site is on server2.proxy.com then you may wish to redirect it back to proxy.com
    if (Config::get("error_redirect")) {
        $url = render_string(Config::get("error_redirect"), array('error_msg' => rawurlencode($ex->getMessage())));
        // Cannot modify header information - headers already sent
        header("HTTP/1.1 302 Found");
        header("Location: {$url}");
    } else {
        echo render_template("./templates/main.php", array('url' => $url, 'error_msg' => $ex->getMessage(), 'version' => Proxy::VERSION));
コード例 #4
0
ファイル: Request.php プロジェクト: yizhl/php-proxy
 public static function createFromGlobals()
 {
     $method = $_SERVER['REQUEST_METHOD'];
     $scheme = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'https' : 'http';
     $url = $scheme . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
     $request = new Request($method, $url);
     // fill in headers
     foreach ($_SERVER as $name => $value) {
         if (strpos($name, 'HTTP_') === 0) {
             $name = substr($name, 5);
             $name = str_replace('_', ' ', $name);
             $name = ucwords(strtolower($name));
             $name = str_replace(' ', '-', $name);
             $request->headers->set($name, $value);
         }
     }
     // for extra convenience
     //$request->params->set('user-ip', $_SERVER['REMOTE_ADDR']);
     // will be empty if content-type is multipart
     $input = file_get_contents("php://input");
     if (count($_FILES) > 0) {
         $request->post->replace($_POST);
         $request->files->replace($_FILES);
     } else {
         if (count($_POST) > 0) {
             $request->post->replace($_POST);
         } else {
             $request->setBody($input);
         }
     }
     // for extra convenience
     $request->prepare();
     return $request;
 }