• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sirn-se / websocket-php / 4771910143

pending completion
4771910143

push

github

Sören Jensen
Connect, handshake, HTTP, etc

212 of 212 new or added lines in 5 files covered. (100.0%)

591 of 670 relevant lines covered (88.21%)

14.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.0
/lib/Http/Response.php
1
<?php
2

3
/**
4
 * File for Phrity\WebSocket\Http\Response class
5
 * @package Phrity > WebSocket > Http
6
 */
7

8
namespace WebSocket\Http;
9

10
use Phrity\Net\Uri;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\UriInterface;
13
use RuntimeException;
14

15
/**
16
 * Phrity\WebSocket\Http\Response class.
17
 */
18
class Response extends Message implements ResponseInterface
19
{
20
    private static $codes = [
21
        100 => 'Continue',
22
        101 => 'Switching Protocols',
23
        102 => 'Processing',
24
        103 => 'Early Hints',
25
        200 => 'OK',
26
        201 => 'Created',
27
        202 => 'Accepted',
28
        203 => 'Non-Authoritative Information',
29
        204 => 'No Content',
30
        205 => 'Reset Content',
31
        206 => 'Partial Content',
32
        207 => 'Multi-Status',
33
        208 => 'Already Reported',
34
        226 => 'IM Used',
35
        300 => 'Multiple Choices',
36
        301 => 'Moved Permanently',
37
        302 => 'Found',
38
        303 => 'See Other',
39
        304 => 'Not Modified',
40
        305 => 'Use Proxy',
41
        307 => 'Temporary Redirect',
42
        308 => 'Permanent Redirect',
43
        400 => 'Bad Request',
44
        401 => 'Unauthorized',
45
        402 => 'Payment Required',
46
        403 => 'Forbidden',
47
        404 => 'Not Found',
48
        405 => 'Method Not Allowed',
49
        406 => 'Not Acceptable',
50
        407 => 'Proxy Authentication Required',
51
        408 => 'Request Timeout',
52
        409 => 'Conflict',
53
        410 => 'Gone',
54
        411 => 'Length Required',
55
        412 => 'Precondition Failed',
56
        413 => 'Content Too Large',
57
        414 => 'URI Too Long',
58
        415 => 'Unsupported Media Type',
59
        416 => 'Range Not Satisfiable',
60
        417 => 'Expectation Failed',
61
        421 => 'Misdirected Request',
62
        422 => 'Unprocessable Content',
63
        423 => 'Locked',
64
        424 => 'Failed Dependency',
65
        425 => 'Too Early',
66
        426 => 'Upgrade Required',
67
        428 => 'Precondition Required',
68
        429 => 'Too Many Requests',
69
        431 => 'Request Header Fields Too Large',
70
        451 => 'Unavailable For Legal Reasons',
71
        500 => 'Internal Server Error',
72
        501 => 'Not Implemented',
73
        502 => 'Bad Gateway',
74
        503 => 'Service Unavailable',
75
        504 => 'Gateway Timeout',
76
        505 => 'HTTP Version Not Supported',
77
        506 => 'Variant Also Negotiates',
78
        507 => 'Insufficient Storage',
79
        508 => 'Loop Detected',
80
        510 => 'Not Extended',
81
        511 => 'Network Authentication Required',
82
        100 => 'Continue',
83
        100 => 'Continue',
84
        100 => 'Continue',
85
        100 => 'Continue',
86
        100 => 'Continue',
87
        100 => 'Continue',
88
        100 => 'Continue',
89
        100 => 'Continue',
90
    ];
91

92
    private $code;
93
    private $reason;
94

95
    public function __construct(int $code = 200, string $reasonPhrase = '')
96
    {
97
        $this->code = $code;
31✔
98
        $this->reason = $reasonPhrase;
31✔
99
    }
100

101
    /**
102
     * Gets the response status code.
103
     * @return int Status code.
104
     */
105
    public function getStatusCode(): int
106
    {
107
        return $this->code;
29✔
108
    }
109

110
    /**
111
     * Return an instance with the specified status code and, optionally, reason phrase.
112
     * @param int $code The 3-digit integer result code to set.
113
     * @param string $reasonPhrase The reason phrase to use.
114
     * @return static
115
     * @throws \InvalidArgumentException For invalid status code arguments.
116
     */
117
    public function withStatus($code, $reasonPhrase = ''): self
118
    {
119
        $new = clone $this;
29✔
120
        $new->code = $code;
29✔
121
        $new->reason = $reasonPhrase;
29✔
122
        return $new;
29✔
123
    }
124

125
    /**
126
     * Gets the response reason phrase associated with the status code.
127
     * @return string Reason phrase; must return an empty string if none present.
128
     */
129
    public function getReasonPhrase(): string
130
    {
131
        return $this->reason ?: self::$codes[$this->code];
×
132
    }
133

134
    public function parse(string $data): self
135
    {
136
        list ($head, $body) = explode("\r\n\r\n", $data);
29✔
137
        $headers = array_filter(explode("\r\n", $head));
29✔
138
        $status = array_shift($headers);
29✔
139

140
        preg_match('!^HTTP/(?P<version>[0-9/.]+) (?P<code>[0-9]*) (?P<reason>.*)!', $status, $matches);
29✔
141
        if (empty($matches)) {
29✔
142
            // @todo: handle error
143
            throw new RuntimeException('Invalid http request');
×
144
        }
145
        $response = $this
29✔
146
            ->withProtocolVersion($matches['version'])
29✔
147
            ->withStatus($matches['code'], $matches['reason']);
29✔
148
        foreach ($headers as $header) {
29✔
149
            $parts = explode(':', $header, 2);
29✔
150
            if (count($parts) == 2) {
29✔
151
                $response = $response->withHeader($parts[0], $parts[1]);
29✔
152
            }
153
        }
154
        return $response;
29✔
155
    }
156

157
    public function render(): string
158
    {
159
        $data = "HTTP/{$this->getProtocolVersion()} {$this->getStatusCode()} {$this->getReasonPhrase()}\r\n";
×
160
        $data .= parent::render();
×
161
        return $data;
×
162
    }
163
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc