Skip to content

Commit 66c4a5a

Browse files
Refactored processRequest method on ConnectionHandler
1 parent a29baff commit 66c4a5a

File tree

2 files changed

+24
-147
lines changed

2 files changed

+24
-147
lines changed

src/ConnectionHandler/ConnectionHandler.php

Lines changed: 18 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -169,25 +169,24 @@ private function processRecord(array $record)
169169

170170
$content = 0 === $record['contentLength'] ? null : $record['contentData'];
171171

172-
switch ($record['type']) {
173-
case DaemonInterface::FCGI_BEGIN_REQUEST:
174-
$this->processBeginRequestRecord($requestId, $content);
175-
break;
176-
177-
case DaemonInterface::FCGI_PARAMS:
178-
$this->processParamsRecord($requestId, $content);
179-
break;
180-
181-
case DaemonInterface::FCGI_STDIN:
182-
$this->processStdinRecord($requestId, $content);
183-
break;
184-
185-
case DaemonInterface::FCGI_ABORT_REQUEST:
186-
$this->processAbortRequestRecord($requestId);
187-
break;
188-
189-
default:
190-
throw new ProtocolException('Unexpected packet of unkown type: '.$record['type']);
172+
if (DaemonInterface::FCGI_BEGIN_REQUEST === $record['type']) {
173+
$this->processBeginRequestRecord($requestId, $content);
174+
} elseif (!isset($this->requests[$requestId])) {
175+
throw new ProtocolException('Invalid request id for record of type: ' . $record['type']);
176+
} elseif (DaemonInterface::FCGI_PARAMS === $record['type']) {
177+
while (strlen($content) > 0) {
178+
$this->readNameValuePair($requestId, $content);
179+
}
180+
} elseif (DaemonInterface::FCGI_STDIN === $record['type']) {
181+
if (null !== $content) {
182+
fwrite($this->requests[$requestId]['stdin'], $content);
183+
} else {
184+
$this->dispatchRequest($requestId);
185+
}
186+
} elseif (DaemonInterface::FCGI_ABORT_REQUEST === $record['type']) {
187+
$this->endRequest($requestId);
188+
} else {
189+
throw new ProtocolException('Unexpected packet of type: '.$record['type']);
191190
}
192191
}
193192

@@ -228,29 +227,6 @@ private function processBeginRequestRecord($requestId, $contentData)
228227
}
229228
}
230229

231-
/**
232-
* Process a FCGI_PARAMS record.
233-
*
234-
* @param int $requestId The request id sending the record
235-
* @param string $contentData The content of the record
236-
*
237-
* @throws ProtocolException If the FCGI_PARAMS record is unexpected
238-
*/
239-
private function processParamsRecord($requestId, $contentData)
240-
{
241-
if (!isset($this->requests[$requestId])) {
242-
throw new ProtocolException('Unexpected FCGI_PARAMS record');
243-
}
244-
245-
if (null === $contentData) {
246-
return;
247-
}
248-
249-
do {
250-
$this->readNameValuePair($requestId, $contentData);
251-
} while (strlen($contentData) > 0);
252-
}
253-
254230
/**
255231
* Read a FastCGI name-value pair from a buffer and add it to the request params
256232
*
@@ -303,45 +279,6 @@ private function readFieldLength(&$buffer)
303279
return $length;
304280
}
305281

306-
/**
307-
* Process a FCGI_STDIN record.
308-
*
309-
* @param int $requestId The request id sending the record
310-
* @param string $contentData The content of the record
311-
*
312-
* @throws ProtocolException If the FCGI_STDIN record is unexpected
313-
*/
314-
private function processStdinRecord($requestId, $contentData)
315-
{
316-
if (!isset($this->requests[$requestId])) {
317-
throw new ProtocolException('Unexpected FCGI_STDIN record');
318-
}
319-
320-
if (null === $contentData) {
321-
$this->dispatchRequest($requestId);
322-
323-
return;
324-
}
325-
326-
fwrite($this->requests[$requestId]['stdin'], $contentData);
327-
}
328-
329-
/**
330-
* Process a FCGI_ABORT_REQUEST record.
331-
*
332-
* @param int $requestId The request id sending the record
333-
*
334-
* @throws ProtocolException If the FCGI_ABORT_REQUEST record is unexpected
335-
*/
336-
private function processAbortRequestRecord($requestId)
337-
{
338-
if (!isset($this->requests[$requestId])) {
339-
throw new ProtocolException('Unexpected FCG_ABORT_REQUEST record');
340-
}
341-
342-
$this->endRequest($requestId);
343-
}
344-
345282
/**
346283
* End the request by writing an FCGI_END_REQUEST record and then removing
347284
* the request from memory and closing the connection if necessary.

test/ConnectionHandler/ConnectionHandlerTest.php

Lines changed: 6 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,10 @@ public function testAbortRequestRecord()
220220
}
221221

222222
/**
223-
* Test that the daemon correctly handles an unexpected abort request record.
223+
* Test that the daemon correctly handles an abort request record with a
224+
* request id that hasn't been initiated.
224225
*/
225-
public function testUnexpectedAbortRequestRecord()
226+
public function testInvalidRequestId()
226227
{
227228
$context = $this->createTestingContext();
228229

@@ -239,7 +240,7 @@ public function testUnexpectedAbortRequestRecord()
239240
$expectedLogMessages = [
240241
[
241242
'level' => 'error',
242-
'message' => 'Unexpected FCG_ABORT_REQUEST record',
243+
'message' => 'Invalid request id for record of type: '.DaemonInterface::FCGI_ABORT_REQUEST,
243244
'context' => [],
244245
],
245246
];
@@ -257,6 +258,7 @@ public function testUnexpectedRecord()
257258
$context = $this->createTestingContext();
258259

259260
// Write a record with a type that makes no sense (application doesn't receive stdout)
261+
$context['clientWrapper']->writeBeginRequestRecord(0, DaemonInterface::FCGI_RESPONDER, 0);
260262
$context['clientWrapper']->writeRecord(DaemonInterface::FCGI_STDOUT, 0);
261263

262264
// Run the handler
@@ -269,7 +271,7 @@ public function testUnexpectedRecord()
269271
$expectedLogMessages = [
270272
[
271273
'level' => 'error',
272-
'message' => 'Unexpected packet of unkown type: '.DaemonInterface::FCGI_STDOUT,
274+
'message' => 'Unexpected packet of type: '.DaemonInterface::FCGI_STDOUT,
273275
'context' => [],
274276
],
275277
];
@@ -312,68 +314,6 @@ public function testUnexpectedBeginRequestRecord()
312314
fclose($context['sockets'][0]);
313315
}
314316

315-
/**
316-
* Test that the daemon throws an error if a stdin record is received for
317-
* an unknown request id.
318-
*/
319-
public function testUnexpectedStdinRecord()
320-
{
321-
$context = $this->createTestingContext();
322-
323-
// Write an unexpected record and close the connection
324-
$context['clientWrapper']->writeStdinRecord(0);
325-
326-
// Run the handler
327-
$context['handler']->ready();
328-
329-
// Check daemon has closed server side connection
330-
$this->assertTrue($context['connection']->isClosed());
331-
332-
// Check the logging has worked
333-
$expectedLogMessages = [
334-
[
335-
'level' => 'error',
336-
'message' => 'Unexpected FCGI_STDIN record',
337-
'context' => [],
338-
],
339-
];
340-
$this->assertEquals($expectedLogMessages, $context['logger']->getMessages());
341-
342-
// Close the client side socket
343-
fclose($context['sockets'][0]);
344-
}
345-
346-
/**
347-
* Test that the daemon throws an error if a params record is received for
348-
* an unknown request id.
349-
*/
350-
public function testUnexpectedParamsRecord()
351-
{
352-
$context = $this->createTestingContext();
353-
354-
// Write an unexpected record and close the connection
355-
$context['clientWrapper']->writeParamsRecord(0);
356-
357-
// Run the handler
358-
$context['handler']->ready();
359-
360-
// Check daemon has closed server side connection
361-
$this->assertTrue($context['connection']->isClosed());
362-
363-
// Check the logging has worked
364-
$expectedLogMessages = [
365-
[
366-
'level' => 'error',
367-
'message' => 'Unexpected FCGI_PARAMS record',
368-
'context' => [],
369-
],
370-
];
371-
$this->assertEquals($expectedLogMessages, $context['logger']->getMessages());
372-
373-
// Close the client side socket
374-
fclose($context['sockets'][0]);
375-
}
376-
377317
/**
378318
* Test that the daemon responds correctly to a non-responder role.
379319
*/

0 commit comments

Comments
 (0)