PluginProbe
Lasso Lite – Affiliate Link Manager & Product Displays / 108
Lasso Lite – Affiliate Link Manager & Product Displays v108
158 157 155 156 154 153 152 151 150 149 148 trunk 0.9.9 104 105 106 107 108 109 110 111 112 113 114 115 All 57 releases
simple-urls / libs / Raven / CurlHandler.php

CurlHandler.php in Lasso Lite – Affiliate Link Manager & Product Displays 108, at libs/Raven/CurlHandler.php

120 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * This file is part of Raven.
4 *
5 * (c) Sentry Team
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 /**
12 * Asynchronous Curl connection manager.
13 *
14 * @package raven
15 */
16
17 // TODO(dcramer): handle ca_cert
18 class Raven_CurlHandler
19 {
20 protected $join_timeout;
21 protected $multi_handle;
22 protected $options;
23 protected $requests;
24
25 public function __construct($options, $join_timeout = 5)
26 {
27 $this->options = $options;
28 $this->multi_handle = curl_multi_init();
29 $this->requests = array();
30 $this->join_timeout = $join_timeout;
31
32 register_shutdown_function(array($this, 'join'));
33 }
34
35 public function __destruct()
36 {
37 $this->join();
38 }
39
40 public function enqueue($url, $data = null, $headers = array())
41 {
42 $ch = curl_init();
43
44 $new_headers = array();
45 foreach ($headers as $key => $value) {
46 array_push($new_headers, $key .': '. $value);
47 }
48 // XXX(dcramer): Prevent 100-continue response form server (Fixes GH-216)
49 $new_headers[] = 'Expect:';
50
51 curl_setopt($ch, CURLOPT_HTTPHEADER, $new_headers);
52 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
53 curl_setopt($ch, CURLOPT_URL, $url);
54
55 curl_setopt_array($ch, $this->options);
56
57 if (isset($data)) {
58 curl_setopt($ch, CURLOPT_POST, true);
59 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
60 }
61
62 curl_multi_add_handle($this->multi_handle, $ch);
63
64 $fd = (int)$ch;
65 $this->requests[$fd] = 1;
66
67 $this->select();
68
69 return $fd;
70 }
71
72 public function join($timeout = null)
73 {
74 if (!isset($timeout)) {
75 $timeout = $this->join_timeout;
76 }
77 $start = time();
78 do {
79 $this->select();
80 if (count($this->requests) === 0) {
81 break;
82 }
83 usleep(10000);
84 } while ($timeout !== 0 && time() - $start < $timeout);
85 }
86
87 /**
88 * @doc http://php.net/manual/en/function.curl-multi-exec.php
89 */
90 protected function select()
91 {
92 do {
93 $mrc = curl_multi_exec($this->multi_handle, $active);
94 } while ($mrc == CURLM_CALL_MULTI_PERFORM);
95
96 while ($active && $mrc == CURLM_OK) {
97 if (curl_multi_select($this->multi_handle) !== -1) {
98 do {
99 $mrc = curl_multi_exec($this->multi_handle, $active);
100 } while ($mrc == CURLM_CALL_MULTI_PERFORM);
101 } else {
102 return;
103 }
104 }
105
106 while ($info = curl_multi_info_read($this->multi_handle)) {
107 $ch = $info['handle'];
108 $fd = (int)$ch;
109
110 curl_multi_remove_handle($this->multi_handle, $ch);
111
112 if (!isset($this->requests[$fd])) {
113 return;
114 }
115
116 unset($this->requests[$fd]);
117 }
118 }
119 }
120