Skip to content

Commit 9f5734b

Browse files
committed
* Fixed response headers parsing
* Implements HEAD and POST (first draft)
1 parent 80c17ee commit 9f5734b

File tree

5 files changed

+76
-10
lines changed

5 files changed

+76
-10
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"type": "library",
55
"keywords": ["http", "client", "psr7", "curl", "wrapper"],
66
"require": {
7+
"php": ">=5.4.0",
78
"psr/http-message": "^1.0"
89
},
910
"require-dev": {

src/Client.php

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class Client
3535
protected $handle = null;
3636
protected $responseHeader = '';
3737
protected $responseHeaders = [];
38+
protected $statusCode;
39+
protected $version;
3840

3941
public function __construct()
4042
{
@@ -78,6 +80,7 @@ public function __clone()
7880

7981
protected function parseHeaders($lines)
8082
{
83+
8184
if (empty($lines)) {
8285
return [];
8386
}
@@ -86,12 +89,13 @@ protected function parseHeaders($lines)
8689
} elseif (!is_array($lines)) {
8790
return false;
8891
}
92+
8993
$status = [];
9094
if (preg_match('%^HTTP/(\d(?:\.\d)?)\s+(\d{3})\s?+(.+)?$%i', $lines[0], $status)) {
91-
$this->status = array_shift($lines);
92-
$this->version = $lines[1];
93-
$this->statusCode = intval($lines[2]);
94-
$this->statusMessage = isset($lines[3]) ? $lines[3] : '';
95+
$this->status = $status[0];
96+
$this->version = $status[1];
97+
$this->statusCode = intval($status[2]);
98+
$this->statusMessage = isset($status[3]) ? $status[3] : '';
9599
}
96100

97101
foreach ($lines as $field) {
@@ -110,6 +114,21 @@ public function sendRequest(RequestInterface $request)
110114
curl_setopt($this->handle, CURLOPT_URL, (string) $request->getUri());
111115
curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, [$this, 'headerFunction']);
112116

117+
if ($request->getMethod() == 'HEAD') {
118+
curl_setopt($this->handle, CURLOPT_NOBODY, true);
119+
}
120+
121+
if ($request->getMethod() == 'POST') {
122+
curl_setopt($this->handle, CURLOPT_POST, true);
123+
if (!empty($request->getBody())) {
124+
curl_setopt($this->handle, CURLOPT_POSTFIELDS, $request->getBody());
125+
}
126+
}
127+
128+
if (in_array($request->getMethod(), ['PUT', 'PATCH', 'DELETE'])) {
129+
curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $request->getMethod());
130+
}
131+
113132
$ret = curl_exec($this->handle);
114133

115134
if ($errno = curl_errno($this->handle)) {
@@ -118,14 +137,26 @@ public function sendRequest(RequestInterface $request)
118137

119138
$this->parseHeaders($this->responseHeader);
120139

121-
$response = new Response($ret, $this->statusCode, $this->responseHeaders);
140+
$response = new Response($ret, $this->statusCode, $this->responseHeaders, $this->version);
122141
return $response;
123142
}
124143

125144
public function get($uri, $headers = [])
126145
{
127146
$request = new Request('GET', $uri, $headers);
128-
print_r($request);
147+
return $this->sendRequest($request);
148+
}
149+
150+
public function head($uri, $headers = [])
151+
{
152+
$request = new Request('HEAD', $uri, $headers);
153+
return $this->sendRequest($request);
154+
}
155+
156+
public function post($uri, $body = '', $headers = [])
157+
{
158+
$request = new Request('POST', $uri, $headers);
159+
$request->setBody($body);
129160
return $this->sendRequest($request);
130161
}
131162
}

src/Request.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,13 @@ public function getBody()
361361
{
362362
return $this->body;
363363
}
364+
365+
public function setBody($body)
366+
{
367+
$this->body = $body;
368+
return $this;
369+
}
370+
364371
/**
365372
* Return an instance with the specified message body.
366373
*

src/Response.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,13 @@
4545
class Response implements ResponseInterface
4646
{
4747
protected $statusCode;
48+
protected $protocolVersion;
49+
protected $headers;
50+
protected $body;
4851

49-
public function __construct($body, $statusCode = 200, $headers = [])
52+
public function __construct($body, $statusCode = 200, $headers = [], $protocolVersion = '1.1')
5053
{
54+
$this->protocolVersion = $protocolVersion;
5155
$this->body = $body;
5256
$this->statusCode = $statusCode;
5357
$this->headers = $headers;

tests/ClientTest.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,33 @@
3030
*/
3131
class ClientTest extends TestCase
3232
{
33-
public function testEmptyUri()
33+
public function testBasicGet()
3434
{
3535
$client = new Client();
36-
$response = $client->get('https://www.google.com/');
37-
$this->assertEquals(200, $response->getStatusCode());
36+
$response = $client->get('http://www.google.com/');
37+
$this->assertEquals(302, $response->getStatusCode());
38+
$this->assertContains('private', $response->getHeader('Cache-Control'));
39+
$this->assertEquals('1.1', $response->getProtocolVersion());
40+
$this->assertNotEmpty($response->getBody());
41+
}
42+
43+
public function testBasicHead()
44+
{
45+
$client = new Client();
46+
$response = $client->head('http://www.google.com/');
47+
$this->assertEquals(302, $response->getStatusCode());
48+
$this->assertContains('private', $response->getHeader('Cache-Control'));
49+
$this->assertEquals('1.1', $response->getProtocolVersion());
50+
$this->assertEmpty($response->getBody());
51+
}
52+
53+
public function testBasicPost()
54+
{
55+
$client = new Client();
56+
$response = $client->post('http://www.google.com/', 'test=test&a=b');
57+
$this->assertEquals(405, $response->getStatusCode());
58+
// $this->assertContains('private', $response->getHeader('Cache-Control'));
59+
$this->assertEquals('1.1', $response->getProtocolVersion());
60+
$this->assertNotEmpty($response->getBody());
3861
}
3962
}

0 commit comments

Comments
 (0)