HttpHeaderParser::parseFirst()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the slince/spike package.
5
 *
6
 * (c) Slince <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Spike\Common\Protocol;
13
14
class HttpHeaderParser
15
{
16
    /**
17
     * The incoming buffer.
18
     *
19
     * @var string
20
     */
21
    protected $buffer;
22
23
    /**
24
     * Push incoming data to the parser.
25
     *
26
     * @param string $data
27
     *
28
     * @return array
29
     */
30
    public function push($data)
31
    {
32
        $this->buffer .= $data;
33
34
        return $this->parse();
35
    }
36
37
    /**
38
     * Parse the incoming buffer.
39
     *
40
     * @return array
41
     */
42
    public function parse()
43
    {
44
        $messages = [];
45
        while ($this->buffer) {
46
            $message = $this->parseFirst();
47
            if (is_null($message)) {
48
                break;
49
            }
50
            $messages[] = $message;
51
        }
52
53
        return $messages;
54
    }
55
56
    /**
57
     * Parse one message from the data.
58
     *
59
     * @return string
60
     */
61
    public function parseFirst()
62
    {
63
        $pos = strpos($this->buffer, "\r\n\r\n");
64
        if (false === $pos) {
65
            return null;
66
        }
67
        $message = substr($this->buffer, 0, $pos + 4);
68
        $this->buffer = substr($this->buffer, strlen($message));
69
70
        return $message;
71
    }
72
73
    /**
74
     * Get the reset of data.
75
     *
76
     * @return string
77
     */
78
    public function getRemainingChunk()
79
    {
80
        return $this->buffer;
81
    }
82
}