67 public function doRequest($uri, $method =
'GET', $headers = array(), $body =
'') {
70 $res = @fopen($uri,
'rb', FALSE,
$cxt);
75 $err = error_get_last();
77 if (empty($err[
'message'])) {
81 $msg =
"File not found, perhaps due to a network failure.";
82 throw new \HPCloud\Transport\FileNotFoundException($msg);
84 $this->
guessError($err[
'message'], $uri, $method);
90 $metadata = stream_get_meta_data($res);
92 $msg = implode(
PHP_EOL, $metadata[
'wrapper_data']);
93 $msg .= sprintf(
"\nWaiting to read %d bytes.\n", $metadata[
'unread_bytes']);
95 if (defined(
'STDOUT')) {
103 $response =
new Response($res, $metadata);
126 if (is_string($resource)) {
127 $in = fopen($resource,
'rb', FALSE);
134 $body .= fread($in, 8192);
138 $res = @fopen($uri,
'rb', FALSE,
$cxt);
142 if ($res === FALSE) {
143 $err = error_get_last();
145 if (empty($err[
'message'])) {
146 throw new \HPCloud\Exception(
"An unknown exception occurred while sending a request.");
148 $this->
guessError($err[
'message'], $uri, $method);
154 $metadata = stream_get_meta_data($res);
156 $response =
new Response($res, $metadata);
175 $regex =
'/HTTP\/1\.[01]? ([0-9]+) ([ a-zA-Z]+)/';
177 preg_match($regex, $err, $matches);
179 if (count($matches) < 3) {
180 throw new \HPCloud\Exception($err);
183 Response::failure($matches[1], $matches[0], $uri, $method);
198 $this->notificationCallback = $callable;
215 if (empty($headers)) {
220 foreach ($headers as
$name => $value) {
222 $buffer[] = sprintf(
"%s: %s",
$name, $value);
224 $headerStr = implode(
"\r\n", $buffer);
226 return $headerStr .
"\r\n";
239 'protocol_version' => $this->httpVersion,
240 'method' => strtoupper($method),
242 'user_agent' => Transporter::HTTP_USER_AGENT . self::HTTP_USER_AGENT_SUFFIX,
247 $config[
'http'][
'content'] = $body;
251 $config[
'http'][
'timeout'] = (float) \HPCloud\
Bootstrap::config(
'transport.timeout');
256 if (!empty($this->notificationCallback)) {
257 $params[
'notification'] = $this->notificationCallback;
262 $params[
'notification'] = array($this,
'printNotifications');
266 $context = stream_context_create($config, $params);
271 static $filesize =
'Unknown';
274 case STREAM_NOTIFY_RESOLVE:
275 $out = sprintf(
"Resolved. %s\n", $msg);
277 case STREAM_NOTIFY_FAILURE:
278 $out = sprintf(
"socket-level failure: %s\n", $msg);
280 case STREAM_NOTIFY_COMPLETED:
281 $out = sprintf(
"Transaction complete. %s\n", $msg);
286 case STREAM_NOTIFY_CONNECT:
287 $out = sprintf(
"Connect... %s\n", $msg);
289 case STREAM_NOTIFY_FILE_SIZE_IS:
290 $out = sprintf(
"Content-length: %d\n", $len);
293 case STREAM_NOTIFY_MIME_TYPE_IS:
294 $out = sprintf(
"Content-Type: %s\n", $msg);
296 case STREAM_NOTIFY_PROGRESS:
297 $out = sprintf($msg .
PHP_EOL);
298 $out .= sprintf(
"%d bytes of %s\n", $bytes, $filesize);
301 $out = sprintf(
"Code: %d, Message: %s\n", $code, $msg);
306 if (defined(
'STDOUT')) {
307 fwrite(STDOUT, $out);