Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.31% covered (success)
92.31%
12 / 13
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ResponseParser
92.31% covered (success)
92.31%
12 / 13
50.00% covered (danger)
50.00%
1 / 2
6.02
0.00% covered (danger)
0.00%
0 / 1
 __construct
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
4.12
 parse
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * SPDX-License-Identifier: EUPL-1.2
5 *
6 * This file is part of icap-flow.
7 *
8 * Licensed under the EUPL, Version 1.2 only (the "Licence");
9 * you may not use this work except in compliance with the Licence.
10 * You may obtain a copy of the Licence at:
11 *
12 *     https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the Licence is distributed on an "AS IS" basis,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 */
18
19declare(strict_types=1);
20
21namespace Ndrstmr\Icap;
22
23use Ndrstmr\Icap\DTO\IcapResponse;
24use Ndrstmr\Icap\Exception\IcapMalformedResponseException;
25
26/**
27 * Parses raw ICAP server responses into {@link IcapResponse} value
28 * objects. A string is one complete response: {@see ResponseDecoder} reads it
29 * by its `Encapsulated` layout (RFC 3507 §4.4) with the same rules the
30 * transports frame it by, and decodes the chunked body. A response that does
31 * not follow them, is cut off, or has bytes behind its end is refused.
32 */
33final class ResponseParser implements ResponseParserInterface
34{
35    private const int DEFAULT_MAX_HEADER_COUNT = 100;
36    private const int DEFAULT_MAX_HEADER_LINE = 8192;
37
38    public function __construct(
39        private readonly int $maxHeaderCount = self::DEFAULT_MAX_HEADER_COUNT,
40        private readonly int $maxHeaderLineLength = self::DEFAULT_MAX_HEADER_LINE,
41    ) {
42        if ($maxHeaderCount < 1 || $maxHeaderLineLength < 1) {
43            throw new \InvalidArgumentException('Parser limits must be >= 1');
44        }
45    }
46
47    #[\Override]
48    public function parse(string $rawResponse): IcapResponse
49    {
50        // All the bytes are here, so the response cannot be larger than they are.
51        $decoder = new ResponseDecoder(
52            maxResponseSize: max(1, strlen($rawResponse)),
53            maxHeaderCount: $this->maxHeaderCount,
54            maxHeaderLineLength: $this->maxHeaderLineLength,
55        );
56        $decoder->feed($rawResponse);
57
58        if (!$decoder->isComplete()) {
59            throw new IcapMalformedResponseException('Invalid ICAP response: it ends before the response is complete.');
60        }
61        if ($decoder->remainder() !== '') {
62            throw new IcapMalformedResponseException('Invalid ICAP response: bytes follow the end of the response.');
63        }
64
65        return $decoder->response();
66    }
67}