Browse Source

Client: Rewritten header parsing.

Adam Klvač 2 years ago
parent
commit
039e71bdf7
2 changed files with 22 additions and 6 deletions
  1. 21 5
      src/Client/Request.php
  2. 1 1
      tests/FastCGI/Client/Request.phpt

+ 21 - 5
src/Client/Request.php

@@ -6,6 +6,7 @@ use AmK\FastCGI\Response;
 use Evenement\EventEmitter;
 use Lisachenko\Protocol\FCGI;
 use React;
+use RingCentral\Psr7\Response as HttpResponse;
 use function RingCentral\Psr7\parse_response;
 
 /**
@@ -66,13 +67,28 @@ class Request extends EventEmitter
 
 			if (($pos = strpos($this->buffer, "\r\n\r\n")) !== false) {
 
-				$header = substr($this->buffer, 0, $pos);
-				if (strcasecmp(substr($header, 0, 8), 'Status: ') === 0) {
-					$header = 'HTTP/1.1 ' . substr($header, 8);
-				}
+				$lines = explode("\r\n", substr($this->buffer, 0, $pos));
+				$headers = [];
+				$status = 200;
+
+				foreach ($lines as $line) {
+
+					$p = strpos($line, ':');
+
+					if ($p === false) {
+						continue;
+					}
 
-				$response = parse_response($header);
+					$header = substr($line, 0, $p);
+					$value = substr($line, $p + 2);
+
+					if (strcasecmp($header, 'status') !== 0) {
+						$headers[$header] = $value;
+					}
+
+				}
 
+				$response = new HttpResponse($status, $headers);
 				$this->stream = new React\Stream\ThroughStream;
 				$this->response = new Response(
 					$response->getStatusCode(),

+ 1 - 1
tests/FastCGI/Client/Request.phpt

@@ -71,7 +71,7 @@ $request->on('end', function() use(&$finished): void {
 	$finished = true;
 });
 
-$request->writeBuffer("Status: 200 OK\r\n\r\n");
+$request->writeBuffer("X-Powered-By: PHP/7.2.27\r\nContent-type: text/html; charset=UTF-8\r\n\r\n");
 
 /** @var $response React\Http\Response */
 $body = $response->getBody();