forked from docker-php/docker-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.php
More file actions
155 lines (128 loc) · 3.21 KB
/
Copy pathClient.php
File metadata and controls
155 lines (128 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace Docker\Http;
use Docker\Http\Exception as HttpException;
/**
* Docker\Http\Client
*/
class Client
{
const CHUNK_SIZE = 8192;
/**
* @var resource
*/
private $socket;
/**
* @var Docker\Http\ResponseParser
*/
private $parser;
/**
* @var string
*/
private $userAgent = 'Docker-PHP';
/**
* @var integer
*/
private $timeout;
/**
* @param string $spec
* @param integer $timeout
*/
public function __construct($spec)
{
$this->spec = $spec;
$this->parser = new ResponseParser();
}
/**
* @param integer $timeout
*
* @return Docker\Http\Request
*/
public function setTimeout($timeout = null)
{
$this->timeout = $timeout;
return $this;
}
/**
* @return integer
*/
public function getTimeout()
{
return $this->timeout;
}
/**
* @return array
*/
public function getDefaultHeaders()
{
return [
'Host' => $this->spec,
'User-Agent' => $this->userAgent,
];
}
/**
* @param Docker\Http\Request $request
* @param boolean $blocking Do we have to wait for content (always blocking for headers) default is true
*
* @return Docker\Http\Response
*/
public function send(Request $request, $blocking = true)
{
$socket = stream_socket_client($this->spec);
fwrite($socket, $request->getHeadersAsString());
$content = $request->getContent();
if (is_resource($content)) {
while (false !== ($read = fread($content, self::CHUNK_SIZE))) {
$frame = dechex(mb_strlen($read))."\r\n".$read."\r\n";
fwrite($socket, $frame);
if (empty($read)) {
break;
}
}
fclose($content);
fwrite($socket, "0\r\n\r\n");
} else {
fwrite($socket, $content);
}
$timeout = $request->getTimeout() ?: $this->timeout;
if (null !== $timeout) {
stream_set_timeout($socket, $request->getTimeout() ?: $this->timeout);
}
try {
$response = $this->parser->parse($socket, $blocking);
} catch (HttpException $e) {
$e->setRequest($request);
throw $e;
}
return $response;
}
/**
* @param string $uri
*
* @return Docker\Http\Request
*/
public function get($uri, $headers = array())
{
$headers = array_replace($this->getDefaultHeaders(), $headers);
return new Request('GET', $uri, $headers);
}
/**
* @param string $uri
*
* @return Docker\Http\Request
*/
public function post($uri, $headers = array())
{
$headers = array_replace($this->getDefaultHeaders(), $headers);
return new Request('POST', $uri, $headers);
}
/**
* @param string $uri
*
* @return Docker\Http\Request
*/
public function delete($uri, $headers = array())
{
$headers = array_replace($this->getDefaultHeaders(), $headers);
return new Request('DELETE', $uri, $headers);
}
}