| 3 |
liveuser |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Guzzle\Http\Curl;
|
|
|
4 |
|
|
|
5 |
use Guzzle\Http\Message\RequestInterface;
|
|
|
6 |
use Guzzle\Http\EntityBody;
|
|
|
7 |
use Guzzle\Http\Message\Response;
|
|
|
8 |
|
|
|
9 |
/**
|
|
|
10 |
* Mediator between curl handles and request objects
|
|
|
11 |
*/
|
|
|
12 |
class RequestMediator
|
|
|
13 |
{
|
|
|
14 |
/** @var RequestInterface */
|
|
|
15 |
protected $request;
|
|
|
16 |
|
|
|
17 |
/** @var bool Whether or not to emit read/write events */
|
|
|
18 |
protected $emitIo;
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* @param RequestInterface $request Request to mediate
|
|
|
22 |
* @param bool $emitIo Set to true to dispatch events on input and output
|
|
|
23 |
*/
|
|
|
24 |
public function __construct(RequestInterface $request, $emitIo = false)
|
|
|
25 |
{
|
|
|
26 |
$this->request = $request;
|
|
|
27 |
$this->emitIo = $emitIo;
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Receive a response header from curl
|
|
|
32 |
*
|
|
|
33 |
* @param resource $curl Curl handle
|
|
|
34 |
* @param string $header Received header
|
|
|
35 |
*
|
|
|
36 |
* @return int
|
|
|
37 |
*/
|
|
|
38 |
public function receiveResponseHeader($curl, $header)
|
|
|
39 |
{
|
|
|
40 |
static $normalize = array("\r", "\n");
|
|
|
41 |
$length = strlen($header);
|
|
|
42 |
$header = str_replace($normalize, '', $header);
|
|
|
43 |
|
|
|
44 |
if (strpos($header, 'HTTP/') === 0) {
|
|
|
45 |
|
|
|
46 |
$startLine = explode(' ', $header, 3);
|
|
|
47 |
$code = $startLine[1];
|
|
|
48 |
$status = isset($startLine[2]) ? $startLine[2] : '';
|
|
|
49 |
|
|
|
50 |
// Only download the body of the response to the specified response
|
|
|
51 |
// body when a successful response is received.
|
|
|
52 |
if ($code >= 200 && $code < 300) {
|
|
|
53 |
$body = $this->request->getResponseBody();
|
|
|
54 |
} else {
|
|
|
55 |
$body = EntityBody::factory();
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
$response = new Response($code, null, $body);
|
|
|
59 |
$response->setStatus($code, $status);
|
|
|
60 |
$this->request->startResponse($response);
|
|
|
61 |
|
|
|
62 |
$this->request->dispatch('request.receive.status_line', array(
|
|
|
63 |
'request' => $this,
|
|
|
64 |
'line' => $header,
|
|
|
65 |
'status_code' => $code,
|
|
|
66 |
'reason_phrase' => $status
|
|
|
67 |
));
|
|
|
68 |
|
|
|
69 |
} elseif ($pos = strpos($header, ':')) {
|
|
|
70 |
$this->request->getResponse()->addHeader(
|
|
|
71 |
trim(substr($header, 0, $pos)),
|
|
|
72 |
trim(substr($header, $pos + 1))
|
|
|
73 |
);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
return $length;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* Received a progress notification
|
|
|
81 |
*
|
|
|
82 |
* @param int $downloadSize Total download size
|
|
|
83 |
* @param int $downloaded Amount of bytes downloaded
|
|
|
84 |
* @param int $uploadSize Total upload size
|
|
|
85 |
* @param int $uploaded Amount of bytes uploaded
|
|
|
86 |
* @param resource $handle CurlHandle object
|
|
|
87 |
*/
|
|
|
88 |
public function progress($downloadSize, $downloaded, $uploadSize, $uploaded, $handle = null)
|
|
|
89 |
{
|
|
|
90 |
$this->request->dispatch('curl.callback.progress', array(
|
|
|
91 |
'request' => $this->request,
|
|
|
92 |
'handle' => $handle,
|
|
|
93 |
'download_size' => $downloadSize,
|
|
|
94 |
'downloaded' => $downloaded,
|
|
|
95 |
'upload_size' => $uploadSize,
|
|
|
96 |
'uploaded' => $uploaded
|
|
|
97 |
));
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Write data to the response body of a request
|
|
|
102 |
*
|
|
|
103 |
* @param resource $curl Curl handle
|
|
|
104 |
* @param string $write Data that was received
|
|
|
105 |
*
|
|
|
106 |
* @return int
|
|
|
107 |
*/
|
|
|
108 |
public function writeResponseBody($curl, $write)
|
|
|
109 |
{
|
|
|
110 |
if ($this->emitIo) {
|
|
|
111 |
$this->request->dispatch('curl.callback.write', array(
|
|
|
112 |
'request' => $this->request,
|
|
|
113 |
'write' => $write
|
|
|
114 |
));
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
if ($response = $this->request->getResponse()) {
|
|
|
118 |
return $response->getBody()->write($write);
|
|
|
119 |
} else {
|
|
|
120 |
// Unexpected data received before response headers - abort transfer
|
|
|
121 |
return 0;
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
/**
|
|
|
126 |
* Read data from the request body and send it to curl
|
|
|
127 |
*
|
|
|
128 |
* @param resource $ch Curl handle
|
|
|
129 |
* @param resource $fd File descriptor
|
|
|
130 |
* @param int $length Amount of data to read
|
|
|
131 |
*
|
|
|
132 |
* @return string
|
|
|
133 |
*/
|
|
|
134 |
public function readRequestBody($ch, $fd, $length)
|
|
|
135 |
{
|
|
|
136 |
if (!($body = $this->request->getBody())) {
|
|
|
137 |
return '';
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
$read = (string) $body->read($length);
|
|
|
141 |
if ($this->emitIo) {
|
|
|
142 |
$this->request->dispatch('curl.callback.read', array('request' => $this->request, 'read' => $read));
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
return $read;
|
|
|
146 |
}
|
|
|
147 |
}
|