-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhttplib.inc.php
More file actions
208 lines (202 loc) · 5.95 KB
/
httplib.inc.php
File metadata and controls
208 lines (202 loc) · 5.95 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/**
* A lightweight HTTP library which acts very similar to
* the Python httplib.
* <code>
* $conn = new HTTPConnection('google.com');
* $conn->request('GET', '/', array('q'=>'http'));
* $response = $conn->getresponse();
* $data = $response->read();
* </code>
* @author David Cramer <dcramer@gmail.com>
* @version 1.0
* @package httplib
*/
/**
* The base exception for HTTPConnection
* @package httplib
*/
class SocketError extends Exception { }
/**
* Thrown when there is an unknown server error
* @package httplib
*/
class UnknownServerError extends SocketError { }
/**
* Thrown when there is an error trying to connection
* @package httplib
*/
class ConnectionError extends SocketError { }
/**
* The base HTTP class for opening connections.
* @package httplib
* @subpackage connection
*/
class HTTPConnection
{
/**
* Constructor defining the connection.
* <code>
* <?php
* $conn = new HTTPConnection('google.com');
* ?>
* </code>
* @param string $host hostname (e.g. domain.com)
* @param int $port port
*/
function __construct($host, $port=80, $timeout=60)
{
$this->host = $host;
$this->port = $port;
$this->timeout = $timeout;
$this->response = null;
}
/**
* Opens the connection and sends the request headers.
* <code>
* <?php
* $conn->request('GET', '/', array('q'=>'http'));
* ?>
* </code>
* @param string $method request method (GET/POST/PUT)
* @param string $path request path
* @param array $params associative array of parameters to send
* @param array $headers associative array of headers to send
*/
function request($method, $path, $params, $headers=array())
{
$this->socket = @fsockopen($this->host, $this->port, $errorNumber, $errorString, (float)$this->timeout);
if (!$this->socket)
{
throw new ConnectionError('Failed connecting to '.$this->host.':'.$this->port.': '.socket_strerror($errorNumber).' ('.$errorNumber.'); '. $error);
}
stream_set_timeout($this->socket, (float)$this->timeout);
$this->params = $params;
$this->method = $method;
$this->headers = $header;
$this->path = $path;
}
/**
* Reads the response from the server.
* <code>
* <?php
* $response = $conn->getresponse();
* ?>
* </code>
* @return HTTPResponse response object
*/
function getresponse()
{
if ($this->response) return $this->response;
$query_string = http_build_query($this->params);
if ($this->method == "GET")
{
$path .= '?' . $query_string;
}
// set default headers
$headers = $this->headers;
if (empty($headers['User-Agent'])) $headers['User-Agent'] = 'php-httplib/1.0 (PHP ' . phpversion() . ')';
if (empty($headers['Content-Type'])) $headers['Content-Type'] = 'application/x-www-form-urlencoded';
if ($this->method == 'POST') $headers['Content-Length'] = strlen($query_string);
$headers['Host'] = $this->host;
// build the header string
$request_header = strtoupper($this->method) . " " . $this->path . " HTTP/1.1\r\n";
foreach ($headers as $key=>&$value)
{
$request_header .= $key . ": " . $value . "\r\n";
}
$request_header .= "Connection: close\r\n\r\n";
if ($this->method == "POST")
{
$request_header .= $query_string;
}
fwrite($this->socket, $request_header);
$response_header = '';
do
{
$response_header .= fread($this->socket, 1);
}
while (!preg_match('/\\r\\n\\r\\n$/', $response_header));
$this->response = new HTTPResponse($this->socket, $response_header);
return $this->response;
}
}
/**
* A secure connection using SSL under the HTTP protocol.
* @package httplib
* @subpackage sslconnection
*/
class HTTPSConnection extends HTTPConnection
{
}
/**
* A response object generated from an HTTPConnection.
* @package httplib
* @subpackage response
*/
class HTTPResponse
{
/**
* @param socket $socket connection socket
* @param int $response response headers
*/
function __construct(&$socket, &$response)
{
$headers = explode("\r\n", $response);
$this->headers = array();
foreach ($headers as &$line)
{
if (strpos($line, 'HTTP/') === 0)
{
$data = explode(' ', $line);
$this->status = $data[1];
$this->message = implode(' ', array_slice($data, 2));
}
elseif (strpos($line, ':'))
{
$data = explode(':', $line);
$value = trim(implode(':', array_slice($data, 1)));
$this->headers[$data[0]] = $value;
}
}
$this->socket = $socket;
}
/**
* Reads the response body.
* <code>
* <?php
* $data = $response->read();
* ?>
* </code>
* @return string response body
*/
function read()
{
$response_content = '';
if ($this->headers['Transfer-Encoding'] == 'chunked')
{
while ($chunk_length = hexdec(fgets($this->socket)))
{
$response_content_chunk = '';
$read_length = 0;
while ($read_length < $chunk_length)
{
$response_content_chunk .= fread($this->socket, $chunk_length - $read_length);
$read_length = strlen($response_content_chunk);
}
$response_content .= $response_content_chunk;
fgets($this->socket);
}
}
else
{
while (!feof($this->socket))
{
$response_content .= fgets($this->socket, 128);
}
}
return chop($response_content);
fclose($this->socket);
}
}
?>