Skip to content

Commit af1a3bd

Browse files
author
Martin Brecht-Precht
committed
Added Response and Statistics.
1 parent 4821557 commit af1a3bd

File tree

10 files changed

+647
-42
lines changed

10 files changed

+647
-42
lines changed

src/Request/Base/RequestInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use BasicHttpClient\Request\Authentication\Base\AuthenticationInterface;
66
use BasicHttpClient\Request\Message\Base\MessageInterface;
77
use BasicHttpClient\Request\Transport\Base\TransportInterface;
8+
use BasicHttpClient\Response\Response;
89

910
/**
1011
* Class Request
@@ -108,4 +109,14 @@ public function hasAuthentications();
108109
*/
109110
public function countAuthentications();
110111

112+
/**
113+
* @return $this
114+
*/
115+
public function perform();
116+
117+
/**
118+
* @return Response
119+
*/
120+
public function getResponse();
121+
111122
}

src/Request/Message/Header/Header.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class Header implements HeaderInterface
3131
*/
3232
public function __construct($name, array $values)
3333
{
34-
$this->name = $name;
35-
$this->values = $values;
34+
$this->setName($name);
35+
$this->setValues($values);
3636
}
3737

3838
/**
@@ -58,12 +58,12 @@ public function getNormalizedName()
5858
*/
5959
public function setName($name)
6060
{
61-
$this->name = $name;
61+
$this->name = trim($name);
6262
return $this;
6363
}
6464

6565
/**
66-
* @return \string[]
66+
* @return string[]
6767
*/
6868
public function getValues()
6969
{
@@ -79,12 +79,14 @@ public function getValuesAsString()
7979
}
8080

8181
/**
82-
* @param \string[] $values
82+
* @param string[] $values
8383
* @return $this
8484
*/
8585
public function setValues($values)
8686
{
87-
$this->values = $values;
87+
foreach ($values as $value) {
88+
$this->values[] = trim($value);
89+
}
8890
return $this;
8991
}
9092

src/Request/Message/Message.php

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,32 @@ public function removeBody()
318318
return $this;
319319
}
320320

321+
/**
322+
* @param resource $curl
323+
* @return $this
324+
*/
325+
public function configureCurl($curl)
326+
{
327+
// Add request headers
328+
if ($this->hasHeaders()) {
329+
$requestHeaders = array();
330+
foreach ($this->getHeaders() as $header) {
331+
$requestHeaders[] = $header->getNormalizedName() . ': ' . $header->getValuesAsString();
332+
}
333+
// Set http header to curl
334+
curl_setopt($curl, CURLOPT_HTTPHEADER, $requestHeaders);
335+
}
336+
// Setup request cookies
337+
if ($this->hasCookies()) {
338+
$requestCookies = array();
339+
foreach ($this->getCookies() as $cookie) {
340+
$requestCookies[] = $cookie->getName() . '=' . $cookie->getValue();
341+
}
342+
curl_setopt($curl, CURLOPT_COOKIE, implode(';', $requestCookies));
343+
}
344+
return $this;
345+
}
346+
321347
/**
322348
* @param string $name
323349
* @return HeaderInterface[]
@@ -366,30 +392,4 @@ private function findHeadersExcludedByName($name)
366392
return $matchingHeaders;
367393
}
368394

369-
/**
370-
* @param resource $curl
371-
* @return $this
372-
*/
373-
public function configureCurl($curl)
374-
{
375-
// Add request headers
376-
if ($this->hasHeaders()) {
377-
$requestHeaders = array();
378-
foreach ($this->getHeaders() as $header) {
379-
$requestHeaders[] = $header->getNormalizedName() . ': ' . $header->getValuesAsString();
380-
}
381-
// Set http header to curl
382-
curl_setopt($curl, CURLOPT_HTTPHEADER, $requestHeaders);
383-
}
384-
// Setup request cookies
385-
if ($this->hasCookies()) {
386-
$requestCookies = array();
387-
foreach ($this->getCookies() as $cookie) {
388-
$requestCookies[] = $cookie->getName() . '=' . $cookie->getValue();
389-
}
390-
curl_setopt($curl, CURLOPT_COOKIE, implode(';', $requestCookies));
391-
}
392-
return $this;
393-
}
394-
395395
}

src/Request/Request.php

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
use BasicHttpClient\Request\Base\RequestInterface;
77
use BasicHttpClient\Request\Message\Base\MessageInterface;
88
use BasicHttpClient\Request\Transport\Base\TransportInterface;
9+
use BasicHttpClient\Request\Transport\HttpsTransport;
910
use BasicHttpClient\Request\Transport\HttpTransport;
11+
use BasicHttpClient\Response\Response;
1012
use BasicHttpClient\Util\UrlUtil;
13+
use CommonException\NetworkException\Base\NetworkException;
14+
use CommonException\NetworkException\ConnectionTimeoutException;
1115

1216
/**
1317
* Class Request
@@ -59,6 +63,11 @@ class Request implements RequestInterface
5963
*/
6064
private $message;
6165

66+
/**
67+
* @var Response
68+
*/
69+
private $response;
70+
6271
/**
6372
* Request constructor.
6473
*/
@@ -115,6 +124,14 @@ public function getPort()
115124
return $this->port;
116125
}
117126

127+
/**
128+
* @return bool
129+
*/
130+
public function hasPort()
131+
{
132+
return !is_null($this->port);
133+
}
134+
118135
/**
119136
* @param int $port
120137
* @return $this
@@ -265,47 +282,89 @@ public function configureCurl($curl)
265282
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
266283
curl_setopt($curl, CURLOPT_USERAGENT, $this->getUserAgent());
267284
curl_setopt($curl, CURLOPT_URL, $this->getEndpoint());
268-
// Setup request method
285+
if ($this->hasPort()) {
286+
curl_setopt($curl, CURLOPT_PORT, $this->getPort());
287+
}
288+
// Request method
289+
curl_setopt($curl, CURLOPT_HTTPGET, true);
290+
if ($this->getMethod() != self::REQUEST_METHOD_GET) {
291+
curl_setopt($curl, CURLOPT_HTTPGET, false);
292+
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->getMethod());
293+
}
294+
// Request body
269295
switch ($this->getMethod()) {
270296
case self::REQUEST_METHOD_GET:
271297
case self::REQUEST_METHOD_HEAD:
272-
curl_setopt($curl, CURLOPT_HTTPGET, true);
273298
// Modify the URL using the body as query string
274299
break;
275300
case self::REQUEST_METHOD_POST:
276-
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
277301
// curl_setopt($curl, CURLOPT_POSTFIELDS, $requestBody);
278302
break;
279303
case self::REQUEST_METHOD_PUT:
280-
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
281304
// curl_setopt($curl, CURLOPT_POSTFIELDS, $requestBody);
282305
break;
283306
case self::REQUEST_METHOD_PATCH:
284-
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
285307
// curl_setopt($curl, CURLOPT_POSTFIELDS, $requestBody);
286308
break;
287-
case self::REQUEST_METHOD_DELETE:
288-
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
289-
break;
290309
}
291310
return $this;
292311
}
293312

294313
/**
295314
* @return $this
315+
* @throws ConnectionTimeoutException
316+
* @throws NetworkException
317+
* @throws \Exception
296318
*/
297319
public function perform()
298320
{
321+
// Reset former result
322+
$this->response = null;
323+
// Perform hook
324+
$this->prePerform();
299325
// Curl basic setup
300326
$curl = curl_init();
301327
$this->configureCurl($curl);
302328
$this->getTransport()->configureCurl($curl);
303329
$this->getMessage()->configureCurl($curl);
304330
// Execute request
305331
$responseBody = curl_exec($curl);
306-
// TODO: Parse the response body
332+
$curlErrorCode = curl_errno($curl);
333+
$curlErrorMessage = curl_error($curl);
334+
if ($curlErrorCode === CURLE_OK) {
335+
$this->response = new Response();
336+
$this->response->populateFromCurlResult($curl, $responseBody);
337+
return $this;
338+
}
307339
curl_close($curl);
308-
return $this;
340+
switch ($curlErrorCode) {
341+
case CURLE_OPERATION_TIMEOUTED:
342+
throw new ConnectionTimeoutException('The request timed out with message: ' . $curlErrorMessage);
343+
break;
344+
default:
345+
throw new NetworkException('The request failed with message: ' . $curlErrorMessage);
346+
break;
347+
}
348+
}
349+
350+
/**
351+
* @return Response
352+
*/
353+
public function getResponse()
354+
{
355+
return $this->response;
356+
}
357+
358+
/**
359+
* @throws \Exception
360+
* @return void
361+
*/
362+
protected function prePerform()
363+
{
364+
$urlUtil = new UrlUtil();
365+
if ($urlUtil->getScheme($this->getEndpoint()) == 'HTTPS' && !$this->getTransport() instanceof HttpsTransport) {
366+
throw new \Exception('Transport misconfiguration. Use HttpsTransport for HTTPS requests.');
367+
}
309368
}
310369

311370
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace BasicHttpClient\Request\Transport;
4+
5+
/**
6+
* Class HttpsTransport
7+
*
8+
* @package BasicHttpClient\Request\Transport
9+
*/
10+
class HttpsTransport extends HttpTransport
11+
{
12+
13+
/**
14+
* Whether to verify the peer SSL certificate
15+
*
16+
* @var bool
17+
*/
18+
protected $verifyPeer = true;
19+
20+
/**
21+
* @return boolean
22+
*/
23+
public function getVerifyPeer()
24+
{
25+
return $this->verifyPeer;
26+
}
27+
28+
/**
29+
* @param boolean $verifyPeer
30+
* @return $this
31+
*/
32+
public function setVerifyPeer($verifyPeer)
33+
{
34+
$this->verifyPeer = $verifyPeer;
35+
return $this;
36+
}
37+
38+
/**
39+
* @param resource $curl
40+
* @return $this
41+
*/
42+
public function configureCurl($curl)
43+
{
44+
parent::configureCurl($curl);
45+
// Verify peer
46+
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->verifyPeer);
47+
return $this;
48+
}
49+
50+
}

0 commit comments

Comments
 (0)