Open Journal Systems  3.3.0
RequestMediator.php
1 <?php
2 
3 namespace Guzzle\Http\Curl;
4 
8 
13 {
15  protected $request;
16 
18  protected $emitIo;
19 
24  public function __construct(RequestInterface $request, $emitIo = false)
25  {
26  $this->request = $request;
27  $this->emitIo = $emitIo;
28  }
29 
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 
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 
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 
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 }
Guzzle\Http\Message\RequestInterface
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/RequestInterface.php:16
Guzzle\Http\Curl
Definition: CurlHandle.php:3
Guzzle\Http\Curl\RequestMediator\writeResponseBody
writeResponseBody($curl, $write)
Definition: RequestMediator.php:114
Guzzle\Http\Curl\RequestMediator\readRequestBody
readRequestBody($ch, $fd, $length)
Definition: RequestMediator.php:140
Guzzle\Http\Curl\RequestMediator\$request
$request
Definition: RequestMediator.php:18
Guzzle\Http\EntityBody\factory
static factory($resource='', $size=null)
Definition: EntityBody.php:36
Guzzle\Http\Message\Response
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Response.php:17
Guzzle\Http\EntityBody
Definition: EntityBody.php:13
Guzzle\Http\Curl\RequestMediator\__construct
__construct(RequestInterface $request, $emitIo=false)
Definition: RequestMediator.php:30
Guzzle\Http\Curl\RequestMediator
Definition: RequestMediator.php:12
Guzzle\Http\Curl\RequestMediator\receiveResponseHeader
receiveResponseHeader($curl, $header)
Definition: RequestMediator.php:44
Guzzle\Http\Curl\RequestMediator\progress
progress($downloadSize, $downloaded, $uploadSize, $uploaded, $handle=null)
Definition: RequestMediator.php:94
Guzzle\Http\Curl\RequestMediator\$emitIo
$emitIo
Definition: RequestMediator.php:24