1: <?php
2:
3: namespace Guzzle\Http\Curl;
4:
5: use Guzzle\Http\Message\RequestInterface;
6:
7: /**
8: * Mediator between curl handles and request objects
9: */
10: class RequestMediator
11: {
12: /**
13: * @var RequestInterface
14: */
15: protected $request;
16:
17: /**
18: * @var bool Whether or not to emit read/write events
19: */
20: protected $emitIo;
21:
22: /**
23: * @var CurlHandle
24: */
25: protected $curlHandle;
26:
27: /**
28: * @param RequestInterface $request Request to mediate
29: * @param bool $emitIo Set to true to dispatch events on input and output
30: */
31: public function __construct(RequestInterface $request, $emitIo = false)
32: {
33: $this->request = $request;
34: $this->emitIo = $emitIo;
35: }
36:
37: /**
38: * Set the associated CurlHandle object
39: *
40: * @param CurlHandle $handle Curl handle
41: *
42: * @return RequestMediator
43: */
44: public function setCurlHandle(CurlHandle $handle)
45: {
46: $this->curlHandle = $handle;
47: $this->request->getParams()->set('curl_handle', $handle);
48:
49: return $this;
50: }
51:
52: /**
53: * Receive a response header from curl
54: *
55: * @param resource $curl Curl handle
56: * @param string $header Received header
57: *
58: * @return int
59: */
60: public function receiveResponseHeader($curl, $header)
61: {
62: return $this->request->receiveResponseHeader($header);
63: }
64:
65: /**
66: * Received a progress notification
67: *
68: * @param int $downloadSize Total download size
69: * @param int $downloaded Amount of bytes downloaded
70: * @param int $uploadSize Total upload size
71: * @param int $uploaded Amount of bytes uploaded
72: */
73: public function progress($downloadSize, $downloaded, $uploadSize, $uploaded)
74: {
75: $this->request->dispatch('curl.callback.progress', array(
76: 'request' => $this->request,
77: 'handle' => $this->curlHandle,
78: 'download_size' => $downloadSize,
79: 'downloaded' => $downloaded,
80: 'upload_size' => $uploadSize,
81: 'uploaded' => $uploaded
82: ));
83: }
84:
85: /**
86: * Write data to the response body of a request
87: *
88: * @param resource $curl Curl handle
89: * @param string $write Data that was received
90: *
91: * @return int
92: */
93: public function writeResponseBody($curl, $write)
94: {
95: if ($this->emitIo) {
96: $this->request->dispatch('curl.callback.write', array(
97: 'request' => $this->request,
98: 'write' => $write
99: ));
100: }
101:
102: return $this->request->getResponse()->getBody()->write($write);
103: }
104:
105: /**
106: * Read data from the request body and send it to curl
107: *
108: * @param resource $ch Curl handle
109: * @param resource $fd File descriptor
110: * @param int $length Amount of data to read
111: *
112: * @return string
113: */
114: public function readRequestBody($ch, $fd, $length)
115: {
116: $read = '';
117:
118: if ($this->request->getBody()) {
119: $read = $this->request->getBody()->read($length);
120: if ($this->emitIo) {
121: $this->request->dispatch('curl.callback.read', array(
122: 'request' => $this->request,
123: 'read' => $read
124: ));
125: }
126: }
127:
128: return !$read ? '' : $read;
129: }
130: }
131: