Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
TransferEncoding
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
1 / 1
 isChunked
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * This file is part of php-fast-forward/http-message.
5 *
6 * This source file is subject to the license bundled
7 * with this source code in the file LICENSE.
8 *
9 * @see      https://github.com/php-fast-forward/http-message
10 *
11 * @copyright Copyright (c) 2025 Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
12 * @license   https://opensource.org/licenses/MIT MIT License
13 */
14
15declare(strict_types=1);
16
17/**
18 * This file is part of php-fast-forward/http-message.
19 *
20 * This source file is subject to the license bundled
21 * with this source code in the file LICENSE.
22 *
23 * @copyright Copyright (c) 2025-2026 Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
24 * @license   https://opensource.org/licenses/MIT MIT License
25 *
26 * @see       https://github.com/php-fast-forward/http-message
27 * @see       https://github.com/php-fast-forward
28 * @see       https://datatracker.ietf.org/doc/html/rfc2119
29 */
30
31namespace FastForward\Http\Message\Header;
32
33/**
34 * Enum TransferEncoding.
35 *
36 * Represents the HTTP `Transfer-Encoding` header values.
37 *
38 * Transfer-Encoding is a hop-by-hop header, meaning it applies only to a
39 * single transport-level connection and MUST NOT be stored or reused by
40 * caches for subsequent requests or responses. The listed values indicate
41 * which transfer codings have been applied to the message body in order to
42 * safely move it between intermediaries.
43 * Implementations using this enum SHOULD ensure that the semantics
44 * of chunked transfer-coding and any compression codings are respected
45 * according to the relevant HTTP specifications.
46 *
47 * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.1
48 * @see https://datatracker.ietf.org/doc/html/rfc7230#section-4.1
49 */
50enum TransferEncoding: string
51{
52    /**
53     * Indicates that the message body is sent as a series of chunks.
54     *
55     * Chunked transfer-coding is the only transfer-coding that is mandatory
56     * for HTTP/1.1 compliance and MUST be understood by all HTTP/1.1 agents
57     * that advertise support for `Transfer-Encoding`.
58     * When present, `chunked` MUST be the final transfer-coding applied to
59     * the payload. It allows the sender to stream content without knowing
60     * the final length in advance.
61     */
62    case Chunked = 'chunked';
63
64    /**
65     * A transfer-coding using the Lempel-Ziv-Welch (LZW) algorithm.
66     *
67     * This coding derives from the historic UNIX `compress` format. It is
68     * largely obsolete in modern HTTP and SHOULD NOT be used for new
69     * deployments. Many clients no longer support it due to past patent
70     * concerns and limited practical usage.
71     */
72    case Compress = 'compress';
73
74    /**
75     * A transfer-coding using the zlib structure with the DEFLATE
76     * compression algorithm.
77     *
78     * This coding uses the zlib framing (RFC 1950) combined with the DEFLATE
79     * algorithm (RFC 1951). Implementations MUST take care not to confuse
80     * this with a “raw deflate” stream without zlib framing.
81     */
82    case Deflate = 'deflate';
83
84    /**
85     * A transfer-coding using the Lempel-Ziv coding (LZ77) with a 32-bit CRC.
86     *
87     * This corresponds to the traditional `gzip` format produced by the
88     * UNIX `gzip` program. Some legacy systems MAY also accept `x-gzip`,
89     * but as a transfer-coding value, `gzip` itself SHOULD be preferred.
90     */
91    case Gzip = 'gzip';
92
93    /**
94     * Determines whether the given `Transfer-Encoding` header indicates that
95     * chunked transfer-coding has been applied.
96     *
97     * The `chunked` token MAY appear as one of several comma-separated values.
98     * This method performs a case-insensitive search for its presence. While
99     * RFC 7230 specifies that `chunked` MUST be the final transfer-coding
100     * when present, this method deliberately checks only for its existence to
101     * provide a more robust, tolerant interpretation of real-world headers.
102     * Callers SHOULD use this method before attempting to parse a message
103     * body as chunked data.
104     *
105     * @param string $transferEncodingHeader the raw `Transfer-Encoding` header value
106     *
107     * @return bool true if `chunked` is present (case-insensitive), false otherwise
108     */
109    public static function isChunked(string $transferEncodingHeader): bool
110    {
111        if ('' === $transferEncodingHeader) {
112            return false;
113        }
114
115        $codings = array_map(\mb_trim(...), explode(',', mb_strtolower($transferEncodingHeader)));
116
117        return \in_array(self::Chunked->value, $codings, true);
118    }
119}