Skip to content

Commit

Permalink
manges http responses paclet-splited in headers
Browse files Browse the repository at this point in the history
  • Loading branch information
etienneroudeix committed Jul 21, 2017
1 parent c2be598 commit 5974206
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 29 deletions.
4 changes: 4 additions & 0 deletions specs/Fixtures/fixtures.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ response1 = "http/requests/response1.txt"

request2 = "http/requests/request2.txt"
response2 = "http/requests/response2.txt"

request3-packet1 = "http/requests/request3_packet1.txt"
request3-packet2 = "http/requests/request3_packet2.txt"
response3 = "http/requests/response3.txt"
1 change: 1 addition & 0 deletions specs/Fixtures/http/requests/request3_packet1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
H
10 changes: 10 additions & 0 deletions specs/Fixtures/http/requests/request3_packet2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
TTP/1.1 404 Not Found
Access-Control-Allow-Headers: accept, content-type, authorization
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
Access-Control-Allow-Origin: *
Content-Type: text/plain; charset=utf-8
X-Content-Type-Options: nosniff
Date: Fri, 21 Jul 2017 11:57:37 GMT
Content-Length: 19

404 page not found
1 change: 1 addition & 0 deletions specs/Fixtures/http/requests/response3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
404 page not found
42 changes: 41 additions & 1 deletion specs/Http/http-request.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,43 @@
*/
describe("RX Http request parser", function () {

beforeEach(function () {
$this->specDone = new \Rx\Subject\Subject();
});
afterEach(function () {
\Rxnet\await($this->specDone->timeout(1000));
});

context("Multiple packets", function () {
it("Parse response in multiple packets, splited in the headers", function () {
$p1 = $this->loadFixture('rawtext:http:request3-packet1');
$p2 = $this->loadFixture('rawtext:http:request3-packet2');
$r = $this->loadFixture('rawtext:http:response3');


$packets = [
new \Rxnet\Event\Event('/packet', $p1),
new \Rxnet\Event\Event('/packet', $p2),
];

$observable = \Rx\Observable::fromArray($packets);

$request = new \Rxnet\Http\HttpRequest(new \GuzzleHttp\Psr7\Request("GET", "/"));

$request->subscribeCallback(function (\GuzzleHttp\Psr7\Response $response) use ($r) {
$responseBody = $response->getBody()->getContents();

expect($responseBody)->to->equal($r);

$this->specDone->onCompleted();
});

$observable->subscribe($request);


});
});

context("Chunked", function () {
it("Parse Multiple chunks in multiple packets", function () {
$p1 = $this->loadFixture('rawtext:http:request1-packet1');
Expand All @@ -27,10 +64,11 @@
$responseBody = $response->getBody()->getContents();

expect($responseBody)->to->equal($r);

$this->specDone->onCompleted();
});

$observable->subscribe($request);

});
it("Parse Multiple chunks in one packet", function () {
$p = $this->loadFixture('rawtext:http:request2');
Expand All @@ -49,6 +87,8 @@
$responseBody = $response->getBody()->getContents();

expect($responseBody)->to->equal($r);

$this->specDone->onCompleted();
});

$observable->subscribe($request);
Expand Down
57 changes: 29 additions & 28 deletions src/Rxnet/Http/HttpRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@ class HttpRequest extends Subject
const HTTP_TIMEOUT_EXCEPTION_MESSAGE = 'Http Timeout';

use NotifyObserverTrait;
/**
* @var string
*/
/** @var string */
public $data;
/**
* @var string
*/
/** @var string */
public $buffer = '';
/**
* @var array
*/
/** @var string */
public $headBuffer = '';
/** @var array */
public $labels = [];

protected $parserCallable;
Expand Down Expand Up @@ -143,31 +139,35 @@ public function onNext($event)
*/
public function parseHead($data)
{
if (false !== strpos($data, "\r\n\r\n")) {
list($headers, $bodyBuffer) = explode("\r\n\r\n", $data, 2);
$this->headBuffer .= $data;

if (false === strpos($this->headBuffer, "\r\n\r\n")) {
return;
}

// extract headers
$response = \GuzzleHttp\Psr7\parse_response($headers);
$this->response = $response;
list($headers, $bodyBuffer) = explode("\r\n\r\n", $this->headBuffer, 2);

$encoding = Arrays::first($response->getHeader('Transfer-Encoding'));
// extract headers
$response = \GuzzleHttp\Psr7\parse_response($headers);
$this->response = $response;

$encoding = Arrays::first($response->getHeader('Transfer-Encoding'));

switch ($encoding) {
case 'chunked':
$this->parserCallable = [$this, 'parseChunk'];
break;
// TODO multipart
default:
$this->parserCallable = [$this, 'parseContentLength'];
if ($length = $response->getHeader("Content-Length")) {
$this->contentLength = (int)Arrays::first($length);
}
}

// Parse rest of body
call_user_func($this->parserCallable, $bodyBuffer);
switch ($encoding) {
case 'chunked':
$this->parserCallable = [$this, 'parseChunk'];
break;
// TODO multipart
default:
$this->parserCallable = [$this, 'parseContentLength'];
if ($length = $response->getHeader("Content-Length")) {
$this->contentLength = (int)Arrays::first($length);
}
}

// Parse rest of body
call_user_func($this->parserCallable, $bodyBuffer);
}

/**
Expand Down Expand Up @@ -274,6 +274,7 @@ public function completed()
}
$this->onCompleted();
$this->buffer = "";
$this->headBuffer = "";
}

public function __toString()
Expand Down

0 comments on commit 5974206

Please sign in to comment.