Skip to content

Commit 9da9223

Browse files
authored
Merge pull request #142 from ostrolucky/adjustable-binary-regex
Add support for adjusting binary detection regex in FullHttpMessageFormatter
2 parents 295c828 + cf70bae commit 9da9223

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ All notable changes to this project will be documented in this file.
66
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
77
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
88

9+
## [1.12.0] - ?
10+
11+
- Added support for adjusting binary detection regex in FullHttpMessageFormatter
12+
913
## [1.11.2] - 2021-08-03
1014

1115
- Support GuzzleHttp/Psr7 version 2.0 in the (deprecated) GuzzleStreamFactory.

spec/Formatter/FullHttpMessageFormatterSpec.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,27 @@ function it_omits_body_with_null_bytes(RequestInterface $request, StreamInterfac
249249
$this->formatRequest($request)->shouldReturn($expectedMessage);
250250
}
251251

252+
function it_allows_to_change_binary_detection(RequestInterface $request, StreamInterface $stream)
253+
{
254+
$this->beConstructedWith(1, '/\x01/');
255+
256+
$stream->isSeekable()->willReturn(true);
257+
$stream->rewind()->shouldBeCalled();
258+
$stream->__toString()->willReturn("\0");
259+
$request->getBody()->willReturn($stream);
260+
$request->getMethod()->willReturn('GET');
261+
$request->getRequestTarget()->willReturn('/foo');
262+
$request->getProtocolVersion()->willReturn('1.1');
263+
$request->getHeaders()->willReturn([]);
264+
265+
$expectedMessage = <<<STR
266+
GET /foo HTTP/1.1
267+
268+
\x0
269+
STR;
270+
$this->formatRequest($request)->shouldReturn($expectedMessage);
271+
}
272+
252273
function it_omits_body_with_line_break(RequestInterface $request, StreamInterface $stream)
253274
{
254275
$this->beConstructedWith(7);

src/Formatter/FullHttpMessageFormatter.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,19 @@ class FullHttpMessageFormatter implements Formatter
2121
*/
2222
private $maxBodyLength;
2323

24+
/**
25+
* @var string
26+
*/
27+
private $binaryDetectionRegex;
28+
2429
/**
2530
* @param int|null $maxBodyLength
31+
* @param string $binaryDetectionRegex By default, this is all non-printable ASCII characters and <DEL> except for \t, \r, \n
2632
*/
27-
public function __construct($maxBodyLength = 1000)
33+
public function __construct($maxBodyLength = 1000, string $binaryDetectionRegex = '/([\x00-\x09\x0C\x0E-\x1F\x7F])/')
2834
{
2935
$this->maxBodyLength = $maxBodyLength;
36+
$this->binaryDetectionRegex = $binaryDetectionRegex;
3037
}
3138

3239
/**
@@ -86,8 +93,7 @@ private function addBody(MessageInterface $request, $message)
8693
$data = $stream->__toString();
8794
$stream->rewind();
8895

89-
// all non-printable ASCII characters and <DEL> except for \t, \r, \n
90-
if (preg_match('/([\x00-\x09\x0C\x0E-\x1F\x7F])/', $data)) {
96+
if (preg_match($this->binaryDetectionRegex, $data)) {
9197
return $message.'[binary stream omitted]';
9298
}
9399

0 commit comments

Comments
 (0)