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

sirn-se / websocket-php / 5608975860

pending completion
5608975860

push

github

Sören Jensen
Middleware support

90 of 90 new or added lines in 8 files covered. (100.0%)

245 of 671 relevant lines covered (36.51%)

1.27 hits per line

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

0.0
/src/Http/Message.php
1
<?php
2

3
/**
4
 * Copyright (C) 2014-2023 Textalk and contributors.
5
 *
6
 * This file is part of Websocket PHP and is free software under the ISC License.
7
 * License text: https://raw.githubusercontent.com/sirn-se/websocket-php/master/COPYING.md
8
 */
9

10
namespace WebSocket\Http;
11

12
use BadMethodCallException;
13
use InvalidArgumentException;
14
use Psr\Http\Message\{
15
    MessageInterface,
16
    StreamInterface
17
};
18

19
/**
20
 * Phrity\WebSocket\Http\Message class.
21
 * Only used for handshake procedure.
22
 */
23
abstract class Message implements MessageInterface
24
{
25
    protected $version = '1.1';
26
    protected $headers = [];
27

28
    /**
29
     * Retrieves the HTTP protocol version as a string.
30
     * @return string HTTP protocol version.
31
     */
32
    public function getProtocolVersion(): string
33
    {
34
        return $this->version;
×
35
    }
36

37
    /**
38
     * Return an instance with the specified HTTP protocol version.
39
     * @param string $version HTTP protocol version
40
     * @return static
41
     */
42
    public function withProtocolVersion(string $version): self
43
    {
44
        $new = clone $this;
×
45
        $new->version = $version;
×
46
        return $new;
×
47
    }
48

49
    /**
50
     * Retrieves all message header values.
51
     * @return string[][] Returns an associative array of the message's headers.
52
     */
53
    public function getHeaders(): array
54
    {
55
        return array_merge(...array_values($this->headers));
×
56
    }
57

58
    /**
59
     * Checks if a header exists by the given case-insensitive name.
60
     * @param string $name Case-insensitive header field name.
61
     * @return bool Returns true if any header names match the given header.
62
     */
63
    public function hasHeader(string $name): bool
64
    {
65
        return array_key_exists(strtolower($name), $this->headers);
×
66
    }
67

68
    /**
69
     * Retrieves a message header value by the given case-insensitive name.
70
     * @param string $name Case-insensitive header field name.
71
     * @return string[] An array of string values as provided for the given header.
72
     */
73
    public function getHeader(string $name): array
74
    {
75
        return $this->hasHeader($name)
×
76
            ? array_merge(...array_values($this->headers[strtolower($name)] ?: []))
×
77
            : [];
×
78
    }
79

80
    /**
81
     * Retrieves a comma-separated string of the values for a single header.
82
     * @param string $name Case-insensitive header field name.
83
     * @return string A string of values as provided for the given header.
84
     */
85
    public function getHeaderLine(string $name): string
86
    {
87
        return implode(',', $this->getHeader($name));
×
88
    }
89

90
    /**
91
     * Return an instance with the provided value replacing the specified header.
92
     * @param string $name Case-insensitive header field name.
93
     * @param string|string[] $value Header value(s).
94
     * @return static
95
     * @throws \InvalidArgumentException for invalid header names or values.
96
     */
97
    public function withHeader(string $name, $value): self
98
    {
99
        $new = clone $this;
×
100
        if ($this->hasHeader($name)) {
×
101
            unset($new->headers[strtolower($name)]);
×
102
        }
103
        $new->handleHeader($name, $value);
×
104
        return $new;
×
105
    }
106

107
    /**
108
     * Return an instance with the specified header appended with the given value.
109
     * @param string $name Case-insensitive header field name to add.
110
     * @param string|string[] $value Header value(s).
111
     * @return static
112
     * @throws \InvalidArgumentException for invalid header names.
113
     * @throws \InvalidArgumentException for invalid header values.
114
     */
115
    public function withAddedHeader(string $name, $value): self
116
    {
117
        $new = clone $this;
×
118
        $new->handleHeader($name, $value);
×
119
        return $new;
×
120
    }
121

122
    /**
123
     * Return an instance without the specified header.
124
     * @param string $name Case-insensitive header field name to remove.
125
     * @return static
126
     */
127
    public function withoutHeader(string $name): self
128
    {
129
        $new = clone $this;
×
130
        if ($this->hasHeader($name)) {
×
131
            unset($new->headers[strtolower($name)]);
×
132
        }
133
        return $new;
×
134
    }
135

136
    /**
137
     * Not implemented, WebSocket only use headers.
138
     */
139
    public function getBody(): StreamInterface
140
    {
141
        throw new BadMethodCallException("Not implemented.");
×
142
    }
143

144
    /**
145
     * Not implemented, WebSocket only use headers.
146
     */
147
    public function withBody(StreamInterface $body): self
148
    {
149
        throw new BadMethodCallException("Not implemented.");
×
150
    }
151

152
    public function getAsArray(): array
153
    {
154
        $lines = [];
×
155
        foreach ($this->getHeaders() as $name => $values) {
×
156
            foreach ($values as $value) {
×
157
                $lines[] = "{$name}: {$value}";
×
158
            }
159
        }
160
        return $lines;
×
161
    }
162

163
    private function handleHeader(string $name, $value): void
164
    {
165
        // @todo: Add all available characters, these are just some of them.
166
        if (!preg_match('|^[0-9a-zA-Z#_-]+$|', $name)) {
×
167
            throw new InvalidArgumentException("'{$name}' is not a valid header field name.");
×
168
        }
169
        $value = is_array($value) ? $value : [$value];
×
170
        foreach ($value as $content) {
×
171
            if (!(is_string($content) || is_numeric($content)) || empty($content = trim($content))) {
×
172
                throw new InvalidArgumentException("Invalid header value(s) provided.");
×
173
            }
174
            $this->headers[strtolower($name)][$name][] = $content;
×
175
        }
176
    }
177
}
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